1 | /******************************************************************************* |
---|---|
2 | * Copyright (c) 2000, 2017 IBM Corporation and others. |
3 | * |
4 | * This program and the accompanying materials |
5 | * are made available under the terms of the Eclipse Public License 2.0 |
6 | * which accompanies this distribution, and is available at |
7 | * https://www.eclipse.org/legal/epl-2.0/ |
8 | * |
9 | * SPDX-License-Identifier: EPL-2.0 |
10 | * |
11 | * Contributors: |
12 | * IBM Corporation - initial API and implementation |
13 | *******************************************************************************/ |
14 | |
15 | package org.eclipse.jdt.core.dom; |
16 | |
17 | import java.util.Iterator; |
18 | import java.util.List; |
19 | |
20 | import org.eclipse.core.runtime.IStatus; |
21 | import org.eclipse.core.runtime.Status; |
22 | import org.eclipse.jdt.core.ICompilationUnit; |
23 | import org.eclipse.jdt.core.IJavaElement; |
24 | import org.eclipse.jdt.core.IPackageFragment; |
25 | import org.eclipse.jdt.core.IPackageFragmentRoot; |
26 | import org.eclipse.jdt.core.JavaCore; |
27 | import org.eclipse.jdt.core.JavaModelException; |
28 | |
29 | import org.eclipse.jdt.core.compiler.CharOperation; |
30 | import org.eclipse.jdt.internal.compiler.env.IBinaryAnnotation; |
31 | import org.eclipse.jdt.internal.compiler.env.IBinaryType; |
32 | import org.eclipse.jdt.internal.compiler.env.INameEnvironment; |
33 | import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer; |
34 | import org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding; |
35 | import org.eclipse.jdt.internal.compiler.lookup.ModuleBinding; |
36 | import org.eclipse.jdt.internal.compiler.lookup.TypeConstants; |
37 | import org.eclipse.jdt.internal.compiler.util.Util; |
38 | import org.eclipse.jdt.internal.core.NameLookup; |
39 | import org.eclipse.jdt.internal.core.SearchableEnvironment; |
40 | |
41 | /** |
42 | * Internal implementation of package bindings. |
43 | */ |
44 | @SuppressWarnings("rawtypes") |
45 | class PackageBinding implements IPackageBinding { |
46 | |
47 | private static final String[] NO_NAME_COMPONENTS = CharOperation.NO_STRINGS; |
48 | private static final String UNNAMED = Util.EMPTY_STRING; |
49 | private static final char PACKAGE_NAME_SEPARATOR = '.'; |
50 | |
51 | private org.eclipse.jdt.internal.compiler.lookup.PackageBinding binding; |
52 | private String name; |
53 | private BindingResolver resolver; |
54 | private String[] components; |
55 | |
56 | PackageBinding(org.eclipse.jdt.internal.compiler.lookup.PackageBinding binding, BindingResolver resolver) { |
57 | this.binding = binding; |
58 | this.resolver = resolver; |
59 | } |
60 | |
61 | @Override |
62 | public IAnnotationBinding[] getAnnotations() { |
63 | try { |
64 | INameEnvironment nameEnvironment = this.binding.environment.nameEnvironment; |
65 | if (!(nameEnvironment instanceof SearchableEnvironment)) |
66 | return AnnotationBinding.NoAnnotations; |
67 | NameLookup nameLookup = ((SearchableEnvironment) nameEnvironment).nameLookup; |
68 | if (nameLookup == null) |
69 | return AnnotationBinding.NoAnnotations; |
70 | final String pkgName = getName(); |
71 | IPackageFragment[] pkgs = nameLookup.findPackageFragments(pkgName, false/*exact match*/); |
72 | if (pkgs == null) |
73 | return AnnotationBinding.NoAnnotations; |
74 | |
75 | for (int i = 0, len = pkgs.length; i < len; i++) { |
76 | int fragType = pkgs[i].getKind(); |
77 | switch(fragType) { |
78 | case IPackageFragmentRoot.K_SOURCE: |
79 | String unitName = "package-info.java"; //$NON-NLS-1$ |
80 | ICompilationUnit unit = pkgs[i].getCompilationUnit(unitName); |
81 | if (unit != null && unit.exists()) { |
82 | ASTParser p = ASTParser.newParser(AST.JLS3_INTERNAL); |
83 | p.setSource(unit); |
84 | p.setResolveBindings(true); |
85 | p.setUnitName(unitName); |
86 | p.setFocalPosition(0); |
87 | p.setKind(ASTParser.K_COMPILATION_UNIT); |
88 | CompilationUnit domUnit = (CompilationUnit) p.createAST(null); |
89 | PackageDeclaration pkgDecl = domUnit.getPackage(); |
90 | if (pkgDecl != null) { |
91 | List annos = pkgDecl.annotations(); |
92 | if (annos == null || annos.isEmpty()) |
93 | return AnnotationBinding.NoAnnotations; |
94 | IAnnotationBinding[] result = new IAnnotationBinding[annos.size()]; |
95 | int index=0; |
96 | for (Iterator it = annos.iterator(); it.hasNext(); index++) { |
97 | result[index] = ((Annotation) it.next()).resolveAnnotationBinding(); |
98 | // not resolving bindings |
99 | if (result[index] == null) |
100 | return AnnotationBinding.NoAnnotations; |
101 | } |
102 | return result; |
103 | } |
104 | } |
105 | break; |
106 | case IPackageFragmentRoot.K_BINARY: |
107 | NameEnvironmentAnswer answer = |
108 | nameEnvironment.findType(TypeConstants.PACKAGE_INFO_NAME, this.binding.compoundName); |
109 | if (answer != null && answer.isBinaryType()) { |
110 | IBinaryType type = answer.getBinaryType(); |
111 | char[][][] missingTypeNames = type.getMissingTypeNames(); |
112 | IBinaryAnnotation[] binaryAnnotations = type.getAnnotations(); |
113 | org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding[] binaryInstances = |
114 | BinaryTypeBinding.createAnnotations(binaryAnnotations, this.binding.environment, missingTypeNames); |
115 | org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding[] allInstances = |
116 | org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding.addStandardAnnotations(binaryInstances, type.getTagBits(), this.binding.environment); |
117 | int total = allInstances.length; |
118 | IAnnotationBinding[] domInstances = new AnnotationBinding[total]; |
119 | for (int a = 0; a < total; a++) { |
120 | final IAnnotationBinding annotationInstance = this.resolver.getAnnotationInstance(allInstances[a]); |
121 | if (annotationInstance == null) {// not resolving binding |
122 | return AnnotationBinding.NoAnnotations; |
123 | } |
124 | domInstances[a] = annotationInstance; |
125 | } |
126 | return domInstances; |
127 | } |
128 | } |
129 | } |
130 | } catch(JavaModelException e) { |
131 | return AnnotationBinding.NoAnnotations; |
132 | } |
133 | return AnnotationBinding.NoAnnotations; |
134 | } |
135 | |
136 | @Override |
137 | public IModuleBinding getModule() { |
138 | ModuleBinding moduleBinding = this.binding.enclosingModule; |
139 | return moduleBinding != null ? this.resolver.getModuleBinding(moduleBinding) : null; |
140 | } |
141 | |
142 | @Override |
143 | public String getName() { |
144 | if (this.name == null) { |
145 | computeNameAndComponents(); |
146 | } |
147 | return this.name; |
148 | } |
149 | |
150 | @Override |
151 | public boolean isUnnamed() { |
152 | return getName().equals(UNNAMED); |
153 | } |
154 | |
155 | @Override |
156 | public String[] getNameComponents() { |
157 | if (this.components == null) { |
158 | computeNameAndComponents(); |
159 | } |
160 | return this.components; |
161 | } |
162 | |
163 | @Override |
164 | public int getKind() { |
165 | return IBinding.PACKAGE; |
166 | } |
167 | |
168 | @Override |
169 | public int getModifiers() { |
170 | return Modifier.NONE; |
171 | } |
172 | |
173 | @Override |
174 | public boolean isDeprecated() { |
175 | return false; |
176 | } |
177 | |
178 | /** |
179 | * @see IBinding#isRecovered() |
180 | */ |
181 | @Override |
182 | public boolean isRecovered() { |
183 | return false; |
184 | } |
185 | |
186 | /** |
187 | * @see IBinding#isSynthetic() |
188 | */ |
189 | @Override |
190 | public boolean isSynthetic() { |
191 | return false; |
192 | } |
193 | |
194 | @Override |
195 | public IJavaElement getJavaElement() { |
196 | INameEnvironment nameEnvironment = this.binding.environment.nameEnvironment; // a package binding always has a LooupEnvironment set |
197 | if (!(nameEnvironment instanceof SearchableEnvironment)) return null; |
198 | // this is not true in standalone DOM/AST |
199 | NameLookup nameLookup = ((SearchableEnvironment) nameEnvironment).nameLookup; |
200 | if (nameLookup == null) return null; |
201 | IJavaElement[] pkgs = nameLookup.findPackageFragments(getName(), false/*exact match*/); |
202 | if (pkgs == null) return null; |
203 | if (pkgs.length == 0) { |
204 | // add additional tracing as this should not happen |
205 | org.eclipse.jdt.internal.core.util.Util.log( |
206 | new Status( |
207 | IStatus.WARNING, |
208 | JavaCore.PLUGIN_ID, |
209 | "Searching for package " + getName() + " returns an empty array")); //$NON-NLS-1$ //$NON-NLS-2$ |
210 | return null; |
211 | } |
212 | return pkgs[0]; |
213 | } |
214 | |
215 | @Override |
216 | public String getKey() { |
217 | return new String(this.binding.computeUniqueKey()); |
218 | } |
219 | |
220 | @Override |
221 | public boolean isEqualTo(IBinding other) { |
222 | if (other == this) { |
223 | // identical binding - equal (key or no key) |
224 | return true; |
225 | } |
226 | if (other == null) { |
227 | // other binding missing |
228 | return false; |
229 | } |
230 | if (!(other instanceof PackageBinding)) { |
231 | return false; |
232 | } |
233 | org.eclipse.jdt.internal.compiler.lookup.PackageBinding packageBinding2 = ((PackageBinding) other).binding; |
234 | return CharOperation.equals(this.binding.compoundName, packageBinding2.compoundName); |
235 | } |
236 | |
237 | private void computeNameAndComponents() { |
238 | char[][] compoundName = this.binding.compoundName; |
239 | if (compoundName == CharOperation.NO_CHAR_CHAR || compoundName == null) { |
240 | this.name = UNNAMED; |
241 | this.components = NO_NAME_COMPONENTS; |
242 | } else { |
243 | int length = compoundName.length; |
244 | this.components = new String[length]; |
245 | StringBuilder buffer = new StringBuilder(); |
246 | for (int i = 0; i < length - 1; i++) { |
247 | this.components[i] = new String(compoundName[i]); |
248 | buffer.append(compoundName[i]).append(PACKAGE_NAME_SEPARATOR); |
249 | } |
250 | this.components[length - 1] = new String(compoundName[length - 1]); |
251 | buffer.append(compoundName[length - 1]); |
252 | this.name = buffer.toString(); |
253 | } |
254 | } |
255 | |
256 | org.eclipse.jdt.internal.compiler.lookup.PackageBinding getCompilerBinding() { |
257 | return this.binding; |
258 | } |
259 | |
260 | /* |
261 | * For debugging purpose only. |
262 | * @see java.lang.Object#toString() |
263 | */ |
264 | @Override |
265 | public String toString() { |
266 | return this.binding.toString(); |
267 | } |
268 | } |
269 |
Members