use of spoon.reflect.declaration.CtTypeParameter 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.declaration.CtTypeParameter in project spoon by INRIA.
the class IntercessionTest method testSettersAreAllGood.
@Test
public void testSettersAreAllGood() throws Exception {
ArrayList classpath = new ArrayList();
for (String classpathEntry : System.getProperty("java.class.path").split(File.pathSeparator)) {
if (!classpathEntry.contains("test-classes")) {
classpath.add(classpathEntry);
}
}
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/main/java/spoon/reflect/");
launcher.addInputResource("./src/main/java/spoon/support/");
launcher.getModelBuilder().setSourceClasspath((String[]) classpath.toArray(new String[] {}));
launcher.buildModel();
final Factory factory = launcher.getFactory();
final List<CtMethod<?>> setters = Query.getElements(factory, new AbstractFilter<CtMethod<?>>(CtMethod.class) {
@Override
public boolean matches(CtMethod<?> element) {
CtType<?> declaringType = element.getDeclaringType();
if (declaringType.getPackage() != null && (declaringType.getPackage().getQualifiedName().startsWith("spoon.support.visitor") || declaringType.getPackage().getQualifiedName().startsWith("spoon.reflect.visitor"))) {
return false;
}
return declaringType.isInterface() && declaringType.getSimpleName().startsWith("Ct") && (element.getSimpleName().startsWith("set") || element.getSimpleName().startsWith("add"));
}
});
for (CtMethod<?> setter : setters) {
final String methodLog = setter.getSimpleName() + " in " + setter.getDeclaringType().getSimpleName();
if (setter.getFormalCtTypeParameters().size() <= 0) {
fail("Your setter " + methodLog + " don't have a generic type for its return type.");
}
boolean isMatch = false;
// New type parameter declaration.
for (CtTypeParameter typeParameter : setter.getFormalCtTypeParameters()) {
if (setter.getType().getSimpleName().equals(typeParameter.getSimpleName())) {
isMatch = true;
if (setter.getAnnotation(Override.class) != null) {
// interface. So the return type can't be the declaring interface.
continue;
}
if (!setter.getDeclaringType().getSimpleName().equals(typeParameter.getSuperclass().getSimpleName())) {
fail("Your setter " + methodLog + " has a type reference who don't extends " + setter.getDeclaringType().getSimpleName());
}
}
}
assertTrue("The type of " + methodLog + " don't match with generic types.", isMatch);
}
}
use of spoon.reflect.declaration.CtTypeParameter in project spoon by INRIA.
the class TypeReferenceTest method testRecursiveTypeReferenceInGenericType.
@Test
public void testRecursiveTypeReferenceInGenericType() throws Exception {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/reference/testclasses/EnumValue.java");
launcher.setSourceOutputDirectory("./target/spoon-test");
launcher.run();
final CtClass<EnumValue> aClass = launcher.getFactory().Class().get(EnumValue.class);
final CtMethod<?> asEnum = aClass.getMethodsByName("asEnum").get(0);
// New type parameter declaration.
final CtTypeParameter typeParameter = asEnum.getFormalCtTypeParameters().get(0);
assertNotNull(typeParameter);
assertNotNull(typeParameter.getSuperclass());
final CtTypeReference<?> extendsGeneric = typeParameter.getSuperclass();
assertNotNull(extendsGeneric);
assertEquals(1, extendsGeneric.getActualTypeArguments().size());
final CtTypeReference circularRef = extendsGeneric.getActualTypeArguments().get(0);
assertNotNull(circularRef);
}
use of spoon.reflect.declaration.CtTypeParameter in project spoon by INRIA.
the class TypeTest method testIntersectionTypeReferenceInGenericsAndCasts.
@Test
public void testIntersectionTypeReferenceInGenericsAndCasts() 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("prepare").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());
assertIntersectionTypeForPozolePrepareMethod(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());
assertTrue(lambdas.get(0).getTypeCasts().get(0) instanceof CtIntersectionTypeReference);
final CtIntersectionTypeReference<?> intersectionType = lambdas.get(0).getTypeCasts().get(0).asCtIntersectionTypeReference();
assertEquals("java.lang.Runnable & java.io.Serializable", intersectionType.toString());
assertEquals(aPozole.getFactory().Type().createReference(Runnable.class), intersectionType.getBounds().stream().collect(Collectors.toList()).get(0));
assertEquals(aPozole.getFactory().Type().createReference(Serializable.class), intersectionType.getBounds().stream().collect(Collectors.toList()).get(1));
canBeBuilt(target, 8, true);
}
use of spoon.reflect.declaration.CtTypeParameter in project spoon by INRIA.
the class TypeTest method testIntersectionTypeOnTopLevelType.
@Test
public void testIntersectionTypeOnTopLevelType() throws Exception {
final CtType<Mole> aMole = buildClass(Mole.class);
assertEquals(1, aMole.getFormalCtTypeParameters().size());
// New type parameter declaration.
final CtTypeParameter typeParameter = aMole.getFormalCtTypeParameters().get(0);
assertIntersectionTypeForMole(aMole, typeParameter.getSuperclass());
}
Aggregations