Search in sources :

Example 96 with TypeFilter

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());
}
Also used : CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtClass(spoon.reflect.declaration.CtClass) CtStatement(spoon.reflect.code.CtStatement) CtConstructorCall(spoon.reflect.code.CtConstructorCall) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 97 with TypeFilter

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());
}
Also used : CtConditional(spoon.reflect.code.CtConditional) Foo(spoon.test.condition.testclasses.Foo) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Example 98 with TypeFilter

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());
}
Also used : CtConditional(spoon.reflect.code.CtConditional) Foo(spoon.test.condition.testclasses.Foo) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Example 99 with TypeFilter

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));
}
Also used : AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) Foo(spoon.test.constructorcallnewclass.testclasses.Foo) ArrayList(java.util.ArrayList) Factory(spoon.reflect.factory.Factory) Panini(spoon.test.constructorcallnewclass.testclasses.Panini) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtClass(spoon.reflect.declaration.CtClass) DeepRepresentationComparator(spoon.support.comparator.DeepRepresentationComparator) CtConstructorCall(spoon.reflect.code.CtConstructorCall) TreeSet(java.util.TreeSet) Launcher(spoon.Launcher) Before(org.junit.Before)

Example 100 with TypeFilter

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());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) Bar(spoon.test.constructorcallnewclass.testclasses.Bar) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Aggregations

TypeFilter (spoon.reflect.visitor.filter.TypeFilter)132 Test (org.junit.Test)119 Launcher (spoon.Launcher)54 Factory (spoon.reflect.factory.Factory)52 CtMethod (spoon.reflect.declaration.CtMethod)43 CtInvocation (spoon.reflect.code.CtInvocation)27 CtClass (spoon.reflect.declaration.CtClass)24 CtLiteral (spoon.reflect.code.CtLiteral)18 ReferenceTypeFilter (spoon.reflect.visitor.filter.ReferenceTypeFilter)18 AbstractTest (fr.inria.AbstractTest)17 List (java.util.List)17 CtTypeReference (spoon.reflect.reference.CtTypeReference)17 ArrayList (java.util.ArrayList)14 CtBlock (spoon.reflect.code.CtBlock)10 CtConstructorCall (spoon.reflect.code.CtConstructorCall)10 CtIf (spoon.reflect.code.CtIf)10 CtStatement (spoon.reflect.code.CtStatement)10 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)10 File (java.io.File)9 CtElement (spoon.reflect.declaration.CtElement)8