Search in sources :

Example 1 with VisitorPartialEvaluator

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());
}
Also used : InlinePartialEvaluator(spoon.support.reflect.eval.InlinePartialEvaluator) Launcher(spoon.Launcher) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtIf(spoon.reflect.code.CtIf) VisitorPartialEvaluator(spoon.support.reflect.eval.VisitorPartialEvaluator) Test(org.junit.Test)

Example 2 with VisitorPartialEvaluator

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);
}
Also used : A(spoon.test.field.testclasses.A) CtFieldRead(spoon.reflect.code.CtFieldRead) VisitorPartialEvaluator(spoon.support.reflect.eval.VisitorPartialEvaluator) BaseClass(spoon.test.field.testclasses.BaseClass) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 Launcher (spoon.Launcher)2 VisitorPartialEvaluator (spoon.support.reflect.eval.VisitorPartialEvaluator)2 CtFieldRead (spoon.reflect.code.CtFieldRead)1 CtIf (spoon.reflect.code.CtIf)1 CtLocalVariable (spoon.reflect.code.CtLocalVariable)1 CtMethod (spoon.reflect.declaration.CtMethod)1 InlinePartialEvaluator (spoon.support.reflect.eval.InlinePartialEvaluator)1 A (spoon.test.field.testclasses.A)1 BaseClass (spoon.test.field.testclasses.BaseClass)1