1 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 getTypeName() { 37 return fInfo.getTypeName(); 38 } 39 public String getPackageName() { 40 return fInfo.getPackageName(); 41 } 42 public String getEnclosingName() { 43 return fInfo.getEnclosingName(); 44 } 45 } 46 47 final String fName; 48 final String 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 EMPTY_STRING= ""; 63 protected TypeInfo(String pkg, String 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 79 public abstract int getElementType(); 80 81 86 public abstract String getPath(); 87 88 97 protected abstract IJavaElement getContainer(IJavaSearchScope scope) throws JavaModelException; 98 99 104 public abstract IPath getPackageFragmentRootPath(); 105 106 109 public abstract String getPackageFragmentRootName(); 110 111 116 public int getModifiers() { 117 return fModifiers; 118 } 119 120 125 public void setModifiers(int modifiers) { 126 fModifiers= modifiers; 127 } 128 129 134 public String getTypeName() { 135 return fName; 136 } 137 138 143 public String getPackageName() { 144 return fPackage; 145 } 146 147 150 public boolean isInterface() { 151 return Flags.isInterface(fModifiers); 152 } 153 154 157 public boolean isEnclosed(IJavaSearchScope scope) { 158 return scope.encloses(getPath()); 159 } 160 161 164 public String getEnclosingName() { 165 if (fEnclosingNames == null || fEnclosingNames.length == 0) 166 return EMPTY_STRING; 167 StringBuffer buf= new StringBuffer (); 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 185 public String getTypeQualifiedName() { 186 if (fEnclosingNames != null && fEnclosingNames.length > 0) { 187 StringBuffer buf= new StringBuffer (); 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 202 public String getFullyQualifiedName() { 203 StringBuffer buf= new StringBuffer (); 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 223 public String getTypeContainerName() { 224 if (fEnclosingNames != null && fEnclosingNames.length > 0) { 225 StringBuffer buf= new StringBuffer (); 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 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 return fName.equals(other.fName) && fPackage.equals(other.fPackage) 258 && CharOperation.equals(fEnclosingNames, other.fEnclosingNames); 259 } 260 261 protected static boolean equals(String s1, String s2) { 262 if (s1 == null || s2 == null) 263 return s1 == s2; 264 return s1.equals(s2); 265 } 266 267 270 public String toString() { 271 StringBuffer buf= new StringBuffer (); 272 buf.append("path= "); buf.append(getPath()); 274 buf.append("; pkg= "); buf.append(fPackage); 276 buf.append("; enclosing= "); buf.append(getEnclosingName()); 278 buf.append("; name= "); buf.append(fName); 280 return buf.toString(); 281 } 282 283 public abstract long getContainerTimestamp(); 284 285 public abstract boolean isContainerDirty(); 286 } 287 | Popular Tags |