use of spoon.reflect.visitor.filter.TypeFilter in project spoon by INRIA.
the class AnnotationTest method testUsageOfTypeAnnotationWithGenericTypesInStatements.
@Test
public void testUsageOfTypeAnnotationWithGenericTypesInStatements() throws Exception {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/AnnotationsAppliedOnAnyTypeInAClass.java");
launcher.buildModel();
Factory factory = launcher.getFactory();
final CtClass<?> ctClass = (CtClass<?>) factory.Type().get("spoon.test.annotation.testclasses.AnnotationsAppliedOnAnyTypeInAClass");
final CtMethod<?> method = ctClass.getMethodsByName("m4").get(0);
// New type parameter declaration.
final List<CtTypeParameter> typeParameters = method.getFormalCtTypeParameters();
assertEquals("Method has 1 generic parameter", 1, typeParameters.size());
assertEquals("Method with an type annotation must be well printed", "@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "T", typeParameters.get(0).toString());
final CtBlock<?> body = method.getBody();
final String expectedFirstStatement = "java.util.List<@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "T> list = new java.util.ArrayList<>()";
final CtStatement firstStatement = body.getStatement(0);
assertEquals("Type annotation on generic parameter declared in the method", expectedFirstStatement, firstStatement.toString());
final CtConstructorCall firstConstructorCall = firstStatement.getElements(new TypeFilter<CtConstructorCall>(CtConstructorCall.class)).get(0);
final CtTypeReference<?> firstTypeReference = firstConstructorCall.getType().getActualTypeArguments().get(0);
assertTrue(firstTypeReference.isImplicit());
assertEquals("T", firstTypeReference.getSimpleName());
final String expectedSecondStatement = "java.util.List<@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "?> list2 = new java.util.ArrayList<>()";
final CtStatement secondStatement = body.getStatement(1);
assertEquals("Wildcard with an type annotation must be well printed", expectedSecondStatement, secondStatement.toString());
final CtConstructorCall secondConstructorCall = secondStatement.getElements(new TypeFilter<CtConstructorCall>(CtConstructorCall.class)).get(0);
final CtTypeReference<?> secondTypeReference = secondConstructorCall.getType().getActualTypeArguments().get(0);
assertTrue(secondTypeReference.isImplicit());
assertEquals("Object", secondTypeReference.getSimpleName());
final String expectedThirdStatement = "java.util.List<spoon.test.annotation.testclasses.@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "BasicAnnotation> list3 = new java.util.ArrayList<spoon.test.annotation.testclasses.@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "BasicAnnotation>()";
assertEquals("Type in generic parameter with an type annotation must be well printed", expectedThirdStatement, body.getStatement(2).toString());
}
use of spoon.reflect.visitor.filter.TypeFilter in project spoon by INRIA.
the class ConditionalTest method testConditional.
@Test
public void testConditional() throws Exception {
final CtType<Foo> aFoo = ModelUtils.buildClass(Foo.class);
final CtConditional aConditional = aFoo.getMethod("m2").getElements(new TypeFilter<CtConditional>(CtConditional.class)).get(0);
assertEquals("return a == 18 ? true : false", aConditional.getParent().toString());
}
use of spoon.reflect.visitor.filter.TypeFilter in project spoon by INRIA.
the class ConditionalTest method testConditionalWithAssignment.
@Test
public void testConditionalWithAssignment() throws Exception {
final CtType<Foo> aFoo = ModelUtils.buildClass(Foo.class);
final CtConditional aConditional = aFoo.getMethod("m").getElements(new TypeFilter<CtConditional>(CtConditional.class)).get(0);
assertEquals("x = (a == 18) ? true : false", aConditional.getParent().toString());
}
use of spoon.reflect.visitor.filter.TypeFilter in project spoon by INRIA.
the class ConstructorCallTest method setUp.
@Before
public void setUp() throws Exception {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/" + Foo.class.getCanonicalName().replace(".", "/") + ".java");
launcher.addInputResource("./src/test/java/" + Panini.class.getCanonicalName().replace(".", "/") + ".java");
launcher.setSourceOutputDirectory("./target/spooned");
launcher.run();
final Factory factory = launcher.getFactory();
final CtClass<?> foo = (CtClass<?>) factory.Type().get(Foo.class);
TreeSet ts = new TreeSet(new DeepRepresentationComparator());
ts.addAll(foo.getElements(new AbstractFilter<CtConstructorCall<?>>(CtConstructorCall.class) {
@Override
public boolean matches(CtConstructorCall<?> element) {
return true;
}
}));
constructorCalls = new ArrayList(ts);
final CtType<Panini> panini = factory.Type().get(Panini.class);
constructorCallsPanini = panini.getElements(new TypeFilter<CtConstructorCall<?>>(CtConstructorCall.class));
}
use of spoon.reflect.visitor.filter.TypeFilter in project spoon by INRIA.
the class NewClassTest method testNewClassInEnumeration.
@Test
public void testNewClassInEnumeration() throws Exception {
Factory factory = null;
try {
factory = build(Bar.class);
} catch (NullPointerException e) {
fail();
}
final CtClass<?> foo = (CtClass<?>) factory.Type().get(Bar.class);
final CtNewClass<?> newClass = foo.getElements(new TypeFilter<CtNewClass<?>>(CtNewClass.class)).get(0);
assertIsConstructor(newClass.getExecutable());
assertHasParameters(1, newClass.getArguments());
assertEquals("\">\"", newClass.getArguments().get(0).toString());
assertIsAnonymous(newClass.getAnonymousClass());
assertSuperClass(Bar.class, newClass.getAnonymousClass());
}
Aggregations