Search in sources :

Example 36 with CtPackage

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

the class TestCompilationUnit method testGetUnitTypeWorksWithCreatedObjects.

@Test
public void testGetUnitTypeWorksWithCreatedObjects() {
    final Launcher launcher = new Launcher();
    CtPackage myPackage = launcher.getFactory().Package().getOrCreate("my.package");
    CompilationUnit cu = launcher.getFactory().createCompilationUnit();
    assertEquals(CompilationUnit.UNIT_TYPE.UNKNOWN, cu.getUnitType());
    cu.setDeclaredPackage(myPackage);
    assertEquals(CompilationUnit.UNIT_TYPE.PACKAGE_DECLARATION, cu.getUnitType());
    cu.setDeclaredTypes(Collections.singletonList(launcher.getFactory().createClass()));
    assertEquals(CompilationUnit.UNIT_TYPE.TYPE_DECLARATION, cu.getUnitType());
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) Launcher(spoon.Launcher) CtPackage(spoon.reflect.declaration.CtPackage) Test(org.junit.Test)

Example 37 with CtPackage

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

the class CtPackageImpl method addPackage.

@Override
public <T extends CtPackage> T addPackage(CtPackage pack) {
    if (pack == null) {
        return (T) this;
    }
    if (packs == CtElementImpl.<CtPackage>emptySet()) {
        this.packs = orderedPackageSet();
    }
    // they are the same
    if (this.getQualifiedName().equals(pack.getQualifiedName())) {
        addAllTypes(pack, this);
        addAllPackages(pack, this);
        return (T) this;
    }
    // it already exists
    for (CtPackage p1 : packs) {
        if (p1.getQualifiedName().equals(pack.getQualifiedName())) {
            addAllTypes(pack, p1);
            addAllPackages(pack, p1);
            return (T) this;
        }
    }
    pack.setParent(this);
    getFactory().getEnvironment().getModelChangeListener().onSetAdd(this, SUB_PACKAGE, this.packs, pack);
    this.packs.add(pack);
    return (T) this;
}
Also used : CtPackage(spoon.reflect.declaration.CtPackage)

Example 38 with CtPackage

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

the class CloneVisitorGenerator method createCloneVisitor.

private CtClass<Object> createCloneVisitor() {
    final CtPackage aPackage = getFactory().Package().getOrCreate(TARGET_CLONE_PACKAGE);
    final CtClass<Object> target = getFactory().Class().get(GENERATING_CLONE);
    target.setSimpleName(TARGET_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_CLONE.equals(reference.getQualifiedName());
        }
    });
    for (CtTypeReference reference : references) {
        reference.setSimpleName(TARGET_CLONE_TYPE);
        reference.setPackage(aPackage.getReference());
    }
    target.getConstructors().forEach(c -> c.addModifier(ModifierKind.PUBLIC));
    return target;
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) CtPackage(spoon.reflect.declaration.CtPackage)

Example 39 with CtPackage

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

the class CtBiScannerGenerator method createBiScanner.

private CtClass<Object> createBiScanner() {
    final CtPackage aPackage = getFactory().Package().getOrCreate(TARGET_BISCANNER_PACKAGE);
    final CtClass<Object> target = getFactory().Class().get(GENERATING_BISCANNER);
    target.setSimpleName("CtBiScannerDefault");
    target.addModifier(ModifierKind.PUBLIC);
    aPackage.addType(target);
    target.addComment(getFactory().Code().createComment("autogenerated by " + getClass().getSimpleName(), CtComment.CommentType.INLINE));
    final List<CtTypeReference> references = target.getElements(new TypeFilter<CtTypeReference>(CtTypeReference.class) {

        @Override
        public boolean matches(CtTypeReference reference) {
            return GENERATING_BISCANNER.equals(reference.getQualifiedName());
        }
    });
    for (CtTypeReference reference : references) {
        reference.setSimpleName("CtBiScannerDefault");
        reference.setPackage(aPackage.getReference());
    }
    return target;
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) CtPackage(spoon.reflect.declaration.CtPackage)

Example 40 with CtPackage

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

the class ReplacementVisitorGenerator method createReplacementVisitor.

private CtClass<Object> createReplacementVisitor() {
    final CtPackage aPackage = getFactory().Package().getOrCreate(TARGET_REPLACE_PACKAGE);
    final CtClass<Object> target = getFactory().Class().get(GENERATING_REPLACE_VISITOR);
    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_REPLACE_VISITOR.equals(reference.getQualifiedName());
        }
    });
    for (CtTypeReference reference : references) {
        reference.setPackage(aPackage.getReference());
    }
    return target;
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) CtPackage(spoon.reflect.declaration.CtPackage)

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