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