public void testBug405908() throws CoreException, IOException { try { createJavaProject("P", new String[] {""}, new String[0], "", CompilerOptions.VERSION_1_5); createFile( "P/A.java", "@interface Generated {\n" + " String comment() default \"\";\n" + " String[] value();\n" + "}\n" + "@Generated()\n" + "class A {\n" + "}"); ICompilationUnit cuA = getCompilationUnit("P/A.java"); CompilationUnit unitA = (CompilationUnit) runConversion(cuA, true, false, true); AbstractTypeDeclaration typeA = (AbstractTypeDeclaration) unitA.types().get(1); IAnnotationBinding[] annotations = typeA.resolveBinding().getAnnotations(); IAnnotationBinding generated = annotations[0]; IMemberValuePairBinding[] mvps = generated.getAllMemberValuePairs(); IMemberValuePairBinding valueBinding = mvps[1]; assertEquals("value", valueBinding.getName()); Object value = valueBinding.getValue(); assertEquals(0, ((Object[]) value).length); } finally { deleteProject("P"); } }
public static IAnnotationBinding getAnnotation(IBinding binding, Class<?> annotationClass) { for (IAnnotationBinding annotation : binding.getAnnotations()) { if (typeEqualsClass(annotation.getAnnotationType(), annotationClass)) { return annotation; } } return null; }
/** Less strict version of the above where we don't care about the annotation's package. */ public static boolean hasNamedAnnotation(IBinding binding, String annotationName) { for (IAnnotationBinding annotation : binding.getAnnotations()) { if (annotation.getName().equals(annotationName)) { return true; } } return false; }
/** * Return true if a binding has a named "Nonnull" annotation. Package names aren't checked because * different nonnull annotations are defined in several Java frameworks, with varying but similar * names. */ public static boolean hasNonnullAnnotation(IBinding binding) { Pattern p = Pattern.compile("No[nt][Nn]ull"); for (IAnnotationBinding annotation : binding.getAnnotations()) { if (p.matcher(annotation.getName()).matches()) { return true; } } return false; }
private boolean hasSignificantAnnotations(IMethodBinding methodBinding) { for (IAnnotationBinding annotation : methodBinding.getAnnotations()) { ITypeBinding annotationType = annotation.getAnnotationType(); if (!hasType(annotationType, "java.lang.Override", "java.lang.SuppressWarnings")) { return true; } } return false; }
/** * Returns true if the specified binding is of an annotation that has a runtime retention policy. */ public static boolean isRuntimeAnnotation(ITypeBinding binding) { if (binding != null && binding.isAnnotation()) { for (IAnnotationBinding ann : binding.getAnnotations()) { if (ann.getName().equals("Retention")) { IVariableBinding retentionBinding = (IVariableBinding) ann.getDeclaredMemberValuePairs()[0].getValue(); return retentionBinding.getName().equals(RetentionPolicy.RUNTIME.name()); } } if (binding.isNested()) { return BindingUtil.isRuntimeAnnotation(binding.getDeclaringClass()); } } return false; }
/** Returns the attributes of a Property annotation. */ public static Set<String> parseAttributeString(IAnnotationBinding propertyAnnotation) { assert propertyAnnotation.getName().equals("Property"); String attributesStr = (String) getAnnotationValue(propertyAnnotation, "value"); Set<String> attributes = Sets.newHashSet(); attributes.addAll(Arrays.asList(attributesStr.split(",\\s*"))); attributes.remove(""); // Clear any empty strings. return attributes; }
public static Object getAnnotationValue(IAnnotationBinding annotation, String name) { for (IMemberValuePairBinding pair : annotation.getAllMemberValuePairs()) { if (name.equals(pair.getName())) { return pair.getValue(); } } return null; }
/** * Returns an alphabetically sorted list of an annotation's member values. This is necessary since * an annotation's values can be specified in any order, but the annotation's constructor needs to * be invoked using its declaration order. */ public static IMemberValuePairBinding[] getSortedMemberValuePairs(IAnnotationBinding annotation) { IMemberValuePairBinding[] valuePairs = annotation.getAllMemberValuePairs(); Arrays.sort( valuePairs, new Comparator<IMemberValuePairBinding>() { @Override public int compare(IMemberValuePairBinding o1, IMemberValuePairBinding o2) { return o1.getName().compareTo(o2.getName()); } }); return valuePairs; }
/** * Returns true if the specified binding is of an annotation that has a runtime retention policy. */ public static boolean isRuntimeAnnotation(IAnnotationBinding binding) { return isRuntimeAnnotation(binding.getAnnotationType()); }