Javaparser: How to insert a statement to output the return value of each method calls using JavaParser?

Created on 19 Dec 2019  路  4Comments  路  Source: javaparser/javaparser

I hope to record all the return value of method calls whose return value is assigned to a variable.
For example:
before:
int x=Integer.parseInt(args[0]);

after:
int x=Integer.parseInt(args[0]);
System.out.println(x);

For those whose return value is not assigned to a variable, there is no need to record.
Is it possible with JavaParser?

Question (JP usage)

All 4 comments

If there isn't a direct solution, this problem may be disintegrated as:

  1. Is it possible to detect a StatementExpr which contains a MethodCallStat and a variable to hold the return value and identify the variable?
  2. Is it possible to add a statement after a StatementExpr?
  1. in the parser it is possible to find the assignment construct. Here are some ways to find out what too look for in the AST: https://javaparser.org/inspecting-an-ast/ . This way you can also find the method-call-that-is-not-assigned construct.
    BUT the parser doesn't tell you if that method is void or not. For that you will need to use the symbol solver.
  2. Yes it is. Statements are mostly contained in a NodeList, and that has a lot of utility members for these kinds of tasks.

Do read the book if you haven't. You can get it for free if you pay 0,00, and that's okay.

Thanks a lot for your recommendation but my schedule is too tight for me to carefully go over the book or the javadoc. Also, as I鈥檓 not a native English speaker, so I am not competent enough to manage it.
So could you please show me the exact way, or the method to be called, to insert a line statement after a specific statement (ExpressionStmt), instead of at the ending of a BlockStmt?

No, I do not like people who ask me to spend my holiday time on their project because they do not want to read the documentation.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

qingdujun picture qingdujun  路  4Comments

NicoN777 picture NicoN777  路  5Comments

matozoid picture matozoid  路  3Comments

nerzid picture nerzid  路  4Comments

sangram82 picture sangram82  路  3Comments