-3

I need to insert a statement as

int listSize = list.size();

So, I tried inserting that statement as

Statement sampleStatement = JavaParser.statement ("int listSize = list.size();");

Java parser exception is thrown.

P.s: I'm using java parser core 2.4.0

  • 2
    what exeption? shouldn't that be 'JavaParser.statement("int listSize = list.size();"); ? – Stultuske Oct 10 '19 at 06:49
  • Welcome to SO. Please read [ask] and then elaborate: what exception do you get (stacktrace)? Where are you trying to insert that statement? Why are you using `JavaParser` in the first place? Assuming you mean `com.sun.tools.javac.parser.JavaParser` do you realize that it's commonly not advicable to directly use `com.sun.*` classes? – Thomas Oct 10 '19 at 06:49
  • 1
    I am almost certain that the exception's message (whatever it might be) is trying to tell you something. – f1sh Oct 10 '19 at 07:00
  • Exception in thread "main" Com.github.javaparser.ParseException: Encountered " "int" "int" at line 1,column1. was expecting one of: "False"... " new"... "null"... " super"... "this"... " true "... ... ... – Pikachu mon Oct 10 '19 at 07:59
  • That's because you provided a declaration, not a statement. Why keep the important part of your questIon secret? – user207421 Oct 10 '19 at 08:04
  • While parsing the file, i have to insert the statement int listSize=list.size(); – Pikachu mon Oct 10 '19 at 08:44
  • While parsing the file, i have to insert the statement int listSize=list.size(); How to make this as a statement? I'll be inserting the statement by declaration.getBody().getStmts().add(3,sampleStatement) . – Pikachu mon Oct 10 '19 at 08:56
  • Err, [use the correct API](https://static.javadoc.io/com.github.javaparser/javaparser-core/3.2.5/com/github/javaparser/JavaParser.html#parseVariableDeclarationExpr-java.lang.String-)? Look it up? You can't turn something that isn't a statement into a statement. You're asking the wrong question. – user207421 Oct 10 '19 at 09:08
  • Oh, then ,how insert this line int listSize=list.size(); in the file using java parser? – Pikachu mon Oct 10 '19 at 09:17

1 Answers1

2

I don't see any statement(..) method within JavaParser class from com.github.javaparser::javaparser-core maven artifact

Shouldn't your code be something like

Statement statement = JavaParser.parseStatement("listSize = list.size();"); ?

Sachin
  • 2,087
  • 16
  • 22