1 11 12 package org.eclipse.jdt.core.dom; 13 14 import java.util.Iterator ; 15 import java.util.List ; 16 import org.eclipse.jdt.core.ICompilationUnit; 17 import org.eclipse.jdt.core.IJavaElement; 18 import org.eclipse.jdt.core.IPackageFragment; 19 import org.eclipse.jdt.core.IPackageFragmentRoot; 20 import org.eclipse.jdt.core.JavaModelException; 21 22 import org.eclipse.jdt.core.compiler.CharOperation; 23 import org.eclipse.jdt.internal.compiler.env.IBinaryAnnotation; 24 import org.eclipse.jdt.internal.compiler.env.IBinaryType; 25 import org.eclipse.jdt.internal.compiler.env.INameEnvironment; 26 import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer; 27 import org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding; 28 import org.eclipse.jdt.internal.compiler.lookup.TypeConstants; 29 import org.eclipse.jdt.internal.compiler.util.Util; 30 import org.eclipse.jdt.internal.core.NameLookup; 31 import org.eclipse.jdt.internal.core.SearchableEnvironment; 32 33 36 class PackageBinding implements IPackageBinding { 37 38 private static final String [] NO_NAME_COMPONENTS = CharOperation.NO_STRINGS; 39 private static final String UNNAMED = Util.EMPTY_STRING; 40 private static final char PACKAGE_NAME_SEPARATOR = '.'; 41 42 private org.eclipse.jdt.internal.compiler.lookup.PackageBinding binding; 43 private String name; 44 private BindingResolver resolver; 45 private String [] components; 46 47 PackageBinding(org.eclipse.jdt.internal.compiler.lookup.PackageBinding binding, BindingResolver resolver) { 48 this.binding = binding; 49 this.resolver = resolver; 50 } 51 52 public IAnnotationBinding[] getAnnotations() { 53 try { 54 INameEnvironment nameEnvironment = this.binding.environment.nameEnvironment; 55 if (!(nameEnvironment instanceof SearchableEnvironment)) 56 return AnnotationBinding.NoAnnotations; 57 NameLookup nameLookup = ((SearchableEnvironment) nameEnvironment).nameLookup; 58 if (nameLookup == null) 59 return AnnotationBinding.NoAnnotations; 60 final String pkgName = getName(); 61 IPackageFragment[] pkgs = nameLookup.findPackageFragments(pkgName, false); 62 if (pkgs == null) 63 return AnnotationBinding.NoAnnotations; 64 65 for (int i = 0, len = pkgs.length; i < len; i++) { 66 int fragType = pkgs[i].getKind(); 67 switch(fragType) { 68 case IPackageFragmentRoot.K_SOURCE: 69 String unitName = "package-info.java"; ICompilationUnit unit = pkgs[i].getCompilationUnit(unitName); 71 if (unit != null) { 72 ASTParser p = ASTParser.newParser(AST.JLS3); 73 p.setSource(unit); 74 p.setResolveBindings(true); 75 p.setUnitName(unitName); 76 p.setFocalPosition(0); 77 p.setKind(ASTParser.K_COMPILATION_UNIT); 78 CompilationUnit domUnit = (CompilationUnit) p.createAST(null); 79 PackageDeclaration pkgDecl = domUnit.getPackage(); 80 if (pkgDecl != null) { 81 List annos = pkgDecl.annotations(); 82 if (annos == null || annos.isEmpty()) 83 return AnnotationBinding.NoAnnotations; 84 IAnnotationBinding[] result = new IAnnotationBinding[annos.size()]; 85 int index=0; 86 for (Iterator it = annos.iterator(); it.hasNext(); index++) { 87 result[index] = ((Annotation) it.next()).resolveAnnotationBinding(); 88 if (result[index] == null) 90 return AnnotationBinding.NoAnnotations; 91 } 92 return result; 93 } 94 } 95 break; 96 case IPackageFragmentRoot.K_BINARY: 97 NameEnvironmentAnswer answer = 98 nameEnvironment.findType(TypeConstants.PACKAGE_INFO_NAME, this.binding.compoundName); 99 if (answer != null && answer.isBinaryType()) { 100 IBinaryType type = answer.getBinaryType(); 101 IBinaryAnnotation[] binaryAnnotations = type.getAnnotations(); 102 org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding[] binaryInstances = 103 BinaryTypeBinding.createAnnotations(binaryAnnotations, this.binding.environment); 104 org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding[] allInstances = 105 org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding.addStandardAnnotations(binaryInstances, type.getTagBits(), this.binding.environment); 106 int total = allInstances.length; 107 IAnnotationBinding[] domInstances = new AnnotationBinding[total]; 108 for (int a = 0; a < total; a++) { 109 final IAnnotationBinding annotationInstance = this.resolver.getAnnotationInstance(allInstances[a]); 110 if (annotationInstance == null) { return AnnotationBinding.NoAnnotations; 112 } 113 domInstances[a] = annotationInstance; 114 } 115 return domInstances; 116 } 117 } 118 } 119 } catch(JavaModelException e) { 120 return AnnotationBinding.NoAnnotations; 121 } 122 return AnnotationBinding.NoAnnotations; 123 } 124 125 128 public String getName() { 129 if (name == null) { 130 computeNameAndComponents(); 131 } 132 return name; 133 } 134 135 138 public boolean isUnnamed() { 139 return getName().equals(UNNAMED); 140 } 141 142 145 public String [] getNameComponents() { 146 if (components == null) { 147 computeNameAndComponents(); 148 } 149 return components; 150 } 151 152 155 public int getKind() { 156 return IBinding.PACKAGE; 157 } 158 159 162 public int getModifiers() { 163 return Modifier.NONE; 164 } 165 166 169 public boolean isDeprecated() { 170 return false; 171 } 172 173 176 public boolean isRecovered() { 177 return false; 178 } 179 180 183 public boolean isSynthetic() { 184 return false; 185 } 186 187 190 public IJavaElement getJavaElement() { 191 INameEnvironment nameEnvironment = this.binding.environment.nameEnvironment; if (!(nameEnvironment instanceof SearchableEnvironment)) return null; 193 NameLookup nameLookup = ((SearchableEnvironment) nameEnvironment).nameLookup; 194 if (nameLookup == null) return null; 195 IJavaElement[] pkgs = nameLookup.findPackageFragments(getName(), false); 196 if (pkgs == null) return null; 197 return pkgs[0]; 198 } 199 200 203 public String getKey() { 204 return new String (this.binding.computeUniqueKey()); 205 } 206 207 211 public boolean isEqualTo(IBinding other) { 212 if (other == this) { 213 return true; 215 } 216 if (other == null) { 217 return false; 219 } 220 if (!(other instanceof PackageBinding)) { 221 return false; 222 } 223 org.eclipse.jdt.internal.compiler.lookup.PackageBinding packageBinding2 = ((PackageBinding) other).binding; 224 return CharOperation.equals(this.binding.compoundName, packageBinding2.compoundName); 225 } 226 227 private void computeNameAndComponents() { 228 char[][] compoundName = this.binding.compoundName; 229 if (compoundName == CharOperation.NO_CHAR_CHAR || compoundName == null) { 230 name = UNNAMED; 231 components = NO_NAME_COMPONENTS; 232 } else { 233 int length = compoundName.length; 234 components = new String [length]; 235 StringBuffer buffer = new StringBuffer (); 236 for (int i = 0; i < length - 1; i++) { 237 components[i] = new String (compoundName[i]); 238 buffer.append(compoundName[i]).append(PACKAGE_NAME_SEPARATOR); 239 } 240 components[length - 1] = new String (compoundName[length - 1]); 241 buffer.append(compoundName[length - 1]); 242 name = buffer.toString(); 243 } 244 } 245 246 250 public String toString() { 251 return this.binding.toString(); 252 } 253 } 254 | Popular Tags |