Search in sources :

Example 1 with AbstractFilter

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;
        }
    });
}
Also used : CtClass(spoon.reflect.declaration.CtClass) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) CtNewClass(spoon.reflect.code.CtNewClass) Foo(spoon.test.constructorcallnewclass.testclasses.Foo) Factory(spoon.reflect.factory.Factory) Before(org.junit.Before)

Example 2 with AbstractFilter

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());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) CtInterface(spoon.reflect.declaration.CtInterface) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) LikeCtClass(spoon.test.generics.testclasses2.LikeCtClass) CtClass(spoon.reflect.declaration.CtClass) CtNewClass(spoon.reflect.code.CtNewClass) Launcher(spoon.Launcher) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 3 with AbstractFilter

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());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) CtLineElementComparator(spoon.support.comparator.CtLineElementComparator) Test(org.junit.Test)

Example 4 with AbstractFilter

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());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtAnnotation(spoon.reflect.declaration.CtAnnotation) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) AnnotationsRepeated(spoon.test.annotation.testclasses.AnnotationsRepeated) CtLocalVariable(spoon.reflect.code.CtLocalVariable) 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 5 with AbstractFilter

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());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtAnnotation(spoon.reflect.declaration.CtAnnotation) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CtLocalVariable(spoon.reflect.code.CtLocalVariable) 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) AnnotationsAppliedOnAnyTypeInAClass(spoon.test.annotation.testclasses.AnnotationsAppliedOnAnyTypeInAClass) Test(org.junit.Test)

Aggregations

AbstractFilter (spoon.reflect.visitor.filter.AbstractFilter)15 Test (org.junit.Test)13 CtClass (spoon.reflect.declaration.CtClass)13 Launcher (spoon.Launcher)11 Factory (spoon.reflect.factory.Factory)8 Annotation (java.lang.annotation.Annotation)4 CtAnnotation (spoon.reflect.declaration.CtAnnotation)4 AnnotationDefaultAnnotation (spoon.test.annotation.testclasses.AnnotationDefaultAnnotation)4 InnerAnnotation (spoon.test.annotation.testclasses.Foo.InnerAnnotation)4 MiddleAnnotation (spoon.test.annotation.testclasses.Foo.MiddleAnnotation)4 OuterAnnotation (spoon.test.annotation.testclasses.Foo.OuterAnnotation)4 GlobalAnnotation (spoon.test.annotation.testclasses.GlobalAnnotation)4 SuperAnnotation (spoon.test.annotation.testclasses.SuperAnnotation)4 TypeAnnotation (spoon.test.annotation.testclasses.TypeAnnotation)4 ArrayList (java.util.ArrayList)3 TreeSet (java.util.TreeSet)3 CtConstructorCall (spoon.reflect.code.CtConstructorCall)3 CtInvocation (spoon.reflect.code.CtInvocation)3 CtLocalVariable (spoon.reflect.code.CtLocalVariable)3 Before (org.junit.Before)2