Search in sources :

Example 46 with CtType

use of spoon.reflect.declaration.CtType in project spoon by INRIA.

the class APITest method testOutputDestinationHandler.

@Test
public void testOutputDestinationHandler() throws IOException {
    // contract: files are created in the directory determined by the output destination handler
    final File outputDest = Files.createTempDirectory("spoon").toFile();
    final OutputDestinationHandler outputDestinationHandler = new OutputDestinationHandler() {

        @Override
        public Path getOutputPath(CtModule module, CtPackage pack, CtType type) {
            String path = "";
            if (module != null) {
                path += module.getSimpleName() + "_";
            }
            if (pack != null) {
                path += pack.getQualifiedName() + "_";
            }
            if (type != null) {
                path += type.getSimpleName() + ".java";
            }
            return new File(outputDest, path).toPath();
        }

        @Override
        public File getDefaultOutputDirectory() {
            return outputDest;
        }
    };
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/api/testclasses/Bar.java");
    launcher.getEnvironment().setOutputDestinationHandler(outputDestinationHandler);
    launcher.run();
    File generatedFile = new File(outputDest, "unnamed module_spoon.test.api.testclasses_Bar.java");
    assertTrue(generatedFile.exists());
}
Also used : CtType(spoon.reflect.declaration.CtType) Launcher(spoon.Launcher) CtPackage(spoon.reflect.declaration.CtPackage) File(java.io.File) OutputDestinationHandler(spoon.support.OutputDestinationHandler) CtModule(spoon.reflect.declaration.CtModule) Test(org.junit.Test)

Example 47 with CtType

use of spoon.reflect.declaration.CtType in project spoon by INRIA.

the class APITest method testOutputDestinationHandlerWithCUFactory.

@Test
public void testOutputDestinationHandlerWithCUFactory() throws IOException {
    // contract: when creating a new CU, its destination is consistent with output destination handler
    final File outputDest = Files.createTempDirectory("spoon").toFile();
    final OutputDestinationHandler outputDestinationHandler = new OutputDestinationHandler() {

        @Override
        public Path getOutputPath(CtModule module, CtPackage pack, CtType type) {
            String path = "";
            if (module != null) {
                path += module.getSimpleName() + "_";
                if (pack == null && type == null) {
                    path += "module-info.java";
                }
            }
            if (pack != null) {
                path += pack.getQualifiedName() + "_";
                if (type == null) {
                    path += "package-info.java";
                }
            }
            if (type != null) {
                path += type.getSimpleName() + ".java";
            }
            return new File(outputDest, path).toPath();
        }

        @Override
        public File getDefaultOutputDirectory() {
            return outputDest;
        }
    };
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setComplianceLevel(9);
    launcher.getEnvironment().setOutputDestinationHandler(outputDestinationHandler);
    Factory factory = launcher.getFactory();
    CtModule module = factory.Module().getOrCreate("simplemodule");
    CompilationUnit cuModule = factory.CompilationUnit().getOrCreate(module);
    CtPackage ctPackage = factory.Package().getOrCreate("my.beautiful.pack");
    module.setRootPackage(factory.Package().get("my"));
    CtType ctType = factory.Class().create("my.beautiful.pack.SuperClass");
    CompilationUnit cuClass = factory.CompilationUnit().getOrCreate(ctType);
    CompilationUnit cuPackage = factory.CompilationUnit().getOrCreate(ctPackage);
    File moduleFile = new File(outputDest.getCanonicalPath(), "simplemodule_module-info.java");
    File packageFile = new File(outputDest.getCanonicalPath(), "simplemodule_my.beautiful.pack_package-info.java");
    File classFile = new File(outputDest.getCanonicalPath(), "simplemodule_my.beautiful.pack_SuperClass.java");
    assertEquals(moduleFile, cuModule.getFile());
    assertEquals(packageFile, cuPackage.getFile());
    assertEquals(classFile, cuClass.getFile());
    Set<String> units = launcher.getFactory().CompilationUnit().getMap().keySet();
    assertEquals(3, units.size());
    assertTrue("Module file not contained (" + moduleFile.getCanonicalPath() + "). \nContent: " + StringUtils.join(units, "\n"), units.contains(moduleFile.getCanonicalPath()));
    assertTrue("Package file not contained (" + packageFile.getCanonicalPath() + "). \nContent: " + StringUtils.join(units, "\n"), units.contains(packageFile.getCanonicalPath()));
    assertTrue("Class file not contained (" + classFile.getCanonicalPath() + "). \nContent: " + StringUtils.join(units, "\n"), units.contains(classFile.getCanonicalPath()));
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) CtType(spoon.reflect.declaration.CtType) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CtPackage(spoon.reflect.declaration.CtPackage) File(java.io.File) OutputDestinationHandler(spoon.support.OutputDestinationHandler) CtModule(spoon.reflect.declaration.CtModule) Test(org.junit.Test)

Example 48 with CtType

use of spoon.reflect.declaration.CtType 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 49 with CtType

use of spoon.reflect.declaration.CtType 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 50 with CtType

use of spoon.reflect.declaration.CtType in project spoon by INRIA.

the class Substitution method insertAll.

/**
 * Inserts all the methods, fields, constructors, initialization blocks (if
 * target is a class), inner types, and super interfaces (except
 * {@link Template}) from a given template by substituting all the template
 * parameters by their values. Members annotated with
 * {@link spoon.template.Local} or {@link Parameter} are not inserted.
 *
 * @param targetType
 * 		the target type
 * @param template
 * 		the source template
 */
public static <T extends Template<?>> void insertAll(CtType<?> targetType, T template) {
    CtClass<T> templateClass = getTemplateCtClass(targetType, template);
    // insert all the interfaces
    insertAllSuperInterfaces(targetType, template);
    // insert all the methods
    insertAllMethods(targetType, template);
    // insert all the constructors and all the initialization blocks (only for classes)
    insertAllConstructors(targetType, template);
    for (CtTypeMember typeMember : templateClass.getTypeMembers()) {
        if (typeMember instanceof CtField) {
            // insert all the fields
            insertGeneratedField(targetType, template, (CtField<?>) typeMember);
        } else if (typeMember instanceof CtType) {
            // insert all the inner types
            insertGeneratedNestedType(targetType, template, (CtType) typeMember);
        }
    }
}
Also used : CtTypeMember(spoon.reflect.declaration.CtTypeMember) CtType(spoon.reflect.declaration.CtType) CtField(spoon.reflect.declaration.CtField)

Aggregations

CtType (spoon.reflect.declaration.CtType)134 Test (org.junit.Test)67 Launcher (spoon.Launcher)60 ArrayList (java.util.ArrayList)42 CtMethod (spoon.reflect.declaration.CtMethod)38 CtTypeReference (spoon.reflect.reference.CtTypeReference)30 DefaultJavaPrettyPrinter (spoon.reflect.visitor.DefaultJavaPrettyPrinter)20 File (java.io.File)19 Factory (spoon.reflect.factory.Factory)19 PrettyPrinter (spoon.reflect.visitor.PrettyPrinter)19 List (java.util.List)18 Collectors (java.util.stream.Collectors)17 CtField (spoon.reflect.declaration.CtField)17 CtElement (spoon.reflect.declaration.CtElement)16 CtPackage (spoon.reflect.declaration.CtPackage)16 InputConfiguration (fr.inria.diversify.utils.sosiefier.InputConfiguration)14 IOException (java.io.IOException)12 SpoonException (spoon.SpoonException)12 DSpotCompiler (fr.inria.diversify.utils.compilation.DSpotCompiler)11 Set (java.util.Set)11