Search in sources :

Example 41 with CtPackage

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;
    }
}
Also used : PackageRuntimeBuilderContext(spoon.support.visitor.java.internal.PackageRuntimeBuilderContext) CtPackage(spoon.reflect.declaration.CtPackage) CtAnnotation(spoon.reflect.declaration.CtAnnotation) Annotation(java.lang.annotation.Annotation)

Example 42 with CtPackage

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);
}
Also used : PackageRuntimeBuilderContext(spoon.support.visitor.java.internal.PackageRuntimeBuilderContext) CtPackage(spoon.reflect.declaration.CtPackage)

Example 43 with 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();
}
Also used : HashMap(java.util.HashMap) Factory(spoon.reflect.factory.Factory) SubstitutionVisitor(spoon.support.template.SubstitutionVisitor) CtType(spoon.reflect.declaration.CtType) CtPackage(spoon.reflect.declaration.CtPackage) List(java.util.List)

Example 44 with CtPackage

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);
}
Also used : Factory(spoon.reflect.factory.Factory) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) CtPackage(spoon.reflect.declaration.CtPackage) File(java.io.File) Test(org.junit.Test)

Example 45 with CtPackage

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);
}
Also used : Factory(spoon.reflect.factory.Factory) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) CtPackage(spoon.reflect.declaration.CtPackage) File(java.io.File) 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