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());
}
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()));
}
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;
}
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;
}
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());
}
Aggregations