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