use of spoon.reflect.declaration.CtType in project spoon by INRIA.
the class GenericsTest method testGetDeclarationOfTypeParameterReference.
@Test
public void testGetDeclarationOfTypeParameterReference() {
Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/generics/testclasses/ExtendedPaella.java");
launcher.addInputResource("./src/test/java/spoon/test/generics/testclasses/Paella.java");
launcher.buildModel();
Factory factory = launcher.getFactory();
CtClass extendedPaella = factory.getModel().getElements(new NamedElementFilter<>(CtClass.class, "ExtendedPaella")).get(0);
List<CtTypeParameter> typeParameterList = extendedPaella.getFormalCtTypeParameters();
assertEquals(1, typeParameterList.size());
CtMethod totoMethod = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "toto")).get(0);
CtTypeReference returnTypeToto = totoMethod.getType();
CtTypeReference paramToto = ((CtParameter) totoMethod.getParameters().get(0)).getType();
CtType declaration = returnTypeToto.getDeclaration();
assertSame(typeParameterList.get(0), declaration);
assertSame(typeParameterList.get(0), paramToto.getDeclaration());
CtMethod machinMethod = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "machin")).get(0);
CtTypeReference returnTypeMachin = machinMethod.getType();
List<CtTypeParameter> formalCtTypeParameters = machinMethod.getFormalCtTypeParameters();
assertEquals(1, formalCtTypeParameters.size());
CtType declarationMachin = returnTypeMachin.getDeclaration();
assertNotSame(typeParameterList.get(0), declarationMachin);
assertSame(formalCtTypeParameters.get(0), declarationMachin);
CtClass innerPaella = factory.getModel().getElements(new NamedElementFilter<>(CtClass.class, "InnerPaella")).get(0);
List<CtTypeParameter> innerTypeParametersList = innerPaella.getFormalCtTypeParameters();
assertEquals(typeParameterList.get(0), innerTypeParametersList.get(0).getSuperclass().getDeclaration());
CtMethod innerMachinMethod = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "innerMachin")).get(0);
CtTypeReference returnTypeInnerMachin = innerMachinMethod.getType();
CtTypeReference paramInnerMachinType = ((CtParameter) innerMachinMethod.getParameters().get(0)).getType();
List<CtTypeParameter> innerMachinFormalCtType = innerMachinMethod.getFormalCtTypeParameters();
assertSame(typeParameterList.get(0), returnTypeInnerMachin.getDeclaration());
assertSame(innerMachinFormalCtType.get(0), paramInnerMachinType.getDeclaration());
CtMethod innerTotoMethod = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "innerToto")).get(0);
CtTypeReference returnInnerToto = innerTotoMethod.getType();
CtTypeReference paramInnerToto = ((CtParameter) innerTotoMethod.getParameters().get(0)).getType();
List<CtTypeParameter> innerTotoFormatCtType = innerTotoMethod.getFormalCtTypeParameters();
assertSame(innerTotoFormatCtType.get(0), paramInnerToto.getDeclaration());
assertSame(innerTypeParametersList.get(0), returnInnerToto.getDeclaration());
}
use of spoon.reflect.declaration.CtType in project spoon by INRIA.
the class FilterTest method testCtScannerListener.
@Test
public void testCtScannerListener() throws Exception {
// contract: CtScannerFunction can be subclassed and configured by a CtScannerListener
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput", "--level", "info" });
launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
launcher.run();
class Context {
long nrOfEnter = 0;
long nrOfEnterRetTrue = 0;
long nrOfExit = 0;
long nrOfResults = 0;
}
Context context1 = new Context();
// scan only packages until top level classes. Do not scan class internals
List<CtElement> result1 = launcher.getFactory().getModel().map(new CtScannerFunction().setListener(new CtScannerListener() {
@Override
public ScanningMode enter(CtElement element) {
context1.nrOfEnter++;
if (element instanceof CtType) {
return ScanningMode.SKIP_CHILDREN;
}
return ScanningMode.NORMAL;
}
@Override
public void exit(CtElement element) {
context1.nrOfExit++;
}
})).list();
// check that test is visiting some nodes
assertTrue(context1.nrOfEnter > 0);
assertTrue(result1.size() > 0);
// contract: if enter is called and returns SKIP_CHILDREN or NORMAL, then exit must be called too. Exceptions are ignored for now
assertEquals(context1.nrOfEnter, context1.nrOfExit);
Context context2 = new Context();
Iterator iter = result1.iterator();
// scan only from packages till top level classes. Do not scan class internals
launcher.getFactory().getModel().map(new CtScannerFunction().setListener(new CtScannerListener() {
int inClass = 0;
@Override
public ScanningMode enter(CtElement element) {
context2.nrOfEnter++;
if (inClass > 0) {
// we are in class. skip this node and all children
return ScanningMode.SKIP_ALL;
}
if (element instanceof CtType) {
inClass++;
}
context2.nrOfEnterRetTrue++;
return ScanningMode.NORMAL;
}
@Override
public void exit(CtElement element) {
context2.nrOfExit++;
if (element instanceof CtType) {
inClass--;
}
assertTrue(inClass == 0 || inClass == 1);
}
})).forEach(ele -> {
context2.nrOfResults++;
assertTrue("ele instanceof " + ele.getClass(), ele instanceof CtPackage || ele instanceof CtType || ele instanceof CtModule);
// check that first and second query returned same results
assertSame(ele, iter.next());
});
// check that test is visiting some nodes
assertTrue(context2.nrOfEnter > 0);
assertTrue(context2.nrOfEnter > context2.nrOfEnterRetTrue);
assertEquals(result1.size(), context2.nrOfResults);
// contract: if enter is called and does not returns SKIP_ALL, then exit must be called too. Exceptions are ignored for now
assertEquals(context2.nrOfEnterRetTrue, context2.nrOfExit);
}
use of spoon.reflect.declaration.CtType in project spoon by INRIA.
the class CtTypeParameterTest method getTypeParamIdentification.
private String getTypeParamIdentification(CtTypeParameter typeParam) {
String result = "<" + typeParam.getSimpleName() + ">";
CtFormalTypeDeclarer l_decl = typeParam.getParent(CtFormalTypeDeclarer.class);
if (l_decl instanceof CtType) {
return ((CtType) l_decl).getQualifiedName() + result;
}
if (l_decl instanceof CtExecutable) {
CtExecutable exec = (CtExecutable) l_decl;
if (exec instanceof CtMethod) {
result = exec.getSignature() + result;
}
return exec.getParent(CtType.class).getQualifiedName() + "#" + result;
}
throw new AssertionError();
}
use of spoon.reflect.declaration.CtType 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.declaration.CtType in project spoon by INRIA.
the class TypeFactoryTest method reflectionAPI.
@Test
public void reflectionAPI() throws Exception {
// Spoon can be used as reflection API
CtType s = new TypeFactory().get(String.class);
assertEquals("String", s.getSimpleName());
assertEquals("java.lang.String", s.getQualifiedName());
assertEquals(3, s.getSuperInterfaces().size());
assertEquals(2, s.getMethodsByName("toLowerCase").size());
}
Aggregations