Search in sources :

Example 6 with CtModel

use of spoon.reflect.CtModel in project spoon by INRIA.

the class CUFilterTest method testWithoutFilters.

@Test
public void testWithoutFilters() {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/resources/noclasspath/same-package");
    launcher.buildModel();
    final CtModel model = launcher.getModel();
    assertEquals(2, model.getAllTypes().size());
    assertEquals("spoon.test.same.B", model.getAllTypes().iterator().next().getMethod("createB").getType().getQualifiedName());
}
Also used : Launcher(spoon.Launcher) CtModel(spoon.reflect.CtModel) Test(org.junit.Test)

Example 7 with CtModel

use of spoon.reflect.CtModel in project spoon by INRIA.

the class CUFilterTest method testSingleExcludeWithoutFilter.

@Test
public void testSingleExcludeWithoutFilter() {
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    launcher.addInputResource("./src/test/resources/noclasspath/same-package/A.java");
    launcher.buildModel();
    final CtModel model = launcher.getModel();
    assertEquals(1, model.getAllTypes().size());
    // make sure `B` is not available in `model.getAllTypes`
    assertEquals("A", model.getAllTypes().iterator().next().getSimpleName());
    // make sure declaration of `B` is unknown in `model`
    final CtReturn ctReturn = model.getAllTypes().iterator().next().getMethod("createB").getBody().getStatement(0);
    final CtConstructorCall ctConstructorCall = (CtConstructorCall) ctReturn.getReturnedExpression();
    assertEquals("spoon.test.same.B", ctConstructorCall.getType().getQualifiedName());
}
Also used : CtReturn(spoon.reflect.code.CtReturn) CtConstructorCall(spoon.reflect.code.CtConstructorCall) Launcher(spoon.Launcher) CtModel(spoon.reflect.CtModel) Test(org.junit.Test)

Example 8 with CtModel

use of spoon.reflect.CtModel in project spoon by INRIA.

the class FactoryTest method testIncrementalModel.

@Test
public void testIncrementalModel() throws Exception {
    // Feed some inputResources to a spoon compiler
    SpoonAPI spoon = new Launcher();
    spoon.addInputResource("src/test/java/spoon/test/factory/testclasses");
    // Build model
    spoon.buildModel();
    assertEquals(1, spoon.getModel().getAllTypes().size());
    // Do something with that model..
    CtModel model = spoon.getModel();
    model.processWith(new AbstractProcessor<CtMethod>() {

        @Override
        public void process(CtMethod element) {
            element.setDefaultMethod(false);
        }
    });
    // Feed some new inputResources
    SpoonAPI spoon2 = new Launcher();
    spoon2.addInputResource("src/test/java/spoon/test/factory/testclasses2");
    // Build models of newly added classes/packages
    spoon2.buildModel();
    assertEquals(1, spoon2.getModel().getAllTypes().size());
    // attach them to the existing model.
    model.getRootPackage().addPackage(spoon2.getModel().getRootPackage());
    // checking the results
    assertEquals(6, model.getAllPackages().size());
    assertEquals(2, model.getAllTypes().size());
    assertEquals(1, model.getElements(new AbstractFilter<CtPackage>() {

        @Override
        public boolean matches(CtPackage element) {
            return "spoon.test.factory.testclasses2".equals(element.getQualifiedName());
        }
    }).size());
}
Also used : Launcher(spoon.Launcher) CtPackage(spoon.reflect.declaration.CtPackage) CtModel(spoon.reflect.CtModel) SpoonAPI(spoon.SpoonAPI) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 9 with CtModel

use of spoon.reflect.CtModel in project spoon by INRIA.

the class FactoryTest method testCtModel.

@Test
public void testCtModel() throws Exception {
    SpoonAPI spoon = new Launcher();
    spoon.addInputResource("src/test/java/spoon/test/factory/testclasses");
    spoon.buildModel();
    CtModel model = spoon.getModel();
    // contains Foo and Foo.@Bar
    assertEquals(1, model.getAllTypes().size());
    // [, spoon, spoon.test, spoon.test.factory, spoon.test.factory.testclasses]
    assertEquals(5, model.getAllPackages().size());
    // add to itself is fine
    model.getRootPackage().addPackage(model.getRootPackage());
    assertEquals(1, model.getAllTypes().size());
    assertEquals(5, model.getAllPackages().size());
    model.getRootPackage().getPackage("spoon").addPackage(model.getRootPackage().getPackage("spoon"));
    assertEquals(1, model.getAllTypes().size());
    assertEquals(5, model.getAllPackages().size());
    model.getRootPackage().addPackage(model.getRootPackage().getPackage("spoon"));
    assertEquals(1, model.getAllTypes().size());
    assertEquals(5, model.getAllPackages().size());
    CtPackage p = model.getElements(new NamedElementFilter<>(CtPackage.class, "spoon")).get(0).clone();
    // if we change the implem, merge is impossible
    CtField f = spoon.getFactory().Core().createField();
    f.setSimpleName("foo");
    f.setType(spoon.getFactory().Type().BYTE);
    p.getElements(new NamedElementFilter<>(CtPackage.class, "testclasses")).get(0).getType("Foo").addField(f);
    try {
        model.getRootPackage().addPackage(p);
        fail("no exception thrown");
    } catch (IllegalStateException success) {
    }
    model.processWith(new AbstractProcessor<CtType>() {

        @Override
        public void process(CtType element) {
            element.delete();
        }
    });
    assertEquals(0, model.getAllTypes().size());
}
Also used : CtType(spoon.reflect.declaration.CtType) CtField(spoon.reflect.declaration.CtField) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) CtPackage(spoon.reflect.declaration.CtPackage) CtModel(spoon.reflect.CtModel) SpoonAPI(spoon.SpoonAPI) Test(org.junit.Test)

Example 10 with CtModel

use of spoon.reflect.CtModel in project spoon by INRIA.

the class ElasticsearchStackoverflowTest method testStackOverflow.

@Test
public void testStackOverflow() {
    Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/resources/noclasspath/elasticsearch-stackoverflow");
    launcher.getEnvironment().setNoClasspath(true);
    launcher.buildModel();
    CtModel model = launcher.getModel();
    Scanner scanner = new Scanner();
    scanner.scan(model.getRootPackage());
    List<CtExecutableReference> executables = launcher.getModel().getElements(new TypeFilter<CtExecutableReference>(CtExecutableReference.class));
    assertFalse(executables.isEmpty());
    boolean result = false;
    for (CtExecutableReference execRef : executables) {
        if (execRef.getSimpleName().equals("setParentTask")) {
            CtTypeReference typeRef = execRef.getDeclaringType();
            assertTrue(typeRef instanceof CtTypeParameterReference);
            assertEquals("ShardRequest", typeRef.getSimpleName());
            CtType typeRefDecl = typeRef.getDeclaration();
            assertEquals("BroadcastShardRequest", typeRefDecl.getSuperclass().getSimpleName());
            assertNull(execRef.getDeclaration());
            result = true;
        }
    }
    assertTrue(result);
}
Also used : CtScanner(spoon.reflect.visitor.CtScanner) CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) CtType(spoon.reflect.declaration.CtType) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) CtModel(spoon.reflect.CtModel) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)13 CtModel (spoon.reflect.CtModel)13 Launcher (spoon.Launcher)12 SpoonAPI (spoon.SpoonAPI)3 CtClass (spoon.reflect.declaration.CtClass)3 CtMethod (spoon.reflect.declaration.CtMethod)3 CtType (spoon.reflect.declaration.CtType)3 CtTypeReference (spoon.reflect.reference.CtTypeReference)3 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)3 CtConstructorCall (spoon.reflect.code.CtConstructorCall)2 CtNewClass (spoon.reflect.code.CtNewClass)2 CtReturn (spoon.reflect.code.CtReturn)2 CtPackage (spoon.reflect.declaration.CtPackage)2 CtExecutableReference (spoon.reflect.reference.CtExecutableReference)2 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)2 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertNotNull (org.junit.Assert.assertNotNull)1 Assert.assertNull (org.junit.Assert.assertNull)1