use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class ParentTest method testParentOfGenericInTypeReference.
@Test
public void testParentOfGenericInTypeReference() throws Exception {
// contract: parent of a generic in a type reference is the type reference.
final Factory factory = build(Tacos.class);
final CtTypeReference referenceWithGeneric = Query.getElements(factory, new ReferenceTypeFilter<CtTypeReference>(CtTypeReference.class) {
@Override
public boolean matches(CtTypeReference reference) {
return reference.getActualTypeArguments().size() > 0 && super.matches(reference);
}
}).get(0);
final CtTypeReference<?> generic = referenceWithGeneric.getActualTypeArguments().get(0);
assertNotNull(generic.getParent());
assertEquals(referenceWithGeneric, generic.getParent());
}
use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class ParentTest method testParentSetInSetter.
@Test
// too fragile because of conventions
@Ignore
public void testParentSetInSetter() throws Exception {
// contract: Check that all setters protect their parameter.
final Launcher launcher = new Launcher();
final Factory factory = launcher.getFactory();
launcher.setArgs(new String[] { "--output-type", "nooutput" });
launcher.getEnvironment().setNoClasspath(true);
// interfaces.
launcher.addInputResource("./src/main/java/spoon/reflect/code");
launcher.addInputResource("./src/main/java/spoon/reflect/declaration");
launcher.addInputResource("./src/main/java/spoon/reflect/reference");
// implementations.
launcher.addInputResource("./src/main/java/spoon/support/reflect/code");
launcher.addInputResource("./src/main/java/spoon/support/reflect/declaration");
launcher.addInputResource("./src/main/java/spoon/support/reflect/reference");
// Utils.
launcher.addInputResource("./src/test/java/spoon/reflect/ast/");
launcher.buildModel();
// Asserts.
new IntercessionScanner(launcher.getFactory()) {
@Override
protected boolean isToBeProcessed(CtMethod<?> candidate) {
return (//
candidate.getSimpleName().startsWith("set") || //
candidate.getSimpleName().startsWith("add")) && //
candidate.hasModifier(ModifierKind.PUBLIC) && //
takeSetterForCtElement(candidate) && //
avoidInterfaces(candidate) && avoidThrowUnsupportedOperationException(candidate);
}
@Override
public void process(CtMethod<?> element) {
if (element.getAnnotation(UnsettableProperty.class) != null) {
// we don't check the contracts for unsettable setters
return;
}
if (element.getSimpleName().startsWith("add")) {
checkAddStrategy(element);
} else {
checkSetStrategy(element);
}
}
private void checkAddStrategy(CtMethod<?> element) {
final CtStatement statement = element.getBody().getStatement(0);
if (!(statement instanceof CtIf)) {
fail("First statement should be an if to check the parameter of the setter." + element.getSignature() + " declared in " + element.getDeclaringType().getQualifiedName());
}
if (!createCheckNull(element.getParameters().get(0)).equals(((CtIf) statement).getCondition())) {
fail("Condition should test if the parameter is null. The condition was " + ((CtIf) statement).getCondition() + "in " + element.getSignature() + " declared in " + element.getDeclaringType().getQualifiedName());
}
}
private void checkSetStrategy(CtMethod<?> element) {
final CtTypeReference<?> type = element.getParameters().get(0).getType();
if (!COLLECTIONS.contains(type) && !(type instanceof CtArrayTypeReference)) {
CtInvocation<?> setParent = searchSetParent(element.getBody());
if (setParent == null) {
return;
}
try {
if (setParent.getParent(CtIf.class) == null) {
fail("Missing condition in " + element.getSignature() + " declared in the class " + element.getDeclaringType().getQualifiedName());
}
} catch (ParentNotInitializedException e) {
fail("Missing parent condition in " + element.getSignature() + " declared in the class " + element.getDeclaringType().getQualifiedName());
}
}
}
/**
* Creates <code>parameter == null</code>.
*
* @param ctParameter <code>parameter</code>
*/
private CtBinaryOperator<Boolean> createCheckNull(CtParameter<?> ctParameter) {
final CtLiteral nullLiteral = factory.Code().createLiteral(null);
nullLiteral.setType(factory.Type().NULL_TYPE.clone());
final CtBinaryOperator<Boolean> operator = //
factory.Code().createBinaryOperator(//
factory.Code().createVariableRead(ctParameter.getReference(), true), nullLiteral, BinaryOperatorKind.EQ);
operator.setType(factory.Type().BOOLEAN_PRIMITIVE);
return operator;
}
private CtInvocation<?> searchSetParent(CtBlock<?> body) {
final List<CtInvocation<?>> ctInvocations = body.getElements(new TypeFilter<CtInvocation<?>>(CtInvocation.class) {
@Override
public boolean matches(CtInvocation<?> element) {
return "setParent".equals(element.getExecutable().getSimpleName()) && super.matches(element);
}
});
return ctInvocations.size() > 0 ? ctInvocations.get(0) : null;
}
}.scan(launcher.getModel().getRootPackage());
}
use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class ParentTest method testParentOfCtPackageReference.
@Test
public void testParentOfCtPackageReference() throws Exception {
// contract: a parent at a top level must be the root package and in the code, the element which call getParent().
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput" });
launcher.getEnvironment().setNoClasspath(true);
launcher.addInputResource("./src/test/resources/reference-package");
launcher.run();
final CtType<Object> panini = launcher.getFactory().Type().get("Panini");
CtElement topLevelParent = panini.getPackage().getParent();
assertNotNull(topLevelParent);
assertEquals(CtPackage.TOP_LEVEL_PACKAGE_NAME, panini.getPackage().getSimpleName());
CtPackage pack1 = factory.Package().getRootPackage();
// the factory are not the same
assertNotEquals(factory, launcher.getFactory());
// so the root packages are not deeply equals
assertNotEquals(pack1, topLevelParent);
final CtTypeReference<?> burritos = panini.getElements(new ReferenceTypeFilter<CtTypeReference<?>>(CtTypeReference.class) {
@Override
public boolean matches(CtTypeReference<?> reference) {
return "Burritos".equals(reference.getSimpleName()) && super.matches(reference);
}
}).get(0);
assertNotNull(burritos.getPackage().getParent());
assertEquals("com.awesome", burritos.getPackage().getSimpleName());
assertEquals(burritos, burritos.getPackage().getParent());
}
use of spoon.reflect.reference.CtTypeReference 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.CtTypeReference 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);
}
Aggregations