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());
}
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);
}
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();
}
}
}
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());
}
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());
}
Aggregations