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());
}
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());
}
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());
}
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());
}
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());
}
Aggregations