1 /*******************************************************************************
2 * Copyright (c) 2000, 2020 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 package org.eclipse.jdt.internal.core;
15
16 import java.util.ArrayList;
17 import java.util.HashMap;
18
19 import org.eclipse.jdt.core.*;
20 import org.eclipse.jdt.core.compiler.CharOperation;
21 import org.eclipse.jdt.internal.compiler.env.IBinaryAnnotation;
22 import org.eclipse.jdt.internal.compiler.env.IBinaryElementValuePair;
23 import org.eclipse.jdt.internal.compiler.env.IBinaryField;
24 import org.eclipse.jdt.internal.compiler.env.IBinaryMethod;
25 import org.eclipse.jdt.internal.compiler.env.IBinaryNestedType;
26 import org.eclipse.jdt.internal.compiler.env.IBinaryType;
27 import org.eclipse.jdt.internal.compiler.lookup.TagBits;
28 import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
29 import org.eclipse.jdt.internal.compiler.util.SuffixConstants;
30
31 /**
32 * Element info for <code>ClassFile</code> handles.
33 */
34
35 @SuppressWarnings({"rawtypes", "unchecked"})
36 /* package */ class ClassFileInfo extends OpenableElementInfo implements SuffixConstants {
37 /**
38 * The children of the <code>BinaryType</code> corresponding to our
39 * <code>ClassFile</code>. These are kept here because we don't have
40 * access to the <code>BinaryType</code> info (<code>ClassFileReader</code>).
41 */
42 protected JavaElement[] binaryChildren = null;
43 /*
44 * The type parameters in this class file.
45 */
46 protected ITypeParameter[] typeParameters;
47
generateAnnotationsInfos(JavaElement member, IBinaryAnnotation[] binaryAnnotations, long tagBits, HashMap newElements)48 private void generateAnnotationsInfos(JavaElement member, IBinaryAnnotation[] binaryAnnotations, long tagBits, HashMap newElements) {
49 generateAnnotationsInfos(member, null, binaryAnnotations, tagBits, newElements);
50 }
51 /**
52 * Creates the handles and infos for the annotations of the given binary member.
53 * Adds new handles to the given vector.
54 */
generateAnnotationsInfos(JavaElement member, char[] parameterName, IBinaryAnnotation[] binaryAnnotations, long tagBits, HashMap newElements)55 private void generateAnnotationsInfos(JavaElement member, char[] parameterName, IBinaryAnnotation[] binaryAnnotations, long tagBits, HashMap newElements) {
56 if (binaryAnnotations != null) {
57 for (int i = 0, length = binaryAnnotations.length; i < length; i++) {
58 IBinaryAnnotation annotationInfo = binaryAnnotations[i];
59 generateAnnotationInfo(member, parameterName, newElements, annotationInfo, null);
60 }
61 }
62 generateStandardAnnotationsInfos(member, parameterName, tagBits, newElements);
63 }
generateAnnotationInfo(JavaElement parent, HashMap newElements, IBinaryAnnotation annotationInfo, String memberValuePairName)64 private void generateAnnotationInfo(JavaElement parent, HashMap newElements, IBinaryAnnotation annotationInfo, String memberValuePairName) {
65 generateAnnotationInfo(parent, null, newElements, annotationInfo, memberValuePairName);
66 }
generateAnnotationInfo(JavaElement parent, char[] parameterName, HashMap newElements, IBinaryAnnotation annotationInfo, String memberValuePairName)67 private void generateAnnotationInfo(JavaElement parent, char[] parameterName, HashMap newElements, IBinaryAnnotation annotationInfo, String memberValuePairName) {
68 char[] typeName = org.eclipse.jdt.core.Signature.toCharArray(CharOperation.replaceOnCopy(annotationInfo.getTypeName(), '/', '.'));
69 Annotation annotation = new Annotation(parent, new String(typeName), memberValuePairName);
70 while (newElements.containsKey(annotation)) {
71 annotation.occurrenceCount++;
72 }
73 newElements.put(annotation, annotationInfo);
74 IBinaryElementValuePair[] pairs = annotationInfo.getElementValuePairs();
75 for (int i = 0, length = pairs.length; i < length; i++) {
76 Object value = pairs[i].getValue();
77 if (value instanceof IBinaryAnnotation) {
78 generateAnnotationInfo(annotation, newElements, (IBinaryAnnotation) value, new String(pairs[i].getName()));
79 } else if (value instanceof Object[]) {
80 // if the value is an array, it can have no more than 1 dimension - no need to recurse
81 Object[] valueArray = (Object[]) value;
82 for (int j = 0, valueArrayLength = valueArray.length; j < valueArrayLength; j++) {
83 Object nestedValue = valueArray[j];
84 if (nestedValue instanceof IBinaryAnnotation) {
85 generateAnnotationInfo(annotation, newElements, (IBinaryAnnotation) nestedValue, new String(pairs[i].getName()));
86 }
87 }
88 }
89 }
90 }
generateStandardAnnotationsInfos(JavaElement javaElement, char[] parameterName, long tagBits, HashMap newElements)91 private void generateStandardAnnotationsInfos(JavaElement javaElement, char[] parameterName, long tagBits, HashMap newElements) {
92 if ((tagBits & TagBits.AllStandardAnnotationsMask) == 0)
93 return;
94 if ((tagBits & TagBits.AnnotationTargetMASK) != 0) {
95 generateStandardAnnotation(javaElement, TypeConstants.JAVA_LANG_ANNOTATION_TARGET, getTargetElementTypes(tagBits), newElements);
96 }
97 if ((tagBits & TagBits.AnnotationRetentionMASK) != 0) {
98 generateStandardAnnotation(javaElement, TypeConstants.JAVA_LANG_ANNOTATION_RETENTION, getRetentionPolicy(tagBits), newElements);
99 }
100 if ((tagBits & TagBits.AnnotationDeprecated) != 0) {
101 generateStandardAnnotation(javaElement, TypeConstants.JAVA_LANG_DEPRECATED, Annotation.NO_MEMBER_VALUE_PAIRS, newElements);
102 }
103 if ((tagBits & TagBits.AnnotationDocumented) != 0) {
104 generateStandardAnnotation(javaElement, TypeConstants.JAVA_LANG_ANNOTATION_DOCUMENTED, Annotation.NO_MEMBER_VALUE_PAIRS, newElements);
105 }
106 if ((tagBits & TagBits.AnnotationInherited) != 0) {
107 generateStandardAnnotation(javaElement, TypeConstants.JAVA_LANG_ANNOTATION_INHERITED, Annotation.NO_MEMBER_VALUE_PAIRS, newElements);
108 }
109 if ((tagBits & TagBits.AnnotationPolymorphicSignature) != 0) {
110 generateStandardAnnotation(javaElement, TypeConstants.JAVA_LANG_INVOKE_METHODHANDLE_$_POLYMORPHICSIGNATURE, Annotation.NO_MEMBER_VALUE_PAIRS, newElements);
111 }
112 if ((tagBits & TagBits.AnnotationSafeVarargs) != 0) {
113 generateStandardAnnotation(javaElement, TypeConstants.JAVA_LANG_SAFEVARARGS, Annotation.NO_MEMBER_VALUE_PAIRS, newElements);
114 }
115 // note that JAVA_LANG_SUPPRESSWARNINGS and JAVA_LANG_OVERRIDE cannot appear in binaries
116 }
117
generateStandardAnnotation(JavaElement javaElement, char[][] typeName, IMemberValuePair[] members, HashMap newElements)118 private void generateStandardAnnotation(JavaElement javaElement, char[][] typeName, IMemberValuePair[] members, HashMap newElements) {
119 IAnnotation annotation = new Annotation(javaElement, new String(CharOperation.concatWith(typeName, '.')));
120 AnnotationInfo annotationInfo = new AnnotationInfo();
121 annotationInfo.members = members;
122 newElements.put(annotation, annotationInfo);
123 }
124
getTargetElementTypes(long tagBits)125 private IMemberValuePair[] getTargetElementTypes(long tagBits) {
126 ArrayList values = new ArrayList();
127 String elementType = new String(CharOperation.concatWith(TypeConstants.JAVA_LANG_ANNOTATION_ELEMENTTYPE, '.')) + '.';
128 if ((tagBits & TagBits.AnnotationForType) != 0) {
129 values.add(elementType + new String(TypeConstants.TYPE));
130 }
131 if ((tagBits & TagBits.AnnotationForField) != 0) {
132 values.add(elementType + new String(TypeConstants.UPPER_FIELD));
133 }
134 if ((tagBits & TagBits.AnnotationForMethod) != 0) {
135 values.add(elementType + new String(TypeConstants.UPPER_METHOD));
136 }
137 if ((tagBits & TagBits.AnnotationForParameter) != 0) {
138 values.add(elementType + new String(TypeConstants.UPPER_PARAMETER));
139 }
140 if ((tagBits & TagBits.AnnotationForConstructor) != 0) {
141 values.add(elementType + new String(TypeConstants.UPPER_CONSTRUCTOR));
142 }
143 if ((tagBits & TagBits.AnnotationForLocalVariable) != 0) {
144 values.add(elementType + new String(TypeConstants.UPPER_LOCAL_VARIABLE));
145 }
146 if ((tagBits & TagBits.AnnotationForAnnotationType) != 0) {
147 values.add(elementType + new String(TypeConstants.UPPER_ANNOTATION_TYPE));
148 }
149 if ((tagBits & TagBits.AnnotationForPackage) != 0) {
150 values.add(elementType + new String(TypeConstants.UPPER_PACKAGE));
151 }
152 if ((tagBits & TagBits.AnnotationForTypeUse) != 0) {
153 values.add(elementType + new String(TypeConstants.TYPE_USE_TARGET));
154 }
155 if ((tagBits & TagBits.AnnotationForTypeParameter) != 0) {
156 values.add(elementType + new String(TypeConstants.TYPE_PARAMETER_TARGET));
157 }
158 if ((tagBits & TagBits.AnnotationForModule) != 0) {
159 values.add(elementType + new String(TypeConstants.UPPER_MODULE));
160 }
161 if ((tagBits & TagBits.AnnotationForRecordComponent) != 0) {
162 values.add(elementType + new String(TypeConstants.UPPER_RECORD_COMPONENT));
163 }
164 final Object value;
165 if (values.size() == 0) {
166 if ((tagBits & TagBits.AnnotationTarget) != 0)
167 value = CharOperation.NO_STRINGS;
168 else
169 return Annotation.NO_MEMBER_VALUE_PAIRS;
170 } else if (values.size() == 1) {
171 value = values.get(0);
172 } else {
173 value = values.toArray(new String[values.size()]);
174 }
175 return new IMemberValuePair[] {
176 new IMemberValuePair() {
177 @Override
178 public int getValueKind() {
179 return IMemberValuePair.K_QUALIFIED_NAME;
180 }
181 @Override
182 public Object getValue() {
183 return value;
184 }
185 @Override
186 public String getMemberName() {
187 return new String(TypeConstants.VALUE);
188 }
189 }
190 };
191 }
192
193 private IMemberValuePair[] getRetentionPolicy(long tagBits) {
194 if ((tagBits & TagBits.AnnotationRetentionMASK) == 0)
195 return Annotation.NO_MEMBER_VALUE_PAIRS;
196 String retention = null;
197 if ((tagBits & TagBits.AnnotationRuntimeRetention) == TagBits.AnnotationRuntimeRetention) {
198 // TagBits.AnnotationRuntimeRetention combines both TagBits.AnnotationClassRetention & TagBits.AnnotationSourceRetention
199 retention = new String(CharOperation.concatWith(TypeConstants.JAVA_LANG_ANNOTATION_RETENTIONPOLICY, '.')) + '.' + new String(TypeConstants.UPPER_RUNTIME);
200 } else if ((tagBits & TagBits.AnnotationSourceRetention) != 0) {
201 retention = new String(CharOperation.concatWith(TypeConstants.JAVA_LANG_ANNOTATION_RETENTIONPOLICY, '.')) + '.' + new String(TypeConstants.UPPER_SOURCE);
202 } else {
203 retention = new String(CharOperation.concatWith(TypeConstants.JAVA_LANG_ANNOTATION_RETENTIONPOLICY, '.')) + '.' + new String(TypeConstants.UPPER_CLASS);
204 }
205 final String value = retention;
206 return
207 new IMemberValuePair[] {
208 new IMemberValuePair() {
209 @Override
210 public int getValueKind() {
211 return IMemberValuePair.K_QUALIFIED_NAME;
212 }
213 @Override
214 public Object getValue() {
215 return value;
216 }
217 @Override
218 public String getMemberName() {
219 return new String(TypeConstants.VALUE);
220 }
221 }
222 };
223 }
224
225 /**
226 * Creates the handles and infos for the fields of the given binary type.
227 * Adds new handles to the given vector.
228 */
229 private void generateFieldInfos(IType type, IBinaryType typeInfo, HashMap newElements, ArrayList childrenHandles) {
230 // Make the fields
231 IBinaryField[] fields = typeInfo.getFields();
232 if (fields == null) {
233 return;
234 }
235 JavaModelManager manager = JavaModelManager.getJavaModelManager();
236 for (int i = 0, fieldCount = fields.length; i < fieldCount; i++) {
237 IBinaryField fieldInfo = fields[i];
238 BinaryField field = new BinaryField((JavaElement)type, manager.intern(new String(fieldInfo.getName())));
239 newElements.put(field, fieldInfo);
240 childrenHandles.add(field);
241 generateAnnotationsInfos(field, fieldInfo.getAnnotations(), fieldInfo.getTagBits(), newElements);
242 }
243 }
244 /**
245 * Creates the handles for the inner types of the given binary type.
246 * Adds new handles to the given vector.
247 */
248 private void generateInnerClassHandles(IType type, IBinaryType typeInfo, ArrayList childrenHandles) {
249 // Add inner types
250 // If the current type is an inner type, innerClasses returns
251 // an extra entry for the current type. This entry must be removed.
252 // Can also return an entry for the enclosing type of an inner type.
253 IBinaryNestedType[] innerTypes = typeInfo.getMemberTypes();
254 if (innerTypes != null) {
255 IPackageFragment pkg = (IPackageFragment) type.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
256 for (int i = 0, typeCount = innerTypes.length; i < typeCount; i++) {
257 IBinaryNestedType binaryType = innerTypes[i];
258 IClassFile parentClassFile= pkg.getClassFile(new String(ClassFile.unqualifiedName(binaryType.getName())) + SUFFIX_STRING_class);
259 IType innerType = new BinaryType((JavaElement) parentClassFile, ClassFile.simpleName(binaryType.getName()));
260 childrenHandles.add(innerType);
261 }
262 }
263 }
264 /**
265 * Creates the handles and infos for the methods of the given binary type.
266 * Adds new handles to the given vector.
267 */
268 private void generateMethodInfos(IType type, IBinaryType typeInfo, HashMap newElements, ArrayList childrenHandles, ArrayList typeParameterHandles) {
269 IBinaryMethod[] methods = typeInfo.getMethods();
270 if (methods == null) {
271 return;
272 }
273 for (int i = 0, methodCount = methods.length; i < methodCount; i++) {
274 IBinaryMethod methodInfo = methods[i];
275 final boolean isConstructor = methodInfo.isConstructor();
276 boolean isEnum = false;
277 try {
278 isEnum = type.isEnum();
279 } catch (JavaModelException e) {
280 // ignore
281 }
282 // TODO (jerome) filter out synthetic members
283 // indexer should not index them as well
284 // if ((methodInfo.getModifiers() & IConstants.AccSynthetic) != 0) continue; // skip synthetic
285 boolean useGenericSignature = true;
286 char[] signature = methodInfo.getGenericSignature();
287 String[] pNames = null;
288 if (signature == null) {
289 useGenericSignature = false;
290 signature = methodInfo.getMethodDescriptor();
291 if (isEnum && isConstructor) {
292 pNames = Signature.getParameterTypes(new String(signature));
293 int length = pNames.length - 2;
294 if (length >= 0) // https://bugs.eclipse.org/bugs/show_bug.cgi?id=436347
295 System.arraycopy(pNames, 2, pNames = new String[length], 0, length);
296 }
297 }
298 String selector = new String(methodInfo.getSelector());
299 if (isConstructor) {
300 selector = type.getElementName();
301 }
302 try {
303 if (!(isEnum && isConstructor && !useGenericSignature)) {
304 pNames = Signature.getParameterTypes(new String(signature));
305 }
306 if (isConstructor
307 && useGenericSignature
308 && type.isMember()
309 && !Flags.isStatic(type.getFlags())) {
310 int length = pNames.length;
311 System.arraycopy(pNames, 0, (pNames = new String[length + 1]), 1, length);
312 char[] descriptor = methodInfo.getMethodDescriptor();
313 final String[] parameterTypes = Signature.getParameterTypes(new String(descriptor));
314 pNames[0] = parameterTypes[0];
315 }
316 } catch (IllegalArgumentException | JavaModelException e) {
317 // protect against malformed .class file (e.g. com/sun/crypto/provider/SunJCE_b.class has a 'a' generic signature)
318 signature = methodInfo.getMethodDescriptor();
319 pNames = Signature.getParameterTypes(new String(signature));
320 }
321 char[][] paramNames= new char[pNames.length][];
322 for (int j= 0; j < pNames.length; j++) {
323 paramNames[j]= pNames[j].toCharArray();
324 }
325 char[][] parameterTypes = ClassFile.translatedNames(paramNames);
326 JavaModelManager manager = JavaModelManager.getJavaModelManager();
327 selector = manager.intern(selector);
328 for (int j= 0; j < pNames.length; j++) {
329 pNames[j]= manager.intern(new String(parameterTypes[j]));
330 }
331 BinaryMethod method = new BinaryMethod((JavaElement)type, selector, pNames);
332 childrenHandles.add(method);
333
334 // ensure that 2 binary methods with the same signature but with different return types have different occurrence counts.
335 // (case of bridge methods in 1.5)
336 while (newElements.containsKey(method))
337 method.occurrenceCount++;
338
339 newElements.put(method, methodInfo);
340
341 int max = pNames.length;
342 char[][] argumentNames = methodInfo.getArgumentNames();
343 if (argumentNames == null || argumentNames.length < max) {
344 argumentNames = new char[max][];
345 for (int j = 0; j < max; j++) {
346 argumentNames[j] = ("arg" + j).toCharArray(); //$NON-NLS-1$
347 }
348 }
349 int startIndex = 0;
350 try {
351 if (isConstructor) {
352 if (isEnum) {
353 startIndex = 2;
354 } else if (type.isMember()
355 && !Flags.isStatic(type.getFlags())) {
356 startIndex = 1;
357 }
358 }
359 } catch(JavaModelException e) {
360 // ignore
361 }
362 for (int j = startIndex; j < max; j++) {
363 IBinaryAnnotation[] parameterAnnotations = methodInfo.getParameterAnnotations(j - startIndex, typeInfo.getFileName());
364 if (parameterAnnotations != null) {
365 LocalVariable localVariable = new LocalVariable(
366 method,
367 new String(argumentNames[j]),
368 0,
369 -1,
370 0,
371 -1,
372 method.parameterTypes[j],
373 null,
374 -1,
375 true);
376 generateAnnotationsInfos(localVariable, argumentNames[j], parameterAnnotations, methodInfo.getTagBits(), newElements);
377 }
378 }
379 generateTypeParameterInfos(method, signature, newElements, typeParameterHandles);
380 generateAnnotationsInfos(method, methodInfo.getAnnotations(), methodInfo.getTagBits(), newElements);
381 Object defaultValue = methodInfo.getDefaultValue();
382 if (defaultValue instanceof IBinaryAnnotation) {
383 generateAnnotationInfo(method, newElements, (IBinaryAnnotation) defaultValue, new String(methodInfo.getSelector()));
384 }
385 }
386 }
387 /**
388 * Creates the handles and infos for the type parameter of the given binary member.
389 * Adds new handles to the given vector.
390 */
391 private void generateTypeParameterInfos(BinaryMember parent, char[] signature, HashMap newElements, ArrayList typeParameterHandles) {
392 if (signature == null) return;
393 char[][] typeParameterSignatures = Signature.getTypeParameters(signature);
394 for (int i = 0, typeParameterCount = typeParameterSignatures.length; i < typeParameterCount; i++) {
395 char[] typeParameterSignature = typeParameterSignatures[i];
396 char[] typeParameterName = Signature.getTypeVariable(typeParameterSignature);
397 CharOperation.replace(typeParameterSignature, '/', '.');
398 char[][] typeParameterBoundSignatures = Signature.getTypeParameterBounds(typeParameterSignature);
399 int boundLength = typeParameterBoundSignatures.length;
400 char[][] typeParameterBounds = new char[boundLength][];
401 for (int j = 0; j < boundLength; j++) {
402 typeParameterBounds[j] = Signature.toCharArray(typeParameterBoundSignatures[j]);
403 }
404 TypeParameter typeParameter = new TypeParameter(parent, new String(typeParameterName));
405 TypeParameterElementInfo info = new TypeParameterElementInfo();
406 info.bounds = typeParameterBounds;
407 info.boundsSignatures = typeParameterBoundSignatures;
408 typeParameterHandles.add(typeParameter);
409
410 // ensure that 2 binary methods with the same signature but with different return types have different occurence counts.
411 // (case of bridge methods in 1.5)
412 while (newElements.containsKey(typeParameter))
413 typeParameter.occurrenceCount++;
414
415 newElements.put(typeParameter, info);
416 }
417 }
418 /**
419 * Creates the handles for <code>BinaryMember</code>s defined in this
420 * <code>ClassFile</code> and adds them to the
421 * <code>JavaModelManager</code>'s cache.
422 */
423 protected void readBinaryChildren(ClassFile classFile, HashMap newElements, IBinaryType typeInfo) {
424 ArrayList childrenHandles = new ArrayList();
425 BinaryType type = (BinaryType) classFile.getType();
426 ArrayList typeParameterHandles = new ArrayList();
427 if (typeInfo != null) { //may not be a valid class file
428 generateAnnotationsInfos(type, typeInfo.getAnnotations(), typeInfo.getTagBits(), newElements);
429 generateTypeParameterInfos(type, typeInfo.getGenericSignature(), newElements, typeParameterHandles);
430 generateFieldInfos(type, typeInfo, newElements, childrenHandles);
431 generateMethodInfos(type, typeInfo, newElements, childrenHandles, typeParameterHandles);
432 generateInnerClassHandles(type, typeInfo, childrenHandles); // Note inner class are separate openables that are not opened here: no need to pass in newElements
433 }
434
435 this.binaryChildren = new JavaElement[childrenHandles.size()];
436 childrenHandles.toArray(this.binaryChildren);
437 int typeParameterHandleSize = typeParameterHandles.size();
438 if (typeParameterHandleSize == 0) {
439 this.typeParameters = TypeParameter.NO_TYPE_PARAMETERS;
440 } else {
441 this.typeParameters = new ITypeParameter[typeParameterHandleSize];
442 typeParameterHandles.toArray(this.typeParameters);
443 }
444 }
445 /**
446 * Removes the binary children handles and remove their infos from
447 * the <code>JavaModelManager</code>'s cache.
448 */
449 void removeBinaryChildren() throws JavaModelException {
450 if (this.binaryChildren != null) {
451 JavaModelManager manager = JavaModelManager.getJavaModelManager();
452 for (int i = 0; i <this.binaryChildren.length; i++) {
453 JavaElement child = this.binaryChildren[i];
454 if (child instanceof BinaryType) {
455 manager.removeInfoAndChildren((JavaElement)child.getParent());
456 } else {
457 manager.removeInfoAndChildren(child);
458 }
459 }
460 this.binaryChildren = JavaElement.NO_ELEMENTS;
461 }
462 if (this.typeParameters != null) {
463 JavaModelManager manager = JavaModelManager.getJavaModelManager();
464 for (int i = 0; i <this.typeParameters.length; i++) {
465 TypeParameter typeParameter = (TypeParameter) this.typeParameters[i];
466 manager.removeInfoAndChildren(typeParameter);
467 }
468 this.typeParameters = TypeParameter.NO_TYPE_PARAMETERS;
469 }
470 }
471 }
472