use of spoon.SpoonAPI in project spoon by INRIA.
the class SpoonArchitectureEnforcerTest method statelessFactory.
@Test
public void statelessFactory() throws Exception {
// the factories must be stateless
SpoonAPI spoon = new Launcher();
spoon.addInputResource("src/main/java/spoon/reflect/factory");
spoon.buildModel();
for (CtType t : spoon.getFactory().Package().getRootPackage().getElements(new AbstractFilter<CtType>() {
@Override
public boolean matches(CtType element) {
return super.matches(element) && element.getSimpleName().contains("Factory");
}
})) {
for (Object o : t.getFields()) {
CtField f = (CtField) o;
if (f.getSimpleName().equals("factory")) {
continue;
}
if (f.hasModifier(ModifierKind.FINAL) || f.hasModifier(ModifierKind.TRANSIENT)) {
continue;
}
fail("architectural constraint: a factory must be stateless");
}
}
}
use of spoon.SpoonAPI in project spoon by INRIA.
the class SpoonArchitectureEnforcerTest method metamodelPackageRule.
@Test
public void metamodelPackageRule() throws Exception {
// all implementations of the metamodel classes have a corresponding interface in the appropriate package
List<String> exceptions = Collections.singletonList("CtWildcardStaticTypeMemberReferenceImpl");
SpoonAPI implementations = new Launcher();
implementations.addInputResource("src/main/java/spoon/support/reflect/declaration");
implementations.addInputResource("src/main/java/spoon/support/reflect/code");
implementations.addInputResource("src/main/java/spoon/support/reflect/reference");
implementations.buildModel();
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.addInputResource("src/main/java/spoon/support/DefaultCoreFactory.java");
interfaces.buildModel();
for (CtType<?> implType : implementations.getModel().getAllTypes()) {
if (!exceptions.contains(implType.getSimpleName())) {
String impl = implType.getQualifiedName().replace(".support", "").replace("Impl", "");
CtType interfaceType = interfaces.getFactory().Type().get(impl);
// the implementation is a subtype of the superinterface
assertTrue(implType.getReference().isSubtypeOf(interfaceType.getReference()));
}
}
}
use of spoon.SpoonAPI in project spoon by INRIA.
the class SpoonMetaModel method getAllInstantiableMetamodelInterfaces.
public List<CtType<? extends CtElement>> getAllInstantiableMetamodelInterfaces() {
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();
SpoonAPI implementations = new Launcher();
implementations.addInputResource("src/main/java/spoon/support/reflect/declaration");
implementations.addInputResource("src/main/java/spoon/support/reflect/code");
implementations.addInputResource("src/main/java/spoon/support/reflect/reference");
implementations.buildModel();
List<CtType<? extends CtElement>> result = new ArrayList<>();
for (CtType<?> itf : interfaces.getModel().getAllTypes()) {
String impl = itf.getQualifiedName().replace("spoon.reflect", "spoon.support.reflect") + "Impl";
CtType implClass = implementations.getFactory().Type().get(impl);
if (implClass != null && !implClass.hasModifier(ModifierKind.ABSTRACT)) {
result.add((CtType<? extends CtElement>) itf);
}
}
return result;
}
use of spoon.SpoonAPI in project spoon by INRIA.
the class InvocationTest method testTypeOfStaticInvocation.
@Test
public void testTypeOfStaticInvocation() throws Exception {
SpoonAPI launcher = new Launcher();
launcher.run(new String[] { "-i", "./src/test/java/spoon/test/invocations/testclasses/", "-o", "./target/spooned/" });
Factory factory = launcher.getFactory();
CtClass<?> aClass = factory.Class().get(Foo.class);
final List<CtInvocation<?>> elements = aClass.getElements(new AbstractFilter<CtInvocation<?>>(CtInvocation.class) {
@Override
public boolean matches(CtInvocation<?> element) {
return element.getTarget() != null;
}
});
assertEquals(2, elements.size());
assertTrue(elements.get(0).getTarget() instanceof CtTypeAccess);
assertTrue(elements.get(1).getTarget() instanceof CtTypeAccess);
}
use of spoon.SpoonAPI in project spoon by INRIA.
the class VisibilityTest method testFullyQualifiedNameOfTypeReferenceWithGeneric.
@Test
public void testFullyQualifiedNameOfTypeReferenceWithGeneric() throws Exception {
// contract: Generics are written when there are specified in the return type of a method.
final String target = "./target/spooned/spoon/test/visibility_generics/testclasses/";
final SpoonAPI launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/visibility/testclasses/A.java");
launcher.addInputResource("./src/test/java/spoon/test/visibility/testclasses/A2.java");
launcher.addInputResource("./src/test/java/spoon/test/visibility/testclasses/Foo.java");
launcher.setSourceOutputDirectory(target);
launcher.run();
final CtClass<A> aClass = launcher.getFactory().Class().get(A.class);
CtType<?> nestedB = aClass.getNestedType("B");
List<CtFieldAccess> elements = nestedB.getElements(new TypeFilter<>(CtFieldAccess.class));
assertEquals(1, elements.size());
assertEquals("(spoon.test.visibility.testclasses.A.B.i)", elements.get(0).toString());
CtMethod<?> instanceOf = aClass.getMethodsByName("instanceOf").get(0);
List<CtBinaryOperator> elements1 = instanceOf.getElements(new TypeFilter<>(CtBinaryOperator.class));
assertEquals(1, elements1.size());
assertEquals("spoon.test.visibility.testclasses.A.B", elements1.get(0).getRightHandOperand().toString());
CtMethod<?> returnType = aClass.getMethodsByName("returnType").get(0);
assertEquals("spoon.test.visibility.testclasses.A<T>.C<T>", returnType.getType().toString());
final CtClass<A2> secondClass = launcher.getFactory().Class().get(A2.class);
nestedB = secondClass.getNestedType("B");
elements = nestedB.getElements(new TypeFilter<>(CtFieldAccess.class));
assertEquals(1, elements.size());
assertEquals("(spoon.test.visibility.testclasses.A2.B.i)", elements.get(0).toString());
instanceOf = secondClass.getMethodsByName("instanceOf").get(0);
elements1 = instanceOf.getElements(new TypeFilter<>(CtBinaryOperator.class));
assertEquals(1, elements1.size());
assertEquals("spoon.test.visibility.testclasses.A2.B", elements1.get(0).getRightHandOperand().toString());
returnType = secondClass.getMethodsByName("returnType").get(0);
assertEquals("spoon.test.visibility.testclasses.A2.C<java.lang.String>", returnType.getType().toString());
returnType = secondClass.getMethodsByName("returnType2").get(0);
assertEquals("spoon.test.visibility.testclasses.Foo<java.lang.String>.Bar<java.lang.String>", returnType.getType().toString());
canBeBuilt(target, 8);
}
Aggregations