use of spoon.reflect.visitor.filter.AbstractFilter in project spoon by INRIA.
the class NewClassTest method setUp.
@Before
public void setUp() throws Exception {
final Factory build = build(Foo.class);
final CtClass<?> foo = (CtClass<?>) build.Type().get(Foo.class);
newClasses = foo.getElements(new AbstractFilter<CtNewClass<?>>(CtNewClass.class) {
@Override
public boolean matches(CtNewClass<?> element) {
return true;
}
});
}
use of spoon.reflect.visitor.filter.AbstractFilter in project spoon by INRIA.
the class GenericsTest method testAccessToGenerics.
@Test
public void testAccessToGenerics() throws Exception {
Launcher spoon = new Launcher();
Factory factory = spoon.createFactory();
SpoonModelBuilder compiler = spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/java/spoon/test/generics/Foo.java", "./src/test/java/spoon/test/generics/Bar.java"));
compiler.build();
CtClass<?> foo = (CtClass<?>) factory.Type().get(Foo.class);
CtInterface<?> bar = (CtInterface<?>) factory.Type().get(Bar.class);
final CtNewClass<?> newAnonymousBar = foo.getElements(new AbstractFilter<CtNewClass<?>>(CtNewClass.class) {
@Override
public boolean matches(CtNewClass<?> element) {
return element.getAnonymousClass() != null && element.getAnonymousClass().isAnonymous();
}
}).get(0);
final List<CtTypeParameter> barTypeParamGenerics = bar.getFormalCtTypeParameters();
final CtTypeReference<?> anonymousBar = newAnonymousBar.getType();
assertEquals("Name of the first generic parameter in Bar interface must to be I.", "I", barTypeParamGenerics.get(0).getSimpleName());
assertEquals("Name of the first generic parameter in Bar usage must to be K.", "K", anonymousBar.getActualTypeArguments().get(0).getSimpleName());
assertEquals("Name of the second generic parameter in Bar interface must to be O.", "O", barTypeParamGenerics.get(1).getSimpleName());
assertEquals("Name of the second generic parameter in Bar usage must to be V.", "V", anonymousBar.getActualTypeArguments().get(1).getSimpleName());
}
use of spoon.reflect.visitor.filter.AbstractFilter in project spoon by INRIA.
the class ClassesTest method testIsAnonymousMethodInCtClass.
@Test
public void testIsAnonymousMethodInCtClass() throws Exception {
CtClass<?> type = build("spoon.test.secondaryclasses.testclasses", "AnonymousClass");
TreeSet<CtClass<?>> ts = new TreeSet<CtClass<?>>(new CtLineElementComparator());
ts.addAll(type.getElements(new AbstractFilter<CtClass<?>>(CtClass.class) {
@Override
public boolean matches(CtClass<?> element) {
return element.isAnonymous();
}
}));
List<CtClass<?>> anonymousClass = new ArrayList<CtClass<?>>();
anonymousClass.addAll(ts);
assertFalse(type.isAnonymous());
assertTrue(ts.first().isAnonymous());
assertTrue(anonymousClass.get(1).isAnonymous());
assertEquals(2, anonymousClass.size());
assertEquals(2, ts.size());
assertEquals("spoon.test.secondaryclasses.testclasses.AnonymousClass$1", anonymousClass.get(0).getQualifiedName());
assertEquals("spoon.test.secondaryclasses.testclasses.AnonymousClass$2", anonymousClass.get(1).getQualifiedName());
}
use of spoon.reflect.visitor.filter.AbstractFilter in project spoon by INRIA.
the class AnnotationTest method testRepeatSameAnnotationOnLocalVariable.
@Test
public void testRepeatSameAnnotationOnLocalVariable() throws Exception {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/AnnotationsRepeated.java");
launcher.buildModel();
Factory factory = launcher.getFactory();
final CtClass<?> ctClass = (CtClass<?>) factory.Type().get(AnnotationsRepeated.class);
final CtMethod<?> method = ctClass.getMethodsByName("methodWithLocalVariable").get(0);
final CtLocalVariable<?> ctLocalVariable = method.getBody().getElements(new AbstractFilter<CtLocalVariable<?>>(CtLocalVariable.class) {
@Override
public boolean matches(CtLocalVariable<?> element) {
return true;
}
}).get(0);
final List<CtAnnotation<? extends Annotation>> annotations = ctLocalVariable.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 \"Local 1\"", "Local 1", ((CtLiteral) annotations.get(0).getValue("value")).getValue());
assertEquals("Argument of the second annotation is \"Local 2\"", "Local 2", ((CtLiteral) annotations.get(1).getValue("value")).getValue());
}
use of spoon.reflect.visitor.filter.AbstractFilter in project spoon by INRIA.
the class AnnotationTest method testUsageOfTypeAnnotationOnLocalVariableInMethod.
@Test
public void testUsageOfTypeAnnotationOnLocalVariableInMethod() throws Exception {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/AnnotationsAppliedOnAnyTypeInAClass.java");
launcher.buildModel();
Factory factory = launcher.getFactory();
final CtClass<?> ctClass = (CtClass<?>) factory.Type().get(AnnotationsAppliedOnAnyTypeInAClass.class);
final CtMethod<?> method = ctClass.getMethodsByName("m6").get(0);
final CtLocalVariable<?> ctLocalVariable = method.getBody().getElements(new AbstractFilter<CtLocalVariable<?>>(CtLocalVariable.class) {
@Override
public boolean matches(CtLocalVariable<?> element) {
return true;
}
}).get(0);
final List<CtAnnotation<? extends Annotation>> typeAnnotations = ctLocalVariable.getType().getAnnotations();
assertEquals("Local variable type with a type annotation must have it in its model", 1, typeAnnotations.size());
assertEquals("Type annotation with the local variable type must be typed by TypeAnnotation", TypeAnnotation.class, typeAnnotations.get(0).getAnnotationType().getActualClass());
assertEquals(CtAnnotatedElementType.TYPE_USE, typeAnnotations.get(0).getAnnotatedElementType());
assertEquals("Local variable type with an type annotation must be well printed", "java.lang.@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "String s = \"\"", ctLocalVariable.toString());
}
Aggregations