use of spoon.reflect.code.CtTypeAccess in project spoon by INRIA.
the class InvocationTest method testTypeOfStaticInvocation.
@Test
public void testTypeOfStaticInvocation() throws Exception {
SpoonAPI launcher = new Launcher();
launcher.run(new String[] { "-i", "./src/test/java/spoon/test/invocations/testclasses/", "-o", "./target/spooned/" });
Factory factory = launcher.getFactory();
CtClass<?> aClass = factory.Class().get(Foo.class);
final List<CtInvocation<?>> elements = aClass.getElements(new AbstractFilter<CtInvocation<?>>(CtInvocation.class) {
@Override
public boolean matches(CtInvocation<?> element) {
return element.getTarget() != null;
}
});
assertEquals(2, elements.size());
assertTrue(elements.get(0).getTarget() instanceof CtTypeAccess);
assertTrue(elements.get(1).getTarget() instanceof CtTypeAccess);
}
use of spoon.reflect.code.CtTypeAccess in project spoon by INRIA.
the class LambdaTest method testTypeAccessInLambdaNoClassPath.
@Test
public void testTypeAccessInLambdaNoClassPath() {
final Launcher runLaunch = new Launcher();
runLaunch.getEnvironment().setNoClasspath(true);
runLaunch.addInputResource("./src/test/resources/noclasspath/lambdas/TypeAccessInLambda.java");
runLaunch.buildModel();
assertEquals("The token 'Strings' has not been parsed as CtTypeAccess", 1, runLaunch.getModel().getElements(new Filter<CtTypeAccess>() {
@Override
public boolean matches(final CtTypeAccess element) {
return element.getAccessedType().getSimpleName().equals("Strings");
}
}).size());
}
use of spoon.reflect.code.CtTypeAccess in project spoon by INRIA.
the class TypeTest method testTypeAccessOfArrayObjectInFullyQualifiedName.
@Test
public void testTypeAccessOfArrayObjectInFullyQualifiedName() throws Exception {
// contract: A type access in fully qualified name must to rewrite well.
final String target = "./target/type";
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/type/testclasses");
launcher.setSourceOutputDirectory(target);
launcher.getEnvironment().setNoClasspath(true);
launcher.run();
final CtClass<Pozole> aPozole = launcher.getFactory().Class().get(Pozole.class);
final CtMethod<?> season = aPozole.getMethodsByName("season").get(0);
final List<CtTypeAccess<?>> typeAccesses = season.getElements(new TypeFilter<CtTypeAccess<?>>(CtTypeAccess.class));
assertEquals(2, typeAccesses.size());
assertTrue(typeAccesses.get(0).getParent() instanceof CtBinaryOperator);
assertEquals(BinaryOperatorKind.INSTANCEOF, ((CtBinaryOperator) typeAccesses.get(0).getParent()).getKind());
assertEquals("a instanceof java.lang.@spoon.test.annotation.testclasses.TypeAnnotation(integer = 1)" + System.lineSeparator() + "Object[]", typeAccesses.get(0).getParent().toString());
assertTrue(typeAccesses.get(1).getParent() instanceof CtBinaryOperator);
assertEquals(BinaryOperatorKind.INSTANCEOF, ((CtBinaryOperator) typeAccesses.get(1).getParent()).getKind());
assertEquals("a instanceof java.lang.Object[]", typeAccesses.get(1).getParent().toString());
canBeBuilt(target, 8, true);
}
use of spoon.reflect.code.CtTypeAccess in project spoon by INRIA.
the class TypeTest method testTypeAccessForDotClass.
@Test
public void testTypeAccessForDotClass() throws Exception {
// contract: When we use .class on a type, this must be a CtTypeAccess.
final String target = "./target/type";
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/type/testclasses");
launcher.setSourceOutputDirectory(target);
launcher.getEnvironment().setNoClasspath(true);
launcher.run();
final CtClass<Pozole> aPozole = launcher.getFactory().Class().get(Pozole.class);
final CtMethod<?> make = aPozole.getMethodsByName("make").get(0);
final List<CtFieldRead<?>> fieldClasses = make.getElements(new TypeFilter<CtFieldRead<?>>(CtFieldRead.class) {
@Override
public boolean matches(CtFieldRead<?> element) {
return "class".equals(element.getVariable().getSimpleName()) && super.matches(element);
}
});
assertEquals(4, fieldClasses.size());
for (CtFieldRead<?> fieldClass : fieldClasses) {
assertTrue(fieldClass.getTarget() instanceof CtTypeAccess);
}
canBeBuilt(target, 8, true);
}
use of spoon.reflect.code.CtTypeAccess in project spoon by INRIA.
the class DefaultJavaPrettyPrinter method visitCtThisAccess.
@Override
public <T> void visitCtThisAccess(CtThisAccess<T> thisAccess) {
try {
enterCtExpression(thisAccess);
// we only write qualified this when this is required
// this is good both in fully-qualified mode and in readable (with-imports) mode
// the implicit information is used for analysis (eg are visibility caused by implicit bugs?) but
// not for pretty-printing
CtTypeAccess target = (CtTypeAccess) thisAccess.getTarget();
CtTypeReference targetType = target.getAccessedType();
// readable mode as close as possible to the original code
if (thisAccess.isImplicit()) {
// what the good target is in JDTTreeBuilder
return;
}
// this is shorter (no qualified this), explicit, and less fragile wrt transformation
if (targetType == null || (thisAccess.getParent(CtType.class) != null && thisAccess.getParent(CtType.class).isTopLevel())) {
printer.writeKeyword("this");
// still go through finally block below
return;
}
// this has to come after the implicit test just before
if (targetType.isAnonymous()) {
printer.writeKeyword("this");
return;
}
// complex case of qualifed this
if (!context.currentThis.isEmpty()) {
CtType lastType = context.currentThis.peekFirst().type;
String lastTypeQualifiedName = lastType.getQualifiedName();
String targetTypeQualifiedName = targetType.getQualifiedName();
if (!lastTypeQualifiedName.equals(targetTypeQualifiedName)) {
if (!targetType.isImplicit()) {
visitCtTypeReferenceWithoutGenerics(targetType);
printer.writeSeparator(".");
}
printer.writeKeyword("this");
return;
}
}
// the default super simple case only comes at the end
printer.writeKeyword("this");
} finally {
exitCtExpression(thisAccess);
}
}
Aggregations