### Question:
JavaCode extends Code { public Collection<String> methodAnnotations(String method) { return getAnnotations(METHOD_NAME_PATTERN, 4, method); } JavaCode(); String typeName(); boolean isClass(); Collection<String> typeAnnotations(); Collection<String> imports(); Collection<String> methods(); Collection<String> methodAnnotations(String method); String methodReturns(String method); String methodArguments(String method); String packageName(); String fullyQualifiedName(); String methodBody(String method); Collection<String> fieldNames(); String fieldType(String fieldName); Iterable<String> fieldAnnotations(String fieldName); String fieldValue(String fieldName); Iterable<String> fileComments(); String superTypeName(); Iterable<String> implementedInterfaces(); boolean isType(String type); boolean fieldIsContant(String fieldName); Iterable<String> fieldComments(String fieldName); String constructorBody(); void ensureImport(String packageName, String typeName); String simpleTypeName(); static final String TYPE_PATTERN; static final String NAME_PATTERN; }### Answer:
@Test public void extractsEmptyMethodAnnotations() { String method = someValue(); code.add("public class %s {", randomJavaId()); code.add(" public void %s() {", method); code.add(" }"); code.add("}"); assertEquals("Method annotations", Collections.emptyList(), code.methodAnnotations(method)); }
@Test public void extractsMethodAnnotations() { String method1 = "m" + someValue(); String annotation1 = "@Z" + someValue(); String annotation2 = "@A" + someValue(); String method2 = "m" + someValue(); String annotation3 = "@" + someValue(); String method3 = "foo"; code.add("public class C {"); code.add(" @Q%s", someValue()); code.add(" public void q%s() {", someValue()); code.add(" }"); code.add(" %s", annotation1); code.add(" %s", annotation2); code.add(" public void %s(@%s %s %s) {", method1, someValue(), someValue(), someValue()); code.add(" }"); code.add(" public void %s() {", method2); code.add(" }"); code.add(" %s", annotation3); code.add(" public void %s", method3); code.add(" () {"); code.add(" }"); code.add("}"); assertEquals("Method annotations", Arrays.asList(annotation2, annotation1), code.methodAnnotations(method1)); assertEquals("No annotations", Collections.emptyList(), code.methodAnnotations(method2)); assertEquals("Unknown method", Collections.emptyList(), code.methodAnnotations(someValue())); assertEquals("Annotations for multi-line method", Arrays.asList(annotation3), code.methodAnnotations(method3)); }