Search in sources :

Example 26 with Launcher

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());
}
Also used : Foo(spoon.test.enums.testclasses.Foo) Launcher(spoon.Launcher) CtEnum(spoon.reflect.declaration.CtEnum) AnnotationTest(spoon.test.annotation.AnnotationTest) Test(org.junit.Test)

Example 27 with Launcher

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);
}
Also used : CtAssignment(spoon.reflect.code.CtAssignment) CtType(spoon.reflect.declaration.CtType) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CtEnum(spoon.reflect.declaration.CtEnum) SpoonResource(spoon.compiler.SpoonResource) Test(org.junit.Test)

Example 28 with Launcher

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());
}
Also used : InlinePartialEvaluator(spoon.support.reflect.eval.InlinePartialEvaluator) Launcher(spoon.Launcher) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtIf(spoon.reflect.code.CtIf) VisitorPartialEvaluator(spoon.support.reflect.eval.VisitorPartialEvaluator) Test(org.junit.Test)

Example 29 with Launcher

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));
    }
}
Also used : CtBlock(spoon.reflect.code.CtBlock) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 30 with Launcher

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));
}
Also used : CtCase(spoon.reflect.code.CtCase) CtSwitch(spoon.reflect.code.CtSwitch) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Aggregations

Launcher (spoon.Launcher)524 Test (org.junit.Test)486 Factory (spoon.reflect.factory.Factory)163 CtClass (spoon.reflect.declaration.CtClass)111 CtMethod (spoon.reflect.declaration.CtMethod)79 File (java.io.File)75 CtType (spoon.reflect.declaration.CtType)66 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)60 CtTypeReference (spoon.reflect.reference.CtTypeReference)48 SpoonModelBuilder (spoon.SpoonModelBuilder)44 ArrayList (java.util.ArrayList)43 CtAnnotation (spoon.reflect.declaration.CtAnnotation)38 CtInvocation (spoon.reflect.code.CtInvocation)32 CtElement (spoon.reflect.declaration.CtElement)27 CtPackage (spoon.reflect.declaration.CtPackage)27 FileSystemFile (spoon.support.compiler.FileSystemFile)26 CtStatement (spoon.reflect.code.CtStatement)25 CtField (spoon.reflect.declaration.CtField)24 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)24 SpoonAPI (spoon.SpoonAPI)22