Search in sources :

Example 16 with CtPackage

use of spoon.reflect.declaration.CtPackage 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 17 with CtPackage

use of spoon.reflect.declaration.CtPackage 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 18 with CtPackage

use of spoon.reflect.declaration.CtPackage 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;
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) CtPackage(spoon.reflect.declaration.CtPackage)

Example 19 with CtPackage

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

the class AbstractCtPackageAssert method isEqualTo.

/**
 * Verifies that the actual value is equal to the given one.
 *
 * @param expected
 * 		The expected package.
 * @return {@code this} assertion object.
 */
public T isEqualTo(CtPackage expected) {
    assertNotNull(expected);
    if (!actual.getSimpleName().equals(expected.getSimpleName())) {
        throw new AssertionError(String.format("The actual package named %1$s isn't equals to the expected package named %2$s", actual.getSimpleName(), expected.getSimpleName()));
    }
    if (processors != null && !processors.isEmpty()) {
        process(actual.getFactory(), processors);
    }
    class TypeComparator implements Comparator<CtType<?>> {

        @Override
        public int compare(CtType<?> o1, CtType<?> o2) {
            return o1.getSimpleName().compareTo(o2.getSimpleName());
        }
    }
    final List<CtType<?>> actualTypes = new ArrayList<>(actual.getTypes());
    Collections.sort(actualTypes, new TypeComparator());
    final List<CtType<?>> expectedTypes = new ArrayList<>(expected.getTypes());
    Collections.sort(expectedTypes, new TypeComparator());
    for (int i = 0; i < actual.getTypes().size(); i++) {
        final CtType<?> actualType = actualTypes.get(i);
        final CtType<?> expectedType = expectedTypes.get(i);
        if (!actualType.toString().equals(expectedType.toString())) {
            throw new AssertionError(String.format("%1$s and %2$s aren't equals.", actualType.getShortRepresentation(), expectedType.getShortRepresentation()));
        }
    }
    class PackageComparator implements Comparator<CtPackage> {

        @Override
        public int compare(CtPackage o1, CtPackage o2) {
            return o1.getSimpleName().compareTo(o2.getSimpleName());
        }
    }
    final List<CtPackage> actualPackages = new ArrayList<>(actual.getPackages());
    Collections.sort(actualPackages, new PackageComparator());
    final List<CtPackage> expectedPackages = new ArrayList<>(expected.getPackages());
    Collections.sort(expectedPackages, new PackageComparator());
    for (int i = 0; i < actualPackages.size(); i++) {
        final CtPackage actualPackage = actualPackages.get(i);
        final CtPackage expectedPackage = expectedPackages.get(i);
        assertThat(actualPackage).isEqualTo(expectedPackage);
    }
    return this.myself;
}
Also used : CtType(spoon.reflect.declaration.CtType) ArrayList(java.util.ArrayList) CtPackage(spoon.reflect.declaration.CtPackage) Comparator(java.util.Comparator)

Example 20 with CtPackage

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

the class ParentTest method testParentOfCtPackageReference.

@Test
public void testParentOfCtPackageReference() throws Exception {
    // contract: a parent at a top level must be the root package and in the code, the element which call getParent().
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput" });
    launcher.getEnvironment().setNoClasspath(true);
    launcher.addInputResource("./src/test/resources/reference-package");
    launcher.run();
    final CtType<Object> panini = launcher.getFactory().Type().get("Panini");
    CtElement topLevelParent = panini.getPackage().getParent();
    assertNotNull(topLevelParent);
    assertEquals(CtPackage.TOP_LEVEL_PACKAGE_NAME, panini.getPackage().getSimpleName());
    CtPackage pack1 = factory.Package().getRootPackage();
    // the factory are not the same
    assertNotEquals(factory, launcher.getFactory());
    // so the root packages are not deeply equals
    assertNotEquals(pack1, topLevelParent);
    final CtTypeReference<?> burritos = panini.getElements(new ReferenceTypeFilter<CtTypeReference<?>>(CtTypeReference.class) {

        @Override
        public boolean matches(CtTypeReference<?> reference) {
            return "Burritos".equals(reference.getSimpleName()) && super.matches(reference);
        }
    }).get(0);
    assertNotNull(burritos.getPackage().getParent());
    assertEquals("com.awesome", burritos.getPackage().getSimpleName());
    assertEquals(burritos, burritos.getPackage().getParent());
}
Also used : CtElement(spoon.reflect.declaration.CtElement) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) CtPackage(spoon.reflect.declaration.CtPackage) Test(org.junit.Test)

Aggregations

CtPackage (spoon.reflect.declaration.CtPackage)53 Test (org.junit.Test)29 Launcher (spoon.Launcher)21 Factory (spoon.reflect.factory.Factory)13 CtType (spoon.reflect.declaration.CtType)12 File (java.io.File)8 CtClass (spoon.reflect.declaration.CtClass)6 CtElement (spoon.reflect.declaration.CtElement)6 CtTypeReference (spoon.reflect.reference.CtTypeReference)6 ArrayList (java.util.ArrayList)4 CtAnnotation (spoon.reflect.declaration.CtAnnotation)4 CtMethod (spoon.reflect.declaration.CtMethod)4 CtModule (spoon.reflect.declaration.CtModule)4 HashSet (java.util.HashSet)3 StringTokenizer (java.util.StringTokenizer)3 SpoonAPI (spoon.SpoonAPI)3 CtStatement (spoon.reflect.code.CtStatement)3 CtField (spoon.reflect.declaration.CtField)3 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)3 Annotation (java.lang.annotation.Annotation)2