### Question:
AnnotationUtils { public static String addXmlElementAnnotation(final String source) { final CompilationUnit unit = JavaSourceUtils.getCompilationUnit(source); final AST ast = unit.getAST(); final ExistingMethodsVisitor methodsVisitor = JavaSourceUtils.getMethodCollection(unit); final List<MethodDeclaration> getterMethods = methodsVisitor.getGetterMethods(); boolean needsImport = false; for (MethodDeclaration getterMethod : getterMethods) { @SuppressWarnings("rawtypes") final List parameters = getterMethod.parameters(); if (parameters == null || parameters.size() == 0) { final Type returnType2 = getterMethod.getReturnType2(); if(returnType2==null){ continue; } if(returnType2 instanceof PrimitiveType){ final PrimitiveType myType = (PrimitiveType) returnType2; if(myType.getPrimitiveTypeCode().equals(PrimitiveType.VOID)){ log.debug("Cannot annotate void type for method: {} ", getterMethod.getName().getIdentifier()); continue; } } final String returnName = returnType2.toString(); if (!KNOWN_XML_TYPES.contains(returnName)) { log.warn("Unknown return type [{}], will skip annotating it with @XmlElement", returnName); continue; } final MarkerAnnotation xmlElementAnnotation = ast.newMarkerAnnotation(); xmlElementAnnotation.setTypeName(ast.newSimpleName(XmlElement.class.getSimpleName())); JavaSourceUtils.addAnnotation(getterMethod, xmlElementAnnotation); needsImport = true; } } if (needsImport) { JavaSourceUtils.addImport(unit, XmlElement.class.getName()); } return JavaSourceUtils.rewrite(unit, ast); } private  AnnotationUtils(); @SuppressWarnings("unchecked") static String addXmlRootAnnotation(final String source, final String name); static String addXmlAdaptorAnnotation(final String source, final String returnType, final AdapterWrapper wrapper); static String addXmlElementAnnotation(final String source); static String addXmlAccessNoneAnnotation(final String source); static T getClassAnnotation(final Class<?> clazz, final Class<T> annotationClass); static Collection<Field> getAnnotatedFields(final Class<?> clazz, final Class<? extends Annotation> annotationClass); @SuppressWarnings({"unchecked"}) static Class<T> findClass(final String name); static Collection<Field> getClassFields(Class<?> clazz); static Collection<Method> getMethods(Class<?> clazz); static String addKnownAdapters(final String source); static final Set<String> KNOWN_XML_TYPES; }### Answer:
@Test public void testMethodAnnotation() throws Exception { final String javaFile = GlobalUtils.readStreamAsText(getClass().getResourceAsStream("/AnnotationTestClass.txt")); assertNotNull("Expected to find /AnnotationTestClass.txt file", javaFile); String annotated = AnnotationUtils.addXmlElementAnnotation(javaFile); int nrOfItems = StringUtils.countMatches(annotated, XmlElement.class.getName()); assertEquals(1, nrOfItems); nrOfItems = StringUtils.countMatches(annotated, "@XmlElement"); assertEquals(8, nrOfItems); annotated = AnnotationUtils.addXmlElementAnnotation(annotated); nrOfItems = StringUtils.countMatches(annotated, XmlElement.class.getName()); assertEquals(1, nrOfItems); nrOfItems = StringUtils.countMatches(annotated, "@XmlElement"); assertEquals(8, nrOfItems); }