use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.
the class JavaReflectionTreeBuilder method scan.
/**
* transforms a java.lang.Class into a CtType (ie a shadow type in Spoon's parlance)
*/
public <T, R extends CtType<T>> R scan(Class<T> clazz) {
CtPackage ctPackage;
CtType<?> ctEnclosingClass;
if (clazz.getEnclosingClass() != null) {
ctEnclosingClass = scan(clazz.getEnclosingClass());
return ctEnclosingClass.getNestedType(clazz.getSimpleName());
} else {
if (clazz.getPackage() == null) {
ctPackage = factory.Package().getRootPackage();
} else {
ctPackage = factory.Package().getOrCreate(clazz.getPackage().getName());
}
if (contexts.isEmpty()) {
enter(new PackageRuntimeBuilderContext(ctPackage));
}
if (clazz.isAnnotation()) {
visitAnnotationClass((Class<Annotation>) clazz);
} else if (clazz.isInterface()) {
visitInterface(clazz);
} else if (clazz.isEnum()) {
visitEnum(clazz);
} else {
visitClass(clazz);
}
exit();
final R type = ctPackage.getType(clazz.getSimpleName());
if (clazz.isPrimitive() && type.getParent() instanceof CtPackage) {
// primitive type isn't in a package.
type.setParent(null);
}
return type;
}
}
use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.
the class JavaReflectionTreeBuilder method visitPackage.
@Override
public void visitPackage(Package aPackage) {
final CtPackage ctPackage = factory.Package().getOrCreate(aPackage.getName());
enter(new PackageRuntimeBuilderContext(ctPackage));
super.visitPackage(aPackage);
exit();
contexts.peek().addPackage(ctPackage);
}
use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.
the class Substitution method createTypeFromTemplate.
/**
* Generates a type (class, interface, enum, ...) from the template model `templateOfType`
* by by substituting all the template parameters by their values.
*
* Inserts all the methods, fields, constructors, initialization blocks (if
* target is a class), inner types, super class and super interfaces.
*
* Note!
* This algorithm does NOT handle interfaces or annotations
* {@link Template}, {@link spoon.template.Local}, {@link TemplateParameter} or {@link Parameter}
* in a special way, it means they all will be added to the generated type too.
* If you do not want to add them then clone your templateOfType and remove these nodes from that model before.
*
* @param qualifiedTypeName
* the qualified name of the new type
* @param templateOfType
* the model used as source of generation.
* @param templateParameters
* the substitution parameters
*/
@SuppressWarnings("unchecked")
public static <T extends CtType<?>> T createTypeFromTemplate(String qualifiedTypeName, CtType<?> templateOfType, Map<String, Object> templateParameters) {
final Factory f = templateOfType.getFactory();
CtTypeReference<T> typeRef = f.Type().createReference(qualifiedTypeName);
CtPackage targetPackage = f.Package().getOrCreate(typeRef.getPackage().getSimpleName());
final Map<String, Object> extendedParams = new HashMap<String, Object>(templateParameters);
extendedParams.put(templateOfType.getSimpleName(), typeRef);
List<CtType<?>> generated = (List) new SubstitutionVisitor(f, extendedParams).substitute(templateOfType.clone());
for (CtType<?> ctType : generated) {
targetPackage.addType(ctType);
}
return (T) typeRef.getTypeDeclaration();
}
use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.
the class CtPackageAssertTest method testEqualityBetweenTwoCtPackageWithDifferentTypes.
@Test(expected = AssertionError.class)
public void testEqualityBetweenTwoCtPackageWithDifferentTypes() throws Exception {
final Factory factory = createFactory();
final CtPackage aRootPackage = factory.Package().getOrCreate("");
aRootPackage.addType(factory.Class().create("spoon.testing.testclasses.Foo").addModifier(ModifierKind.PUBLIC));
assertThat(build(new File("./src/test/java/spoon/testing/testclasses/")).Package().getRootPackage()).isEqualTo(aRootPackage);
}
use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.
the class CtPackageAssertTest method testEqualityBetweenTwoCtPackage.
@Test
public void testEqualityBetweenTwoCtPackage() throws Exception {
final Factory factory = createFactory();
final CtPackage aRootPackage = factory.Package().getOrCreate("");
aRootPackage.addType(factory.Class().create("spoon.testing.testclasses.Foo").addModifier(ModifierKind.PUBLIC));
aRootPackage.addType(factory.Class().create("spoon.testing.testclasses.Bar").addModifier(ModifierKind.PUBLIC));
assertThat(build(new File("./src/test/java/spoon/testing/testclasses/")).Package().getRootPackage()).isEqualTo(aRootPackage);
}
Aggregations