Search in sources :

Example 21 with CtTypeParameter

use of spoon.reflect.declaration.CtTypeParameter 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 22 with CtTypeParameter

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

the class IntercessionTest method testSettersAreAllGood.

@Test
public void testSettersAreAllGood() throws Exception {
    ArrayList classpath = new ArrayList();
    for (String classpathEntry : System.getProperty("java.class.path").split(File.pathSeparator)) {
        if (!classpathEntry.contains("test-classes")) {
            classpath.add(classpathEntry);
        }
    }
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/main/java/spoon/reflect/");
    launcher.addInputResource("./src/main/java/spoon/support/");
    launcher.getModelBuilder().setSourceClasspath((String[]) classpath.toArray(new String[] {}));
    launcher.buildModel();
    final Factory factory = launcher.getFactory();
    final List<CtMethod<?>> setters = Query.getElements(factory, new AbstractFilter<CtMethod<?>>(CtMethod.class) {

        @Override
        public boolean matches(CtMethod<?> element) {
            CtType<?> declaringType = element.getDeclaringType();
            if (declaringType.getPackage() != null && (declaringType.getPackage().getQualifiedName().startsWith("spoon.support.visitor") || declaringType.getPackage().getQualifiedName().startsWith("spoon.reflect.visitor"))) {
                return false;
            }
            return declaringType.isInterface() && declaringType.getSimpleName().startsWith("Ct") && (element.getSimpleName().startsWith("set") || element.getSimpleName().startsWith("add"));
        }
    });
    for (CtMethod<?> setter : setters) {
        final String methodLog = setter.getSimpleName() + " in " + setter.getDeclaringType().getSimpleName();
        if (setter.getFormalCtTypeParameters().size() <= 0) {
            fail("Your setter " + methodLog + " don't have a generic type for its return type.");
        }
        boolean isMatch = false;
        // New type parameter declaration.
        for (CtTypeParameter typeParameter : setter.getFormalCtTypeParameters()) {
            if (setter.getType().getSimpleName().equals(typeParameter.getSimpleName())) {
                isMatch = true;
                if (setter.getAnnotation(Override.class) != null) {
                    // interface. So the return type can't be the declaring interface.
                    continue;
                }
                if (!setter.getDeclaringType().getSimpleName().equals(typeParameter.getSuperclass().getSimpleName())) {
                    fail("Your setter " + methodLog + " has a type reference who don't extends " + setter.getDeclaringType().getSimpleName());
                }
            }
        }
        assertTrue("The type of " + methodLog + " don't match with generic types.", isMatch);
    }
}
Also used : CtType(spoon.reflect.declaration.CtType) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) ArrayList(java.util.ArrayList) Launcher(spoon.Launcher) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 23 with CtTypeParameter

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

the class TypeReferenceTest method testRecursiveTypeReferenceInGenericType.

@Test
public void testRecursiveTypeReferenceInGenericType() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/reference/testclasses/EnumValue.java");
    launcher.setSourceOutputDirectory("./target/spoon-test");
    launcher.run();
    final CtClass<EnumValue> aClass = launcher.getFactory().Class().get(EnumValue.class);
    final CtMethod<?> asEnum = aClass.getMethodsByName("asEnum").get(0);
    // New type parameter declaration.
    final CtTypeParameter typeParameter = asEnum.getFormalCtTypeParameters().get(0);
    assertNotNull(typeParameter);
    assertNotNull(typeParameter.getSuperclass());
    final CtTypeReference<?> extendsGeneric = typeParameter.getSuperclass();
    assertNotNull(extendsGeneric);
    assertEquals(1, extendsGeneric.getActualTypeArguments().size());
    final CtTypeReference circularRef = extendsGeneric.getActualTypeArguments().get(0);
    assertNotNull(circularRef);
}
Also used : CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) EnumValue(spoon.test.reference.testclasses.EnumValue) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 24 with CtTypeParameter

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

the class TypeTest method testIntersectionTypeReferenceInGenericsAndCasts.

@Test
public void testIntersectionTypeReferenceInGenericsAndCasts() 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("prepare").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());
    assertIntersectionTypeForPozolePrepareMethod(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());
    assertTrue(lambdas.get(0).getTypeCasts().get(0) instanceof CtIntersectionTypeReference);
    final CtIntersectionTypeReference<?> intersectionType = lambdas.get(0).getTypeCasts().get(0).asCtIntersectionTypeReference();
    assertEquals("java.lang.Runnable & java.io.Serializable", intersectionType.toString());
    assertEquals(aPozole.getFactory().Type().createReference(Runnable.class), intersectionType.getBounds().stream().collect(Collectors.toList()).get(0));
    assertEquals(aPozole.getFactory().Type().createReference(Serializable.class), intersectionType.getBounds().stream().collect(Collectors.toList()).get(1));
    canBeBuilt(target, 8, true);
}
Also used : Serializable(java.io.Serializable) CtLambda(spoon.reflect.code.CtLambda) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) CtIntersectionTypeReference(spoon.reflect.reference.CtIntersectionTypeReference) CtClass(spoon.reflect.declaration.CtClass) Pozole(spoon.test.type.testclasses.Pozole) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 25 with CtTypeParameter

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

the class TypeTest method testIntersectionTypeOnTopLevelType.

@Test
public void testIntersectionTypeOnTopLevelType() throws Exception {
    final CtType<Mole> aMole = buildClass(Mole.class);
    assertEquals(1, aMole.getFormalCtTypeParameters().size());
    // New type parameter declaration.
    final CtTypeParameter typeParameter = aMole.getFormalCtTypeParameters().get(0);
    assertIntersectionTypeForMole(aMole, typeParameter.getSuperclass());
}
Also used : CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) Mole(spoon.test.type.testclasses.Mole) 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