如何在Java中一次性获取所有编译时错误?

huangapple go评论84阅读模式
英文:

How do I get all the Compile Time Errors at once in Java?

问题

I want to compile a java file and get all the errors to the list. So far I am using java's built-in compiler API, but it has some problems.

For example,

//demo.java
class demo
{
    public static void main(String[] args) {
        int array = new int[10]
    }
}

we can see that there is a semicolon missing and [] (array brackets) are missing.
But Java Compiler API will only return

demo.java:4: error: ';' expected
        int array = new int[10]
                               ^
1 error

But It should say there is missing [] brackets and a semicolon ;

Is there any other way to compile a java file and return all the errors?

If there is any way to do it using Java Compiler API, then let me know.

If there are any other external Libraries or tools that compile a java file and return the error status.
External Library should:

  • If the file has no errors then the library should say that the file has no errors.
  • If the Java file has some errors then the library should return a list of errors with their line number and error message.

I searched a lot about this on the web. But I found only one solution, i.e., Java compiler API. But I want to try some other tools too.

英文:

I want to compile a java file and get all the errors to the list. So far I am using java's built in compiler API, but it has some problems.

For example,

//demo.java
class demo
{
    public static void main(String[] args) {
        int array = new int[10]
    }
}

we can see that there is semicolon missing and [] (array brackets) are missing.
But Java Compiler API will only return

demo.java:4: error: ';' expected
        int array = new int[10]
                               ^
1 error

But It should say there is missing [] brackets and semicolon ;

Is there any other way to compile java file and return all the errors?

If there is any way to do it using Java Compiler API then let me know.

If there are any other external Libraries or tool that compile a java file and return the error status.
External Library should:

  • If the file has no errors then the library should say that file has no errors.
  • If the Java file has some errors then library should return list of errors with their line number and error message.

I searched a lot about this web. But Only found the one solution i.e. Java compiler API. But I want to try some other tools too.

答案1

得分: 4

Java编译器不会警告您关于"缺少[]",因为这不是Java编译器可能发出的错误;如果您修复了代码中的缺少分号(这是语法错误),然后报告的错误是:

不兼容的类型:无法将int[]转换为int

这作为错误是有道理的,因为变量的类型声明为int,但初始化表达式的类型是int[]

通过将错误描述为"缺少[]",您使它听起来像另一个语法错误,但实际上"不兼容的类型"错误不是语法错误;它发生在type-checking阶段之后,该阶段发生在parsing之后。但是,如果在解析阶段存在任何语法错误,那么编译器不会进入类型检查阶段,因为没有有效的解析树可供类型检查。

因此,无论您使用命令行javac工具还是编译器API,Java编译器都不会在同一编译单元中报告语法错误和类型错误。

英文:

The Java compiler does not warn you about a "missing []" because that is not a possible error that the Java compiler can ever emit; if you fix the missing semicolon (which is a syntax error) in this code, then the error reported is:

incompatible types: int[] cannot be converted to int

which makes sense as an error, because the variable's type is declared as int but the initializer's expression is of type int[].

By describing the error as "missing []", you make it sound like another syntax error, but actually an "incompatible types" error is not a syntax error; it occurs during the type-checking stage, which occurs after parsing. However, if there are any syntax errors at the parsing stage then the compiler doesn't enter the type-checking stage because there is no valid parse tree to type-check.

So the Java compiler will not report both a syntax error and a type error in the same compilation unit, regardless of whether you use the command-line javac tool or the compiler API.

答案2

得分: 2

这是不可能的。当Java编译器遇到错误时,它会停止处理源文件。C/C++编译器会按您所需的方式处理,但大多数后续的错误消息会误导并且不实用。

要获取所有错误的列表,编译器需要使用人工智能技术。编译器需要猜测您的意思。

英文:

This is impossible. The Java compiler stops processing a source file when it hits an error. The C/C++ compiler does what you want but most following error messages are misleading and not useful.

To get a list of all errors, the compiler would need to use AI technologies. The compiler would need to guess what you meant.

huangapple
  • 本文由 发表于 2020年8月3日 01:27:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63219089.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定