use of spoon.support.reflect.eval.VisitorPartialEvaluator in project spoon by INRIA.
the class EvalTest method testVisitorPartialEvaluatorScanner.
@Test
public void testVisitorPartialEvaluatorScanner() throws Exception {
Launcher launcher = new Launcher();
launcher.addInputResource("src/test/java/spoon/test/eval/Foo.java");
launcher.buildModel();
VisitorPartialEvaluator eval = new VisitorPartialEvaluator();
CtType<?> foo = launcher.getFactory().Type().get((Class<?>) Foo.class);
foo.accept(new InlinePartialEvaluator(eval));
assertEquals("false", foo.getElements(new TypeFilter<>(CtLocalVariable.class)).get(0).getDefaultExpression().toString());
// the if has been removed
assertEquals(0, foo.getElements(new TypeFilter<>(CtIf.class)).size());
}
use of spoon.support.reflect.eval.VisitorPartialEvaluator in project spoon by INRIA.
the class FieldTest method testGetDefaultExpression.
@Test
public void testGetDefaultExpression() throws Exception {
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/field/testclasses/A.java");
spoon.addInputResource("./src/test/java/spoon/test/field/testclasses/BaseClass.java");
spoon.buildModel();
final CtClass<A> aClass = spoon.getFactory().Class().get(A.class);
CtClass<A.ClassB> bClass = aClass.getFactory().Class().get(A.ClassB.class);
List<CtMethod<?>> methods = bClass.getMethodsByName("getKey");
assertEquals(1, methods.size());
CtReturn<?> returnExpression = methods.get(0).getBody().getStatement(0);
CtFieldRead fieldRead = (CtFieldRead) returnExpression.getReturnedExpression();
assertEquals("spoon.test.field.testclasses.BaseClass.PREFIX", fieldRead.toString());
CtField<?> field = fieldRead.getVariable().getDeclaration();
CtClass<BaseClass> baseClass = aClass.getFactory().Class().get(BaseClass.class);
CtField<?> expectedField = baseClass.getField("PREFIX");
assertEquals(expectedField, field);
VisitorPartialEvaluator visitorPartial = new VisitorPartialEvaluator();
Object retour = visitorPartial.evaluate(methods.get(0));
assertTrue(retour != null);
}
Aggregations