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 Object getAnnotationValue(IAnnotationBinding annotation, String name) { for (IMemberValuePairBinding pair : annotation.getAllMemberValuePairs()) { if (name.equals(pair.getName())) { return pair.getValue(); } } return null; }
// Prints an annotation's values as a constructor argument list. If // the annotation type declares default values, then for any value that // isn't specified in the annotation will use the default. private void printAnnotationParameters(Annotation annotation) { IAnnotationBinding binding = Types.getAnnotationBinding(annotation); IMemberValuePairBinding[] valueBindings = BindingUtil.getSortedMemberValuePairs(binding); for (int i = 0; i < valueBindings.length; i++) { if (i > 0) { print(' '); } IMemberValuePairBinding valueBinding = valueBindings[i]; print(i == 0 ? "With" : "with"); printf("%s:", NameTable.capitalize(valueBinding.getName())); Object value = valueBinding.getValue(); printAnnotationValue(annotation.getAST(), value); } }