use of spoon.reflect.code.CtFieldAccess in project spoon by INRIA.
the class LambdaTest method testFieldAccessInLambdaNoClassPathExternal1Example.
@Test
public void testFieldAccessInLambdaNoClassPathExternal1Example() {
final Launcher runLaunch = new Launcher();
runLaunch.getEnvironment().setNoClasspath(true);
runLaunch.addInputResource("./src/test/resources/noclasspath/lambdas/external1");
runLaunch.buildModel();
assertEquals(3, runLaunch.getModel().getElements(new Filter<CtFieldAccess>() {
@Override
public boolean matches(final CtFieldAccess element) {
return element.getVariable().getSimpleName().equals("DEFAULT_RATING");
}
}).size());
}
use of spoon.reflect.code.CtFieldAccess in project spoon by INRIA.
the class VisibilityTest method testFullyQualifiedNameOfTypeReferenceWithGeneric.
@Test
public void testFullyQualifiedNameOfTypeReferenceWithGeneric() throws Exception {
// contract: Generics are written when there are specified in the return type of a method.
final String target = "./target/spooned/spoon/test/visibility_generics/testclasses/";
final SpoonAPI launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/visibility/testclasses/A.java");
launcher.addInputResource("./src/test/java/spoon/test/visibility/testclasses/A2.java");
launcher.addInputResource("./src/test/java/spoon/test/visibility/testclasses/Foo.java");
launcher.setSourceOutputDirectory(target);
launcher.run();
final CtClass<A> aClass = launcher.getFactory().Class().get(A.class);
CtType<?> nestedB = aClass.getNestedType("B");
List<CtFieldAccess> elements = nestedB.getElements(new TypeFilter<>(CtFieldAccess.class));
assertEquals(1, elements.size());
assertEquals("(spoon.test.visibility.testclasses.A.B.i)", elements.get(0).toString());
CtMethod<?> instanceOf = aClass.getMethodsByName("instanceOf").get(0);
List<CtBinaryOperator> elements1 = instanceOf.getElements(new TypeFilter<>(CtBinaryOperator.class));
assertEquals(1, elements1.size());
assertEquals("spoon.test.visibility.testclasses.A.B", elements1.get(0).getRightHandOperand().toString());
CtMethod<?> returnType = aClass.getMethodsByName("returnType").get(0);
assertEquals("spoon.test.visibility.testclasses.A<T>.C<T>", returnType.getType().toString());
final CtClass<A2> secondClass = launcher.getFactory().Class().get(A2.class);
nestedB = secondClass.getNestedType("B");
elements = nestedB.getElements(new TypeFilter<>(CtFieldAccess.class));
assertEquals(1, elements.size());
assertEquals("(spoon.test.visibility.testclasses.A2.B.i)", elements.get(0).toString());
instanceOf = secondClass.getMethodsByName("instanceOf").get(0);
elements1 = instanceOf.getElements(new TypeFilter<>(CtBinaryOperator.class));
assertEquals(1, elements1.size());
assertEquals("spoon.test.visibility.testclasses.A2.B", elements1.get(0).getRightHandOperand().toString());
returnType = secondClass.getMethodsByName("returnType").get(0);
assertEquals("spoon.test.visibility.testclasses.A2.C<java.lang.String>", returnType.getType().toString());
returnType = secondClass.getMethodsByName("returnType2").get(0);
assertEquals("spoon.test.visibility.testclasses.Foo<java.lang.String>.Bar<java.lang.String>", returnType.getType().toString());
canBeBuilt(target, 8);
}
use of spoon.reflect.code.CtFieldAccess in project spoon by INRIA.
the class DefaultJavaPrettyPrinter method visitCtCase.
@Override
@SuppressWarnings("rawtypes")
public <E> void visitCtCase(CtCase<E> caseStatement) {
enterCtStatement(caseStatement);
if (caseStatement.getCaseExpression() != null) {
printer.writeKeyword("case").writeSpace();
// writing enum case expression
if (caseStatement.getCaseExpression() instanceof CtFieldAccess) {
final CtFieldReference variable = ((CtFieldAccess) caseStatement.getCaseExpression()).getVariable();
// In noclasspath mode, we don't have always the type of the declaring type.
if (variable.getType() != null && variable.getDeclaringType() != null && variable.getType().getQualifiedName().equals(variable.getDeclaringType().getQualifiedName())) {
printer.writeIdentifier(variable.getSimpleName());
} else {
scan(caseStatement.getCaseExpression());
}
} else {
scan(caseStatement.getCaseExpression());
}
} else {
printer.writeKeyword("default");
}
printer.writeSpace().writeSeparator(":").incTab();
for (CtStatement statement : caseStatement.getStatements()) {
printer.writeln();
elementPrinterHelper.writeStatement(statement);
}
printer.decTab();
}
use of spoon.reflect.code.CtFieldAccess in project spoon by INRIA.
the class FieldAccessTest method testFieldAccessNoClasspath.
@Test
public void testFieldAccessNoClasspath() throws Exception {
Launcher launcher = new Launcher();
launcher.addInputResource("src/test/resources/import-resources/fr/inria/");
launcher.getEnvironment().setNoClasspath(true);
launcher.run();
CtType<?> ctType = launcher.getFactory().Class().get("FooNoClassPath");
CtFieldAccess ctFieldAccess = ctType.getElements(new TypeFilter<>(CtFieldAccess.class)).get(0);
assertEquals("(game.board.width)", ctFieldAccess.toString());
CtFieldReference ctFieldReferenceWith = ctFieldAccess.getVariable();
assertEquals("width", ctFieldReferenceWith.getSimpleName());
CtFieldAccess ctFieldAccessBoard = (CtFieldAccess) ctFieldAccess.getTarget();
assertEquals("game.board", ctFieldAccessBoard.toString());
CtFieldReference ctFieldReferenceBoard = ctFieldAccessBoard.getVariable();
assertEquals("board", ctFieldReferenceBoard.getSimpleName());
CtFieldAccess ctFieldAccessGame = (CtFieldAccess) ctFieldAccessBoard.getTarget();
assertEquals("game.board", ctFieldAccessBoard.toString());
CtFieldReference ctFieldReferenceGame = ctFieldAccessGame.getVariable();
assertEquals("game", ctFieldReferenceGame.getSimpleName());
}
use of spoon.reflect.code.CtFieldAccess in project spoon by INRIA.
the class NoClasspathTest method test.
@Test
public void test() throws Exception {
// do we still have a correct model when the complete classpath is not given as input?
Launcher spoon = new Launcher();
spoon.getEnvironment().setNoClasspath(true);
spoon.getEnvironment().setLevel("OFF");
spoon.addInputResource("./src/test/resources/spoon/test/noclasspath/fields");
spoon.getEnvironment().setSourceOutputDirectory(new File("target/spooned/apitest"));
spoon.run();
Factory factory = spoon.getFactory();
CtClass<Object> clazz = factory.Class().get("Foo");
assertEquals("Foo", clazz.getSimpleName());
CtTypeReference<?> superclass = clazz.getSuperclass();
// "Unknown" is not in the classpath at all
assertEquals("Unknown", superclass.getSimpleName());
try {
superclass.getActualClass();
fail();
} catch (SpoonClassNotFoundException e) {
// expected
}
assertNull(superclass.getDeclaration());
// should be empty as in noClasspath the actual class cannot be retrieved
assertTrue(superclass.getAllFields().isEmpty());
// now we really make sure we don't have the class in the classpath
try {
superclass.getActualClass();
fail();
} catch (SpoonClassNotFoundException e) {
// expected
}
{
CtMethod<?> method = clazz.getMethod("method", new CtTypeReference[0]);
assertNotNull(method);
List<CtInvocation<?>> invocations = method.getElements(new TypeFilter<CtInvocation<?>>(CtInvocation.class));
assertEquals(1, invocations.size());
CtInvocation<?> c = invocations.get(0);
assertEquals("method", c.getExecutable().getSimpleName());
assertEquals("x.method()", method.getBody().getStatement(1).toString());
}
{
CtMethod<?> method = clazz.getMethod("m2", new CtTypeReference[0]);
assertNotNull(method);
List<CtInvocation<?>> invocations = method.getElements(new TypeFilter<CtInvocation<?>>(CtInvocation.class));
assertEquals(3, invocations.size());
CtInvocation<?> c = invocations.get(1);
assertEquals("second", c.getExecutable().getSimpleName());
assertEquals("x.first().second().third()", method.getBody().getStatement(1).toString());
}
{
CtMethod<?> method = clazz.getMethod("m1", new CtTypeReference[0]);
assertNotNull(method);
List<CtInvocation<?>> invocations = method.getElements(new TypeFilter<CtInvocation<?>>(CtInvocation.class));
assertEquals(1, invocations.size());
invocations.get(0);
assertEquals("x.y.z.method()", method.getBody().getStatement(0).toString());
}
{
CtMethod<?> method = clazz.getMethod("m3", new CtTypeReference[0]);
assertNotNull(method);
List<CtInvocation<?>> invocations = method.getElements(new TypeFilter<CtInvocation<?>>(CtInvocation.class));
assertEquals(1, invocations.size());
invocations.get(0);
CtLocalVariable<?> statement = method.getBody().getStatement(0);
CtFieldAccess<?> fa = (CtFieldAccess<?>) statement.getDefaultExpression();
assertTrue(fa.getTarget() instanceof CtInvocation);
assertEquals("field", fa.getVariable().getSimpleName());
assertEquals("int x = first().field", statement.toString());
}
}
Aggregations