0

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.

Tugrul
  • 1,760
  • 4
  • 24
  • 39

1 Answers1

2

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

Federico Tomassetti
  • 2,100
  • 1
  • 19
  • 26
  • Thanks Federico. I'm working on VariableDeclarator to extract variable name and value and add it to list with package name. At least I search the variable into the list with comparing package and names to get value. You said that there is no easy way to do this with parser. Ok if I need some logical operations I have to implement basic interpreter. Thanks a lot again. – Tugrul Dec 22 '16 at 09:24