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);
}
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());
}
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);
}
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);
}
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
}
}
Aggregations