Search in sources :

Example 36 with CtInvocation

use of spoon.reflect.code.CtInvocation in project spoon by INRIA.

the class FilterTest method testReflectionBasedTypeFilter.

@Test
public void testReflectionBasedTypeFilter() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput" });
    launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
    launcher.run();
    // First collect all classes using tested TypeFilter
    List<CtClass<?>> allClasses = launcher.getFactory().Package().getRootPackage().getElements(new TypeFilter<CtClass<?>>(CtClass.class));
    assertTrue(allClasses.size() > 0);
    allClasses.forEach(result -> {
        assertTrue(result instanceof CtClass);
    });
    // then do it using Filter whose type is computed by reflection
    List<CtClass<?>> allClasses2 = launcher.getFactory().Package().getRootPackage().getElements(new Filter<CtClass<?>>() {

        @Override
        public boolean matches(CtClass<?> element) {
            return true;
        }
    });
    assertArrayEquals(allClasses.toArray(), allClasses2.toArray());
    // then do it using Filter implemented by lambda expression
    List<CtClass<?>> allClasses3 = launcher.getFactory().Package().getRootPackage().getElements((CtClass<?> element) -> true);
    assertArrayEquals(allClasses.toArray(), allClasses3.toArray());
    // last try AbstractFilter constructor without class parameter
    final CtClass<Tacos> aTacos = launcher.getFactory().Class().get(Tacos.class);
    final CtInvocation<?> invSize = aTacos.getElements(new AbstractFilter<CtInvocation<?>>() {

        /*no class is needed here*/
        @Override
        public boolean matches(CtInvocation<?> element) {
            if (element.getExecutable() == null) {
                return false;
            }
            return "size".equals(element.getExecutable().getSimpleName()) && super.matches(element);
        }
    }).get(0);
    assertNotNull(invSize);
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtInvocation(spoon.reflect.code.CtInvocation) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) FieldAccessFilterTacos(spoon.test.filters.testclasses.FieldAccessFilterTacos) Tacos(spoon.test.filters.testclasses.Tacos) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 37 with CtInvocation

use of spoon.reflect.code.CtInvocation in project spoon by INRIA.

the class FieldAccessTest method testFieldAccessDeclaredInADefaultClass.

@Test
public void testFieldAccessDeclaredInADefaultClass() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput" });
    launcher.addInputResource("./src/test/java/spoon/test/fieldaccesses/testclasses/Tacos.java");
    launcher.addInputResource("./src/test/java/spoon/test/fieldaccesses/testclasses/internal/Foo.java");
    launcher.addInputResource("./src/test/java/spoon/test/fieldaccesses/testclasses/internal/Bar.java");
    launcher.run();
    final CtType<Object> aTacos = launcher.getFactory().Type().get(Tacos.class);
    final CtType<Object> aFoo = launcher.getFactory().Type().get("spoon.test.fieldaccesses.testclasses.internal.Foo");
    final CtTypeAccess<Object> aFooAccess = launcher.getFactory().Code().createTypeAccess(aFoo.getReference());
    final CtType<Object> aSubInner = launcher.getFactory().Type().get("spoon.test.fieldaccesses.testclasses.internal.Bar$Inner$SubInner");
    aFoo.addNestedType(aSubInner);
    final CtTypeAccess<Object> aSubInnerAccess = launcher.getFactory().Code().createTypeAccess(aSubInner.getReference());
    final CtType<Object> aKnowOrder = launcher.getFactory().Type().get("spoon.test.fieldaccesses.testclasses.internal.Bar$Inner$KnownOrder");
    aFoo.addNestedType(aKnowOrder);
    final CtTypeAccess<Object> aKnownOrderAccess = launcher.getFactory().Code().createTypeAccess(aKnowOrder.getReference());
    final CtMethod<Object> aMethod = aTacos.getMethod("m");
    final List<CtInvocation<?>> invs = aMethod.getElements(new TypeFilter<>(CtInvocation.class));
    assertEquals(aFooAccess, ((CtFieldAccess) invs.get(0).getArguments().get(0)).getTarget());
    assertEquals("inv(spoon.test.fieldaccesses.testclasses.internal.Foo.i)", invs.get(0).toString());
    assertEquals(aFooAccess, ((CtFieldAccess) invs.get(1).getArguments().get(0)).getTarget());
    assertEquals("inv(spoon.test.fieldaccesses.testclasses.internal.Foo.i)", invs.get(1).toString());
    assertEquals(aSubInnerAccess, ((CtFieldAccess) invs.get(2).getArguments().get(0)).getTarget());
    assertEquals("inv(spoon.test.fieldaccesses.testclasses.internal.Foo.SubInner.j)", invs.get(2).toString());
    assertEquals(aSubInnerAccess, ((CtFieldAccess) invs.get(3).getArguments().get(0)).getTarget());
    assertEquals("inv(spoon.test.fieldaccesses.testclasses.internal.Foo.SubInner.j)", invs.get(3).toString());
    assertEquals(aKnownOrderAccess, ((CtFieldAccess) invs.get(4).getArguments().get(0)).getTarget());
    assertEquals("runIteratorTest(spoon.test.fieldaccesses.testclasses.internal.Foo.KnownOrder.KNOWN_ORDER)", invs.get(4).toString());
    assertEquals(aKnownOrderAccess, ((CtFieldAccess) invs.get(5).getArguments().get(0)).getTarget());
    assertEquals("runIteratorTest(spoon.test.fieldaccesses.testclasses.internal.Foo.KnownOrder.KNOWN_ORDER)", invs.get(5).toString());
    final CtParameter<?> aKnownOrderParameter = aTacos.getMethod("runIteratorTest", aKnowOrder.getReference()).getParameters().get(0);
    assertEquals(aKnowOrder.getReference(), aKnownOrderParameter.getType());
    assertEquals("spoon.test.fieldaccesses.testclasses.internal.Foo.KnownOrder knownOrder", aKnownOrderParameter.toString());
    final CtParameter<?> aSubInnerParameter = aTacos.getMethod("inv", aSubInner.getReference()).getParameters().get(0);
    assertEquals(aSubInner.getReference(), aSubInnerParameter.getType());
    assertEquals("spoon.test.fieldaccesses.testclasses.internal.Foo.SubInner foo", aSubInnerParameter.toString());
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 38 with CtInvocation

use of spoon.reflect.code.CtInvocation in project spoon by INRIA.

the class TypeReferenceTest method testRecursiveTypeReference.

@Test
public void testRecursiveTypeReference() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/reference/testclasses/Tacos.java");
    launcher.setSourceOutputDirectory("./target/spoon-test");
    launcher.run();
    final CtInvocation<?> inv = Query.getElements(launcher.getFactory(), new TypeFilter<CtInvocation<?>>(CtInvocation.class) {

        @Override
        public boolean matches(CtInvocation<?> element) {
            return !element.getExecutable().isConstructor() && super.matches(element);
        }
    }).get(0);
    assertNotNull(inv.getExecutable());
    final CtTypeReference<?> returnType = inv.getExecutable().getType();
    assertNotNull(returnType);
    assertEquals(1, returnType.getActualTypeArguments().size());
    final CtTypeParameterReference genericType = (CtTypeParameterReference) returnType.getActualTypeArguments().get(0);
    assertNotNull(genericType);
    assertNotNull(genericType.getBoundingType());
    CtTypeReference<?> extendsGeneric = genericType.getBoundingType();
    assertNotNull(extendsGeneric);
    assertEquals(1, extendsGeneric.getActualTypeArguments().size());
    CtTypeParameterReference genericExtends = (CtTypeParameterReference) extendsGeneric.getActualTypeArguments().get(0);
    assertNotNull(genericExtends);
    assertNotNull(genericExtends.getBoundingType());
    assertTrue(genericExtends.getBoundingType() instanceof CtTypeReference);
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) CtInvocation(spoon.reflect.code.CtInvocation) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) Test(org.junit.Test)

Example 39 with CtInvocation

use of spoon.reflect.code.CtInvocation in project spoon by INRIA.

the class ReplaceTest method testReplaceStatement.

@Test
public void testReplaceStatement() {
    CtMethod<?> sample = factory.Package().get("spoon.test.replace.testclasses").getType("Foo").getMethod("foo");
    Assert.assertTrue(sample.getBody().getStatement(0) instanceof CtVariable);
    CtStatement replacement = factory.Core().createInvocation();
    sample.getBody().getStatement(0).replace(replacement);
    Assert.assertTrue(sample.getBody().getStatement(0) instanceof CtInvocation);
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) CtStatement(spoon.reflect.code.CtStatement) CtVariable(spoon.reflect.declaration.CtVariable) Test(org.junit.Test)

Example 40 with CtInvocation

use of spoon.reflect.code.CtInvocation in project spoon by INRIA.

the class ReplaceTest method testReplaceExecutableReferenceByAnotherOne.

@Test
public void testReplaceExecutableReferenceByAnotherOne() throws Exception {
    // contract: replace an executable reference to another one in an invocation.
    final Factory factory = build(Tacos.class);
    final CtType<Tacos> aTacos = factory.Type().get(Tacos.class);
    final CtInvocation inv = aTacos.getMethodsByName("m3").get(0).getElements(new TypeFilter<>(CtInvocation.class)).get(0);
    final CtExecutableReference oldExecutable = inv.getExecutable();
    final CtExecutableReference<Object> newExecutable = factory.Executable().createReference("void java.io.PrintStream#print(java.lang.String)");
    assertSame(oldExecutable, inv.getExecutable());
    oldExecutable.replace(newExecutable);
    assertSame(newExecutable, inv.getExecutable());
    assertEquals("print(java.lang.String)", inv.getExecutable().toString());
    assertEquals("java.io.PrintStream", inv.getExecutable().getDeclaringType().toString());
    // contract: replace of single value by multiple values in single value field must fail
    try {
        newExecutable.replace(Arrays.asList(oldExecutable, null));
        fail();
    } catch (InvalidReplaceException e) {
    // OK
    }
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) Tacos(spoon.test.replace.testclasses.Tacos) InvalidReplaceException(spoon.support.visitor.replace.InvalidReplaceException) Factory(spoon.reflect.factory.Factory) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) Test(org.junit.Test)

Aggregations

CtInvocation (spoon.reflect.code.CtInvocation)64 Test (org.junit.Test)49 Factory (spoon.reflect.factory.Factory)30 Launcher (spoon.Launcher)27 CtMethod (spoon.reflect.declaration.CtMethod)25 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)25 CtClass (spoon.reflect.declaration.CtClass)18 CtTypeReference (spoon.reflect.reference.CtTypeReference)11 List (java.util.List)9 CtExecutableReference (spoon.reflect.reference.CtExecutableReference)9 ReferenceTypeFilter (spoon.reflect.visitor.filter.ReferenceTypeFilter)9 CtStatement (spoon.reflect.code.CtStatement)8 CtBlock (spoon.reflect.code.CtBlock)7 CtElement (spoon.reflect.declaration.CtElement)7 CtIf (spoon.reflect.code.CtIf)6 CtLiteral (spoon.reflect.code.CtLiteral)6 AbstractTest (fr.inria.AbstractTest)5 InputProgram (fr.inria.diversify.utils.sosiefier.InputProgram)5 ArrayList (java.util.ArrayList)5 Arrays (java.util.Arrays)5