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();
}
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());
}
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());
}
}
}
Aggregations