001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.corext.refactoring.base;
011:
012: import org.eclipse.jdt.core.IClassFile;
013: import org.eclipse.jdt.core.ICompilationUnit;
014: import org.eclipse.jdt.core.IImportDeclaration;
015: import org.eclipse.jdt.core.IMember;
016: import org.eclipse.jdt.core.IMethod;
017: import org.eclipse.jdt.core.ISourceRange;
018: import org.eclipse.jdt.core.ITypeRoot;
019: import org.eclipse.jdt.core.JavaModelException;
020: import org.eclipse.jdt.core.dom.ASTNode;
021: import org.eclipse.jdt.core.dom.IMethodBinding;
022:
023: import org.eclipse.jdt.internal.corext.SourceRange;
024: import org.eclipse.jdt.internal.corext.dom.Selection;
025: import org.eclipse.ltk.core.refactoring.RefactoringStatusContext;
026:
027: /**
028: * A Java element context that can be used to annotate a </code>RefactoringStatusEntry<code>
029: * with detailed information about an error detected in an <code>IJavaElement</code>.
030: */
031: public abstract class JavaStatusContext extends
032: RefactoringStatusContext {
033:
034: private static class MemberSourceContext extends JavaStatusContext {
035: private IMember fMember;
036:
037: private MemberSourceContext(IMember member) {
038: fMember = member;
039: }
040:
041: public boolean isBinary() {
042: return fMember.isBinary();
043: }
044:
045: public ICompilationUnit getCompilationUnit() {
046: return fMember.getCompilationUnit();
047: }
048:
049: public IClassFile getClassFile() {
050: return fMember.getClassFile();
051: }
052:
053: public ISourceRange getSourceRange() {
054: try {
055: return fMember.getSourceRange();
056: } catch (JavaModelException e) {
057: return new SourceRange(0, 0);
058: }
059: }
060: }
061:
062: private static class ImportDeclarationSourceContext extends
063: JavaStatusContext {
064: private IImportDeclaration fImportDeclartion;
065:
066: private ImportDeclarationSourceContext(
067: IImportDeclaration declaration) {
068: fImportDeclartion = declaration;
069: }
070:
071: public boolean isBinary() {
072: return false;
073: }
074:
075: public ICompilationUnit getCompilationUnit() {
076: return (ICompilationUnit) fImportDeclartion.getParent()
077: .getParent();
078: }
079:
080: public IClassFile getClassFile() {
081: return null;
082: }
083:
084: public ISourceRange getSourceRange() {
085: try {
086: return fImportDeclartion.getSourceRange();
087: } catch (JavaModelException e) {
088: return new SourceRange(0, 0);
089: }
090: }
091: }
092:
093: private static class CompilationUnitSourceContext extends
094: JavaStatusContext {
095: private ICompilationUnit fCUnit;
096: private ISourceRange fSourceRange;
097:
098: private CompilationUnitSourceContext(ICompilationUnit cunit,
099: ISourceRange range) {
100: fCUnit = cunit;
101: fSourceRange = range;
102: if (fSourceRange == null)
103: fSourceRange = new SourceRange(0, 0);
104: }
105:
106: public boolean isBinary() {
107: return false;
108: }
109:
110: public ICompilationUnit getCompilationUnit() {
111: return fCUnit;
112: }
113:
114: public IClassFile getClassFile() {
115: return null;
116: }
117:
118: public ISourceRange getSourceRange() {
119: return fSourceRange;
120: }
121:
122: public String toString() {
123: return getSourceRange() + " in " + super .toString(); //$NON-NLS-1$
124: }
125: }
126:
127: private static class ClassFileSourceContext extends
128: JavaStatusContext {
129: private IClassFile fClassFile;
130: private ISourceRange fSourceRange;
131:
132: private ClassFileSourceContext(IClassFile classFile,
133: ISourceRange range) {
134: fClassFile = classFile;
135: fSourceRange = range;
136: if (fSourceRange == null)
137: fSourceRange = new SourceRange(0, 0);
138: }
139:
140: public boolean isBinary() {
141: return true;
142: }
143:
144: public ICompilationUnit getCompilationUnit() {
145: return null;
146: }
147:
148: public IClassFile getClassFile() {
149: return fClassFile;
150: }
151:
152: public ISourceRange getSourceRange() {
153: return fSourceRange;
154: }
155:
156: public String toString() {
157: return getSourceRange() + " in " + super .toString(); //$NON-NLS-1$
158: }
159: }
160:
161: /**
162: * Creates an status entry context for the given member
163: *
164: * @param member the java member for which the context is supposed
165: * to be created
166: * @return the status entry context or <code>null</code> if the
167: * context cannot be created
168: */
169: public static RefactoringStatusContext create(IMember member) {
170: if (member == null || !member.exists())
171: return null;
172: return new MemberSourceContext(member);
173: }
174:
175: /**
176: * Creates an status entry context for the given import declaration
177: *
178: * @param declaration the import declaration for which the context is
179: * supposed to be created
180: * @return the status entry context or <code>null</code> if the
181: * context cannot be created
182: */
183: public static RefactoringStatusContext create(
184: IImportDeclaration declaration) {
185: if (declaration == null || !declaration.exists())
186: return null;
187: return new ImportDeclarationSourceContext(declaration);
188: }
189:
190: /**
191: * Creates an status entry context for the given method binding
192: *
193: * @param method the method binding for which the context is supposed to be created
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(IMethodBinding method) {
198: return create((IMethod) method.getJavaElement());
199: }
200:
201: /**
202: * Creates an status entry context for the given type root.
203: *
204: * @param typeRoot the type root containing the error
205: * @return the status entry context or <code>Context.NULL_CONTEXT</code> if the
206: * context cannot be created
207: */
208: public static RefactoringStatusContext create(ITypeRoot typeRoot) {
209: return create(typeRoot, (ISourceRange) null);
210: }
211:
212: /**
213: * Creates an status entry context for the given type root and source range.
214: *
215: * @param typeRoot the type root containing the error
216: * @param range the source range that has caused the error or
217: * <code>null</code> if the source range is unknown
218: * @return the status entry context or <code>null</code> if the
219: * context cannot be created
220: */
221: public static RefactoringStatusContext create(ITypeRoot typeRoot,
222: ISourceRange range) {
223: if (typeRoot instanceof ICompilationUnit)
224: return new CompilationUnitSourceContext(
225: (ICompilationUnit) typeRoot, range);
226: else if (typeRoot instanceof IClassFile)
227: return new ClassFileSourceContext((IClassFile) typeRoot,
228: range);
229: else
230: return null;
231: }
232:
233: /**
234: * Creates an status entry context for the given type root and AST node.
235: *
236: * @param typeRoot the type root containing the error
237: * @param node an astNode denoting the source range that has caused the error
238: *
239: * @return the status entry context or <code>Context.NULL_CONTEXT</code> if the
240: * context cannot be created
241: */
242: public static RefactoringStatusContext create(ITypeRoot typeRoot,
243: ASTNode node) {
244: ISourceRange range = null;
245: if (node != null)
246: range = new SourceRange(node.getStartPosition(), node
247: .getLength());
248: return create(typeRoot, range);
249: }
250:
251: /**
252: * Creates an status entry context for the given type root and selection.
253: *
254: * @param typeRoot the type root containing the error
255: * @param selection a selection denoting the source range that has caused the error
256: *
257: * @return the status entry context or <code>Context.NULL_CONTEXT</code> if the
258: * context cannot be created
259: */
260: public static RefactoringStatusContext create(ITypeRoot typeRoot,
261: Selection selection) {
262: ISourceRange range = null;
263: if (selection != null)
264: range = new SourceRange(selection.getOffset(), selection
265: .getLength());
266: return create(typeRoot, range);
267: }
268:
269: /**
270: * Returns whether this context is for a class file.
271: *
272: * @return <code>true</code> if from a class file, and <code>false</code> if
273: * from a compilation unit
274: */
275: public abstract boolean isBinary();
276:
277: /**
278: * Returns the compilation unit this context is working on. Returns <code>null</code>
279: * if the context is a binary context.
280: *
281: * @return the compilation unit
282: */
283: public abstract ICompilationUnit getCompilationUnit();
284:
285: /**
286: * Returns the class file this context is working on. Returns <code>null</code>
287: * if the context is not a binary context.
288: *
289: * @return the class file
290: */
291: public abstract IClassFile getClassFile();
292:
293: /**
294: * Returns the source range associated with this element.
295: *
296: * @return the source range
297: */
298: public abstract ISourceRange getSourceRange();
299:
300: /* (non-Javadoc)
301: * Method declared on Context.
302: */
303: public Object getCorrespondingElement() {
304: if (isBinary())
305: return getClassFile();
306: else
307: return getCompilationUnit();
308: }
309: }
|