Search in sources :

Example 1 with ErasureModelA

use of spoon.test.ctType.testclasses.ErasureModelA in project spoon by INRIA.

the class CtTypeParameterTest method testTypeSame.

@Test
public void testTypeSame() throws Exception {
    CtClass<?> ctModel = (CtClass<?>) ModelUtils.buildClass(ErasureModelA.class);
    CtTypeParameter tpA = ctModel.getFormalCtTypeParameters().get(0);
    CtTypeParameter tpB = ctModel.getFormalCtTypeParameters().get(1);
    CtTypeParameter tpC = ctModel.getFormalCtTypeParameters().get(2);
    CtTypeParameter tpD = ctModel.getFormalCtTypeParameters().get(3);
    CtConstructor<?> ctModelCons = ctModel.getConstructors().iterator().next();
    CtMethod<?> ctModelMethod = ctModel.getMethodsByName("method").get(0);
    CtMethod<?> ctModelMethod2 = ctModel.getMethodsByName("method2").get(0);
    CtClass<?> ctModelB = ctModel.filterChildren(new NamedElementFilter<>(CtClass.class, "ModelB")).first();
    CtTypeParameter tpA2 = ctModelB.getFormalCtTypeParameters().get(0);
    CtTypeParameter tpB2 = ctModelB.getFormalCtTypeParameters().get(1);
    CtTypeParameter tpC2 = ctModelB.getFormalCtTypeParameters().get(2);
    CtTypeParameter tpD2 = ctModelB.getFormalCtTypeParameters().get(3);
    CtConstructor<?> ctModelBCons = ctModelB.getConstructors().iterator().next();
    CtMethod<?> ctModelBMethod = ctModelB.getMethodsByName("method").get(0);
    // the type parameters of ErasureModelA and ErasureModelA$ModelB are same if they are on the same position.
    checkIsSame(ctModel.getFormalCtTypeParameters(), ctModelB.getFormalCtTypeParameters(), true);
    // the type parameters of ErasureModelA#constructor and ErasureModelA$ModelB constructor are same, because constructors has same formal type parameters
    // https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.4
    checkIsSame(ctModelCons.getFormalCtTypeParameters(), ctModelBCons.getFormalCtTypeParameters(), true);
    // the type parameters of ctModel ErasureModelA#method and ErasureModelA$ModelB#method are same if they are on the same position.
    checkIsSame(ctModelMethod.getFormalCtTypeParameters(), ctModelBMethod.getFormalCtTypeParameters(), true);
    // the type parameters of ctModel ErasureModelA#constructor and ErasureModelA$ModelB#method are never same, because they have different type of scope (Method!=Constructor)
    checkIsSame(ctModelCons.getFormalCtTypeParameters(), ctModelBMethod.getFormalCtTypeParameters(), false);
    // the type parameters of ctModel ErasureModelA#method and ErasureModelA#method2 are same, because they have same formal type parameters
    // https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.4
    checkIsSame(ctModelMethod.getFormalCtTypeParameters(), ctModelMethod2.getFormalCtTypeParameters(), true);
    CtClass<?> ctModelC = ctModel.filterChildren(new NamedElementFilter<>(CtClass.class, "ModelC")).first();
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) ErasureModelA(spoon.test.ctType.testclasses.ErasureModelA) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)1 CtClass (spoon.reflect.declaration.CtClass)1 CtTypeParameter (spoon.reflect.declaration.CtTypeParameter)1 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)1 ErasureModelA (spoon.test.ctType.testclasses.ErasureModelA)1