use of spoon.reflect.declaration.CtTypeParameter in project spoon by INRIA.
the class CtMethodImpl method setFormalCtTypeParameters.
@Override
public <C extends CtFormalTypeDeclarer> C setFormalCtTypeParameters(List<CtTypeParameter> formalTypeParameters) {
getFactory().getEnvironment().getModelChangeListener().onListDeleteAll(this, TYPE_PARAMETER, this.formalCtTypeParameters, new ArrayList<>(this.formalCtTypeParameters));
if (formalTypeParameters == null || formalTypeParameters.isEmpty()) {
this.formalCtTypeParameters = CtElementImpl.emptyList();
return (C) this;
}
if (this.formalCtTypeParameters == CtElementImpl.<CtTypeParameter>emptyList()) {
this.formalCtTypeParameters = new ArrayList<>(TYPE_TYPE_PARAMETERS_CONTAINER_DEFAULT_CAPACITY);
}
this.formalCtTypeParameters.clear();
for (CtTypeParameter formalTypeParameter : formalTypeParameters) {
addFormalCtTypeParameter(formalTypeParameter);
}
return (C) this;
}
use of spoon.reflect.declaration.CtTypeParameter in project spoon by INRIA.
the class CtConstructorImpl method setFormalCtTypeParameters.
@Override
public <C extends CtFormalTypeDeclarer> C setFormalCtTypeParameters(List<CtTypeParameter> formalTypeParameters) {
if (formalTypeParameters == null || formalTypeParameters.isEmpty()) {
this.formalCtTypeParameters = CtElementImpl.emptyList();
return (C) this;
}
if (this.formalCtTypeParameters == CtElementImpl.<CtTypeParameter>emptyList()) {
this.formalCtTypeParameters = new ArrayList<>(TYPE_TYPE_PARAMETERS_CONTAINER_DEFAULT_CAPACITY);
}
getFactory().getEnvironment().getModelChangeListener().onListDeleteAll(this, TYPE_PARAMETER, this.formalCtTypeParameters, new ArrayList<>(this.formalCtTypeParameters));
this.formalCtTypeParameters.clear();
for (CtTypeParameter formalTypeParameter : formalTypeParameters) {
addFormalCtTypeParameter(formalTypeParameter);
}
return (C) this;
}
use of spoon.reflect.declaration.CtTypeParameter 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;
}
use of spoon.reflect.declaration.CtTypeParameter 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.declaration.CtTypeParameter in project spoon by INRIA.
the class AnnotationTest method testUsageOfTypeAnnotationWithGenericTypesInClassDeclaration.
@Test
public void testUsageOfTypeAnnotationWithGenericTypesInClassDeclaration() throws Exception {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/AnnotationsAppliedOnAnyTypeInAClass.java");
launcher.buildModel();
Factory factory = launcher.getFactory();
final CtClass<?> ctClass = (CtClass<?>) factory.Type().get("spoon.test.annotation.testclasses.AnnotationsAppliedOnAnyTypeInAClass");
final CtClass<?> genericClass = ctClass.getElements(new NamedElementFilter<>(CtClass.class, "DummyGenericClass")).get(0);
// New type parameter declaration.
final List<CtTypeParameter> typeParameters = genericClass.getFormalCtTypeParameters();
assertEquals("Generic class has 2 generics parameters.", 2, typeParameters.size());
assertEquals("First generic type must have type annotation", "@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "T", typeParameters.get(0).toString());
assertEquals("Second generic type must have type annotation", "@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "K", typeParameters.get(1).toString());
final CtTypeReference<?> superInterface = genericClass.getSuperInterfaces().toArray(new CtTypeReference<?>[0])[0];
final String expected = "spoon.test.annotation.testclasses.BasicAnnotation<@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "T>";
assertEquals("Super interface has a generic type with type annotation", expected, superInterface.toString());
}
Aggregations