Search in sources :

Example 1 with CtModel

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

the class CtClassTest method testCloneAnonymousClassInvocationWithAutoimports.

@Test
public void testCloneAnonymousClassInvocationWithAutoimports() {
    // contract: after cloning an anonymous class invocation, we still should be able to print it, when using autoimport
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/ctClass/testclasses/AnonymousClass.java");
    launcher.getEnvironment().setAutoImports(true);
    launcher.buildModel();
    CtModel model = launcher.getModel();
    CtNewClass newClassInvocation = launcher.getModel().getElements(new TypeFilter<CtNewClass>(CtNewClass.class)).get(0);
    CtNewClass newClassInvocationCloned = newClassInvocation.clone();
    CtClass anonymousClass = newClassInvocation.getAnonymousClass();
    CtClass anonymousClassCloned = newClassInvocationCloned.getAnonymousClass();
    // The test stops failing if we set the parent below
    // newClassInvocationCloned.setParent(launcher.getFactory().Class().get(AnonymousClass.class));
    assertEquals(0, anonymousClass.getAllFields().size());
    assertEquals(0, anonymousClassCloned.getAllFields().size());
    assertTrue(newClassInvocation.toString().length() > 0);
    assertTrue(newClassInvocationCloned.toString().length() > 0);
    assertEquals(newClassInvocation.toString(), newClassInvocationCloned.toString());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtNewClass(spoon.reflect.code.CtNewClass) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtModel(spoon.reflect.CtModel) Test(org.junit.Test)

Example 2 with CtModel

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

the class CtClassTest method testCloneAnonymousClassInvocation.

@Test
public void testCloneAnonymousClassInvocation() {
    // contract: after cloning an anonymous class invocation, we still should be able to print it, when not using autoimport
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/ctClass/testclasses/AnonymousClass.java");
    launcher.getEnvironment().setAutoImports(false);
    launcher.buildModel();
    CtModel model = launcher.getModel();
    CtNewClass newClassInvocation = launcher.getModel().getElements(new TypeFilter<CtNewClass>(CtNewClass.class)).get(0);
    CtNewClass newClassInvocationCloned = newClassInvocation.clone();
    CtClass anonymousClass = newClassInvocation.getAnonymousClass();
    CtClass anonymousClassCloned = newClassInvocationCloned.getAnonymousClass();
    // The test stops failing if we set the parent below
    // newClassInvocationCloned.setParent(launcher.getFactory().Class().get(AnonymousClass.class));
    assertEquals(0, anonymousClass.getAllFields().size());
    assertEquals(0, anonymousClassCloned.getAllFields().size());
    assertTrue(newClassInvocation.toString().length() > 0);
    assertTrue(newClassInvocationCloned.toString().length() > 0);
    assertEquals(newClassInvocation.toString(), newClassInvocationCloned.toString());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtNewClass(spoon.reflect.code.CtNewClass) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtModel(spoon.reflect.CtModel) Test(org.junit.Test)

Example 3 with CtModel

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

the class CUFilterTest method testSingleExcludeWithFilter.

@Test
public void testSingleExcludeWithFilter() {
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    launcher.addInputResource("./src/test/resources/noclasspath/same-package");
    launcher.getModelBuilder().addCompilationUnitFilter(new CompilationUnitFilter() {

        @Override
        public boolean exclude(final String path) {
            return path.endsWith("B.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 known 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 : CompilationUnitFilter(spoon.support.compiler.jdt.CompilationUnitFilter) CtReturn(spoon.reflect.code.CtReturn) CtConstructorCall(spoon.reflect.code.CtConstructorCall) Launcher(spoon.Launcher) CtModel(spoon.reflect.CtModel) Test(org.junit.Test)

Example 4 with CtModel

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

the class LauncherTest method testLLauncherBuildModelReturnAModel.

@Test
public void testLLauncherBuildModelReturnAModel() throws Exception {
    // contract: Launcher#buildModel should return a consistent CtModel
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/resources/spoon/test/api/Foo.java");
    launcher.getEnvironment().setNoClasspath(true);
    CtModel model = launcher.buildModel();
    assertNotNull(model);
    assertEquals(2, model.getAllTypes().size());
}
Also used : CtModel(spoon.reflect.CtModel) Test(org.junit.Test)

Example 5 with CtModel

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

the class InvocationTest method testIssue1753.

@Test
public void testIssue1753() {
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    launcher.addInputResource("./src/test/resources/noclasspath/elasticsearch1753");
    final CtModel model = launcher.buildModel();
    final List<CtExecutable> executables = model.getElements(new TypeFilter<>(CtExecutable.class)).stream().filter(i -> i.getPosition().getLine() == 190).collect(Collectors.toList());
    assertEquals(1, executables.size());
    final CtExecutable exe = executables.get(0);
    assertNotNull(exe.getReference().getDeclaration());
}
Also used : TypeFilter(spoon.reflect.visitor.filter.TypeFilter) ModelUtils.build(spoon.testing.utils.ModelUtils.build) Launcher(spoon.Launcher) CtInvocation(spoon.reflect.code.CtInvocation) Bar(spoon.test.invocations.testclasses.Bar) Assert.assertNotNull(org.junit.Assert.assertNotNull) Foo(spoon.test.invocations.testclasses.Foo) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Factory(spoon.reflect.factory.Factory) Collectors(java.util.stream.Collectors) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) SpoonAPI(spoon.SpoonAPI) CtTypeAccess(spoon.reflect.code.CtTypeAccess) CtModel(spoon.reflect.CtModel) CtClass(spoon.reflect.declaration.CtClass) Assert.fail(org.junit.Assert.fail) CtExecutable(spoon.reflect.declaration.CtExecutable) Assert.assertEquals(org.junit.Assert.assertEquals) CtMethod(spoon.reflect.declaration.CtMethod) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtModel(spoon.reflect.CtModel) CtExecutable(spoon.reflect.declaration.CtExecutable) 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