JavaParser extracting annotation parameter value

694 views Asked by At

I'm working on a project to parse jersey based annotations to build an request interaction graph between projects from java source code.

It's easy to parse and get info when everythink like that

@GET    
@Path("/parameter/{param}")
public Response getParam(@PathParam("param") String prm) {    

    String output = "Jersey say : " + prm;    
    return Response.status(200).entity(output).build(); 

}

I can extract values like {Method:"GET", Path:"parameter/{param}"}

Other case are

private static final PARAMETER_PATH="/parameter/{param}";

@GET    
@Path(PARAMETER_PATH)
public Response getParam(@PathParam("param") String prm) {    

    String output = "Jersey say : " + prm;    
    return Response.status(200).entity(output).build(); 

}

or

private static String PATH_VARIABLE_STR = "{param}";
private static PARAMETER_PATH = "/parameter/"+PATH_VARIABLE_STR;

    @GET    
    @Path(PARAMETER_PATH)
    public Response getParam(@PathParam("param") String prm) {    

        String output = "Jersey say : " + prm;    
        return Response.status(200).entity(output).build(); 

    }

In last two cases, I could not extract the real value.

How can I get the value of the variable while I'm working on static source code with javaparser, ast or etc.

I'm waiting for your suggestions. Thanks.

1

There are 1 answers

1
Federico Tomassetti On

The AST produced by JavaParser or other parsers does not calculate values. If you have 3 + 5 in your code, this is an addition, a parser will not have the logic to interpret it and conclude that that is equivalent to 8. You would need to implement that logic yourself. You want to basically create an interpreter for Java. I would say you can easily do that for a couple of basic operations: consider literals, the basic 4 mathematical operations and not much more. Of course if you try to consider the whole Java language that thing would spiral out of control very quickly.

Disclaimer: I am a JavaParser contributor