Search in sources :

Example 1 with BaseClass

use of spoon.test.field.testclasses.BaseClass 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)1 Launcher (spoon.Launcher)1 CtFieldRead (spoon.reflect.code.CtFieldRead)1 CtMethod (spoon.reflect.declaration.CtMethod)1 VisitorPartialEvaluator (spoon.support.reflect.eval.VisitorPartialEvaluator)1 A (spoon.test.field.testclasses.A)1 BaseClass (spoon.test.field.testclasses.BaseClass)1