Search in sources :

Example 11 with CtTypeParameterReference

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

the class JavaReflectionTreeBuilderTest method testScannerGenericsInClass.

@Test
public void testScannerGenericsInClass() throws Exception {
    final CtType<ComparableComparatorBug> aType = new JavaReflectionTreeBuilder(createFactory()).scan(ComparableComparatorBug.class);
    assertNotNull(aType);
    // New type parameter declaration.
    assertEquals(1, aType.getFormalCtTypeParameters().size());
    CtTypeParameter ctTypeParameter = aType.getFormalCtTypeParameters().get(0);
    assertEquals("E extends java.lang.Comparable<? super E>", ctTypeParameter.toString());
    assertEquals(1, ctTypeParameter.getSuperclass().getActualTypeArguments().size());
    assertTrue(ctTypeParameter.getSuperclass().getActualTypeArguments().get(0) instanceof CtTypeParameterReference);
    assertEquals("? super E", ctTypeParameter.getSuperclass().getActualTypeArguments().get(0).toString());
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) ComparableComparatorBug(spoon.test.generics.ComparableComparatorBug) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) Test(org.junit.Test)

Example 12 with CtTypeParameterReference

use of spoon.reflect.reference.CtTypeParameterReference 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 13 with CtTypeParameterReference

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

the class LambdaTest method testGetDeclarationOnTypeParameterFromLambda.

@Test
public void testGetDeclarationOnTypeParameterFromLambda() {
    List<CtTypeParameterReference> listCtTPR = launcher.getModel().getElements(new TypeFilter<>(CtTypeParameterReference.class));
    for (CtTypeParameterReference typeParameterReference : listCtTPR) {
        if (!(typeParameterReference instanceof CtWildcardReference) && typeParameterReference.getDeclaration() == null) {
            System.err.println(typeParameterReference.getSimpleName() + " from parent " + typeParameterReference.getParent(CtClass.class).getPosition() + "  has null declaration");
            typeParameterReference.getDeclaration();
            fail();
        }
    }
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) CtWildcardReference(spoon.reflect.reference.CtWildcardReference) Test(org.junit.Test)

Example 14 with CtTypeParameterReference

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

the class TypeReferenceTest method testTypeReferenceSpecifiedInClassDeclarationInNoClasspathWithGenerics.

@Test
public void testTypeReferenceSpecifiedInClassDeclarationInNoClasspathWithGenerics() throws Exception {
    // contract: Gets the import of a type specified in the declaration of a class.
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/resources/noclasspath/Demo2.java");
    launcher.setSourceOutputDirectory("./target/class-declaration");
    launcher.getEnvironment().setNoClasspath(true);
    launcher.run();
    final CtClass<Object> aClass = launcher.getFactory().Class().get("Demo2");
    Set<CtTypeReference<?>> superInterfaces = aClass.getSuperInterfaces();
    final CtTypeReference superInterface = superInterfaces.toArray(new CtTypeReference[superInterfaces.size()])[0];
    assertEquals("Bar", superInterface.getSimpleName());
    assertEquals(2, superInterface.getActualTypeArguments().size());
    final CtTypeReference<?> first = superInterface.getActualTypeArguments().get(0);
    assertTrue(first instanceof CtTypeParameterReference);
    assertEquals("?", first.getSimpleName());
    final CtTypeReference<?> second = superInterface.getActualTypeArguments().get(1);
    assertTrue(second instanceof CtTypeParameterReference);
    assertEquals("?", second.getSimpleName());
    // New.
    final CtTypeReference<?> bound = ((CtTypeParameterReference) second).getBoundingType();
    assertEquals("Tacos", bound.getSimpleName());
    assertEquals(1, bound.getActualTypeArguments().size());
    assertEquals("?", bound.getActualTypeArguments().get(0).getSimpleName());
    assertEquals("example.FooBar", superInterface.getDeclaringType().getQualifiedName());
    assertEquals("example.FooBar<?, ? extends Tacos<?>>.Bar<?, ? extends Tacos<?>>", superInterface.toString());
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 15 with CtTypeParameterReference

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

the class TypeReferenceTest method testClearBoundsForTypeParameterReference.

@Test
public void testClearBoundsForTypeParameterReference() throws Exception {
    final Factory factory = createFactory();
    final CtTypeParameterReference reference = factory.Type().createTypeParameterReference("T");
    reference.addBound(factory.Type().createReference(String.class));
    assertNotNull(reference.getBoundingType());
    reference.setBounds(null);
    assertEquals(factory.Type().OBJECT, reference.getBoundingType());
    assertTrue(reference.isDefaultBoundingType());
    reference.addBound(factory.Type().createReference(String.class));
    assertNotNull(reference.getBoundingType());
    reference.setBounds(new ArrayList<>());
    assertEquals(factory.Type().OBJECT, reference.getBoundingType());
    assertTrue(reference.isDefaultBoundingType());
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) Test(org.junit.Test)

Aggregations

CtTypeParameterReference (spoon.reflect.reference.CtTypeParameterReference)32 CtTypeReference (spoon.reflect.reference.CtTypeReference)11 Test (org.junit.Test)10 CtTypeParameter (spoon.reflect.declaration.CtTypeParameter)10 ArrayList (java.util.ArrayList)6 Factory (spoon.reflect.factory.Factory)4 Launcher (spoon.Launcher)3 SpoonException (spoon.SpoonException)3 CtWildcardReference (spoon.reflect.reference.CtWildcardReference)3 Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)2 TypeVariableBinding (org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding)2 WildcardBinding (org.eclipse.jdt.internal.compiler.lookup.WildcardBinding)2 CtMethod (spoon.reflect.declaration.CtMethod)2 CtType (spoon.reflect.declaration.CtType)2 CtArrayTypeReference (spoon.reflect.reference.CtArrayTypeReference)2 CtIntersectionTypeReference (spoon.reflect.reference.CtIntersectionTypeReference)2 CtScanner (spoon.reflect.visitor.CtScanner)2 MainTest (spoon.test.main.MainTest)2 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)2 List (java.util.List)1