eclipse-cs
eclipse-cs copied to clipboard
MismatchedTokenException occurred while parsing file
Hello,
I am regularly getting the following exception while doing development within eclipse. These exception did not occur on 6.16.0.
!ENTRY net.sf.eclipsecs.core 4 0 2019-10-29 13:36:40.087
!MESSAGE Checkstyle-Plugin: Exception was thrown while processing C:\IDE\4.12\workspace\my-project\src\mypath\MyClass.java
!STACK 0
com.puppycrawl.tools.checkstyle.api.CheckstyleException: Exception was thrown while processing C:\IDE\4.12\workspace\my-project\src\mypath\MyClass.java
at com.puppycrawl.tools.checkstyle.Checker.processFiles(Checker.java:304)
at com.puppycrawl.tools.checkstyle.Checker.process(Checker.java:217)
at net.sf.eclipsecs.core.builder.Auditor.runAudit(Auditor.java:158)
at net.sf.eclipsecs.core.builder.CheckstyleBuilder.handleBuildSelection(CheckstyleBuilder.java:307)
at net.sf.eclipsecs.core.jobs.RunCheckstyleOnFilesJob.runInWorkspace(RunCheckstyleOnFilesJob.java:119)
at org.eclipse.core.internal.resources.InternalWorkspaceJob.run(InternalWorkspaceJob.java:42)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)
Caused by: com.puppycrawl.tools.checkstyle.api.CheckstyleException: MismatchedTokenException occurred while parsing file C:\IDE\4.12\workspace\my-project\src\mypath\MyClass.java.
at com.puppycrawl.tools.checkstyle.JavaParser.parse(JavaParser.java:98)
at com.puppycrawl.tools.checkstyle.TreeWalker.processFiltered(TreeWalker.java:187)
at com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck.process(AbstractFileSetCheck.java:81)
at com.puppycrawl.tools.checkstyle.Checker.processFile(Checker.java:329)
at com.puppycrawl.tools.checkstyle.Checker.processFiles(Checker.java:291)
... 6 more
Caused by: C:\IDE\4.12\workspace\my-project\src\mypath\MyClass.java:364:9: expecting EOF, found '}'
at antlr.Parser.match(Parser.java:211)
at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.compilationUnit(GeneratedJavaRecognizer.java:211)
at com.puppycrawl.tools.checkstyle.JavaParser.parse(JavaParser.java:92)
... 10 more
Such parse errors can happen if checkstyle executed on non compilable sources. If plugin is smart enough to distinguish draft file(non compile-able) and compiled it should run checkstyle only on compiled sources. If user manually execute checkstyle on file that is compiled, such error is expected
Yes, indeed it has issues with source files with syntax errors and it is easy to reproduce such exceptions:
- Create a new java project, enable checkstyle with default settings
- Create a java class of name bug172
- Replace the contents of this file with the following:
public class bug172
{
public static void main(String[] args)
{
{}
}
}
}
I think the eclipse plugin of checkstyle should at least not throw exceptions (e.g. catch them internally) in case of syntax errors, as such are not an unexpected/exceptional case when running checkstyle within an eclipse plugin.
@ManfredTremmel , do you know if this is possible to make use experience less scary ? nuance is that chekcstyle library do have problems with some java files on very rare or new syntax - https://github.com/checkstyle/checkstyle/issues?q=is%3Aopen+is%3Aissue+label%3Aantlr . So parse error mean:
- user need to fix compilation issue
- checkstyle is missing to support syntax and issue need to be reported.
Do you have any suggestion on how to improve this ? I think it is ok to print whole stacktrace to log file, but I am not sure what is shown in UI.
This is how it looks when there's a compilation issue:

Goal is to remove the "Checkstyle execution failed due to an internal error" when it's a compilation issue, but not when it's a checkstyle syntax support issue.
We could try to skip files that have compilation errors. This works: https://github.com/Calixte/eclipse-cs/commit/b671f0bb5b021c6e345e91d75239c166192e929c One could argue that not all Java Problems at level Error would prevent Checkstyle from running. We could filter for only fatal errors by looking at the category id of the marker, a bit like it's done in the hasFatalError method here.
We could also catch the exception, and report it only if there's no error marker on the file.
Checkstyle should be executed only on compilable java source file, in other case behavior is not deterministic.
Next version will skip files that have Java compiler errors.
Just noting that MismatchedTokenException and the like can happen for files that are compilable but Checkstyle doesn't support them, like newer features.
Thank you, @Bananeweizen