removed unused private methods.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / core / dom / PackageBinding.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2008 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 net.sourceforge.phpdt.core.dom;
13
14 import java.util.Iterator;
15 import java.util.List;
16 import net.sourceforge.phpdt.core.ICompilationUnit;
17 import net.sourceforge.phpdt.core.IJavaElement;
18 import net.sourceforge.phpdt.core.IPackageFragment;
19 import net.sourceforge.phpdt.core.IPackageFragmentRoot;
20 import net.sourceforge.phpdt.core.JavaModelException;
21
22 import net.sourceforge.phpdt.core.compiler.CharOperation;
23 import net.sourceforge.phpdt.internal.compiler.env.IBinaryAnnotation;
24 import net.sourceforge.phpdt.internal.compiler.env.IBinaryType;
25 import net.sourceforge.phpdt.internal.compiler.env.INameEnvironment;
26 import net.sourceforge.phpdt.internal.compiler.env.NameEnvironmentAnswer;
27 import net.sourceforge.phpdt.internal.compiler.lookup.BinaryTypeBinding;
28 import net.sourceforge.phpdt.internal.compiler.lookup.TypeConstants;
29 import net.sourceforge.phpdt.internal.compiler.util.Util;
30 import net.sourceforge.phpdt.internal.core.NameLookup;
31 import net.sourceforge.phpdt.internal.core.SearchableEnvironment;
32
33 /**
34  * Internal implementation of package bindings.
35  */
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 net.sourceforge.phpdt.internal.compiler.lookup.PackageBinding binding;
43         private String name;
44         private BindingResolver resolver;
45         private String[] components;
46
47         PackageBinding(net.sourceforge.phpdt.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/*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 unitName = "package-info.java"; //$NON-NLS-1$
70                                                 ICompilationUnit unit = pkgs[i].getCompilationUnit(unitName);
71                                                 if (unit != null && unit.exists()) {
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                                                                         // 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                                                         char[][][] missingTypeNames = type.getMissingTypeNames();
102                                                         IBinaryAnnotation[] binaryAnnotations = type.getAnnotations();
103                                                         net.sourceforge.phpdt.internal.compiler.lookup.AnnotationBinding[] binaryInstances =
104                                                                 BinaryTypeBinding.createAnnotations(binaryAnnotations, this.binding.environment, missingTypeNames);
105                                                         net.sourceforge.phpdt.internal.compiler.lookup.AnnotationBinding[] allInstances =
106                                                                 net.sourceforge.phpdt.internal.compiler.lookup.AnnotationBinding.addStandardAnnotations(binaryInstances, type.getTagBits(), this.binding.environment);
107                                                         int total = allInstances.length;
108                                                         IAnnotationBinding[] domInstances = new AnnotationBinding[total];
109                                                         for (int a = 0; a < total; a++) {
110                                                                 final IAnnotationBinding annotationInstance = this.resolver.getAnnotationInstance(allInstances[a]);
111                                                                 if (annotationInstance == null) {// not resolving binding
112                                                                         return AnnotationBinding.NoAnnotations;
113                                                                 }
114                                                                 domInstances[a] = annotationInstance;
115                                                         }
116                                                         return domInstances;
117                                                 }
118                                 }
119                         }
120                 } catch(JavaModelException e) {
121                         return AnnotationBinding.NoAnnotations;
122                 }
123                 return AnnotationBinding.NoAnnotations;
124         }
125
126         /*
127          * @see IBinding#getName()
128          */
129         public String getName() {
130                 if (name == null) {
131                         computeNameAndComponents();
132                 }
133                 return name;
134         }
135
136         /*
137          * @see IPackageBinding#isUnnamed()
138          */
139         public boolean isUnnamed() {
140                 return getName().equals(UNNAMED);
141         }
142
143         /*
144          * @see IPackageBinding#getNameComponents()
145          */
146         public String[] getNameComponents() {
147                 if (components == null) {
148                         computeNameAndComponents();
149                 }
150                 return components;
151         }
152
153         /*
154          * @see IBinding#getKind()
155          */
156         public int getKind() {
157                 return IBinding.PACKAGE;
158         }
159
160         /*
161          * @see IBinding#getModifiers()
162          */
163         public int getModifiers() {
164                 return Modifier.NONE;
165         }
166
167         /*
168          * @see IBinding#isDeprecated()
169          */
170         public boolean isDeprecated() {
171                 return false;
172         }
173
174         /**
175          * @see IBinding#isRecovered()
176          */
177         public boolean isRecovered() {
178                 return false;
179         }
180
181         /**
182          * @see IBinding#isSynthetic()
183          */
184         public boolean isSynthetic() {
185                 return false;
186         }
187
188         /*
189          * @see IBinding#getJavaElement()
190          */
191         public IJavaElement getJavaElement() {
192                 INameEnvironment nameEnvironment = this.binding.environment.nameEnvironment; // a package binding always has a LooupEnvironment set
193                 if (!(nameEnvironment instanceof SearchableEnvironment)) return null;
194                 NameLookup nameLookup = ((SearchableEnvironment) nameEnvironment).nameLookup;
195                 if (nameLookup == null) return null;
196                 IJavaElement[] pkgs = nameLookup.findPackageFragments(getName(), false/*exact match*/);
197                 if (pkgs == null) return null;
198                 return pkgs[0];
199         }
200
201         /*
202          * @see IBinding#getKey()
203          */
204         public String getKey() {
205                 return new String(this.binding.computeUniqueKey());
206         }
207
208         /*
209          * @see IBinding#isEqualTo(Binding)
210          * @since 3.1
211          */
212         public boolean isEqualTo(IBinding other) {
213                 if (other == this) {
214                         // identical binding - equal (key or no key)
215                         return true;
216                 }
217                 if (other == null) {
218                         // other binding missing
219                         return false;
220                 }
221                 if (!(other instanceof PackageBinding)) {
222                         return false;
223                 }
224                 net.sourceforge.phpdt.internal.compiler.lookup.PackageBinding packageBinding2 = ((PackageBinding) other).binding;
225                 return CharOperation.equals(this.binding.compoundName, packageBinding2.compoundName);
226         }
227
228         private void computeNameAndComponents() {
229                 char[][] compoundName = this.binding.compoundName;
230                 if (compoundName == CharOperation.NO_CHAR_CHAR || compoundName == null) {
231                         name = UNNAMED;
232                         components = NO_NAME_COMPONENTS;
233                 } else {
234                         int length = compoundName.length;
235                         components = new String[length];
236                         StringBuffer buffer = new StringBuffer();
237                         for (int i = 0; i < length - 1; i++) {
238                                 components[i] = new String(compoundName[i]);
239                                 buffer.append(compoundName[i]).append(PACKAGE_NAME_SEPARATOR);
240                         }
241                         components[length - 1] = new String(compoundName[length - 1]);
242                         buffer.append(compoundName[length - 1]);
243                         name = buffer.toString();
244                 }
245         }
246
247         /*
248          * For debugging purpose only.
249          * @see java.lang.Object#toString()
250          */
251         public String toString() {
252                 return this.binding.toString();
253         }
254 }