Search in sources :

Example 16 with CtLocalVariable

use of spoon.reflect.code.CtLocalVariable in project spoon by INRIA.

the class VariableAccessTest method testReferencesInInitExpression.

@Test
public void testReferencesInInitExpression() throws Exception {
    /* test getReference on local variable
		*  getReference().getDeclaration() must be circular
		*/
    final CtType<Tortillas> aTortillas = buildClass(Tortillas.class);
    final CtMethod<Object> make = aTortillas.getMethod("make", aTortillas.getFactory().Type().stringType());
    final CtLocalVariable localVarNumber = make.getBody().getStatement(1);
    List<CtLocalVariableReference<?>> refs = localVarNumber.map(new LocalVariableReferenceFunction()).list();
    assertEquals(1, refs.size());
    assertSame(localVarNumber, refs.get(0).getParent(CtLocalVariable.class));
}
Also used : CtLocalVariableReference(spoon.reflect.reference.CtLocalVariableReference) Tortillas(spoon.test.reference.testclasses.Tortillas) CtLocalVariable(spoon.reflect.code.CtLocalVariable) LocalVariableReferenceFunction(spoon.reflect.visitor.filter.LocalVariableReferenceFunction) Test(org.junit.Test)

Example 17 with CtLocalVariable

use of spoon.reflect.code.CtLocalVariable in project spoon by INRIA.

the class ReplaceTest method testReplaceReplace.

@Test
public void testReplaceReplace() throws Exception {
    // bug found by Benoit
    CtClass<?> foo = factory.Package().get("spoon.test.replace.testclasses").getType("Foo");
    CtMethod<?> fooMethod = foo.getElements(new NamedElementFilter<>(CtMethod.class, "foo")).get(0);
    assertEquals("foo", fooMethod.getSimpleName());
    CtMethod<?> barMethod = foo.getElements(new NamedElementFilter<>(CtMethod.class, "bar")).get(0);
    assertEquals("bar", barMethod.getSimpleName());
    CtLocalVariable<?> assignment = (CtLocalVariable<?>) fooMethod.getBody().getStatements().get(0);
    CtLocalVariable<?> newAssignment = barMethod.getBody().getStatement(0);
    assignment.replace(newAssignment);
    assertEquals(fooMethod.getBody(), newAssignment.getParent());
    CtLiteral<Integer> lit = (CtLiteral<Integer>) foo.getElements(new TypeFilter<CtLiteral<?>>(CtLiteral.class)).get(0);
    final CtElement parent = lit.getParent();
    CtLiteral<Integer> newLit = factory.Code().createLiteral(0);
    lit.replace(newLit);
    assertEquals("int y = 0", fooMethod.getBody().getStatement(0).toString());
    assertEquals(parent, newLit.getParent());
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) CtElement(spoon.reflect.declaration.CtElement) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) CtLocalVariable(spoon.reflect.code.CtLocalVariable) Test(org.junit.Test)

Example 18 with CtLocalVariable

use of spoon.reflect.code.CtLocalVariable in project spoon by INRIA.

the class VariableReferencesTest method testVariableScopeFunction.

@Test
public void testVariableScopeFunction() throws Exception {
    // visits all the CtVariable elements whose name is "field" and search for all elements in their scopes
    // Comparing with the result found by basic functions
    List list = modelClass.filterChildren((CtVariable<?> var) -> {
        if (var.getSimpleName().equals("field")) {
            if (var instanceof CtField) {
                // field scope is not supported
                return false;
            }
            CtElement[] real = var.map(new VariableScopeFunction()).list().toArray(new CtElement[0]);
            if (var instanceof CtLocalVariable) {
                assertArrayEquals(var.map(new LocalVariableScopeFunction()).list().toArray(new CtElement[0]), real);
            } else if (var instanceof CtField) {
            // assertArrayEquals(var.map(new FieldScopeFunction()).list().toArray(new CtElement[0]), real);
            } else if (var instanceof CtParameter) {
                assertArrayEquals(var.map(new ParameterScopeFunction()).list().toArray(new CtElement[0]), real);
            } else if (var instanceof CtCatchVariable) {
                assertArrayEquals(var.map(new CatchVariableScopeFunction()).list().toArray(new CtElement[0]), real);
            } else {
                fail("Unexpected variable of type " + var.getClass().getName());
            }
            return true;
        }
        return false;
    }).list();
    assertTrue(list.size() > 0);
}
Also used : CatchVariableScopeFunction(spoon.reflect.visitor.filter.CatchVariableScopeFunction) LocalVariableScopeFunction(spoon.reflect.visitor.filter.LocalVariableScopeFunction) VariableScopeFunction(spoon.reflect.visitor.filter.VariableScopeFunction) ParameterScopeFunction(spoon.reflect.visitor.filter.ParameterScopeFunction) CatchVariableScopeFunction(spoon.reflect.visitor.filter.CatchVariableScopeFunction) CtField(spoon.reflect.declaration.CtField) CtElement(spoon.reflect.declaration.CtElement) List(java.util.List) CtParameter(spoon.reflect.declaration.CtParameter) CtCatchVariable(spoon.reflect.code.CtCatchVariable) CtLocalVariable(spoon.reflect.code.CtLocalVariable) LocalVariableScopeFunction(spoon.reflect.visitor.filter.LocalVariableScopeFunction) VariableReferencesModelTest(spoon.test.query_function.testclasses.VariableReferencesModelTest) Test(org.junit.Test)

Example 19 with CtLocalVariable

use of spoon.reflect.code.CtLocalVariable in project spoon by INRIA.

the class VariableReferencesTest method getLiteralValue.

private Integer getLiteralValue(CtVariable<?> var) {
    CtExpression<?> exp = var.getDefaultExpression();
    if (exp != null) {
        try {
            return getLiteralValue(exp);
        } catch (ClassCastException e) {
        }
    }
    if (var instanceof CtParameter) {
        CtParameter param = (CtParameter) var;
        CtExecutable<?> l_exec = param.getParent(CtExecutable.class);
        int l_argIdx = l_exec.getParameters().indexOf(param);
        assertTrue(l_argIdx >= 0);
        if (l_exec instanceof CtLambda) {
            CtLambda<?> lambda = (CtLambda<?>) l_exec;
            CtLocalVariable<?> lamVar = (CtLocalVariable) lambda.getParent();
            CtLocalVariableReference<?> lamVarRef = lamVar.getParent().filterChildren((CtLocalVariableReference ref) -> ref.getSimpleName().equals(lamVar.getSimpleName())).first();
            CtAbstractInvocation inv = lamVarRef.getParent(CtAbstractInvocation.class);
            return getLiteralValue((CtExpression<?>) inv.getArguments().get(l_argIdx));
        } else {
            CtExecutableReference<?> l_execRef = l_exec.getReference();
            List<CtAbstractInvocation<?>> list = l_exec.getFactory().Package().getRootPackage().filterChildren((CtAbstractInvocation inv) -> {
                // return inv.getExecutable().equals(l_execRef);
                return inv.getExecutable().getExecutableDeclaration() == l_exec;
            }).list();
            CtAbstractInvocation inv = list.get(0);
            Integer firstValue = getLiteralValue((CtExpression<?>) inv.getArguments().get(l_argIdx));
            // check that all found method invocations are using same key
            list.forEach(inv2 -> {
                assertEquals(firstValue, getLiteralValue((CtExpression<?>) inv2.getArguments().get(l_argIdx)));
            });
            return firstValue;
        }
    }
    return getCommentValue(var);
}
Also used : CtLambda(spoon.reflect.code.CtLambda) CtExpression(spoon.reflect.code.CtExpression) CtAbstractInvocation(spoon.reflect.code.CtAbstractInvocation) CtParameter(spoon.reflect.declaration.CtParameter) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtLocalVariableReference(spoon.reflect.reference.CtLocalVariableReference)

Example 20 with CtLocalVariable

use of spoon.reflect.code.CtLocalVariable 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

CtLocalVariable (spoon.reflect.code.CtLocalVariable)29 Test (org.junit.Test)21 Factory (spoon.reflect.factory.Factory)15 Launcher (spoon.Launcher)10 CtParameter (spoon.reflect.declaration.CtParameter)7 CtTypeReference (spoon.reflect.reference.CtTypeReference)7 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)7 CtStatement (spoon.reflect.code.CtStatement)6 CtClass (spoon.reflect.declaration.CtClass)6 CtMethod (spoon.reflect.declaration.CtMethod)6 CtElement (spoon.reflect.declaration.CtElement)5 CtLocalVariableReference (spoon.reflect.reference.CtLocalVariableReference)5 List (java.util.List)4 CtComment (spoon.reflect.code.CtComment)4 CtTry (spoon.reflect.code.CtTry)4 CtField (spoon.reflect.declaration.CtField)4 CtType (spoon.reflect.declaration.CtType)4 CtAssignment (spoon.reflect.code.CtAssignment)3 CtCatchVariable (spoon.reflect.code.CtCatchVariable)3 CtExpression (spoon.reflect.code.CtExpression)3