use of spoon.reflect.reference.CtTypeParameterReference in project spoon by INRIA.
the class GenericsTest method testTypeParameterDeclarer.
@Test
public void testTypeParameterDeclarer() throws Exception {
// contract: one can lookup the declarer of a type parameter if it is in appropriate context (the declararer is in the parent hierarchy)
CtClass<?> classThatDefinesANewTypeArgument = build("spoon.test.generics", "ClassThatDefinesANewTypeArgument");
CtTypeParameter typeParam = classThatDefinesANewTypeArgument.getFormalCtTypeParameters().get(0);
assertEquals("T", classThatDefinesANewTypeArgument.getFormalCtTypeParameters().get(0).getSimpleName());
assertSame(classThatDefinesANewTypeArgument, typeParam.getTypeParameterDeclarer());
CtTypeParameterReference typeParamReference = typeParam.getReference();
assertSame(typeParam, typeParamReference.getDeclaration());
// creating an appropriate context
CtMethod m = classThatDefinesANewTypeArgument.getFactory().createMethod();
m.setParent(classThatDefinesANewTypeArgument);
// setting the return type of the method
m.setType(typeParamReference);
classThatDefinesANewTypeArgument.addMethod(m);
// the final assertions
assertSame(typeParam, typeParamReference.getDeclaration());
assertSame(classThatDefinesANewTypeArgument, typeParamReference.getDeclaration().getParent());
// now testing that the getDeclaration of a type parameter is actually a dynamic lookup
CtClass<?> c2 = classThatDefinesANewTypeArgument.clone();
c2.addMethod(m);
assertSame(c2, typeParamReference.getDeclaration().getParent());
// even if we rename it
// renaming the reference
typeParamReference.setSimpleName("R");
// renaming the declaration
c2.getFormalCtTypeParameters().get(0).setSimpleName("R");
assertSame(c2, typeParamReference.getDeclaration().getParent());
}
use of spoon.reflect.reference.CtTypeParameterReference in project spoon by INRIA.
the class GenericsTest method testTypeParameterReference.
@Test
public void testTypeParameterReference() throws Exception {
Factory factory = build(ClassThatBindsAGenericType.class, ClassThatDefinesANewTypeArgument.class);
CtClass<?> classThatBindsAGenericType = factory.Class().get(ClassThatBindsAGenericType.class);
CtClass<?> classThatDefinesANewTypeArgument = factory.Class().get(ClassThatDefinesANewTypeArgument.class);
CtTypeReference<?> tr1 = classThatBindsAGenericType.getSuperclass();
CtTypeReference<?> trExtends = tr1.getActualTypeArguments().get(0);
CtTypeParameter tr2 = classThatDefinesANewTypeArgument.getFormalCtTypeParameters().get(0);
CtTypeReference<?> tr3 = classThatDefinesANewTypeArgument.getMethodsByName("foo").get(0).getParameters().get(0).getReference().getType();
// an bound type is not an TypeParameterRefernce
assertTrue(!(trExtends instanceof CtTypeParameterReference));
// a used type parameter T is a CtTypeParameterReference
assertTrue(tr3 instanceof CtTypeParameterReference);
assertEquals("File", trExtends.getSimpleName());
assertEquals(java.io.File.class, trExtends.getActualClass());
assertEquals("T", tr2.getSimpleName());
assertEquals("T", tr3.getSimpleName());
}
use of spoon.reflect.reference.CtTypeParameterReference in project spoon by INRIA.
the class GenericsTest method checkFakeTpl.
private void checkFakeTpl(CtInterface<?> fakeTplItf) {
assertNotNull(fakeTplItf);
CtMethod<?> applyMethod = fakeTplItf.getMethodsByName("apply").get(0);
CtTypeReference<?> returnType = applyMethod.getType();
assertEquals("T", returnType.getSimpleName());
assertTrue(returnType instanceof CtTypeParameterReference);
assertEquals("CtElement", returnType.getSuperclass().getSimpleName());
CtParameter<?> targetType = applyMethod.getParameters().get(0);
List<CtTypeReference<?>> targetTypeArgument = targetType.getType().getActualTypeArguments();
assertEquals(1, targetTypeArgument.size());
assertTrue(targetTypeArgument.get(0) instanceof CtWildcardReference);
CtMethod<?> testMethod = fakeTplItf.getMethodsByName("test").get(0);
List<CtParameter<?>> parameters = testMethod.getParameters();
assertEquals(3, parameters.size());
CtParameter thirdParam = parameters.get(2);
assertTrue(thirdParam.getType() instanceof CtTypeParameterReference);
}
use of spoon.reflect.reference.CtTypeParameterReference in project spoon by INRIA.
the class AbstractTypingContext method adaptTypeParameterReferenceBoundingType.
private CtTypeReference<?> adaptTypeParameterReferenceBoundingType(CtTypeParameterReference typeParamRef, CtTypeReference<?> boundingType) {
CtTypeParameterReference typeParamRefAdapted = typeParamRef.clone();
typeParamRefAdapted.setParent(typeParamRef.getParent());
typeParamRefAdapted.setBoundingType(boundingType.equals(boundingType.getFactory().Type().getDefaultBoundingType()) ? boundingType.getFactory().Type().getDefaultBoundingType() : adaptType(boundingType));
return typeParamRefAdapted;
}
use of spoon.reflect.reference.CtTypeParameterReference in project spoon by INRIA.
the class ClassTypingContext method resolveTypeParameters.
/**
* resolve typeRefs declared in scope of declarer using actual type arguments registered in typeScopeToActualTypeArguments
* @param typeRefs to be resolved type references
* @return resolved type references - one for each `typeRefs`
* @throws SpoonException if they cannot be resolved. It should not normally happen. If it happens then spoon AST model is probably not consistent.
*/
private List<CtTypeReference<?>> resolveTypeParameters(List<CtTypeReference<?>> typeRefs) {
List<CtTypeReference<?>> result = new ArrayList<>(typeRefs.size());
for (CtTypeReference<?> typeRef : typeRefs) {
if (typeRef instanceof CtTypeParameterReference) {
CtTypeParameterReference typeParamRef = (CtTypeParameterReference) typeRef;
CtTypeParameter typeParam = typeParamRef.getDeclaration();
CtFormalTypeDeclarer declarer = typeParam.getTypeParameterDeclarer();
typeRef = resolveTypeParameter(declarer, typeParamRef, typeParam, typeRef);
}
result.add(typeRef);
}
return result;
}
Aggregations