use of spoon.reflect.declaration.CtInterface in project spoon by INRIA.
the class FilterTest method testFunctionQueryStep.
@Test
public void testFunctionQueryStep() throws Exception {
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 {
int count = 0;
}
Context context = new Context();
CtQuery query = launcher.getFactory().Package().getRootPackage().filterChildren((CtClass<?> c) -> {
return true;
}).name("filter CtClass only").map((CtClass<?> c) -> c.getSuperInterfaces()).name("super interfaces").map((CtTypeReference<?> iface) -> iface.getTypeDeclaration()).map((CtType<?> iface) -> iface.getAllMethods()).name("allMethods if interface").map((CtMethod<?> method) -> method.getSimpleName().equals("make")).map((CtMethod<?> m) -> m.getType()).map((CtTypeReference<?> t) -> t.getTypeDeclaration());
((CtQueryImpl) query).logging(true);
query.forEach((CtInterface<?> c) -> {
assertEquals("ITostada", c.getSimpleName());
context.count++;
});
assertTrue(context.count > 0);
}
use of spoon.reflect.declaration.CtInterface in project spoon by INRIA.
the class GenericsTest method testAccessToGenerics.
@Test
public void testAccessToGenerics() throws Exception {
Launcher spoon = new Launcher();
Factory factory = spoon.createFactory();
SpoonModelBuilder compiler = spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/java/spoon/test/generics/Foo.java", "./src/test/java/spoon/test/generics/Bar.java"));
compiler.build();
CtClass<?> foo = (CtClass<?>) factory.Type().get(Foo.class);
CtInterface<?> bar = (CtInterface<?>) factory.Type().get(Bar.class);
final CtNewClass<?> newAnonymousBar = foo.getElements(new AbstractFilter<CtNewClass<?>>(CtNewClass.class) {
@Override
public boolean matches(CtNewClass<?> element) {
return element.getAnonymousClass() != null && element.getAnonymousClass().isAnonymous();
}
}).get(0);
final List<CtTypeParameter> barTypeParamGenerics = bar.getFormalCtTypeParameters();
final CtTypeReference<?> anonymousBar = newAnonymousBar.getType();
assertEquals("Name of the first generic parameter in Bar interface must to be I.", "I", barTypeParamGenerics.get(0).getSimpleName());
assertEquals("Name of the first generic parameter in Bar usage must to be K.", "K", anonymousBar.getActualTypeArguments().get(0).getSimpleName());
assertEquals("Name of the second generic parameter in Bar interface must to be O.", "O", barTypeParamGenerics.get(1).getSimpleName());
assertEquals("Name of the second generic parameter in Bar usage must to be V.", "V", anonymousBar.getActualTypeArguments().get(1).getSimpleName());
}
use of spoon.reflect.declaration.CtInterface in project spoon by INRIA.
the class SpoonArchitectureEnforcerTest method testInterfacesAreCtScannable.
@Test
public void testInterfacesAreCtScannable() {
// contract: all non-leaf interfaces of the metamodel should be visited by CtInheritanceScanner
Launcher interfaces = new Launcher();
interfaces.addInputResource("src/main/java/spoon/support");
interfaces.addInputResource("src/main/java/spoon/reflect/declaration");
interfaces.addInputResource("src/main/java/spoon/reflect/code");
interfaces.addInputResource("src/main/java/spoon/reflect/reference");
interfaces.addInputResource("src/main/java/spoon/support/reflect/declaration");
interfaces.addInputResource("src/main/java/spoon/support/reflect/code");
interfaces.addInputResource("src/main/java/spoon/support/reflect/reference");
interfaces.addInputResource("src/main/java/spoon/reflect/visitor/CtScanner.java");
interfaces.buildModel();
CtClass<?> ctScanner = interfaces.getFactory().Class().get(CtInheritanceScanner.class);
List<String> missingMethods = new ArrayList<>();
new SpoonMetaModel(interfaces.getFactory()).getConcepts().forEach(mmConcept -> {
if (mmConcept.getKind() == ABSTRACT && mmConcept.getModelInterface() != null) {
CtInterface abstractIface = mmConcept.getModelInterface();
String methodName = "scan" + abstractIface.getSimpleName();
if (ctScanner.getMethodsByName(methodName).isEmpty()) {
missingMethods.add(methodName);
}
}
});
assertTrue("The following methods are missing in CtScanner: \n" + StringUtils.join(missingMethods, "\n"), missingMethods.isEmpty());
}
use of spoon.reflect.declaration.CtInterface in project spoon by INRIA.
the class ReplaceScanner method getTypeFromTypeParameterReference.
private CtTypeReference getTypeFromTypeParameterReference(CtTypeParameterReference ctTypeParameterRef) {
final CtMethod parentMethod = ctTypeParameterRef.getParent(CtMethod.class);
for (CtTypeParameter formal : parentMethod.getFormalCtTypeParameters()) {
if (formal.getSimpleName().equals(ctTypeParameterRef.getSimpleName())) {
return ((CtTypeParameterReference) formal).getBoundingType();
}
}
final CtInterface parentInterface = ctTypeParameterRef.getParent(CtInterface.class);
for (CtTypeParameter formal : parentInterface.getFormalCtTypeParameters()) {
if (formal.getSimpleName().equals(ctTypeParameterRef.getSimpleName())) {
return formal.getReference().getBoundingType();
}
}
throw new SpoonException("Can't get the type of the CtTypeParameterReference " + ctTypeParameterRef);
}
use of spoon.reflect.declaration.CtInterface in project spoon by INRIA.
the class TypeFactory method createTypeAdapter.
/**
* Create a {@link GenericTypeAdapter} for adapting of formal type parameters from any compatible context to the context of provided `formalTypeDeclarer`
*
* @param formalTypeDeclarer
* the target scope of the returned {@link GenericTypeAdapter}
*/
public GenericTypeAdapter createTypeAdapter(CtFormalTypeDeclarer formalTypeDeclarer) {
class Visitor extends CtAbstractVisitor {
GenericTypeAdapter adapter;
@Override
public <T> void visitCtClass(CtClass<T> ctClass) {
adapter = new ClassTypingContext(ctClass);
}
@Override
public <T> void visitCtInterface(CtInterface<T> intrface) {
adapter = new ClassTypingContext(intrface);
}
@Override
public <T> void visitCtMethod(CtMethod<T> m) {
adapter = new MethodTypingContext().setMethod(m);
}
@Override
public <T> void visitCtConstructor(CtConstructor<T> c) {
adapter = new MethodTypingContext().setConstructor(c);
}
}
Visitor visitor = new Visitor();
((CtElement) formalTypeDeclarer).accept(visitor);
return visitor.adapter;
}
Aggregations