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));
}
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());
}
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);
}
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);
}
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());
}
}
Aggregations