/** * This test checks if it is searched for types when no package was found. The test {@link * #test01()} checks if the package search is executed before the type search. The search for * packages is much faster than searching for types, therefore it should get executed before * searching for types. */ public void test02() { NameEnvironmentDummy nameEnv = new NameEnvironmentDummy(false); PackageBinding packageBinding = new PackageBinding(new LookupEnvironment(null, new CompilerOptions(), null, nameEnv)); Binding resultBinding = packageBinding.getTypeOrPackage("java.lang.String".toCharArray()); assertNull(resultBinding); // (not implemented) assertTrue(nameEnv.isPackageSearchExecuted); assertTrue(nameEnv.isTypeSearchExecuted); }
/** Figures if @Deprecated annotation is specified, do not resolve entire annotations. */ public static void resolveDeprecatedAnnotations( BlockScope scope, Annotation[] annotations, Binding recipient) { if (recipient != null) { int kind = recipient.kind(); if (annotations != null) { int length; if ((length = annotations.length) >= 0) { switch (kind) { case Binding.PACKAGE: PackageBinding packageBinding = (PackageBinding) recipient; if ((packageBinding.tagBits & TagBits.DeprecatedAnnotationResolved) != 0) return; break; case Binding.TYPE: case Binding.GENERIC_TYPE: ReferenceBinding type = (ReferenceBinding) recipient; if ((type.tagBits & TagBits.DeprecatedAnnotationResolved) != 0) return; break; case Binding.METHOD: MethodBinding method = (MethodBinding) recipient; if ((method.tagBits & TagBits.DeprecatedAnnotationResolved) != 0) return; break; case Binding.FIELD: FieldBinding field = (FieldBinding) recipient; if ((field.tagBits & TagBits.DeprecatedAnnotationResolved) != 0) return; break; case Binding.LOCAL: LocalVariableBinding local = (LocalVariableBinding) recipient; if ((local.tagBits & TagBits.DeprecatedAnnotationResolved) != 0) return; break; default: return; } for (int i = 0; i < length; i++) { TypeReference annotationTypeRef = annotations[i].type; // only resolve type name if 'Deprecated' last token if (!CharOperation.equals( TypeConstants.JAVA_LANG_DEPRECATED[2], annotationTypeRef.getLastToken())) return; TypeBinding annotationType = annotations[i].type.resolveType(scope); if (annotationType != null && annotationType.isValidBinding() && annotationType.id == TypeIds.T_JavaLangDeprecated) { switch (kind) { case Binding.PACKAGE: PackageBinding packageBinding = (PackageBinding) recipient; packageBinding.tagBits |= (TagBits.AnnotationDeprecated | TagBits.DeprecatedAnnotationResolved); return; case Binding.TYPE: case Binding.GENERIC_TYPE: case Binding.TYPE_PARAMETER: ReferenceBinding type = (ReferenceBinding) recipient; type.tagBits |= (TagBits.AnnotationDeprecated | TagBits.DeprecatedAnnotationResolved); return; case Binding.METHOD: MethodBinding method = (MethodBinding) recipient; method.tagBits |= (TagBits.AnnotationDeprecated | TagBits.DeprecatedAnnotationResolved); return; case Binding.FIELD: FieldBinding field = (FieldBinding) recipient; field.tagBits |= (TagBits.AnnotationDeprecated | TagBits.DeprecatedAnnotationResolved); return; case Binding.LOCAL: LocalVariableBinding local = (LocalVariableBinding) recipient; local.tagBits |= (TagBits.AnnotationDeprecated | TagBits.DeprecatedAnnotationResolved); return; default: return; } } } } } switch (kind) { case Binding.PACKAGE: PackageBinding packageBinding = (PackageBinding) recipient; packageBinding.tagBits |= TagBits.DeprecatedAnnotationResolved; return; case Binding.TYPE: case Binding.GENERIC_TYPE: case Binding.TYPE_PARAMETER: ReferenceBinding type = (ReferenceBinding) recipient; type.tagBits |= TagBits.DeprecatedAnnotationResolved; return; case Binding.METHOD: MethodBinding method = (MethodBinding) recipient; method.tagBits |= TagBits.DeprecatedAnnotationResolved; return; case Binding.FIELD: FieldBinding field = (FieldBinding) recipient; field.tagBits |= TagBits.DeprecatedAnnotationResolved; return; case Binding.LOCAL: LocalVariableBinding local = (LocalVariableBinding) recipient; local.tagBits |= TagBits.DeprecatedAnnotationResolved; return; default: return; } } }
/** * Resolve annotations, and check duplicates, answers combined tagBits for recognized standard * annotations */ public static void resolveAnnotations( BlockScope scope, Annotation[] sourceAnnotations, Binding recipient) { AnnotationBinding[] annotations = null; int length = sourceAnnotations == null ? 0 : sourceAnnotations.length; if (recipient != null) { switch (recipient.kind()) { case Binding.PACKAGE: PackageBinding packageBinding = (PackageBinding) recipient; if ((packageBinding.tagBits & TagBits.AnnotationResolved) != 0) return; packageBinding.tagBits |= (TagBits.AnnotationResolved | TagBits.DeprecatedAnnotationResolved); break; case Binding.TYPE: case Binding.GENERIC_TYPE: ReferenceBinding type = (ReferenceBinding) recipient; if ((type.tagBits & TagBits.AnnotationResolved) != 0) return; type.tagBits |= (TagBits.AnnotationResolved | TagBits.DeprecatedAnnotationResolved); if (length > 0) { annotations = new AnnotationBinding[length]; type.setAnnotations(annotations); } break; case Binding.METHOD: MethodBinding method = (MethodBinding) recipient; if ((method.tagBits & TagBits.AnnotationResolved) != 0) return; method.tagBits |= (TagBits.AnnotationResolved | TagBits.DeprecatedAnnotationResolved); if (length > 0) { annotations = new AnnotationBinding[length]; method.setAnnotations(annotations); } break; case Binding.FIELD: FieldBinding field = (FieldBinding) recipient; if ((field.tagBits & TagBits.AnnotationResolved) != 0) return; field.tagBits |= (TagBits.AnnotationResolved | TagBits.DeprecatedAnnotationResolved); if (length > 0) { annotations = new AnnotationBinding[length]; field.setAnnotations(annotations); } break; case Binding.LOCAL: LocalVariableBinding local = (LocalVariableBinding) recipient; if ((local.tagBits & TagBits.AnnotationResolved) != 0) return; local.tagBits |= (TagBits.AnnotationResolved | TagBits.DeprecatedAnnotationResolved); if (length > 0) { annotations = new AnnotationBinding[length]; local.setAnnotations(annotations); } break; default: return; } } if (sourceAnnotations == null) return; for (int i = 0; i < length; i++) { Annotation annotation = sourceAnnotations[i]; final Binding annotationRecipient = annotation.recipient; if (annotationRecipient != null && recipient != null) { // only local and field can share annnotations switch (recipient.kind()) { case Binding.FIELD: FieldBinding field = (FieldBinding) recipient; field.tagBits = ((FieldBinding) annotationRecipient).tagBits; break; case Binding.LOCAL: LocalVariableBinding local = (LocalVariableBinding) recipient; local.tagBits = ((LocalVariableBinding) annotationRecipient).tagBits; break; } if (annotations != null) { // need to fill the instances array annotations[0] = annotation.getCompilerAnnotation(); for (int j = 1; j < length; j++) { Annotation annot = sourceAnnotations[j]; annotations[j] = annot.getCompilerAnnotation(); } } return; } else { annotation.recipient = recipient; annotation.resolveType(scope); // null if receiver is a package binding if (annotations != null) { annotations[i] = annotation.getCompilerAnnotation(); } } } // check duplicate annotations if (annotations != null) { AnnotationBinding[] distinctAnnotations = annotations; // only copy after 1st duplicate is detected for (int i = 0; i < length; i++) { AnnotationBinding annotation = distinctAnnotations[i]; if (annotation == null) continue; TypeBinding annotationType = annotation.getAnnotationType(); boolean foundDuplicate = false; for (int j = i + 1; j < length; j++) { AnnotationBinding otherAnnotation = distinctAnnotations[j]; if (otherAnnotation == null) continue; if (otherAnnotation.getAnnotationType() == annotationType) { foundDuplicate = true; if (distinctAnnotations == annotations) { System.arraycopy( distinctAnnotations, 0, distinctAnnotations = new AnnotationBinding[length], 0, length); } distinctAnnotations[j] = null; // report it only once scope.problemReporter().duplicateAnnotation(sourceAnnotations[j]); } } if (foundDuplicate) { scope.problemReporter().duplicateAnnotation(sourceAnnotations[i]); } } } }