Search in sources :

Example 1 with CtAnnotationType

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

the class CtTypeImpl method getNestedType.

@Override
@SuppressWarnings("unchecked")
public <N extends CtType<?>> N getNestedType(final String name) {
    class NestedTypeScanner extends EarlyTerminatingScanner<CtType<?>> {

        private boolean checkType(CtType<?> type) {
            if (type.getSimpleName().equals(name) && CtTypeImpl.this.equals(type.getDeclaringType())) {
                setResult(type);
                terminate();
                return true;
            }
            return false;
        }

        @Override
        public <U> void visitCtClass(spoon.reflect.declaration.CtClass<U> ctClass) {
            if (!checkType(ctClass)) {
                final List<CtTypeMember> typeMembers = new ArrayList<>();
                for (CtTypeMember typeMember : ctClass.getTypeMembers()) {
                    if (typeMember instanceof CtType || typeMember instanceof CtConstructor || typeMember instanceof CtMethod) {
                        typeMembers.add(typeMember);
                    }
                }
                scan(typeMembers);
            }
        }

        @Override
        public <U> void visitCtInterface(spoon.reflect.declaration.CtInterface<U> intrface) {
            if (!checkType(intrface)) {
                final List<CtTypeMember> typeMembers = new ArrayList<>();
                for (CtTypeMember typeMember : intrface.getTypeMembers()) {
                    if (typeMember instanceof CtType || typeMember instanceof CtMethod) {
                        typeMembers.add(typeMember);
                    }
                }
                scan(typeMembers);
            }
        }

        @Override
        public <U extends java.lang.Enum<?>> void visitCtEnum(spoon.reflect.declaration.CtEnum<U> ctEnum) {
            if (!checkType(ctEnum)) {
                final List<CtTypeMember> typeMembers = new ArrayList<>();
                for (CtTypeMember typeMember : ctEnum.getTypeMembers()) {
                    if (typeMember instanceof CtType || typeMember instanceof CtConstructor || typeMember instanceof CtMethod) {
                        typeMembers.add(typeMember);
                    }
                }
                scan(typeMembers);
            }
        }

        @Override
        public <A extends Annotation> void visitCtAnnotationType(CtAnnotationType<A> annotationType) {
            if (!checkType(annotationType)) {
                scan(annotationType.getNestedTypes());
            }
        }
    }
    NestedTypeScanner scanner = new NestedTypeScanner();
    scanner.scan(this);
    return (N) scanner.getResult();
}
Also used : ArrayList(java.util.ArrayList) EarlyTerminatingScanner(spoon.reflect.visitor.EarlyTerminatingScanner) Annotation(java.lang.annotation.Annotation) CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtConstructor(spoon.reflect.declaration.CtConstructor) CtType(spoon.reflect.declaration.CtType) CtTypeMember(spoon.reflect.declaration.CtTypeMember) CtAnnotationType(spoon.reflect.declaration.CtAnnotationType) CtMethod(spoon.reflect.declaration.CtMethod)

Example 2 with CtAnnotationType

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

the class AnnotationTest method testAnnotationInterfacePreserveMethods.

@Test
public void testAnnotationInterfacePreserveMethods() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/PortRange.java");
    launcher.buildModel();
    Factory factory = launcher.getFactory();
    final CtAnnotationType<?> ctAnnotationType = (CtAnnotationType) factory.Type().get(PortRange.class);
    List<CtMethod<?>> ctMethodMin = ctAnnotationType.getMethodsByName("min");
    assertEquals("Method min is preserved after transformation", 1, ctMethodMin.size());
    List<CtMethod<?>> ctMethodMax = ctAnnotationType.getMethodsByName("max");
    assertEquals("Method max is preserved after transformation", 1, ctMethodMax.size());
    List<CtMethod<?>> ctMethodMessage = ctAnnotationType.getMethodsByName("message");
    assertEquals("Method message is preserved after transformation", 1, ctMethodMessage.size());
    List<CtMethod<?>> ctMethodGroups = ctAnnotationType.getMethodsByName("groups");
    assertEquals("Method groups is preserved after transformation", 1, ctMethodGroups.size());
    List<CtMethod<?>> ctMethodPayload = ctAnnotationType.getMethodsByName("payload");
    assertEquals("Method payload is preserved after transformation", 1, ctMethodPayload.size());
}
Also used : PortRange(spoon.test.annotation.testclasses.PortRange) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CtAnnotationType(spoon.reflect.declaration.CtAnnotationType) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 3 with CtAnnotationType

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

the class PackageTest method testAddAnnotationToPackage.

@Test
public void testAddAnnotationToPackage() throws Exception {
    // contract: Created package-info should used imports in auto-import
    final Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/pkg/testclasses/Foo.java");
    spoon.getEnvironment().setAutoImports(true);
    File outputDir = new File("./target/spoon-packageinfo");
    spoon.getEnvironment().setSourceOutputDirectory(outputDir);
    spoon.buildModel();
    CtAnnotationType annotationType = (CtAnnotationType) spoon.getFactory().Annotation().get(GlobalAnnotation.class);
    CtAnnotation annotation = spoon.getFactory().Core().createAnnotation();
    annotation.setAnnotationType(annotationType.getReference());
    CtPackage ctPackage = spoon.getFactory().Package().get("spoon.test.pkg.testclasses");
    ctPackage.addAnnotation(annotation);
    JavaOutputProcessor outputProcessor = spoon.createOutputWriter();
    outputProcessor.process(ctPackage);
    File packageInfo = new File(outputDir, "spoon/test/pkg/testclasses/package-info.java");
    assertTrue(packageInfo.exists());
    canBeBuilt(packageInfo, 8);
    List<String> lines = Files.readAllLines(packageInfo.toPath());
    assertFalse(lines.isEmpty());
    for (String s : lines) {
        if (s.trim().startsWith("import")) {
            assertEquals("import spoon.test.annotation.testclasses.GlobalAnnotation;", s.trim());
        }
        if (s.trim().startsWith("@")) {
            assertEquals("@GlobalAnnotation", s.trim());
        }
    }
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) GlobalAnnotation(spoon.test.annotation.testclasses.GlobalAnnotation) Launcher(spoon.Launcher) CtAnnotationType(spoon.reflect.declaration.CtAnnotationType) CtPackage(spoon.reflect.declaration.CtPackage) File(java.io.File) JavaOutputProcessor(spoon.support.JavaOutputProcessor) Test(org.junit.Test)

Aggregations

CtAnnotationType (spoon.reflect.declaration.CtAnnotationType)3 Test (org.junit.Test)2 Launcher (spoon.Launcher)2 CtAnnotation (spoon.reflect.declaration.CtAnnotation)2 CtMethod (spoon.reflect.declaration.CtMethod)2 File (java.io.File)1 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 CtConstructor (spoon.reflect.declaration.CtConstructor)1 CtPackage (spoon.reflect.declaration.CtPackage)1 CtType (spoon.reflect.declaration.CtType)1 CtTypeMember (spoon.reflect.declaration.CtTypeMember)1 Factory (spoon.reflect.factory.Factory)1 EarlyTerminatingScanner (spoon.reflect.visitor.EarlyTerminatingScanner)1 JavaOutputProcessor (spoon.support.JavaOutputProcessor)1 GlobalAnnotation (spoon.test.annotation.testclasses.GlobalAnnotation)1 PortRange (spoon.test.annotation.testclasses.PortRange)1