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