Search in sources :

Example 31 with CtPackage

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

the class APITest method testBasicAPIUsage.

@Test
public void testBasicAPIUsage() throws Exception {
    // this test shows a basic usage of the Launcher API without command line
    // and asserts there is no exception
    Launcher spoon = new Launcher();
    spoon.setArgs(new String[] { "--compile", "--output-type", "compilationunits" });
    spoon.addInputResource("src/test/resources/spoon/test/api");
    spoon.run();
    Factory factory = spoon.getFactory();
    for (CtPackage p : factory.Package().getAll()) {
        spoon.getEnvironment().debugMessage("package: " + p.getQualifiedName());
    }
    for (CtType<?> s : factory.Class().getAll()) {
        spoon.getEnvironment().debugMessage("class: " + s.getQualifiedName());
    }
}
Also used : Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CtPackage(spoon.reflect.declaration.CtPackage) Test(org.junit.Test)

Example 32 with CtPackage

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

the class SpoonArchitectureEnforcerTest method testFactorySubFactory.

@Test
public void testFactorySubFactory() throws Exception {
    // contract:: all subfactory methods must also be in the main factory
    // this is very important for usability and discoverability
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/main/java/spoon/reflect/factory");
    class SanityCheck {

        int val = 0;
    }
    ;
    SanityCheck sanityCheck = new SanityCheck();
    launcher.addProcessor(new AbstractManualProcessor() {

        @Override
        public void process() {
            CtType factoryImpl = getFactory().Interface().get(Factory.class);
            CtPackage factoryPackage = getFactory().Package().getOrCreate("spoon.reflect.factory");
            CtInterface itf = getFactory().Interface().create("MegaFactoryItf");
            CtClass impl = getFactory().Class().create("MegaFactory");
            for (CtType<?> t : factoryPackage.getTypes()) {
                // 
                if (t.getSimpleName().startsWith("Mega"))
                    continue;
                for (CtMethod<?> m : t.getMethods()) {
                    // we check only public methods
                    if (m.hasModifier(ModifierKind.PUBLIC) == false)
                        continue;
                    // we only consider factory methods
                    if (!m.getSimpleName().startsWith("create"))
                        continue;
                    // too generic, what should we create??
                    if (m.getSimpleName().equals("create")) {
                        String simpleNameType = m.getType().getSimpleName().replace("Ct", "");
                        CtMethod method = m.clone();
                        method.setSimpleName("create" + simpleNameType);
                        assertTrue(method.getSignature() + " (from " + t.getQualifiedName() + ") is not present in the main factory", factoryImpl.hasMethod(method));
                        continue;
                    }
                    // too generic, is it a fieldref? an execref? etc
                    if (m.getSimpleName().equals("createReference"))
                        continue;
                    if (m.getModifiers().contains(ModifierKind.ABSTRACT))
                        continue;
                    sanityCheck.val++;
                    // the core assertion
                    assertTrue(m.getSignature() + " is not present in the main factory", factoryImpl.hasMethod(m));
                }
            }
        }
    });
    launcher.run();
    assertTrue(sanityCheck.val > 100);
}
Also used : CtInterface(spoon.reflect.declaration.CtInterface) CtClass(spoon.reflect.declaration.CtClass) AbstractManualProcessor(spoon.processing.AbstractManualProcessor) CtType(spoon.reflect.declaration.CtType) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CtPackage(spoon.reflect.declaration.CtPackage) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 33 with CtPackage

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

the class SpoonArchitectureEnforcerTest method testSpecPackage.

@Test
public void testSpecPackage() throws Exception {
    // contract: when a pull-request introduces a new package, it is made explicit during code review
    // when a pull-request introduces a new package, this test fails and the author has to explicitly declare the new package here
    Set<String> officialPackages = new TreeSet<>();
    officialPackages.add("spoon.compiler.builder");
    officialPackages.add("spoon.compiler");
    officialPackages.add("spoon.experimental.modelobs.action");
    officialPackages.add("spoon.experimental.modelobs.context");
    officialPackages.add("spoon.experimental.modelobs");
    officialPackages.add("spoon.experimental");
    officialPackages.add("spoon.legacy");
    officialPackages.add("spoon.processing");
    officialPackages.add("spoon.refactoring");
    officialPackages.add("spoon.reflect.annotations");
    officialPackages.add("spoon.reflect.code");
    officialPackages.add("spoon.reflect.cu.position");
    officialPackages.add("spoon.reflect.cu");
    officialPackages.add("spoon.reflect.declaration");
    officialPackages.add("spoon.reflect.eval");
    officialPackages.add("spoon.reflect.factory");
    officialPackages.add("spoon.reflect.path.impl");
    officialPackages.add("spoon.reflect.path");
    officialPackages.add("spoon.reflect.reference");
    officialPackages.add("spoon.reflect.visitor.chain");
    officialPackages.add("spoon.reflect.visitor.filter");
    officialPackages.add("spoon.reflect.visitor.printer");
    officialPackages.add("spoon.reflect.visitor");
    officialPackages.add("spoon.reflect");
    officialPackages.add("spoon.support.comparator");
    officialPackages.add("spoon.support.compiler.jdt");
    officialPackages.add("spoon.support.compiler");
    officialPackages.add("spoon.support.gui");
    officialPackages.add("spoon.support.reflect.code");
    officialPackages.add("spoon.support.reflect.cu.position");
    officialPackages.add("spoon.support.reflect.cu");
    officialPackages.add("spoon.support.reflect.declaration");
    officialPackages.add("spoon.support.reflect.eval");
    officialPackages.add("spoon.reflect.meta");
    officialPackages.add("spoon.reflect.meta.impl");
    officialPackages.add("spoon.support.reflect.reference");
    officialPackages.add("spoon.support.reflect");
    officialPackages.add("spoon.support.template");
    officialPackages.add("spoon.support.util");
    officialPackages.add("spoon.support.visitor.clone");
    officialPackages.add("spoon.support.visitor.equals");
    officialPackages.add("spoon.support.visitor.java.internal");
    officialPackages.add("spoon.support.visitor.java.reflect");
    officialPackages.add("spoon.support.visitor.java");
    officialPackages.add("spoon.support.visitor.replace");
    officialPackages.add("spoon.support.visitor");
    officialPackages.add("spoon.support");
    officialPackages.add("spoon.template");
    officialPackages.add("spoon.testing.utils");
    officialPackages.add("spoon.testing");
    officialPackages.add("spoon");
    // root package
    officialPackages.add("");
    SpoonAPI spoon = new Launcher();
    spoon.addInputResource("src/main/java/");
    spoon.buildModel();
    final Set<String> currentPackages = new TreeSet<>();
    spoon.getModel().processWith(new AbstractProcessor<CtPackage>() {

        @Override
        public void process(CtPackage element) {
            currentPackages.add(element.getQualifiedName());
        }
    });
    assertSetEquals("you have created a new package or removed an existing one, please declare it explicitly in SpoonArchitectureEnforcerTest#testSpecPackage", officialPackages, currentPackages);
}
Also used : TreeSet(java.util.TreeSet) Launcher(spoon.Launcher) CtPackage(spoon.reflect.declaration.CtPackage) SpoonAPI(spoon.SpoonAPI) Test(org.junit.Test)

Example 34 with CtPackage

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

the class AnnotationTest method testRepeatSameAnnotationOnPackage.

@Test
public void testRepeatSameAnnotationOnPackage() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/AnnotationsRepeated.java");
    launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/package-info.java");
    launcher.buildModel();
    Factory factory = launcher.getFactory();
    final CtPackage pkg = factory.Package().get("spoon.test.annotation.testclasses");
    final List<CtAnnotation<? extends Annotation>> annotations = pkg.getAnnotations();
    assertEquals("Local variable must to have multi annotation of the same type", 2, annotations.size());
    assertEquals("Type of the first annotation is AnnotationRepeated", AnnotationRepeated.class, annotations.get(0).getAnnotationType().getActualClass());
    assertEquals("Type of the second annotation is AnnotationRepeated", AnnotationRepeated.class, annotations.get(1).getAnnotationType().getActualClass());
    assertEquals("Argument of the first annotation is \"Package 1\"", "Package 1", ((CtLiteral) annotations.get(0).getValue("value")).getValue());
    assertEquals("Argument of the second annotation is \"Package 2\"", "Package 2", ((CtLiteral) annotations.get(1).getValue("value")).getValue());
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CtPackage(spoon.reflect.declaration.CtPackage) TypeAnnotation(spoon.test.annotation.testclasses.TypeAnnotation) GlobalAnnotation(spoon.test.annotation.testclasses.GlobalAnnotation) SuperAnnotation(spoon.test.annotation.testclasses.SuperAnnotation) AnnotationDefaultAnnotation(spoon.test.annotation.testclasses.AnnotationDefaultAnnotation) InnerAnnotation(spoon.test.annotation.testclasses.Foo.InnerAnnotation) Annotation(java.lang.annotation.Annotation) MiddleAnnotation(spoon.test.annotation.testclasses.Foo.MiddleAnnotation) OuterAnnotation(spoon.test.annotation.testclasses.Foo.OuterAnnotation) CtAnnotation(spoon.reflect.declaration.CtAnnotation) Test(org.junit.Test)

Example 35 with CtPackage

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

the class TestCompilationUnit method testGetUnitTypeWorksWithDeclaredPackage.

@Test
public void testGetUnitTypeWorksWithDeclaredPackage() {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/pkg/package-info.java");
    launcher.buildModel();
    CtPackage ctPackage = launcher.getFactory().Package().get("spoon.test.pkg");
    CompilationUnit compilationUnit = ctPackage.getPosition().getCompilationUnit();
    assertEquals(CompilationUnit.UNIT_TYPE.PACKAGE_DECLARATION, compilationUnit.getUnitType());
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) Launcher(spoon.Launcher) CtPackage(spoon.reflect.declaration.CtPackage) 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