Search in sources :

Example 1 with CtFieldAccess

use of spoon.reflect.code.CtFieldAccess in project spoon by INRIA.

the class FilterTest method testFieldAccessFilter.

@Test
public void testFieldAccessFilter() throws Exception {
    // also specifies VariableAccessFilter since FieldAccessFilter is only a VariableAccessFilter with additional static typing
    CtClass<?> foo = factory.Package().get("spoon.test.filters").getType("Foo");
    assertEquals("Foo", foo.getSimpleName());
    List<CtNamedElement> elements = foo.getElements(new NamedElementFilter<>(CtNamedElement.class, "i"));
    assertEquals(1, elements.size());
    CtFieldReference<?> ref = (CtFieldReference<?>) (elements.get(0)).getReference();
    List<CtFieldAccess<?>> expressions = foo.getElements(new FieldAccessFilter(ref));
    assertEquals(2, expressions.size());
    final Factory build = build(FieldAccessFilterTacos.class);
    final CtType<FieldAccessFilterTacos> fieldAccessFilterTacos = build.Type().get(FieldAccessFilterTacos.class);
    try {
        List<CtField> fields = fieldAccessFilterTacos.getElements(new TypeFilter<CtField>(CtField.class));
        for (CtField ctField : fields) {
            fieldAccessFilterTacos.getElements(new FieldAccessFilter(ctField.getReference()));
        }
    } catch (NullPointerException e) {
        fail("FieldAccessFilter must not throw a NPE.");
    }
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) CtFieldReference(spoon.reflect.reference.CtFieldReference) Factory(spoon.reflect.factory.Factory) FieldAccessFilterTacos(spoon.test.filters.testclasses.FieldAccessFilterTacos) CtField(spoon.reflect.declaration.CtField) CtNamedElement(spoon.reflect.declaration.CtNamedElement) FieldAccessFilter(spoon.reflect.visitor.filter.FieldAccessFilter) Test(org.junit.Test)

Example 2 with CtFieldAccess

use of spoon.reflect.code.CtFieldAccess in project spoon by INRIA.

the class FieldAccessTest method testFieldAccessInLambda.

@Test
public void testFieldAccessInLambda() throws Exception {
    Factory build = null;
    try {
        build = build(MyClass.class);
    } catch (NullPointerException ignore) {
        fail();
    }
    final CtFieldAccess logFieldAccess = Query.getElements(build, new TypeFilter<>(CtFieldAccess.class)).get(0);
    assertEquals(Logger.class, logFieldAccess.getType().getActualClass());
    assertEquals("LOG", logFieldAccess.getVariable().getSimpleName());
    assertEquals(MyClass.class, logFieldAccess.getVariable().getDeclaringType().getActualClass());
    String expectedLambda = "() -> {" + System.lineSeparator() + "    spoon.test.fieldaccesses.MyClass.LOG.info(\"bla\");" + System.lineSeparator() + "}";
    assertEquals(expectedLambda, logFieldAccess.getParent(CtLambda.class).toString());
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Example 3 with CtFieldAccess

use of spoon.reflect.code.CtFieldAccess in project spoon by INRIA.

the class TargetedExpressionTest method testTargetOfFieldAccess.

@Test
public void testTargetOfFieldAccess() throws Exception {
    Factory factory = build(Foo.class, Bar.class, SuperClass.class);
    final CtClass<Object> type = factory.Class().get(Foo.class);
    CtConstructor<?> constructor = type.getConstructors().toArray(new CtConstructor<?>[0])[0];
    final List<CtFieldAccess<?>> elements = constructor.getElements(new TypeFilter<CtFieldAccess<?>>(CtFieldAccess.class));
    assertEquals(2, elements.size());
    assertEquals("Target is CtThisAccessImpl if there is a 'this' explicit.", CtThisAccessImpl.class, elements.get(0).getTarget().getClass());
    assertNotNull("Target isn't null if there is a 'this' explicit.", elements.get(1).getTarget());
    assertTrue(elements.get(1).getTarget().isImplicit());
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) Factory(spoon.reflect.factory.Factory) CtConstructor(spoon.reflect.declaration.CtConstructor) Test(org.junit.Test)

Example 4 with CtFieldAccess

use of spoon.reflect.code.CtFieldAccess in project spoon by INRIA.

the class TargetedExpressionTest method testStaticTargetsOfFieldAccessNoClasspath.

@Test
public void testStaticTargetsOfFieldAccessNoClasspath() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    launcher.addInputResource("./src/test/resources/spoon/test/noclasspath/targeted/Foo.java");
    launcher.setSourceOutputDirectory("./target/noclasspath");
    launcher.run();
    final CtTypeReference<Object> expectedFoo = launcher.getFactory().Class().createReference("Foo");
    final CtTypeReference<Object> expectedBar = launcher.getFactory().Class().createReference("Bar");
    final CtTypeReference<Object> expectedFiiFuu = launcher.getFactory().Class().create("Fii.Fuu").getReference();
    final CtThisAccess<Object> expectedThisAccess = launcher.getFactory().Code().createThisAccess(expectedFoo);
    final CtTypeAccess<Object> expectedTypeAccess = launcher.getFactory().Code().createTypeAccess(expectedFoo);
    final CtTypeAccess<Object> expectedBarTypeAccess = launcher.getFactory().Code().createTypeAccess(expectedBar);
    final CtMethod<?> fieldMethod = launcher.getFactory().Class().get("Foo").getMethodsByName("field").get(0);
    final List<CtFieldAccess<?>> elements = fieldMethod.getElements(new TypeFilter<>(CtFieldAccess.class));
    assertEquals(10, elements.size());
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedFoo).target(CtConstructorCallImpl.class).result("new Foo().i"), elements.get(0));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedFoo).target(elements.get(2)).result("foo.i"), elements.get(1));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedFoo).target(expectedThisAccess).result("foo"), elements.get(2));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedFoo).target(expectedThisAccess).result("this.i"), elements.get(3));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedFoo).target(expectedThisAccess).result("foo"), elements.get(4));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedFoo).target(expectedTypeAccess.toString()).result("Foo.staticField"), elements.get(5));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().result("staticField"), elements.get(6));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedBar).target(expectedBarTypeAccess).result("Bar.staticFieldBar"), elements.get(7));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedBar).target(expectedBarTypeAccess).result("Bar.staticFieldBar"), elements.get(8));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedFiiFuu).target(launcher.getFactory().Code().createTypeAccess(expectedFiiFuu)).result("Fii.Fuu.i"), elements.get(9));
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 5 with CtFieldAccess

use of spoon.reflect.code.CtFieldAccess in project spoon by INRIA.

the class TargetedExpressionTest method testTargetsOfFieldInAnonymousClass.

@Test
public void testTargetsOfFieldInAnonymousClass() throws Exception {
    final Factory factory = build(Foo.class, Bar.class, SuperClass.class);
    final CtClass<Foo> type = factory.Class().get(Foo.class);
    final CtTypeReference<Foo> expectedType = type.getReference();
    final CtClass<?> anonymousClass = type.getElements(new TypeFilter<CtClass>(CtClass.class) {

        @Override
        public boolean matches(CtClass element) {
            return element.isAnonymous() && super.matches(element);
        }
    }).get(0);
    final CtTypeReference<?> expectedAnonymousType = anonymousClass.getReference();
    final CtThisAccess<Foo> expectedThisAccess = factory.Code().createThisAccess(expectedType);
    final CtThisAccess expectedAnonymousThisAccess = factory.Code().createThisAccess(expectedAnonymousType);
    final CtMethod<?> method = anonymousClass.getMethodsByName("invStatic").get(0);
    final List<CtFieldAccess> elements = method.getElements(new TypeFilter<>(CtFieldAccess.class));
    assertEquals(3, elements.size());
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedType).target(expectedThisAccess).result("this.i"), elements.get(0));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedAnonymousType).target(expectedAnonymousThisAccess).result("this.i"), elements.get(1));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedAnonymousType).target(expectedAnonymousThisAccess).result("i"), elements.get(2));
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) Foo(spoon.test.targeted.testclasses.Foo) Factory(spoon.reflect.factory.Factory) CtThisAccess(spoon.reflect.code.CtThisAccess) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtClass(spoon.reflect.declaration.CtClass) Test(org.junit.Test)

Aggregations

CtFieldAccess (spoon.reflect.code.CtFieldAccess)22 Test (org.junit.Test)17 Factory (spoon.reflect.factory.Factory)11 Launcher (spoon.Launcher)7 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)7 CtInvocation (spoon.reflect.code.CtInvocation)4 CtFieldReference (spoon.reflect.reference.CtFieldReference)4 Foo (spoon.test.targeted.testclasses.Foo)4 CtTypeReference (spoon.reflect.reference.CtTypeReference)3 List (java.util.List)2 CtAssignment (spoon.reflect.code.CtAssignment)2 CtExpression (spoon.reflect.code.CtExpression)2 CtFieldWrite (spoon.reflect.code.CtFieldWrite)2 CtLocalVariable (spoon.reflect.code.CtLocalVariable)2 CtThisAccess (spoon.reflect.code.CtThisAccess)2 CtElement (spoon.reflect.declaration.CtElement)2 CtField (spoon.reflect.declaration.CtField)2 CtMethod (spoon.reflect.declaration.CtMethod)2 CtNamedElement (spoon.reflect.declaration.CtNamedElement)2 CtParameter (spoon.reflect.declaration.CtParameter)2