Search in sources :

Example 6 with ClassTypingContext

use of spoon.support.visitor.ClassTypingContext in project spoon by INRIA.

the class GenericsTest method testIsSameSignatureWithMethodGenerics.

@Test
public void testIsSameSignatureWithMethodGenerics() {
    Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/generics/testclasses2/SameSignature2.java");
    launcher.buildModel();
    CtClass ctClass = launcher.getFactory().Class().get(SameSignature2.class);
    CtMethod classMethod = (CtMethod) ctClass.getMethodsByName("visitCtConditional").get(0);
    CtType<?> iface = launcher.getFactory().Type().get("spoon.test.generics.testclasses2.ISameSignature");
    CtMethod ifaceMethod = (CtMethod) iface.getMethodsByName("visitCtConditional").get(0);
    ClassTypingContext ctcSub = new ClassTypingContext(ctClass.getReference());
    assertTrue(ctcSub.isOverriding(classMethod, ifaceMethod));
    assertTrue(ctcSub.isOverriding(ifaceMethod, classMethod));
    assertTrue(ctcSub.isSubSignature(classMethod, ifaceMethod));
    assertTrue(ctcSub.isSubSignature(ifaceMethod, classMethod));
    assertTrue(ctcSub.isSameSignature(classMethod, ifaceMethod));
    assertTrue(ctcSub.isSameSignature(ifaceMethod, classMethod));
}
Also used : LikeCtClass(spoon.test.generics.testclasses2.LikeCtClass) CtClass(spoon.reflect.declaration.CtClass) ClassTypingContext(spoon.support.visitor.ClassTypingContext) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 7 with ClassTypingContext

use of spoon.support.visitor.ClassTypingContext in project spoon by INRIA.

the class GenericsTest method testClassTypingContextMethodSignature.

@Test
public void testClassTypingContextMethodSignature() throws Exception {
    // core contracts of MethodTypingContext#adaptMethod
    Factory factory = build(new File("src/test/java/spoon/test/generics/testclasses"));
    CtClass<?> ctClassLunch = factory.Class().get(Lunch.class);
    CtClass<?> ctClassWeddingLunch = factory.Class().get(WeddingLunch.class);
    // represents <C> void eatMe(A paramA, B paramB, C paramC){}
    CtMethod<?> trLunch_eatMe = ctClassLunch.filterChildren(new NamedElementFilter<>(CtMethod.class, "eatMe")).first();
    // represents <C> void eatMe(M paramA, K paramB, C paramC)
    CtMethod<?> trWeddingLunch_eatMe = ctClassWeddingLunch.filterChildren(new NamedElementFilter<>(CtMethod.class, "eatMe")).first();
    ClassTypingContext ctcWeddingLunch = new ClassTypingContext(ctClassWeddingLunch);
    assertTrue(ctcWeddingLunch.isOverriding(trLunch_eatMe, trLunch_eatMe));
    assertTrue(ctcWeddingLunch.isOverriding(trLunch_eatMe, trWeddingLunch_eatMe));
    assertTrue(ctcWeddingLunch.isSubSignature(trLunch_eatMe, trWeddingLunch_eatMe));
    // contract: check that adapting of methods still produces same results, even when scopeMethod is already assigned
    assertTrue(ctcWeddingLunch.isOverriding(trWeddingLunch_eatMe, trLunch_eatMe));
    assertTrue(ctcWeddingLunch.isOverriding(trWeddingLunch_eatMe, trWeddingLunch_eatMe));
    assertTrue(ctcWeddingLunch.isSubSignature(trWeddingLunch_eatMe, trWeddingLunch_eatMe));
}
Also used : ClassTypingContext(spoon.support.visitor.ClassTypingContext) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) File(java.io.File) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 8 with ClassTypingContext

use of spoon.support.visitor.ClassTypingContext in project spoon by INRIA.

the class GenericsTest method testIsSameSignatureWithGenerics.

@Test
public void testIsSameSignatureWithGenerics() {
    Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/generics/testclasses/SameSignature.java");
    launcher.buildModel();
    CtClass ctClass = launcher.getFactory().Class().get(SameSignature.class);
    List<CtMethod> methods = ctClass.getMethodsByName("forEach");
    assertEquals(1, methods.size());
    CtType<?> iterableItf = launcher.getFactory().Type().get(Iterable.class);
    List<CtMethod<?>> methodsItf = iterableItf.getMethodsByName("forEach");
    assertEquals(1, methodsItf.size());
    ClassTypingContext ctc = new ClassTypingContext(ctClass.getReference());
    assertTrue(ctc.isOverriding(methods.get(0), methodsItf.get(0)));
    assertTrue(ctc.isSubSignature(methods.get(0), methodsItf.get(0)));
    assertTrue(ctc.isSameSignature(methods.get(0), methodsItf.get(0)));
}
Also used : LikeCtClass(spoon.test.generics.testclasses2.LikeCtClass) CtClass(spoon.reflect.declaration.CtClass) ClassTypingContext(spoon.support.visitor.ClassTypingContext) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 9 with ClassTypingContext

use of spoon.support.visitor.ClassTypingContext in project spoon by INRIA.

the class CtExecutableReferenceImpl method isOverriding.

@Override
public boolean isOverriding(CtExecutableReference<?> executable) {
    CtExecutable<?> exec = executable.getExecutableDeclaration();
    CtExecutable<?> thisExec = getExecutableDeclaration();
    if (exec == null || thisExec == null) {
        // the declaration of this executable is not in spoon model
        // use light detection algorithm, which ignores generic types
        final boolean isSame = getSimpleName().equals(executable.getSimpleName()) && getParameters().equals(executable.getParameters()) && getActualTypeArguments().equals(executable.getActualTypeArguments());
        if (!isSame) {
            return false;
        }
        if (!getDeclaringType().isSubtypeOf(executable.getDeclaringType())) {
            return false;
        }
        return true;
    }
    if (exec instanceof CtMethod<?> && thisExec instanceof CtMethod<?>) {
        return new ClassTypingContext(((CtTypeMember) thisExec).getDeclaringType()).isOverriding((CtMethod<?>) thisExec, (CtMethod<?>) exec);
    }
    // it is not a method. So we can return true only if it is reference to the this executable
    return exec == getDeclaration();
}
Also used : ClassTypingContext(spoon.support.visitor.ClassTypingContext) CtTypeMember(spoon.reflect.declaration.CtTypeMember)

Example 10 with ClassTypingContext

use of spoon.support.visitor.ClassTypingContext 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

ClassTypingContext (spoon.support.visitor.ClassTypingContext)17 Test (org.junit.Test)11 MainTest (spoon.test.main.MainTest)11 CtMethod (spoon.reflect.declaration.CtMethod)10 CtClass (spoon.reflect.declaration.CtClass)6 LikeCtClass (spoon.test.generics.testclasses2.LikeCtClass)5 Launcher (spoon.Launcher)4 File (java.io.File)3 Factory (spoon.reflect.factory.Factory)3 AllTypeMembersFunction (spoon.reflect.visitor.filter.AllTypeMembersFunction)3 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)3 MethodTypingContext (spoon.support.visitor.MethodTypingContext)3 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 CtTypeMember (spoon.reflect.declaration.CtTypeMember)2 CtTypeParameter (spoon.reflect.declaration.CtTypeParameter)2 CtLambda (spoon.reflect.code.CtLambda)1 CtNewClass (spoon.reflect.code.CtNewClass)1 CtReturn (spoon.reflect.code.CtReturn)1