Search in sources :

Example 6 with SpoonAPI

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");
        }
    }
}
Also used : CtType(spoon.reflect.declaration.CtType) CtField(spoon.reflect.declaration.CtField) Launcher(spoon.Launcher) SpoonAPI(spoon.SpoonAPI) Test(org.junit.Test)

Example 7 with SpoonAPI

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()));
        }
    }
}
Also used : CtType(spoon.reflect.declaration.CtType) Launcher(spoon.Launcher) SpoonAPI(spoon.SpoonAPI) Test(org.junit.Test)

Example 8 with SpoonAPI

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;
}
Also used : CtType(spoon.reflect.declaration.CtType) CtElement(spoon.reflect.declaration.CtElement) ArrayList(java.util.ArrayList) Launcher(spoon.Launcher) SpoonAPI(spoon.SpoonAPI)

Example 9 with SpoonAPI

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);
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CtTypeAccess(spoon.reflect.code.CtTypeAccess) SpoonAPI(spoon.SpoonAPI) Test(org.junit.Test)

Example 10 with SpoonAPI

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);
}
Also used : A(spoon.test.visibility.testclasses.A) CtFieldAccess(spoon.reflect.code.CtFieldAccess) CtBinaryOperator(spoon.reflect.code.CtBinaryOperator) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) A2(spoon.test.visibility.testclasses.A2) Launcher(spoon.Launcher) SpoonAPI(spoon.SpoonAPI) Test(org.junit.Test)

Aggregations

Launcher (spoon.Launcher)21 SpoonAPI (spoon.SpoonAPI)21 Test (org.junit.Test)19 CtType (spoon.reflect.declaration.CtType)9 ArrayList (java.util.ArrayList)8 CtField (spoon.reflect.declaration.CtField)6 CtMethod (spoon.reflect.declaration.CtMethod)6 Factory (spoon.reflect.factory.Factory)6 CtClass (spoon.reflect.declaration.CtClass)5 CtTypeReference (spoon.reflect.reference.CtTypeReference)5 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)5 File (java.io.File)4 Collections (java.util.Collections)4 List (java.util.List)4 Set (java.util.Set)4 Assert.assertTrue (org.junit.Assert.assertTrue)4 CtElement (spoon.reflect.declaration.CtElement)4 ModifierKind (spoon.reflect.declaration.ModifierKind)4 Arrays (java.util.Arrays)3 Collectors (java.util.stream.Collectors)3