Search in sources :

Example 6 with CtTypeAccess

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);
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CtTypeAccess(spoon.reflect.code.CtTypeAccess) SpoonAPI(spoon.SpoonAPI) Test(org.junit.Test)

Example 7 with 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());
}
Also used : Launcher(spoon.Launcher) CtTypeAccess(spoon.reflect.code.CtTypeAccess) Test(org.junit.Test)

Example 8 with CtTypeAccess

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);
}
Also used : Pozole(spoon.test.type.testclasses.Pozole) CtBinaryOperator(spoon.reflect.code.CtBinaryOperator) Launcher(spoon.Launcher) CtTypeAccess(spoon.reflect.code.CtTypeAccess) Test(org.junit.Test)

Example 9 with CtTypeAccess

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);
}
Also used : CtFieldRead(spoon.reflect.code.CtFieldRead) Pozole(spoon.test.type.testclasses.Pozole) Launcher(spoon.Launcher) CtTypeAccess(spoon.reflect.code.CtTypeAccess) Test(org.junit.Test)

Example 10 with CtTypeAccess

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);
    }
}
Also used : CtType(spoon.reflect.declaration.CtType) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtTypeAccess(spoon.reflect.code.CtTypeAccess)

Aggregations

CtTypeAccess (spoon.reflect.code.CtTypeAccess)14 Test (org.junit.Test)6 Launcher (spoon.Launcher)5 CtTypeReference (spoon.reflect.reference.CtTypeReference)5 CtExpression (spoon.reflect.code.CtExpression)3 CtType (spoon.reflect.declaration.CtType)3 Factory (spoon.reflect.factory.Factory)3 Writable (spoon.reflect.visitor.PrintingContext.Writable)3 Pozole (spoon.test.type.testclasses.Pozole)3 Collection (java.util.Collection)2 CtBinaryOperator (spoon.reflect.code.CtBinaryOperator)2 CtFieldRead (spoon.reflect.code.CtFieldRead)2 CtNewArray (spoon.reflect.code.CtNewArray)2 CtFieldReference (spoon.reflect.reference.CtFieldReference)2 AmplificationHelper (fr.inria.diversify.utils.AmplificationHelper)1 Field (java.lang.reflect.Field)1 Collections (java.util.Collections)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 ArrayAllocationExpression (org.eclipse.jdt.internal.compiler.ast.ArrayAllocationExpression)1