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