Search in sources :

Example 11 with CtFieldAccess

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

Example 12 with CtFieldAccess

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);
}
Also used : A(spoon.test.visibility.testclasses.A) CtFieldAccess(spoon.reflect.code.CtFieldAccess) CtBinaryOperator(spoon.reflect.code.CtBinaryOperator) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) A2(spoon.test.visibility.testclasses.A2) Launcher(spoon.Launcher) SpoonAPI(spoon.SpoonAPI) Test(org.junit.Test)

Example 13 with CtFieldAccess

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();
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) CtStatement(spoon.reflect.code.CtStatement) CtFieldReference(spoon.reflect.reference.CtFieldReference)

Example 14 with CtFieldAccess

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());
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) Launcher(spoon.Launcher) CtFieldReference(spoon.reflect.reference.CtFieldReference) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Example 15 with CtFieldAccess

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());
    }
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtInvocation(spoon.reflect.code.CtInvocation) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) SpoonClassNotFoundException(spoon.support.SpoonClassNotFoundException) List(java.util.List) File(java.io.File) CtMethod(spoon.reflect.declaration.CtMethod) 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