KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > util > TypeInfo


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.util;
12
13 import org.eclipse.core.runtime.IPath;
14
15 import org.eclipse.jdt.core.Flags;
16 import org.eclipse.jdt.core.IClassFile;
17 import org.eclipse.jdt.core.ICompilationUnit;
18 import org.eclipse.jdt.core.IJavaElement;
19 import org.eclipse.jdt.core.IType;
20 import org.eclipse.jdt.core.JavaModelException;
21 import org.eclipse.jdt.core.compiler.CharOperation;
22 import org.eclipse.jdt.core.search.IJavaSearchScope;
23
24 import org.eclipse.jdt.ui.dialogs.ITypeInfoRequestor;
25
26 public abstract class TypeInfo {
27
28     public static class TypeInfoAdapter implements ITypeInfoRequestor {
29         private TypeInfo fInfo;
30         public void setInfo(TypeInfo info) {
31             fInfo= info;
32         }
33         public int getModifiers() {
34             return fInfo.getModifiers();
35         }
36         public String JavaDoc getTypeName() {
37             return fInfo.getTypeName();
38         }
39         public String JavaDoc getPackageName() {
40             return fInfo.getPackageName();
41         }
42         public String JavaDoc getEnclosingName() {
43             return fInfo.getEnclosingName();
44         }
45     }
46     
47     final String JavaDoc fName;
48     final String JavaDoc fPackage;
49     final char[][] fEnclosingNames;
50     
51     int fModifiers;
52     
53     public static final int UNRESOLVABLE_TYPE_INFO= 1;
54     public static final int JAR_FILE_ENTRY_TYPE_INFO= 2;
55     public static final int IFILE_TYPE_INFO= 3;
56     
57     static final char SEPARATOR= '/';
58     static final char EXTENSION_SEPARATOR= '.';
59     static final char PACKAGE_PART_SEPARATOR='.';
60     
61     static final String JavaDoc EMPTY_STRING= ""; //$NON-NLS-1$
62

63     protected TypeInfo(String JavaDoc pkg, String JavaDoc name, char[][] enclosingTypes, int modifiers) {
64         fPackage= pkg;
65         fName= name;
66         fModifiers= modifiers;
67         fEnclosingNames= enclosingTypes;
68     }
69     
70     public int hashCode() {
71         return (fPackage.hashCode() << 16) + fName.hashCode();
72     }
73     
74     /**
75      * Returns this type info's kind encoded as an integer.
76      *
77      * @return the type info's kind
78      */

79     public abstract int getElementType();
80     
81     /**
82      * Returns the path reported by the <tt>ITypeNameRequestor</tt>.
83      *
84      * @return the path of the type info
85      */

86     public abstract String JavaDoc getPath();
87     
88     /**
89      * Returns the container (class file or CU) this type info is contained
90      * in.
91      *
92      * @param scope the scope used to resolve the <tt>IJavaElement</tt>.
93      * @return the container this type info is contained in.
94      * @throws JavaModelException if an error occurs while access the Java
95      * model.
96      */

97     protected abstract IJavaElement getContainer(IJavaSearchScope scope) throws JavaModelException;
98     
99     /**
100      * Returns the package fragment root path of this type info.
101      *
102      * @return the package fragment root as an <tt>IPath</tt>.
103      */

104     public abstract IPath getPackageFragmentRootPath();
105     
106     /**
107      * Returns the package fragment root name of this type info
108      */

109     public abstract String JavaDoc getPackageFragmentRootName();
110     
111     /**
112      * Returns the type's modifiers
113      *
114      * @return the type's modifiers
115      */

116     public int getModifiers() {
117         return fModifiers;
118     }
119     
120     /**
121      * Sets the modifiers to the given value.
122      *
123      * @param modifiers the new modifiers
124      */

125     public void setModifiers(int modifiers) {
126         fModifiers= modifiers;
127     }
128     
129     /**
130      * Returns the type name.
131      *
132      * @return the info's type name.
133      */

134     public String JavaDoc getTypeName() {
135         return fName;
136     }
137     
138     /**
139      * Returns the package name.
140      *
141      * @return the info's package name.
142      */

143     public String JavaDoc getPackageName() {
144         return fPackage;
145     }
146
147     /**
148      * Returns true iff the type info describes an interface.
149      */

150     public boolean isInterface() {
151         return Flags.isInterface(fModifiers);
152     }
153     
154     /**
155      * Returns true if the info is enclosed in the given scope
156      */

157     public boolean isEnclosed(IJavaSearchScope scope) {
158         return scope.encloses(getPath());
159     }
160
161     /**
162      * Gets the enclosing name (dot separated).
163      */

164     public String JavaDoc getEnclosingName() {
165         if (fEnclosingNames == null || fEnclosingNames.length == 0)
166             return EMPTY_STRING;
167         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
168         for (int i= 0; i < fEnclosingNames.length; i++) {
169             if (i != 0) {
170                 buf.append('.');
171             }
172             buf.append(fEnclosingNames[i]);
173         }
174         return buf.toString();
175     }
176     
177     public boolean isInnerType() {
178         return fEnclosingNames != null && fEnclosingNames.length > 0;
179     }
180     
181     /**
182      * Gets the type qualified name: Includes enclosing type names, but
183      * not package name. Identifiers are separated by dots.
184      */

185     public String JavaDoc getTypeQualifiedName() {
186         if (fEnclosingNames != null && fEnclosingNames.length > 0) {
187             StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
188             for (int i= 0; i < fEnclosingNames.length; i++) {
189                 buf.append(fEnclosingNames[i]);
190                 buf.append('.');
191             }
192             buf.append(fName);
193             return buf.toString();
194         }
195         return fName;
196     }
197     
198     /**
199      * Gets the fully qualified type name: Includes enclosing type names and
200      * package. All identifiers are separated by dots.
201      */

202     public String JavaDoc getFullyQualifiedName() {
203         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
204         if (fPackage.length() > 0) {
205             buf.append(fPackage);
206             buf.append('.');
207         }
208         if (fEnclosingNames != null) {
209             for (int i= 0; i < fEnclosingNames.length; i++) {
210                 buf.append(fEnclosingNames[i]);
211                 buf.append('.');
212             }
213         }
214         buf.append(fName);
215         return buf.toString();
216     }
217     
218     /**
219      * Gets the fully qualified type container name: Package name or
220      * enclosing type name with package name.
221      * All identifiers are separated by dots.
222      */

223     public String JavaDoc getTypeContainerName() {
224         if (fEnclosingNames != null && fEnclosingNames.length > 0) {
225             StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
226             if (fPackage.length() > 0) {
227                 buf.append(fPackage);
228             }
229             for (int i= 0; i < fEnclosingNames.length; i++) {
230                 if (buf.length() > 0) {
231                     buf.append('.');
232                 }
233                 buf.append(fEnclosingNames[i]);
234             }
235             return buf.toString();
236         }
237         return fPackage;
238     }
239     
240     /**
241      * Resolves the type in a scope if was searched for.
242      * The parent project of JAR files is the first project found in scope.
243      * Returns null if the type could not be resolved
244      */

245     public IType resolveType(IJavaSearchScope scope) throws JavaModelException {
246         IJavaElement elem = getContainer(scope);
247         if (elem instanceof ICompilationUnit)
248             return JavaModelUtil.findTypeInCompilationUnit((ICompilationUnit)elem, getTypeQualifiedName());
249         else if (elem instanceof IClassFile)
250             return ((IClassFile)elem).getType();
251         return null;
252     }
253
254     protected boolean doEquals(TypeInfo other) {
255         // Don't compare the modifiers since they aren't relevant to identify
256
// a type.
257
return fName.equals(other.fName) && fPackage.equals(other.fPackage)
258             && CharOperation.equals(fEnclosingNames, other.fEnclosingNames);
259     }
260     
261     protected static boolean equals(String JavaDoc s1, String JavaDoc s2) {
262         if (s1 == null || s2 == null)
263             return s1 == s2;
264         return s1.equals(s2);
265     }
266     
267     /* non java-doc
268      * debugging only
269      */

270     public String JavaDoc toString() {
271         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
272         buf.append("path= "); //$NON-NLS-1$
273
buf.append(getPath());
274         buf.append("; pkg= "); //$NON-NLS-1$
275
buf.append(fPackage);
276         buf.append("; enclosing= "); //$NON-NLS-1$
277
buf.append(getEnclosingName());
278         buf.append("; name= "); //$NON-NLS-1$
279
buf.append(fName);
280         return buf.toString();
281     }
282     
283     public abstract long getContainerTimestamp();
284     
285     public abstract boolean isContainerDirty();
286 }
287
Popular Tags