How to check the correctness of the Java source file (no errors)?

In my code, I open file.java and parse it with JavaParser .

FileInputStream in = new FileInputStream(".../file.java"); CompilationUnit cu; try { // parse the file cu = JavaParser.parse(in); } finally { in.close(); } ........ 

file.java

 public class File{ public void ownMethod(){...} public static void main(String[] args){ ownMethod(5); //Note the extra parameter } } 

There is an error in the .java file: the main method calls the ownMethod method with one parameter, but ownMethod expects 0 parameters. JavaParser does not detect this error and parses the .java file with this error. How can I find out (in my code) if there are any errors in file.java? Is this possible without using a java compiler?

+6
source share
1 answer

Is this possible without using a java compiler?

No. Any solution that your (invents) invents will lead you to another compiler. Analysis and error checking is an important part of the compiler's job.

+4
source

Source: https://habr.com/ru/post/979252/


All Articles