Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #1029 by overriding visitCtAnnotationMethod in SignaturePrinter #1030

Merged
merged 3 commits into from
Dec 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/main/java/spoon/support/visitor/SignaturePrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package spoon.support.visitor;

import spoon.reflect.declaration.CtAnnotationMethod;
import spoon.reflect.declaration.CtConstructor;
import spoon.reflect.declaration.CtExecutable;
import spoon.reflect.declaration.CtMethod;
Expand Down Expand Up @@ -119,6 +120,11 @@ public <T> void visitCtConstructor(CtConstructor<T> c) {
write(")");
}

@Override
public <T> void visitCtAnnotationMethod(CtAnnotationMethod<T> annotationMethod) {
visitCtMethod(annotationMethod);
}

/**
* For methods, this implementation of signature contains the return type, which corresponds
* to what the Java compile and virtual machine call a "descriptor".
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/spoon/test/annotation/AnnotationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import spoon.test.annotation.testclasses.SuperAnnotation;
import spoon.test.annotation.testclasses.TestInterface;
import spoon.test.annotation.testclasses.TypeAnnotation;
import spoon.test.annotation.testclasses.PortRange;

import java.io.File;
import java.lang.annotation.Annotation;
Expand Down Expand Up @@ -829,6 +830,25 @@ public void testFieldAndMethodInAnnotation() throws Exception {
assertEquals(2, aTypeAnnotation.getMethods().size());
}

@Test
public void testAnnotationInterfacePreserveMethods() throws Exception {
final CtAnnotationType<?> ctAnnotationType = (CtAnnotationType<?>) this.factory.Type().get(PortRange.class);
List<CtMethod<?>> ctMethodMin = ctAnnotationType.getMethodsByName("min");
assertEquals("Method min is preserved after transformation", 1, ctMethodMin.size());

List<CtMethod<?>> ctMethodMax = ctAnnotationType.getMethodsByName("max");
assertEquals("Method max is preserved after transformation", 1, ctMethodMax.size());

List<CtMethod<?>> ctMethodMessage = ctAnnotationType.getMethodsByName("message");
assertEquals("Method message is preserved after transformation", 1, ctMethodMessage.size());

List<CtMethod<?>> ctMethodGroups = ctAnnotationType.getMethodsByName("groups");
assertEquals("Method groups is preserved after transformation", 1, ctMethodGroups.size());

List<CtMethod<?>> ctMethodPayload = ctAnnotationType.getMethodsByName("payload");
assertEquals("Method payload is preserved after transformation", 1, ctMethodPayload.size());
}

abstract class AbstractElementsProcessor<A extends Annotation, E extends CtElement>
extends AbstractAnnotationProcessor<A, E> {
final List<CtElement> elements = new ArrayList<>();
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/spoon/test/annotation/testclasses/PortRange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package spoon.test.annotation.testclasses;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE_USE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Target({ METHOD, FIELD, ANNOTATION_TYPE, TYPE_USE })
@Retention(RUNTIME)
@Documented
public @interface PortRange {
int min() default 1;

int max() default 65535;

String message() default "{org.hibernate.validator.constraints.Range.message}";

Class<?>[] groups() default {};

Class<?>[] payload() default {};
}
8 changes: 4 additions & 4 deletions src/test/java/spoon/test/generics/GenericsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -312,26 +312,26 @@ public void testConstructorCallGenerics() throws Exception {

final CtMethod<?> m = aTacos.getMethodsByName("m").get(0);
final CtElement local1 = m.getBody().getStatement(0).getElements(new TypeFilter<>(CtLocalVariable.class)).get(0);
final CtTypeReference<Object> leftSideLocal1 = (CtTypeReference<Object>) local1.getReferences(new ReferenceTypeFilter<>(CtTypeReference.class)).get(0);
final CtTypeReference<Object> leftSideLocal1 = (CtTypeReference<Object>) local1.getElements(new ReferenceTypeFilter<>(CtTypeReference.class)).get(0);
final CtConstructorCall<Object> rightSideLocal1 = (CtConstructorCall<Object>) local1.getElements(new TypeFilter<>(CtConstructorCall.class)).get(0);
assertEquals(1, leftSideLocal1.getActualTypeArguments().size());
assertEquals(1, rightSideLocal1.getType().getActualTypeArguments().size());
assertEquals("java.util.List<java.lang.String> l = new java.util.ArrayList<>()", local1.toString());

final CtElement local2 = m.getBody().getStatement(1).getElements(new TypeFilter<>(CtLocalVariable.class)).get(0);
final CtTypeReference<Object> leftSideLocal2 = (CtTypeReference<Object>) local2.getReferences(new ReferenceTypeFilter<>(CtTypeReference.class)).get(0);
final CtTypeReference<Object> leftSideLocal2 = (CtTypeReference<Object>) local2.getElements(new ReferenceTypeFilter<>(CtTypeReference.class)).get(0);
assertEquals(0, leftSideLocal2.getActualTypeArguments().size());
assertEquals("java.util.List l2", local2.toString());

final CtElement local3 = m.getBody().getStatement(2).getElements(new TypeFilter<>(CtLocalVariable.class)).get(0);
final CtTypeReference<Object> leftSideLocal3 = (CtTypeReference<Object>) local3.getReferences(new ReferenceTypeFilter<>(CtTypeReference.class)).get(0);
final CtTypeReference<Object> leftSideLocal3 = (CtTypeReference<Object>) local3.getElements(new ReferenceTypeFilter<>(CtTypeReference.class)).get(0);
final CtConstructorCall<Object> rightSideLocal3 = (CtConstructorCall<Object>) local3.getElements(new TypeFilter<>(CtConstructorCall.class)).get(0);
assertEquals(2, leftSideLocal3.getActualTypeArguments().size());
assertEquals(2, rightSideLocal3.getType().getActualTypeArguments().size());
assertEquals("spoon.test.generics.testclasses.IBurritos<?, ?> burritos = new Burritos<>()", local3.toString());

final CtElement local4 = m.getBody().getStatement(3).getElements(new TypeFilter<>(CtLocalVariable.class)).get(0);
final CtTypeReference<Object> leftSideLocal4 = (CtTypeReference<Object>) local4.getReferences(new ReferenceTypeFilter<>(CtTypeReference.class)).get(0);
final CtTypeReference<Object> leftSideLocal4 = (CtTypeReference<Object>) local4.getElements(new ReferenceTypeFilter<>(CtTypeReference.class)).get(0);
final CtConstructorCall<Object> rightSideLocal4 = (CtConstructorCall<Object>) local4.getElements(new TypeFilter<>(CtConstructorCall.class)).get(0);
assertEquals(1, leftSideLocal4.getActualTypeArguments().size());
assertEquals(1, rightSideLocal4.getType().getActualTypeArguments().size());
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/spoon/test/parent/ParentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void testParentOfCtPackageReference() throws Exception {
// so the root packages are not deeply equals
assertNotEquals(pack1, topLevelParent);

final CtTypeReference<?> burritos = panini.getReferences(new ReferenceTypeFilter<CtTypeReference<?>>(CtTypeReference.class) {
final CtTypeReference<?> burritos = panini.getElements(new ReferenceTypeFilter<CtTypeReference<?>>(CtTypeReference.class) {
@Override
public boolean matches(CtTypeReference<?> reference) {
return "Burritos".equals(reference.getSimpleName()) && super.matches(reference);
Expand Down Expand Up @@ -187,7 +187,7 @@ public void testParentOfCtExecutableReference() throws Exception {
public void testParentOfGenericInTypeReference() throws Exception {
// contract: parent of a generic in a type reference is the type reference.
final Factory factory = build(Tacos.class);
final CtTypeReference referenceWithGeneric = Query.getReferences(factory, new ReferenceTypeFilter<CtTypeReference>(CtTypeReference.class) {
final CtTypeReference referenceWithGeneric = Query.getElements(factory, new ReferenceTypeFilter<CtTypeReference>(CtTypeReference.class) {
@Override
public boolean matches(CtTypeReference reference) {
return reference.getActualTypeArguments().size() > 0 && super.matches(reference);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/spoon/test/refactoring/RefactoringTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void testRefactoringClassChangeAllCtTypeReferenceAssociatedWithClassConce
final CtClass<?> aClassX = launcher.getFactory().Class().get("spoon.test.refactoring.testclasses.AClassX");
assertNotNull(aClassX);

final List<CtTypeReference<?>> references = Query.getReferences(aClassX.getFactory(), new AbstractReferenceFilter<CtTypeReference<?>>(CtTypeReference.class) {
final List<CtTypeReference<?>> references = Query.getElements(aClassX.getFactory(), new AbstractReferenceFilter<CtTypeReference<?>>(CtTypeReference.class) {
@Override
public boolean matches(CtTypeReference<?> reference) {
return aClassX.getQualifiedName().equals(reference.getQualifiedName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public void testExecutableReferences() throws Exception {

CtClass<?> classMyClass = Query.getElements(factory, new NameFilter<CtClass>("MyClass")).get(0);
assertEquals("MyClass", classMyClass.getSimpleName());
List<CtExecutableReference<?>> refsExecutableClass1 = Query.getReferences(classMyClass,
List<CtExecutableReference<?>> refsExecutableClass1 = Query.getElements(classMyClass,
new AbstractReferenceFilter<CtExecutableReference<?>>(CtExecutableReference.class) {
public boolean matches(CtExecutableReference<?> reference) {
return true;
Expand All @@ -224,7 +224,7 @@ public boolean matches(CtExecutableReference<?> reference) {

CtClass<?> classMyClass2 = Query.getElements(factory, new NameFilter<CtClass>("MyClass2")).get(0);
assertEquals("MyClass2", classMyClass2.getSimpleName());
List<CtExecutableReference<?>> refsExecutableClass2 = Query.getReferences(classMyClass2,
List<CtExecutableReference<?>> refsExecutableClass2 = Query.getElements(classMyClass2,
new AbstractReferenceFilter<CtExecutableReference<?>>(CtExecutableReference.class) {
public boolean matches(CtExecutableReference<?> reference) {
return true;
Expand Down Expand Up @@ -277,7 +277,7 @@ public boolean matches(CtExecutableReference<?> reference) {
}

private List<CtExecutableReference<?>> getCtConstructorsReferencedInCtConstructor(CtConstructor<?> aConstructor) {
return aConstructor.getReferences(new AbstractReferenceFilter<CtExecutableReference<?>>(CtExecutableReference.class) {
return aConstructor.getElements(new AbstractReferenceFilter<CtExecutableReference<?>>(CtExecutableReference.class) {
@Override
public boolean matches(CtExecutableReference<?> reference) {
return reference.isConstructor();
Expand All @@ -295,7 +295,7 @@ public boolean matches(CtClass<?> element) {
}

private List<CtExecutableReference<?>> getReferencesOfAMethod(CtMethod<?> method1) {
return method1.getReferences(new ReferenceTypeFilter<CtExecutableReference<?>>(CtExecutableReference.class));
return method1.getElements(new ReferenceTypeFilter<CtExecutableReference<?>>(CtExecutableReference.class));
}

private CtMethod<?> getCtMethodByNameFromCtClass(CtClass<?> clazz, String nameMethod5) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/spoon/test/reference/TypeReferenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void loadReferencedClassFromClasspath() throws Exception {
// now we retrieve the reference to ReferencedClass
CtTypeReference referencedType = null;
ReferenceTypeFilter<CtTypeReference> referenceTypeFilter = new ReferenceTypeFilter<CtTypeReference>(CtTypeReference.class);
List<CtTypeReference> elements = Query.getReferences(theClass, referenceTypeFilter);
List<CtTypeReference> elements = Query.getElements(theClass, referenceTypeFilter);
for (CtTypeReference reference : elements) {
if (reference.getQualifiedName().equals(referencedQualifiedName)) {
referencedType = reference;
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/spoon/test/reference/VariableAccessTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void testVariableAccessDeclarationInAnonymousClass() throws Exception {
CtClass<?> type = build("spoon.test.reference", "FooBar");
assertEquals("FooBar", type.getSimpleName());

final CtParameterReference<?> ref = type.getReferences(new AbstractReferenceFilter<CtParameterReference<?>>(CtParameterReference.class) {
final CtParameterReference<?> ref = type.getElements(new AbstractReferenceFilter<CtParameterReference<?>>(CtParameterReference.class) {
@Override
public boolean matches(CtParameterReference<?> reference) {
return "myArg".equals(reference.getSimpleName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ReferenceQueryTest {
@Test
public void getAllTypeReferencesInEnum() throws Exception {
CtEnum<ReferenceQueryTestEnum> testEnum = build("spoon.test.reflect.visitor", "ReferenceQueryTestEnum");
List< CtTypeReference<?> > enumTypeRefs = Query.getReferences(testEnum, new ReferenceTypeFilter< CtTypeReference<?> >(CtTypeReference.class));
List< CtTypeReference<?> > enumTypeRefs = Query.getElements(testEnum, new ReferenceTypeFilter< CtTypeReference<?> >(CtTypeReference.class));
TypeFactory typeFactory = testEnum.getFactory().Type();
for (Class<?> c : new Class<?>[]{Integer.class, Long.class, Boolean.class, Number.class, String.class, Void.class}) {
Assert.assertTrue("the reference query on the enum should return all the types defined in the enum declaration", enumTypeRefs.contains(typeFactory.createReference(c)));
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/spoon/test/replace/ReplaceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public void testReplaceIntegerReference() throws Exception {
public void testReplaceAllTypeRefenceWithGenerics() throws Exception {
// contract: replace all type references with a generic to the same type reference without generics.
final Factory factory = build(Tacos.class);
final List<CtTypeReference> references = Query.getReferences(factory, new ReferenceTypeFilter<CtTypeReference>(CtTypeReference.class) {
final List<CtTypeReference> references = Query.getElements(factory, new ReferenceTypeFilter<CtTypeReference>(CtTypeReference.class) {
@Override
public boolean matches(CtTypeReference reference) {
return reference.getActualTypeArguments().size() > 0 && super.matches(reference);
Expand Down Expand Up @@ -280,7 +280,7 @@ public void testReplaceAPackageReferenceByAnotherOne() throws Exception {

final CtType<Object> panini = launcher.getFactory().Type().get("Panini");

final CtTypeReference<?> burritos = panini.getReferences(new ReferenceTypeFilter<CtTypeReference<?>>(CtTypeReference.class) {
final CtTypeReference<?> burritos = panini.getElements(new ReferenceTypeFilter<CtTypeReference<?>>(CtTypeReference.class) {
@Override
public boolean matches(CtTypeReference<?> reference) {
return "Burritos".equals(reference.getSimpleName()) && super.matches(reference);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/spoon/test/signature/SignatureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ public void testArgumentNotNullForExecutableReference() throws Exception {
launcher.getEnvironment().setNoClasspath(true);
launcher.run();

final List<CtExecutableReference> references = Query.getReferences(launcher.getFactory(), new ReferenceTypeFilter<CtExecutableReference>(CtExecutableReference.class) {
final List<CtExecutableReference> references = Query.getElements(launcher.getFactory(), new ReferenceTypeFilter<CtExecutableReference>(CtExecutableReference.class) {
@Override
public boolean matches(CtExecutableReference reference) {
return "addField".equals(reference.getSimpleName()) && super.matches(reference);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/spoon/test/visibility/VisibilityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void testName() throws Exception {
"-o", "./target/spooned/visibility"
});

final List<CtFieldReference<?>> references = Query.getReferences(launcher.getFactory(), new AbstractReferenceFilter<CtFieldReference<?>>(CtFieldReference.class) {
final List<CtFieldReference<?>> references = Query.getElements(launcher.getFactory(), new AbstractReferenceFilter<CtFieldReference<?>>(CtFieldReference.class) {
@Override
public boolean matches(CtFieldReference<?> reference) {
return "x".equals(reference.getSimpleName());
Expand Down