Search in sources :

Example 41 with CtTypeParameter

use of spoon.reflect.declaration.CtTypeParameter in project spoon by INRIA.

the class CtTypeParameterReferenceImpl method getDeclaration.

@Override
public CtTypeParameter getDeclaration() {
    if (!isParentInitialized()) {
        return null;
    }
    CtElement e = this;
    CtElement parent = getParent();
    if (parent instanceof CtTypeReference) {
        if (parent.isParentInitialized() == false) {
            return null;
        }
        parent = parent.getParent();
    }
    if (parent instanceof CtExecutableReference) {
        CtExecutableReference parentExec = (CtExecutableReference) parent;
        if (!parentExec.getDeclaringType().equals(e)) {
            CtElement parent2 = parentExec.getExecutableDeclaration();
            if (parent2 instanceof CtMethod) {
                e = parent2;
            } else {
                e = e.getParent(CtFormalTypeDeclarer.class);
            }
        } else {
            e = e.getParent(CtFormalTypeDeclarer.class);
        }
    } else {
        e = e.getParent(CtFormalTypeDeclarer.class);
    }
    // collecting all formal type declarers of the hierarchy
    while (e != null) {
        CtTypeParameter result = findTypeParamDeclaration((CtFormalTypeDeclarer) e, this.getSimpleName());
        if (result != null) {
            return result;
        }
        e = e.getParent(CtFormalTypeDeclarer.class);
    }
    return null;
}
Also used : CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) CtElement(spoon.reflect.declaration.CtElement) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtFormalTypeDeclarer(spoon.reflect.declaration.CtFormalTypeDeclarer) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) CtMethod(spoon.reflect.declaration.CtMethod)

Example 42 with CtTypeParameter

use of spoon.reflect.declaration.CtTypeParameter in project spoon by INRIA.

the class TypeTest method testTypeReferenceInGenericsAndCasts.

@Test
public void testTypeReferenceInGenericsAndCasts() throws Exception {
    final String target = "./target/type";
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/type/testclasses");
    launcher.setSourceOutputDirectory(target);
    launcher.getEnvironment().setNoClasspath(true);
    launcher.run();
    final CtClass<Pozole> aPozole = launcher.getFactory().Class().get(Pozole.class);
    final CtMethod<?> prepare = aPozole.getMethodsByName("finish").get(0);
    // Intersection type in generic types.
    final List<CtClass> localTypes = prepare.getElements(new TypeFilter<>(CtClass.class));
    assertEquals(1, localTypes.size());
    // New type parameter declaration.
    final CtTypeParameter typeParameter = localTypes.get(0).getFormalCtTypeParameters().get(0);
    assertNotNull(typeParameter);
    assertEquals("T", typeParameter.getSimpleName());
    assertIntersectionTypeForPozoleFinishMethod(aPozole, typeParameter.getSuperclass());
    // Intersection type in casts.
    final List<CtLambda<?>> lambdas = prepare.getElements(new TypeFilter<CtLambda<?>>(CtLambda.class));
    assertEquals(1, lambdas.size());
    assertEquals(1, lambdas.get(0).getTypeCasts().size());
    assertEquals("java.lang.Runnable", lambdas.get(0).getTypeCasts().get(0).toString());
    assertEquals(aPozole.getFactory().Type().createReference(Runnable.class), lambdas.get(0).getTypeCasts().get(0));
    canBeBuilt(target, 8, true);
}
Also used : CtLambda(spoon.reflect.code.CtLambda) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) CtClass(spoon.reflect.declaration.CtClass) Pozole(spoon.test.type.testclasses.Pozole) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 43 with CtTypeParameter

use of spoon.reflect.declaration.CtTypeParameter in project spoon by INRIA.

the class TypeTest method testTypeInfoIsInterface.

@Test
public void testTypeInfoIsInterface() throws Exception {
    // contract: isInterface returns true only for interfaces
    CtType<?> clazz = build("spoon.test.model", "ClassWithSuperOutOfModel");
    checkIsSomething("class", clazz);
    CtType<?> type = build("spoon.test.model", "InterfaceWrithFields");
    checkIsSomething("interface", type);
    checkIsSomething("enum", type.getFactory().Enum().create(type.getPackage(), "someEnum"));
    CtType<?> ctAnnotation = type.getFactory().Annotation().create(type.getPackage(), "someAnnotation");
    checkIsSomething("annotation", ctAnnotation);
    CtTypeParameter ctTypeParam = type.getFactory().Core().createTypeParameter();
    ctTypeParam.setSimpleName("T");
    clazz.addFormalCtTypeParameter(ctTypeParam);
    checkIsSomething("generics", ctTypeParam);
}
Also used : CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) Test(org.junit.Test)

Aggregations

CtTypeParameter (spoon.reflect.declaration.CtTypeParameter)43 Test (org.junit.Test)25 MainTest (spoon.test.main.MainTest)12 Factory (spoon.reflect.factory.Factory)10 CtTypeParameterReference (spoon.reflect.reference.CtTypeParameterReference)10 Launcher (spoon.Launcher)9 CtClass (spoon.reflect.declaration.CtClass)9 CtTypeReference (spoon.reflect.reference.CtTypeReference)8 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)7 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)7 ArrayList (java.util.ArrayList)6 CtMethod (spoon.reflect.declaration.CtMethod)6 CtType (spoon.reflect.declaration.CtType)5 LikeCtClass (spoon.test.generics.testclasses2.LikeCtClass)4 SpoonException (spoon.SpoonException)3 CtFormalTypeDeclarer (spoon.reflect.declaration.CtFormalTypeDeclarer)3 CtLambda (spoon.reflect.code.CtLambda)2 CtElement (spoon.reflect.declaration.CtElement)2 CtExecutable (spoon.reflect.declaration.CtExecutable)2 CtInterface (spoon.reflect.declaration.CtInterface)2