use of spoon.SpoonAPI in project spoon by INRIA.
the class MetamodelTest method testRoleOnField.
@Test
public void testRoleOnField() {
// contract: all non-final fields must be annotated with {@link spoon.reflect.annotations.MetamodelPropertyField}
SpoonAPI implementations = new Launcher();
implementations.addInputResource("src/main/java/spoon/support/reflect");
implementations.buildModel();
Factory factory = implementations.getFactory();
CtTypeReference metamodelPropertyField = factory.Type().get(MetamodelPropertyField.class).getReference();
final List<String> result = new ArrayList();
List<CtField> fieldWithoutAnnotation = (List<CtField>) implementations.getModel().getElements(new TypeFilter<CtField>(CtField.class) {
@Override
public boolean matches(CtField candidate) {
if (candidate.hasModifier(ModifierKind.FINAL) || candidate.hasModifier(ModifierKind.STATIC) || candidate.hasModifier(ModifierKind.TRANSIENT)) {
return false;
}
if (// not a role
"parent".equals(candidate.getSimpleName()) || "metadata".equals(candidate.getSimpleName()) || // cache field
"valueOfMethod".equals(candidate.getSimpleName())) {
return false;
}
CtClass parent = candidate.getParent(CtClass.class);
return parent != null && (parent.isSubtypeOf(candidate.getFactory().createCtTypeReference(CtReference.class)) || parent.isSubtypeOf(candidate.getFactory().createCtTypeReference(CtElement.class)));
}
}).stream().map(x -> {
result.add(x.toString());
return x;
}).filter(f -> f.getAnnotation(metamodelPropertyField) == null).collect(Collectors.toList());
assertTrue(result.contains("@spoon.reflect.annotations.MetamodelPropertyField(role = spoon.reflect.path.CtRole.IS_SHADOW)\nboolean isShadow;"));
assertTrue(result.contains("@spoon.reflect.annotations.MetamodelPropertyField(role = spoon.reflect.path.CtRole.TYPE)\nspoon.reflect.reference.CtTypeReference<T> type;"));
assertTrue(result.size() > 100);
Assert.assertEquals(Collections.emptyList(), fieldWithoutAnnotation);
final CtTypeReference propertySetter = factory.Type().get(PropertySetter.class).getReference();
final CtTypeReference propertyGetter = factory.Type().get(PropertyGetter.class).getReference();
List<CtField> fields = factory.getModel().getElements(new AnnotationFilter<CtField>(MetamodelPropertyField.class));
for (CtField field : fields) {
CtClass parent = field.getParent(CtClass.class);
CtExpression roleExpression = field.getAnnotation(metamodelPropertyField).getValue("role");
List<String> roles = new ArrayList<>();
if (roleExpression instanceof CtFieldRead) {
roles.add(((CtFieldRead) roleExpression).getVariable().getSimpleName());
} else if (roleExpression instanceof CtNewArray) {
List<CtFieldRead> elements = ((CtNewArray) roleExpression).getElements();
for (int i = 0; i < elements.size(); i++) {
CtFieldRead ctFieldRead = elements.get(i);
roles.add(ctFieldRead.getVariable().getSimpleName());
}
}
CtQuery superQuery = parent.map(new SuperInheritanceHierarchyFunction());
List<CtMethod> methods = superQuery.map((CtType type) -> type.getMethodsAnnotatedWith(propertyGetter, propertySetter)).list();
boolean setterFound = false;
boolean getterFound = false;
for (CtMethod method : methods) {
CtAnnotation getterAnnotation = method.getAnnotation(propertyGetter);
CtAnnotation setterAnnotation = method.getAnnotation(propertySetter);
if (getterAnnotation != null) {
getterFound |= roles.contains(((CtFieldRead) getterAnnotation.getValue("role")).getVariable().getSimpleName());
}
if (setterAnnotation != null) {
setterFound |= roles.contains(((CtFieldRead) setterAnnotation.getValue("role")).getVariable().getSimpleName());
}
}
assertTrue(roles + " must have a getter in " + parent.getQualifiedName(), getterFound);
assertTrue(roles + " must have a setter in " + parent.getQualifiedName(), setterFound);
}
}
use of spoon.SpoonAPI in project spoon by INRIA.
the class MetamodelTest method testGetterSetterFroRole.
@Test
public void testGetterSetterFroRole() {
// contract: all roles in spoon metamodel must at least have a setter and a getter
SpoonAPI interfaces = new Launcher();
interfaces.addInputResource("src/main/java/spoon/reflect/declaration");
interfaces.addInputResource("src/main/java/spoon/reflect/code");
interfaces.addInputResource("src/main/java/spoon/reflect/reference");
interfaces.buildModel();
Factory factory = interfaces.getFactory();
CtTypeReference propertyGetter = factory.Type().get(PropertyGetter.class).getReference();
CtTypeReference propertySetter = factory.Type().get(PropertySetter.class).getReference();
Set<String> expectedRoles = Arrays.stream(CtRole.values()).map(r -> r.name()).collect(Collectors.toSet());
List<CtMethod<?>> getters = interfaces.getModel().getElements(new AnnotationFilter<CtMethod<?>>(PropertyGetter.class));
Set<String> getterRoles = getters.stream().map(g -> ((CtFieldRead) g.getAnnotation(propertyGetter).getValue("role")).getVariable().getSimpleName()).collect(Collectors.toSet());
Set<CtMethod<?>> isNotGetter = getters.stream().filter(m -> !(m.getSimpleName().startsWith("get") || m.getSimpleName().startsWith("is"))).collect(Collectors.toSet());
List<CtMethod<?>> setters = interfaces.getModel().getElements(new AnnotationFilter<CtMethod<?>>(PropertySetter.class));
Set<String> setterRoles = setters.stream().map(g -> ((CtFieldRead) g.getAnnotation(propertySetter).getValue("role")).getVariable().getSimpleName()).collect(Collectors.toSet());
Set<CtMethod<?>> isNotSetter = setters.stream().filter(m -> !(m.getSimpleName().startsWith("set") || m.getSimpleName().startsWith("add") || m.getSimpleName().startsWith("insert") || m.getSimpleName().startsWith("remove"))).collect(Collectors.toSet());
Assert.assertEquals(expectedRoles, getterRoles);
Assert.assertEquals(expectedRoles, setterRoles);
Assert.assertEquals(Collections.EMPTY_SET, isNotGetter);
Assert.assertEquals(Collections.EMPTY_SET, isNotSetter);
}
use of spoon.SpoonAPI in project spoon by INRIA.
the class SpoonArchitectureEnforcerTest method testSpecPackage.
@Test
public void testSpecPackage() throws Exception {
// contract: when a pull-request introduces a new package, it is made explicit during code review
// when a pull-request introduces a new package, this test fails and the author has to explicitly declare the new package here
Set<String> officialPackages = new TreeSet<>();
officialPackages.add("spoon.compiler.builder");
officialPackages.add("spoon.compiler");
officialPackages.add("spoon.experimental.modelobs.action");
officialPackages.add("spoon.experimental.modelobs.context");
officialPackages.add("spoon.experimental.modelobs");
officialPackages.add("spoon.experimental");
officialPackages.add("spoon.legacy");
officialPackages.add("spoon.processing");
officialPackages.add("spoon.refactoring");
officialPackages.add("spoon.reflect.annotations");
officialPackages.add("spoon.reflect.code");
officialPackages.add("spoon.reflect.cu.position");
officialPackages.add("spoon.reflect.cu");
officialPackages.add("spoon.reflect.declaration");
officialPackages.add("spoon.reflect.eval");
officialPackages.add("spoon.reflect.factory");
officialPackages.add("spoon.reflect.path.impl");
officialPackages.add("spoon.reflect.path");
officialPackages.add("spoon.reflect.reference");
officialPackages.add("spoon.reflect.visitor.chain");
officialPackages.add("spoon.reflect.visitor.filter");
officialPackages.add("spoon.reflect.visitor.printer");
officialPackages.add("spoon.reflect.visitor");
officialPackages.add("spoon.reflect");
officialPackages.add("spoon.support.comparator");
officialPackages.add("spoon.support.compiler.jdt");
officialPackages.add("spoon.support.compiler");
officialPackages.add("spoon.support.gui");
officialPackages.add("spoon.support.reflect.code");
officialPackages.add("spoon.support.reflect.cu.position");
officialPackages.add("spoon.support.reflect.cu");
officialPackages.add("spoon.support.reflect.declaration");
officialPackages.add("spoon.support.reflect.eval");
officialPackages.add("spoon.reflect.meta");
officialPackages.add("spoon.reflect.meta.impl");
officialPackages.add("spoon.support.reflect.reference");
officialPackages.add("spoon.support.reflect");
officialPackages.add("spoon.support.template");
officialPackages.add("spoon.support.util");
officialPackages.add("spoon.support.visitor.clone");
officialPackages.add("spoon.support.visitor.equals");
officialPackages.add("spoon.support.visitor.java.internal");
officialPackages.add("spoon.support.visitor.java.reflect");
officialPackages.add("spoon.support.visitor.java");
officialPackages.add("spoon.support.visitor.replace");
officialPackages.add("spoon.support.visitor");
officialPackages.add("spoon.support");
officialPackages.add("spoon.template");
officialPackages.add("spoon.testing.utils");
officialPackages.add("spoon.testing");
officialPackages.add("spoon");
// root package
officialPackages.add("");
SpoonAPI spoon = new Launcher();
spoon.addInputResource("src/main/java/");
spoon.buildModel();
final Set<String> currentPackages = new TreeSet<>();
spoon.getModel().processWith(new AbstractProcessor<CtPackage>() {
@Override
public void process(CtPackage element) {
currentPackages.add(element.getQualifiedName());
}
});
assertSetEquals("you have created a new package or removed an existing one, please declare it explicitly in SpoonArchitectureEnforcerTest#testSpecPackage", officialPackages, currentPackages);
}
use of spoon.SpoonAPI in project spoon by INRIA.
the class ConstructorTest method setUp.
@Before
public void setUp() throws Exception {
SpoonAPI launcher = new Launcher();
launcher.run(new String[] { "-i", "./src/test/java/spoon/test/constructor/testclasses/", "-o", "./target/spooned/" });
factory = launcher.getFactory();
aClass = factory.Class().get(Tacos.class);
}
use of spoon.SpoonAPI in project spoon by INRIA.
the class VisibilityTest method testName.
@Test
public void testName() throws Exception {
final SpoonAPI launcher = new Launcher();
launcher.run(new String[] { "-i", "./src/test/java/spoon/test/visibility/testclasses/Tacos.java", "-o", "./target/spooned/visibility" });
final List<CtFieldReference<?>> references = Query.getElements(launcher.getFactory(), new AbstractReferenceFilter<CtFieldReference<?>>(CtFieldReference.class) {
@Override
public boolean matches(CtFieldReference<?> reference) {
return "x".equals(reference.getSimpleName());
}
});
assertEquals(1, references.size());
final CtFieldReference<?> field = references.get(0);
assertNotNull(field.getDeclaration());
final CtClass<?> tacos = launcher.getFactory().Class().get("spoon.test.visibility.testclasses.Tacos");
assertEquals(tacos, field.getDeclaringType().getDeclaration());
assertEquals(tacos.getFields().get(0), field.getDeclaration());
}
Aggregations