use of spoon.reflect.declaration.CtTypeParameter in project spoon by INRIA.
the class CtTypeParameterReferenceImpl method getDeclaration.
@Override
public CtTypeParameter getDeclaration() {
if (!isParentInitialized()) {
return null;
}
CtElement e = this;
CtElement parent = getParent();
if (parent instanceof CtTypeReference) {
if (parent.isParentInitialized() == false) {
return null;
}
parent = parent.getParent();
}
if (parent instanceof CtExecutableReference) {
CtExecutableReference parentExec = (CtExecutableReference) parent;
if (!parentExec.getDeclaringType().equals(e)) {
CtElement parent2 = parentExec.getExecutableDeclaration();
if (parent2 instanceof CtMethod) {
e = parent2;
} else {
e = e.getParent(CtFormalTypeDeclarer.class);
}
} else {
e = e.getParent(CtFormalTypeDeclarer.class);
}
} else {
e = e.getParent(CtFormalTypeDeclarer.class);
}
// collecting all formal type declarers of the hierarchy
while (e != null) {
CtTypeParameter result = findTypeParamDeclaration((CtFormalTypeDeclarer) e, this.getSimpleName());
if (result != null) {
return result;
}
e = e.getParent(CtFormalTypeDeclarer.class);
}
return null;
}
use of spoon.reflect.declaration.CtTypeParameter in project spoon by INRIA.
the class TypeTest method testTypeReferenceInGenericsAndCasts.
@Test
public void testTypeReferenceInGenericsAndCasts() 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("finish").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());
assertIntersectionTypeForPozoleFinishMethod(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());
assertEquals("java.lang.Runnable", lambdas.get(0).getTypeCasts().get(0).toString());
assertEquals(aPozole.getFactory().Type().createReference(Runnable.class), lambdas.get(0).getTypeCasts().get(0));
canBeBuilt(target, 8, true);
}
use of spoon.reflect.declaration.CtTypeParameter in project spoon by INRIA.
the class TypeTest method testTypeInfoIsInterface.
@Test
public void testTypeInfoIsInterface() throws Exception {
// contract: isInterface returns true only for interfaces
CtType<?> clazz = build("spoon.test.model", "ClassWithSuperOutOfModel");
checkIsSomething("class", clazz);
CtType<?> type = build("spoon.test.model", "InterfaceWrithFields");
checkIsSomething("interface", type);
checkIsSomething("enum", type.getFactory().Enum().create(type.getPackage(), "someEnum"));
CtType<?> ctAnnotation = type.getFactory().Annotation().create(type.getPackage(), "someAnnotation");
checkIsSomething("annotation", ctAnnotation);
CtTypeParameter ctTypeParam = type.getFactory().Core().createTypeParameter();
ctTypeParam.setSimpleName("T");
clazz.addFormalCtTypeParameter(ctTypeParam);
checkIsSomething("generics", ctTypeParam);
}
Aggregations