Search code examples
javaabstract-syntax-treeeclipse-jdt

JDT ASTParser - How to resolve constant value?


I have a visitor which should output caller type, its name, parameter type, corresponding type in method declaration and if it's a constant variable, its value

public boolean visit(MethodInvocation e)
{
   Expression methodExpr = e.getExpression();
   String caller = methodExpr != null ? methodExpr.resolveTypeBinding().getQualifiedName() : e.resolveTypeBinding().getQualifiedName();
   String methodName = e.getName().getFullyQualifiedName();

   System.out.println("Caller: " + caller);
    System.out.println("Method: " + methodName);

    List<Object> args = e.arguments();
    int num = 0;
    for(ITypeBinding o : e.resolveMethodBinding().getParameterTypes()/*e.arguments()*/)
    {
        String argType = o.getQualifiedName();
        Object arg = args.get(num++);
        System.out.println("\tArg type: " + argType + " / Arg value: " + arg + " / Resolved value: " + null);
    }
    System.out.println();
    return true;
}

And my current task is 1) how to identify constant; 2) get its value Has anybody any ideas? thanks


Solution

  • Constant value can be resolved by calling arg.resolveConstantExpressionValue()