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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.corext.refactoring.base;
13 import org.eclipse.ltk.core.refactoring.RefactoringStatusContext;
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;
27 import org.eclipse.jdt.internal.corext.dom.Selection;
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>.
33 public abstract class JavaStatusContext extends RefactoringStatusContext {
35 private static class MemberSourceContext extends JavaStatusContext {
36 private IMember fMember;
37 private MemberSourceContext(IMember member) {
41 public boolean isBinary() {
42 return fMember.isBinary();
45 public ICompilationUnit getCompilationUnit() {
46 return fMember.getCompilationUnit();
49 public IClassFile getClassFile() {
50 return fMember.getClassFile();
53 public ISourceRange getSourceRange() {
55 return fMember.getSourceRange();
56 } catch (JavaModelException e) {
57 return new SourceRange(0,0);
62 private static class ImportDeclarationSourceContext extends JavaStatusContext {
63 private IImportDeclaration fImportDeclartion;
64 private ImportDeclarationSourceContext(IImportDeclaration declaration) {
65 fImportDeclartion= declaration;
68 public boolean isBinary() {
72 public ICompilationUnit getCompilationUnit() {
73 return (ICompilationUnit)fImportDeclartion.getParent().getParent();
76 public IClassFile getClassFile() {
80 public ISourceRange getSourceRange() {
82 return fImportDeclartion.getSourceRange();
83 } catch (JavaModelException e) {
84 return new SourceRange(0,0);
89 private static class CompilationUnitSourceContext extends JavaStatusContext {
90 private ICompilationUnit fCUnit;
91 private ISourceRange fSourceRange;
92 private CompilationUnitSourceContext(ICompilationUnit cunit, ISourceRange range) {
95 if (fSourceRange == null)
96 fSourceRange= new SourceRange(0,0);
99 public boolean isBinary() {
103 public ICompilationUnit getCompilationUnit() {
107 public IClassFile getClassFile() {
111 public ISourceRange getSourceRange() {
115 public String toString() {
116 return getSourceRange() + " in " + super.toString(); //$NON-NLS-1$
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;
126 if (fSourceRange == null)
127 fSourceRange= new SourceRange(0,0);
130 public boolean isBinary() {
134 public ICompilationUnit getCompilationUnit() {
138 public IClassFile getClassFile() {
142 public ISourceRange getSourceRange() {
146 public String toString() {
147 return getSourceRange() + " in " + super.toString(); //$NON-NLS-1$
152 * Creates an status entry context for the given member
154 * @param member the java member for which the context is supposed
156 * @return the status entry context or <code>null</code> if the
157 * context cannot be created
159 public static RefactoringStatusContext create(IMember member) {
160 if (member == null || !member.exists())
162 return new MemberSourceContext(member);
166 * Creates an status entry context for the given import declaration
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
173 public static RefactoringStatusContext create(IImportDeclaration declaration) {
174 if (declaration == null || !declaration.exists())
176 return new ImportDeclarationSourceContext(declaration);
180 * Creates an status entry context for the given method binding
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
186 public static RefactoringStatusContext create(IMethodBinding method) {
187 return create((IMethod) method.getJavaElement());
191 * Creates an status entry context for the given type root.
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
197 public static RefactoringStatusContext create(ITypeRoot typeRoot) {
198 return create(typeRoot, (ISourceRange)null);
202 * Creates an status entry context for the given type root and source range.
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
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);
220 * Creates an status entry context for the given type root and AST node.
222 * @param typeRoot the type root containing the error
223 * @param node an astNode denoting the source range that has caused the error
225 * @return the status entry context or <code>Context.NULL_CONTEXT</code> if the
226 * context cannot be created
228 public static RefactoringStatusContext create(ITypeRoot typeRoot, ASTNode node) {
229 ISourceRange range= null;
231 range= new SourceRange(node.getStartPosition(), node.getLength());
232 return create(typeRoot, range);
236 * Creates an status entry context for the given type root and selection.
238 * @param typeRoot the type root containing the error
239 * @param selection a selection denoting the source range that has caused the error
241 * @return the status entry context or <code>Context.NULL_CONTEXT</code> if the
242 * context cannot be created
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);
252 * Returns whether this context is for a class file.
254 * @return <code>true</code> if from a class file, and <code>false</code> if
255 * from a compilation unit
257 public abstract boolean isBinary();
260 * Returns the compilation unit this context is working on. Returns <code>null</code>
261 * if the context is a binary context.
263 * @return the compilation unit
265 public abstract ICompilationUnit getCompilationUnit();
268 * Returns the class file this context is working on. Returns <code>null</code>
269 * if the context is not a binary context.
271 * @return the class file
273 public abstract IClassFile getClassFile();
276 * Returns the source range associated with this element.
278 * @return the source range
280 public abstract ISourceRange getSourceRange();
283 * Method declared on Context.
286 public Object getCorrespondingElement() {
288 return getClassFile();
290 return getCompilationUnit();