javaparser icon indicating copy to clipboard operation
javaparser copied to clipboard

Q: does javaparser work on un-compilable source code as input? i.e syntax correct but semantics wrong source code?

Open mw66 opened this issue 3 years ago • 5 comments

Hi,

does javaparser work on un-compilable source code as input? i.e syntax correct but semantics wrong source code?

un-compilable: means not accepted by the regular javac compiler; but the syntax is correct.

E.g. I have some old Java code, that is syntax correct, but semantics wrong (e.g. missing some class type decls); I plan to use javaparser first transform and generate some new code, and only compile the final new code.

Does javaparser work in this scenario? or the old code has to be compilable first to use javaparser?

Can javaparser do partial semantic analysis? i.e do it as much as it can, but do not abort on (undecidable) semantic errors?

Thanks.

mw66 avatar Aug 02 '22 00:08 mw66

Another question is: can I programmatically run the semantic analysis on list of classes?

Ideally, the classes need to be processed in topological dependency order in multiple passes:

1st pass: only establish the topological dependency order: e.g. [class A, class B, class C, class D ...] where only class A is compilable, the subsequent classes has dependency (either is-a or has-a relationship) on the preceding classes, so cannot be fully semantically analyzed until the preceding classes has been. And these classes [B, C, D ...] may contain other semantically un-compilable elements, so in the 1st pass only topological dependency order can be established. Let's call this partial semantic analysis.

2nd pass: class B is processed (with its complete dependency info on class A), now become compilable. The rest of [class C, class D ...] are partially semantic analyzed again.

3rd pass: class C is processed (with its complete dependency info on class A & B), now become compilable. The rest of [class D ...] are partially semantic analyzed again.

4th pass: ... and so on, until all done.

Is this multiple partial semantic analysis passes possible in javaparser?

mw66 avatar Aug 02 '22 04:08 mw66

As the last resort, maybe I need to work at the AST level (i.e. only syntax, without semantic analysis), just wonder is there any AST level api doc / example / tutorial somewhere?

Thanks.

mw66 avatar Aug 02 '22 05:08 mw66

does javaparser work on un-compilable source code as input? i.e syntax correct but semantics wrong source code?

Hi @mw66 you can parse java code with JavaParser if the syntax of your code is correct but you could have unsolved exception if you try to use the symbol solver to resolve types that JP doesn't know.

jlerbsc avatar Aug 02 '22 06:08 jlerbsc

Is this multiple partial semantic analysis passes possible in javaparser?

If I understand your question correctly I would say that it is your algorithm which manages this functionality. It is not dependent on the possibilities of JP

jlerbsc avatar Aug 02 '22 06:08 jlerbsc

As the last resort, maybe I need to work at the AST level (i.e. only syntax, without semantic analysis), just wonder is there any AST level api doc / example / tutorial somewhere?

You can probably start with this https://github.com/javaparser/javaparser-visited

jlerbsc avatar Aug 02 '22 06:08 jlerbsc

I close this issue but you are free to reopen it if you have new questions.

jlerbsc avatar Aug 18 '22 06:08 jlerbsc