KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > core > dom > PackageBinding


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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
12 package org.eclipse.jdt.core.dom;
13
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
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 /**
34  * Internal implementation of package bindings.
35  */

36 class PackageBinding implements IPackageBinding {
37
38     private static final String JavaDoc[] NO_NAME_COMPONENTS = CharOperation.NO_STRINGS;
39     private static final String JavaDoc 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 JavaDoc name;
44     private BindingResolver resolver;
45     private String JavaDoc[] 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 JavaDoc pkgName = getName();
61             IPackageFragment[] pkgs = nameLookup.findPackageFragments(pkgName, false/*exact match*/);
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 JavaDoc unitName = "package-info.java"; //$NON-NLS-1$
70
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 JavaDoc 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 JavaDoc it = annos.iterator(); it.hasNext(); index++) {
87                                     result[index] = ((Annotation) it.next()).resolveAnnotationBinding();
88                                     // not resolving bindings
89
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) {// not resolving binding
111
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     /*
126      * @see IBinding#getName()
127      */

128     public String JavaDoc getName() {
129         if (name == null) {
130             computeNameAndComponents();
131         }
132         return name;
133     }
134
135     /*
136      * @see IPackageBinding#isUnnamed()
137      */

138     public boolean isUnnamed() {
139         return getName().equals(UNNAMED);
140     }
141
142     /*
143      * @see IPackageBinding#getNameComponents()
144      */

145     public String JavaDoc[] getNameComponents() {
146         if (components == null) {
147             computeNameAndComponents();
148         }
149         return components;
150     }
151
152     /*
153      * @see IBinding#getKind()
154      */

155     public int getKind() {
156         return IBinding.PACKAGE;
157     }
158
159     /*
160      * @see IBinding#getModifiers()
161      */

162     public int getModifiers() {
163         return Modifier.NONE;
164     }
165
166     /*
167      * @see IBinding#isDeprecated()
168      */

169     public boolean isDeprecated() {
170         return false;
171     }
172
173     /**
174      * @see IBinding#isRecovered()
175      */

176     public boolean isRecovered() {
177         return false;
178     }
179
180     /**
181      * @see IBinding#isSynthetic()
182      */

183     public boolean isSynthetic() {
184         return false;
185     }
186
187     /*
188      * @see IBinding#getJavaElement()
189      */

190     public IJavaElement getJavaElement() {
191         INameEnvironment nameEnvironment = this.binding.environment.nameEnvironment; // a package binding always has a LooupEnvironment set
192
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/*exact match*/);
196         if (pkgs == null) return null;
197         return pkgs[0];
198     }
199
200     /*
201      * @see IBinding#getKey()
202      */

203     public String JavaDoc getKey() {
204         return new String JavaDoc(this.binding.computeUniqueKey());
205     }
206
207     /*
208      * @see IBinding#isEqualTo(Binding)
209      * @since 3.1
210      */

211     public boolean isEqualTo(IBinding other) {
212         if (other == this) {
213             // identical binding - equal (key or no key)
214
return true;
215         }
216         if (other == null) {
217             // other binding missing
218
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 JavaDoc[length];
235             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
236             for (int i = 0; i < length - 1; i++) {
237                 components[i] = new String JavaDoc(compoundName[i]);
238                 buffer.append(compoundName[i]).append(PACKAGE_NAME_SEPARATOR);
239             }
240             components[length - 1] = new String JavaDoc(compoundName[length - 1]);
241             buffer.append(compoundName[length - 1]);
242             name = buffer.toString();
243         }
244     }
245
246     /*
247      * For debugging purpose only.
248      * @see java.lang.Object#toString()
249      */

250     public String JavaDoc toString() {
251         return this.binding.toString();
252     }
253 }
254
Popular Tags