Search in sources :

Example 1 with CtIntersectionTypeReference

use of spoon.reflect.reference.CtIntersectionTypeReference in project spoon by INRIA.

the class ConstructorTest method assertIntersectionTypeInConstructor.

private void assertIntersectionTypeInConstructor(CtTypeReference<?> boundingType1) {
    assertTrue(boundingType1 instanceof CtIntersectionTypeReference);
    CtIntersectionTypeReference<?> boundingType = boundingType1.asCtIntersectionTypeReference();
    final List<CtTypeReference<?>> bounds = boundingType.getBounds().stream().collect(Collectors.toList());
    CtTypeReference<?> genericTacos = bounds.get(0);
    assertEquals("Tacos", genericTacos.getSimpleName());
    assertEquals(1, genericTacos.getAnnotations().size());
    assertEquals(1, genericTacos.getActualTypeArguments().size());
    CtTypeParameterReference wildcard = (CtTypeParameterReference) genericTacos.getActualTypeArguments().get(0);
    assertEquals("?", wildcard.getSimpleName());
    assertEquals(1, wildcard.getAnnotations().size());
    assertEquals("C", wildcard.getBoundingType().getSimpleName());
    assertEquals(1, wildcard.getBoundingType().getAnnotations().size());
    assertEquals("Serializable", bounds.get(1).getSimpleName());
    assertEquals(1, bounds.get(1).getAnnotations().size());
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtIntersectionTypeReference(spoon.reflect.reference.CtIntersectionTypeReference)

Example 2 with CtIntersectionTypeReference

use of spoon.reflect.reference.CtIntersectionTypeReference 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 3 with CtIntersectionTypeReference

use of spoon.reflect.reference.CtIntersectionTypeReference in project spoon by INRIA.

the class CtTypeImpl method isSameParameter.

private boolean isSameParameter(CtMethod<?> method, CtTypeReference<?> ctParameterType, CtTypeReference<?> expectedType) {
    if (expectedType instanceof CtTypeParameterReference) {
        /*
			 * the expectedType is a generic parameter whose declaration should be searched in scope of method
			 * (not in scope of it's parent, where it can found another/wrong type parameter declaration of same name.
			 */
        CtTypeParameterReference tpr = (CtTypeParameterReference) expectedType;
        expectedType = tpr.clone();
        expectedType.setParent(method);
        if (expectedType.getDeclaration() == null) {
            return false;
        }
    }
    if (expectedType instanceof CtTypeParameterReference && ctParameterType instanceof CtTypeParameterReference) {
        // both types are generic
        if (!ctParameterType.equals(expectedType)) {
            return false;
        }
    } else if (expectedType instanceof CtTypeParameterReference) {
        // expectedType type is generic, ctParameterType is real type
        if (!expectedType.getTypeErasure().getQualifiedName().equals(ctParameterType.getQualifiedName())) {
            return false;
        }
    } else if (ctParameterType instanceof CtTypeParameterReference) {
        // ctParameterType is generic, expectedType type is real type
        CtTypeParameter declaration = (CtTypeParameter) ctParameterType.getDeclaration();
        if (declaration != null && declaration.getSuperclass() instanceof CtIntersectionTypeReference) {
            for (CtTypeReference<?> ctTypeReference : declaration.getSuperclass().asCtIntersectionTypeReference().getBounds()) {
                if (ctTypeReference.equals(expectedType)) {
                    return true;
                }
            }
        } else if (declaration != null && declaration.getSuperclass() != null) {
            return declaration.getSuperclass().equals(expectedType);
        } else {
            return getFactory().Type().objectType().equals(expectedType);
        }
    } else if (!expectedType.getQualifiedName().equals(ctParameterType.getQualifiedName())) {
        // both are real types
        return false;
    }
    return true;
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) CtIntersectionTypeReference(spoon.reflect.reference.CtIntersectionTypeReference)

Aggregations

CtIntersectionTypeReference (spoon.reflect.reference.CtIntersectionTypeReference)3 CtTypeParameter (spoon.reflect.declaration.CtTypeParameter)2 CtTypeParameterReference (spoon.reflect.reference.CtTypeParameterReference)2 Serializable (java.io.Serializable)1 Test (org.junit.Test)1 Launcher (spoon.Launcher)1 CtLambda (spoon.reflect.code.CtLambda)1 CtClass (spoon.reflect.declaration.CtClass)1 CtTypeReference (spoon.reflect.reference.CtTypeReference)1 Pozole (spoon.test.type.testclasses.Pozole)1