use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class SpoonArchitectureEnforcerTest method testGoodTestClassNames.
@Test
public void testGoodTestClassNames() throws Exception {
// contract: to be run by Maven surefire, all test classes must be called Test* or *Test
// reference: "By default, the Surefire Plugin will automatically include all test classes with the following wildcard patterns:"
// "**/Test*.java" and "**/*Test.java"
// http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html
SpoonAPI spoon = new Launcher();
spoon.addInputResource("src/test/java/");
spoon.buildModel();
for (CtMethod<?> meth : spoon.getModel().getElements(new TypeFilter<CtMethod>(CtMethod.class) {
@Override
public boolean matches(CtMethod element) {
return super.matches(element) && element.getAnnotation(Test.class) != null;
}
})) {
assertTrue("naming contract violated for " + meth.getParent(CtClass.class).getSimpleName(), meth.getParent(CtClass.class).getSimpleName().startsWith("Test") || meth.getParent(CtClass.class).getSimpleName().endsWith("Test"));
}
// contract: the Spoon test suite does not depend on Junit 3 classes and methods
// otherwise, intellij automatically selects the junit3 runner, finds nothing
// and crashes with a dirty exception
assertEquals(0, spoon.getModel().getElements(new TypeFilter<CtTypeReference>(CtTypeReference.class) {
@Override
public boolean matches(CtTypeReference element) {
CtMethod parent = element.getParent(CtMethod.class);
return "junit.framework.TestCase".equals(element.getQualifiedName());
}
}).size());
}
use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class ReplaceScanner method createInvocation.
private <T> CtInvocation<?> createInvocation(Factory factory, CtMethod<T> candidate, List<CtExpression<?>> invArgs, CtInvocation getter, Class getterTypeClass) {
CtInvocation<?> invocation;
Type type;
if (getterTypeClass.equals(Collection.class) || getterTypeClass.equals(List.class)) {
invocation = factory.Code().createInvocation(null, this.list, invArgs);
type = Type.LIST;
} else if (getterTypeClass.equals(Map.class)) {
invocation = factory.Code().createInvocation(null, this.map, invArgs);
type = Type.MAP;
} else if (getterTypeClass.equals(Set.class)) {
invocation = factory.Code().createInvocation(null, this.set, invArgs);
type = Type.SET;
} else {
invocation = factory.Code().createInvocation(null, this.element, invArgs);
type = Type.ELEMENT;
}
// Listener
final String name = getter.getExecutable().getSimpleName().substring(3);
final String listenerName = getter.getExecutable().getDeclaringType().getSimpleName() + name + "ReplaceListener";
CtClass listener;
if (listeners.containsKey(listenerName)) {
listener = listeners.get(listenerName);
} else {
final CtTypeReference getterType = getGetterType(factory, getter);
listener = createListenerClass(factory, listenerName, getterType, type);
final CtMethod setter = getSetter(name, getter.getTarget().getType().getDeclaration());
final CtField field = updateField(listener, setter.getDeclaringType().getReference());
updateConstructor(listener, setter.getDeclaringType().getReference());
updateSetter(factory, (CtMethod<?>) listener.getMethodsByName("set").get(0), getterType, field, setter);
// Add auto-generated comment.
final CtComment comment = factory.Core().createComment();
comment.setCommentType(CtComment.CommentType.INLINE);
comment.setContent("auto-generated, see " + ReplacementVisitorGenerator.class.getName());
listener.addComment(comment);
listeners.put(listenerName, listener);
}
invocation.addArgument(getConstructorCall(listener, factory.Code().createVariableRead(candidate.getParameters().get(0).getReference(), false)));
return invocation;
}
use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class CloneVisitorGenerator method createCloneBuilder.
private CtClass<Object> createCloneBuilder() {
final CtPackage aPackage = getFactory().Package().getOrCreate(TARGET_CLONE_PACKAGE);
final CtClass<Object> target = getFactory().Class().get(GENERATING_BUILDER_CLONE);
target.setSimpleName(TARGET_BUILDER_CLONE_TYPE);
target.addModifier(ModifierKind.PUBLIC);
aPackage.addType(target);
final List<CtTypeReference> references = target.getElements(new TypeFilter<CtTypeReference>(CtTypeReference.class) {
@Override
public boolean matches(CtTypeReference reference) {
return GENERATING_BUILDER_CLONE.equals(reference.getQualifiedName());
}
});
for (CtTypeReference reference : references) {
reference.setSimpleName(TARGET_BUILDER_CLONE_TYPE);
reference.setPackage(aPackage.getReference());
}
return target;
}
use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class IntercessionTest method testResetCollectionInSetters.
@Test
// interesting but too fragile with conventions
@Ignore
public void testResetCollectionInSetters() throws Exception {
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput" });
final Factory factory = launcher.getFactory();
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");
launcher.buildModel();
new IntercessionScanner(factory) {
@Override
protected boolean isToBeProcessed(CtMethod<?> candidate) {
return //
candidate.getSimpleName().startsWith("set") && //
candidate.hasModifier(ModifierKind.PUBLIC) && //
takeSetterCollection(candidate) && //
avoidInterfaces(candidate) && // && avoidSpecificMethods(candidate) //
avoidThrowUnsupportedOperationException(candidate);
}
private boolean takeSetterCollection(CtMethod<?> candidate) {
final CtTypeReference<?> type = candidate.getParameters().get(0).getType();
final List<CtTypeReference<?>> actualTypeArguments = type.getActualTypeArguments();
return COLLECTIONS.contains(type) && actualTypeArguments.size() == 1 && actualTypeArguments.get(0).isSubtypeOf(CTELEMENT_REFERENCE);
}
@Override
protected void process(CtMethod<?> element) {
if (element.getAnnotation(UnsettableProperty.class) != null) {
// we don't check the contracts for unsettable setters
return;
}
final CtStatement statement = element.getBody().getStatement(0);
if (!(statement instanceof CtIf)) {
fail(log(element, "First statement should be an if to check the parameter of the setter"));
}
final CtIf anIf = (CtIf) statement;
if (!createCheckNull(element.getParameters().get(0)).equals(anIf.getCondition())) {
fail(log(element, "Condition should test if the parameter is null.\nThe condition was " + anIf.getCondition()));
}
if (!(anIf.getThenStatement() instanceof CtBlock)) {
fail(log(element, "Should have a block in the if condition to have the initialization and the return."));
}
if (element.getParameters().get(0).getType().equals(SET_REFERENCE)) {
if (!hasCallEmptyInv(anIf.getThenStatement(), SET_REFERENCE)) {
fail(log(element, "Should initilize the list with CtElementImpl#emptySet()."));
}
} else {
if (!hasCallEmptyInv(anIf.getThenStatement(), LIST_REFERENCE)) {
fail(log(element, "Should initilize the list with CtElementImpl#emptyList()."));
}
}
}
private boolean hasCallEmptyInv(CtBlock thenStatement, CtTypeReference<? extends Collection> collectionReference) {
if (!(thenStatement.getStatement(0) instanceof CtAssignment)) {
return false;
}
final CtExpression assignment = ((CtAssignment) thenStatement.getStatement(0)).getAssignment();
if (!(assignment instanceof CtInvocation)) {
return false;
}
final CtInvocation inv = (CtInvocation) assignment;
if (collectionReference.equals(SET_REFERENCE)) {
if (!inv.getExecutable().getSimpleName().equals("emptySet")) {
return false;
}
} else if (collectionReference.equals(LIST_REFERENCE)) {
if (!inv.getExecutable().getSimpleName().equals("emptyList")) {
return false;
}
}
return true;
}
/**
* Creates <code>list == null && list.isEmpty()</code>.
*
* @param ctParameter <code>list</code>
*/
private CtBinaryOperator<Boolean> createCheckNull(CtParameter<?> ctParameter) {
final CtVariableAccess<?> variableRead = factory.Code().createVariableRead(ctParameter.getReference(), true);
final CtLiteral nullLiteral = factory.Code().createLiteral(null);
nullLiteral.setType(factory.Type().nullType());
final CtBinaryOperator<Boolean> checkNull = factory.Code().createBinaryOperator(variableRead, nullLiteral, BinaryOperatorKind.EQ);
checkNull.setType(factory.Type().BOOLEAN_PRIMITIVE);
final CtMethod<Boolean> isEmptyMethod = ctParameter.getType().getTypeDeclaration().getMethod(factory.Type().booleanPrimitiveType(), "isEmpty");
final CtInvocation<Boolean> isEmpty = factory.Code().createInvocation(variableRead, isEmptyMethod.getReference());
final CtBinaryOperator<Boolean> condition = factory.Code().createBinaryOperator(checkNull, isEmpty, BinaryOperatorKind.OR);
return condition.setType(factory.Type().booleanPrimitiveType());
}
private String log(CtMethod<?> element, String message) {
return message + "\nin " + element.getSignature() + "\ndeclared in " + element.getDeclaringType().getQualifiedName();
}
}.scan(factory.getModel().getUnnamedModule());
}
use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class ImportBuilderTest method testWithSimpleImport.
@Test
public void testWithSimpleImport() {
// contract: when the source has one import, the same import is created as a reference in auto-import mode
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/imports/testclasses/ClassWithInvocation.java");
spoon.getEnvironment().setAutoImports(true);
spoon.buildModel();
CtClass classA = spoon.getFactory().Class().get(ClassWithInvocation.class);
CompilationUnit unitA = spoon.getFactory().CompilationUnit().getMap().get(classA.getPosition().getFile().getPath());
Collection<CtImport> imports = unitA.getImports();
assertEquals(1, imports.size());
CtImport ref = imports.iterator().next();
assertEquals("import spoon.test.annotation.testclasses.GlobalAnnotation;\n", ref.toString());
assertTrue(ref.getReference() instanceof CtTypeReference);
CtTypeReference refType = (CtTypeReference) ref.getReference();
assertEquals("spoon.test.annotation.testclasses.GlobalAnnotation", refType.getQualifiedName());
}
Aggregations