use of spoon.Launcher in project spoon by INRIA.
the class EnumsTest method testAnnotationsOnEnum.
@Test
public void testAnnotationsOnEnum() throws Exception {
final Launcher launcher = new Launcher();
launcher.run(new String[] { "-i", "./src/test/java/spoon/test/enums/testclasses", "-o", "./target/spooned" });
final CtEnum<?> foo = (CtEnum) launcher.getFactory().Type().get(Foo.class);
assertEquals(1, foo.getFields().size());
assertEquals(1, foo.getFields().get(0).getAnnotations().size());
assertEquals(Deprecated.class, AnnotationTest.getActualClassFromAnnotation(foo.getFields().get(0).getAnnotations().get(0)));
assertEquals("public enum Foo {" + DefaultJavaPrettyPrinter.LINE_SEPARATOR + " @java.lang.Deprecated" + DefaultJavaPrettyPrinter.LINE_SEPARATOR + " Bar;}", foo.toString());
}
use of spoon.Launcher in project spoon by INRIA.
the class EnumsTypeTest method testEnumsType.
@Test
public void testEnumsType() throws Exception {
// contract: shadow enum should still be considered as an enum
Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/resources/reference-test/EnumsRef.java");
Factory factory = launcher.getFactory();
List<SpoonResource> classpath = SpoonResourceHelper.resources("./src/test/resources/reference-test/EnumJar.jar");
String[] dependencyClasspath = new String[] { classpath.get(0).getPath() };
factory.getEnvironment().setSourceClasspath(dependencyClasspath);
assertEquals(1, classpath.size());
launcher.buildModel();
List<CtAssignment> assignments = Query.getElements(factory, new TypeFilter<>(CtAssignment.class));
CtTypeReference typeRefFromSource = assignments.get(0).getType();
CtType typeFromSource = typeRefFromSource.getTypeDeclaration();
assertTrue(typeRefFromSource.isEnum());
assertTrue(typeFromSource.isEnum());
assertTrue(typeFromSource instanceof CtEnum);
CtTypeReference typeRefFromJar = assignments.get(1).getType();
CtType typeFromJar = typeRefFromJar.getTypeDeclaration();
// fail
assertTrue(typeRefFromJar.isEnum());
// fail
assertTrue(typeFromJar.isEnum());
// fail
assertTrue(typeFromJar instanceof CtEnum);
}
use of spoon.Launcher in project spoon by INRIA.
the class EvalTest method testVisitorPartialEvaluatorScanner.
@Test
public void testVisitorPartialEvaluatorScanner() throws Exception {
Launcher launcher = new Launcher();
launcher.addInputResource("src/test/java/spoon/test/eval/Foo.java");
launcher.buildModel();
VisitorPartialEvaluator eval = new VisitorPartialEvaluator();
CtType<?> foo = launcher.getFactory().Type().get((Class<?>) Foo.class);
foo.accept(new InlinePartialEvaluator(eval));
assertEquals("false", foo.getElements(new TypeFilter<>(CtLocalVariable.class)).get(0).getDefaultExpression().toString());
// the if has been removed
assertEquals(0, foo.getElements(new TypeFilter<>(CtIf.class)).size());
}
use of spoon.Launcher in project spoon by INRIA.
the class TestCtBlock method testAddStatementInBlock.
@Test
public void testAddStatementInBlock() {
// contract: we can add a statement at a specific index
// the statements with a higher index are pushed after
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/ctBlock/testclasses/Toto.java");
spoon.buildModel();
CtClass<?> toto = spoon.getFactory().Class().get(Toto.class);
CtMethod foo = toto.getMethodsByName("foo").get(0);
CtBlock block = foo.getBody();
CtBlock originalBlock = block.clone();
assertEquals(4, block.getStatements().size());
block.addStatement(1, (CtStatement) spoon.getFactory().createInvocation().setExecutable(foo.getReference()));
assertEquals(5, block.getStatements().size());
assertEquals(originalBlock.getStatement(0), block.getStatement(0));
for (int i = 1; i < 4; i++) {
assertEquals(originalBlock.getStatement(i), block.getStatement(i + 1));
}
}
use of spoon.Launcher in project spoon by INRIA.
the class TestCtBlock method testAddStatementInCase.
@Test
public void testAddStatementInCase() {
// contract: we can add a statement at a specific index
// the statements with a higher index are pushed after
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/ctBlock/testclasses/Toto.java");
spoon.buildModel();
CtClass<?> toto = spoon.getFactory().Class().get(Toto.class);
CtMethod bar = toto.getMethodsByName("bar").get(0);
CtSwitch<?> ctSwitch = (CtSwitch) bar.getBody().getStatement(0);
CtCase firstCase = ctSwitch.getCases().get(0);
CtCase originalFirstCase = firstCase.clone();
CtCase secondCase = ctSwitch.getCases().get(1);
CtCase originalSecondCase = secondCase.clone();
assertEquals(3, firstCase.getStatements().size());
firstCase.addStatement(3, spoon.getFactory().createBreak());
assertEquals(4, firstCase.getStatements().size());
for (int i = 0; i < 3; i++) {
assertEquals(originalFirstCase.getStatements().get(i), firstCase.getStatements().get(i));
}
assertEquals(2, secondCase.getStatements().size());
secondCase.addStatement(1, (CtStatement) spoon.getFactory().createInvocation().setExecutable(bar.getReference()));
assertEquals(3, secondCase.getStatements().size());
assertEquals(originalSecondCase.getStatements().get(0), secondCase.getStatements().get(0));
assertEquals(originalSecondCase.getStatements().get(1), secondCase.getStatements().get(2));
}
Aggregations