]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/core refactoring/org/eclipse/jdt/internal/corext/refactoring/base/JavaStatusContext.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / core refactoring / org / eclipse / jdt / internal / corext / refactoring / base / JavaStatusContext.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.corext.refactoring.base;
12
13 import org.eclipse.ltk.core.refactoring.RefactoringStatusContext;
14
15 import org.eclipse.jdt.core.IClassFile;
16 import org.eclipse.jdt.core.ICompilationUnit;
17 import org.eclipse.jdt.core.IImportDeclaration;
18 import org.eclipse.jdt.core.IMember;
19 import org.eclipse.jdt.core.IMethod;
20 import org.eclipse.jdt.core.ISourceRange;
21 import org.eclipse.jdt.core.ITypeRoot;
22 import org.eclipse.jdt.core.JavaModelException;
23 import org.eclipse.jdt.core.SourceRange;
24 import org.eclipse.jdt.core.dom.ASTNode;
25 import org.eclipse.jdt.core.dom.IMethodBinding;
26
27 import org.eclipse.jdt.internal.corext.dom.Selection;
28
29 /**
30  * A Java element context that can be used to annotate a </code>RefactoringStatusEntry<code>
31  * with detailed information about an error detected in an <code>IJavaElement</code>.
32  */
33 public abstract class JavaStatusContext extends RefactoringStatusContext {
34
35         private static class MemberSourceContext extends JavaStatusContext {
36                 private IMember fMember;
37                 private MemberSourceContext(IMember member) {
38                         fMember= member;
39                 }
40                 @Override
41                 public boolean isBinary() {
42                         return fMember.isBinary();
43                 }
44                 @Override
45                 public ICompilationUnit getCompilationUnit() {
46                         return fMember.getCompilationUnit();
47                 }
48                 @Override
49                 public IClassFile getClassFile() {
50                         return fMember.getClassFile();
51                 }
52                 @Override
53                 public ISourceRange getSourceRange() {
54                         try {
55                                 return fMember.getSourceRange();
56                         } catch (JavaModelException e) {
57                                 return new SourceRange(0,0);
58                         }
59                 }
60         }
61
62         private static class ImportDeclarationSourceContext extends JavaStatusContext {
63                 private IImportDeclaration fImportDeclartion;
64                 private ImportDeclarationSourceContext(IImportDeclaration declaration) {
65                         fImportDeclartion= declaration;
66                 }
67                 @Override
68                 public boolean isBinary() {
69                         return false;
70                 }
71                 @Override
72                 public ICompilationUnit getCompilationUnit() {
73                         return (ICompilationUnit)fImportDeclartion.getParent().getParent();
74                 }
75                 @Override
76                 public IClassFile getClassFile() {
77                         return null;
78                 }
79                 @Override
80                 public ISourceRange getSourceRange() {
81                         try {
82                                 return fImportDeclartion.getSourceRange();
83                         } catch (JavaModelException e) {
84                                 return new SourceRange(0,0);
85                         }
86                 }
87         }
88
89         private static class CompilationUnitSourceContext extends JavaStatusContext {
90                 private ICompilationUnit fCUnit;
91                 private ISourceRange fSourceRange;
92                 private CompilationUnitSourceContext(ICompilationUnit cunit, ISourceRange range) {
93                         fCUnit= cunit;
94                         fSourceRange= range;
95                         if (fSourceRange == null)
96                                 fSourceRange= new SourceRange(0,0);
97                 }
98                 @Override
99                 public boolean isBinary() {
100                         return false;
101                 }
102                 @Override
103                 public ICompilationUnit getCompilationUnit() {
104                         return fCUnit;
105                 }
106                 @Override
107                 public IClassFile getClassFile() {
108                         return null;
109                 }
110                 @Override
111                 public ISourceRange getSourceRange() {
112                         return fSourceRange;
113                 }
114                 @Override
115                 public String toString() {
116                         return getSourceRange() + " in " + super.toString(); //$NON-NLS-1$
117                 }
118         }
119
120         private static class ClassFileSourceContext extends JavaStatusContext {
121                 private IClassFile fClassFile;
122                 private ISourceRange fSourceRange;
123                 private ClassFileSourceContext(IClassFile classFile, ISourceRange range) {
124                         fClassFile= classFile;
125                         fSourceRange= range;
126                         if (fSourceRange == null)
127                                 fSourceRange= new SourceRange(0,0);
128                 }
129                 @Override
130                 public boolean isBinary() {
131                         return true;
132                 }
133                 @Override
134                 public ICompilationUnit getCompilationUnit() {
135                         return null;
136                 }
137                 @Override
138                 public IClassFile getClassFile() {
139                         return fClassFile;
140                 }
141                 @Override
142                 public ISourceRange getSourceRange() {
143                         return fSourceRange;
144                 }
145                 @Override
146                 public String toString() {
147                         return getSourceRange() + " in " + super.toString(); //$NON-NLS-1$
148                 }
149         }
150
151         /**
152          * Creates an status entry context for the given member
153          *
154          * @param member the java member for which the context is supposed
155          *  to be created
156          * @return the status entry context or <code>null</code> if the
157          *      context cannot be created
158          */
159         public static RefactoringStatusContext create(IMember member) {
160                 if (member == null || !member.exists())
161                         return null;
162                 return new MemberSourceContext(member);
163         }
164
165         /**
166          * Creates an status entry context for the given import declaration
167          *
168          * @param declaration the import declaration for which the context is
169          *  supposed to be created
170          * @return the status entry context or <code>null</code> if the
171          *      context cannot be created
172          */
173         public static RefactoringStatusContext create(IImportDeclaration declaration) {
174                 if (declaration == null || !declaration.exists())
175                         return null;
176                 return new ImportDeclarationSourceContext(declaration);
177         }
178
179         /**
180          * Creates an status entry context for the given method binding
181          *
182          * @param method the method binding for which the context is supposed to be created
183          * @return the status entry context or <code>Context.NULL_CONTEXT</code> if the
184          *      context cannot be created
185          */
186         public static RefactoringStatusContext create(IMethodBinding method) {
187                 return create((IMethod) method.getJavaElement());
188         }
189
190         /**
191          * Creates an status entry context for the given type root.
192          *
193          * @param typeRoot the type root containing the error
194          * @return the status entry context or <code>Context.NULL_CONTEXT</code> if the
195          *      context cannot be created
196          */
197         public static RefactoringStatusContext create(ITypeRoot typeRoot) {
198                 return create(typeRoot, (ISourceRange)null);
199         }
200
201         /**
202          * Creates an status entry context for the given type root and source range.
203          *
204          * @param typeRoot the type root containing the error
205          * @param range the source range that has caused the error or
206          *  <code>null</code> if the source range is unknown
207          * @return the status entry context or <code>null</code> if the
208          *      context cannot be created
209          */
210         public static RefactoringStatusContext create(ITypeRoot typeRoot, ISourceRange range) {
211                 if (typeRoot instanceof ICompilationUnit)
212                         return new CompilationUnitSourceContext((ICompilationUnit) typeRoot, range);
213                 else if (typeRoot instanceof IClassFile)
214                         return new ClassFileSourceContext((IClassFile) typeRoot, range);
215                 else
216                         return null;
217         }
218
219         /**
220          * Creates an status entry context for the given type root and AST node.
221          *
222          * @param typeRoot the type root containing the error
223          * @param node an astNode denoting the source range that has caused the error
224          *
225          * @return the status entry context or <code>Context.NULL_CONTEXT</code> if the
226          *      context cannot be created
227          */
228         public static RefactoringStatusContext create(ITypeRoot typeRoot, ASTNode node) {
229                 ISourceRange range= null;
230                 if (node != null)
231                         range= new SourceRange(node.getStartPosition(), node.getLength());
232                 return create(typeRoot, range);
233         }
234
235         /**
236          * Creates an status entry context for the given type root and selection.
237          *
238          * @param typeRoot the type root containing the error
239          * @param selection a selection denoting the source range that has caused the error
240          *
241          * @return the status entry context or <code>Context.NULL_CONTEXT</code> if the
242          *      context cannot be created
243          */
244         public static RefactoringStatusContext create(ITypeRoot typeRoot, Selection selection) {
245                 ISourceRange range= null;
246                 if (selection != null)
247                         range= new SourceRange(selection.getOffset(), selection.getLength());
248                 return create(typeRoot, range);
249         }
250
251         /**
252          * Returns whether this context is for a class file.
253          *
254          * @return <code>true</code> if from a class file, and <code>false</code> if
255          *   from a compilation unit
256          */
257         public abstract boolean isBinary();
258
259         /**
260          * Returns the compilation unit this context is working on. Returns <code>null</code>
261          * if the context is a binary context.
262          *
263          * @return the compilation unit
264          */
265         public abstract ICompilationUnit getCompilationUnit();
266
267         /**
268          * Returns the class file this context is working on. Returns <code>null</code>
269          * if the context is not a binary context.
270          *
271          * @return the class file
272          */
273         public abstract IClassFile getClassFile();
274
275         /**
276          * Returns the source range associated with this element.
277          *
278          * @return the source range
279          */
280         public abstract ISourceRange getSourceRange();
281
282         /* (non-Javadoc)
283          * Method declared on Context.
284          */
285         @Override
286         public Object getCorrespondingElement() {
287                 if (isBinary())
288                         return getClassFile();
289                 else
290                         return getCompilationUnit();
291         }
292 }
293