Search in sources :

Example 1 with CtInterface

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);
}
Also used : CtInterface(spoon.reflect.declaration.CtInterface) CtType(spoon.reflect.declaration.CtType) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtQuery(spoon.reflect.visitor.chain.CtQuery) Launcher(spoon.Launcher) CtQueryImpl(spoon.reflect.visitor.chain.CtQueryImpl) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 2 with CtInterface

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());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) CtInterface(spoon.reflect.declaration.CtInterface) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) LikeCtClass(spoon.test.generics.testclasses2.LikeCtClass) CtClass(spoon.reflect.declaration.CtClass) CtNewClass(spoon.reflect.code.CtNewClass) Launcher(spoon.Launcher) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 3 with CtInterface

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());
}
Also used : CtInterface(spoon.reflect.declaration.CtInterface) SpoonMetaModel(spoon.test.metamodel.SpoonMetaModel) ArrayList(java.util.ArrayList) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 4 with CtInterface

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);
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) CtInterface(spoon.reflect.declaration.CtInterface) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) SpoonException(spoon.SpoonException) CtMethod(spoon.reflect.declaration.CtMethod)

Example 5 with CtInterface

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;
}
Also used : CtAbstractVisitor(spoon.reflect.visitor.CtAbstractVisitor) CtClass(spoon.reflect.declaration.CtClass) CtInterface(spoon.reflect.declaration.CtInterface) ClassTypingContext(spoon.support.visitor.ClassTypingContext) GenericTypeAdapter(spoon.support.visitor.GenericTypeAdapter) CtAbstractVisitor(spoon.reflect.visitor.CtAbstractVisitor) MethodTypingContext(spoon.support.visitor.MethodTypingContext) CtElement(spoon.reflect.declaration.CtElement) CtMethod(spoon.reflect.declaration.CtMethod) CtConstructor(spoon.reflect.declaration.CtConstructor)

Aggregations

CtInterface (spoon.reflect.declaration.CtInterface)12 CtMethod (spoon.reflect.declaration.CtMethod)8 Test (org.junit.Test)7 CtClass (spoon.reflect.declaration.CtClass)7 Launcher (spoon.Launcher)6 CtType (spoon.reflect.declaration.CtType)4 ArrayList (java.util.ArrayList)3 SpoonException (spoon.SpoonException)3 CtElement (spoon.reflect.declaration.CtElement)3 Factory (spoon.reflect.factory.Factory)3 CtBlock (spoon.reflect.code.CtBlock)2 CtIf (spoon.reflect.code.CtIf)2 CtConstructor (spoon.reflect.declaration.CtConstructor)2 CtField (spoon.reflect.declaration.CtField)2 CtParameter (spoon.reflect.declaration.CtParameter)2 CtTypeParameter (spoon.reflect.declaration.CtTypeParameter)2 CtInheritanceScanner (spoon.reflect.visitor.CtInheritanceScanner)2 File (java.io.File)1 Collection (java.util.Collection)1 List (java.util.List)1