use of spoon.Launcher in project spoon by INRIA.
the class FilterTest method testSubInheritanceHierarchyResolver.
@Test
public void testSubInheritanceHierarchyResolver() throws Exception {
// contract; SubInheritanceHierarchyResolver supports finding subtypes in an incremental manner
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput", "--level", "info" });
launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
launcher.buildModel();
SubInheritanceHierarchyResolver resolver = new SubInheritanceHierarchyResolver(launcher.getModel().getRootPackage());
// contract: by default, nothing is sent to the consumer
resolver.forEachSubTypeInPackage(new CtConsumer<CtType<?>>() {
@Override
public void accept(CtType<?> ctType) {
fail();
}
});
// we add a type
resolver.addSuperType(launcher.getFactory().Type().createReference(AbstractTostada.class));
class Counter {
int counter = 0;
}
Counter c = new Counter();
resolver.forEachSubTypeInPackage(new CtConsumer<CtType<?>>() {
@Override
public void accept(CtType<?> ctType) {
c.counter++;
}
});
// there are 5 subtypes of AbstractTostada
assertEquals(5, c.counter);
// we add a type already visited
resolver.addSuperType(launcher.getFactory().Type().createReference(Tostada.class));
// nothing is sent to the consumer
resolver.forEachSubTypeInPackage(new CtConsumer<CtType<?>>() {
@Override
public void accept(CtType<?> ctType) {
fail();
}
});
// we add a new type
resolver.addSuperType(launcher.getFactory().Type().createReference(ITostada.class));
Counter c2 = new Counter();
resolver.forEachSubTypeInPackage(new CtConsumer<CtType<?>>() {
@Override
public void accept(CtType<?> ctType) {
c2.counter++;
assertEquals("spoon.test.filters.testclasses.Tacos", ctType.getQualifiedName());
}
});
// only one subtype remains unvisited
assertEquals(1, c2.counter);
}
use of spoon.Launcher in project spoon by INRIA.
the class FilterTest method testOverridingMethodFromSubClassOfInterface.
@Test
public void testOverridingMethodFromSubClassOfInterface() throws Exception {
// contract: When we ask all overriding methods from an overriding method, we must returns all methods
// below and not above (including the declaration).
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput" });
launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
launcher.run();
final CtClass<AbstractTostada> anAbstractTostada = launcher.getFactory().Class().get(AbstractTostada.class);
List<CtMethod<?>> overridingMethods = Query.getElements(launcher.getFactory(), new OverridingMethodFilter(anAbstractTostada.getMethodsByName("make").get(0)));
assertEquals(2, overridingMethods.size());
assertEquals("spoon.test.filters.testclasses.AbstractTostada$1", overridingMethods.get(0).getParent(CtClass.class).getQualifiedName());
assertEquals(Tostada.class, overridingMethods.get(1).getParent(CtClass.class).getActualClass());
final CtClass<Tostada> aTostada = launcher.getFactory().Class().get(Tostada.class);
overridingMethods = Query.getElements(launcher.getFactory(), new OverridingMethodFilter(aTostada.getMethodsByName("make").get(0)));
assertEquals(1, overridingMethods.size());
assertEquals("spoon.test.filters.testclasses.AbstractTostada$1", overridingMethods.get(0).getParent(CtClass.class).getQualifiedName());
}
use of spoon.Launcher in project spoon by INRIA.
the class GenericsTest method testAccessToGenerics.
@Test
public void testAccessToGenerics() throws Exception {
Launcher spoon = new Launcher();
Factory factory = spoon.createFactory();
SpoonModelBuilder compiler = spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/java/spoon/test/generics/Foo.java", "./src/test/java/spoon/test/generics/Bar.java"));
compiler.build();
CtClass<?> foo = (CtClass<?>) factory.Type().get(Foo.class);
CtInterface<?> bar = (CtInterface<?>) factory.Type().get(Bar.class);
final CtNewClass<?> newAnonymousBar = foo.getElements(new AbstractFilter<CtNewClass<?>>(CtNewClass.class) {
@Override
public boolean matches(CtNewClass<?> element) {
return element.getAnonymousClass() != null && element.getAnonymousClass().isAnonymous();
}
}).get(0);
final List<CtTypeParameter> barTypeParamGenerics = bar.getFormalCtTypeParameters();
final CtTypeReference<?> anonymousBar = newAnonymousBar.getType();
assertEquals("Name of the first generic parameter in Bar interface must to be I.", "I", barTypeParamGenerics.get(0).getSimpleName());
assertEquals("Name of the first generic parameter in Bar usage must to be K.", "K", anonymousBar.getActualTypeArguments().get(0).getSimpleName());
assertEquals("Name of the second generic parameter in Bar interface must to be O.", "O", barTypeParamGenerics.get(1).getSimpleName());
assertEquals("Name of the second generic parameter in Bar usage must to be V.", "V", anonymousBar.getActualTypeArguments().get(1).getSimpleName());
}
use of spoon.Launcher in project spoon by INRIA.
the class GenericsTest method testInvocationGenerics.
@Test
public void testInvocationGenerics() throws Exception {
final Launcher launcher = new Launcher();
launcher.run(new String[] { "-i", "./src/test/java/spoon/test/generics/testclasses/", "-o", "./target/spooned/" });
final CtClass<?> aTacos = launcher.getFactory().Class().get(Tacos.class);
final CtConstructor<?> defaultConstructor = aTacos.getConstructor();
final CtInvocation<?> explicitConstructorCall = (CtInvocation<?>) defaultConstructor.getBody().getStatement(0).getElements(new TypeFilter<>(CtInvocation.class)).get(0);
assertEquals(1, explicitConstructorCall.getExecutable().getActualTypeArguments().size());
assertEquals("<java.lang.String>this(1)", explicitConstructorCall.toString());
final CtMethod<?> m = aTacos.getMethodsByName("m2").get(0);
final CtInvocation invocation1 = m.getBody().getStatement(0).getElements(new TypeFilter<>(CtInvocation.class)).get(0);
assertEquals(1, invocation1.getExecutable().getActualTypeArguments().size());
assertEquals("this.<java.lang.String>makeTacos(null)", invocation1.toString());
final CtInvocation invocation2 = m.getBody().getStatement(1).getElements(new TypeFilter<>(CtInvocation.class)).get(0);
assertEquals(0, invocation2.getExecutable().getActualTypeArguments().size());
assertEquals("this.makeTacos(null)", invocation2.toString());
canBeBuilt("./target/spooned/spoon/test/generics/testclasses/", 8);
}
use of spoon.Launcher in project spoon by INRIA.
the class GenericsTest method testMethodsWithGenericsWhoExtendsObject.
@Test
public void testMethodsWithGenericsWhoExtendsObject() throws Exception {
final Launcher launcher = new Launcher();
launcher.run(new String[] { "-i", "./src/test/java/spoon/test/generics/testclasses/", "-o", "./target/spooned/" });
final CtClass<?> aTacos = launcher.getFactory().Class().get(Tacos.class);
final CtMethod<?> m = aTacos.getMethodsByName("m4").get(0);
final CtInvocation<?> invocation1 = m.getBody().getStatement(0).getElements(new TypeFilter<>(CtInvocation.class)).get(0);
assertEquals(2, invocation1.getExecutable().getActualTypeArguments().size());
assertEquals("spoon.test.generics.testclasses.Tacos.<V, C>makeTacos()", invocation1.toString());
final CtInvocation<?> invocation2 = m.getBody().getStatement(1).getElements(new TypeFilter<>(CtInvocation.class)).get(0);
assertEquals(0, invocation2.getExecutable().getActualTypeArguments().size());
assertEquals("spoon.test.generics.testclasses.Tacos.makeTacos()", invocation2.toString());
}
Aggregations