Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

org.eclipse.jdt.core.compiler.InvalidInputException in MarkerUtil.initScanner #2134

Closed
iloveeclipse opened this issue Aug 5, 2022 · 0 comments · Fixed by #2135
Closed

org.eclipse.jdt.core.compiler.InvalidInputException in MarkerUtil.initScanner #2134

iloveeclipse opened this issue Aug 5, 2022 · 0 comments · Fixed by #2135
Assignees

Comments

@iloveeclipse
Copy link
Member

Consider following innocent code in a Java project configured with Java 8 source/target compatibility level

public class UnderscoreInLiterals {
    private static final long XYZ = 42_000;
    static class X {
        Y y;
    }
    static class Y {
    }
}

The Spotbugs looks at the X and wants report some issue. To properly resolve inner class reference it uses possibility that the sources are available (usually it scans only bytecode) and creates an IScanner instance via default API:

IScanner scanner = ToolFactory.createScanner(false, false, false, true);

The project is perfectly compilable, no errors nowhere, so one could think that should work?
Nope. To my real surprise, JDT says literally org.eclipse.jdt.core.compiler.InvalidInputException: Underscores_In_Literals_Not_Below_17 and fails to parse this innocent piece of code!

Could not init scanner for type: class X [in UnderscoreInLiterals [in [Working copy] UnderscoreInLiterals.java [in <default> [in src [in A]]]]]
  Y y
org.eclipse.jdt.core.compiler.InvalidInputException: Underscores_In_Literals_Not_Below_17
	at org.eclipse.jdt.internal.compiler.parser.Scanner.consumeDigits(Scanner.java:1029)
	at org.eclipse.jdt.internal.compiler.parser.Scanner.consumeDigits(Scanner.java:1017)
	at org.eclipse.jdt.internal.compiler.parser.Scanner.scanNumber(Scanner.java:4194)
	at org.eclipse.jdt.internal.compiler.parser.Scanner.getNextToken0(Scanner.java:1894)
	at org.eclipse.jdt.internal.compiler.parser.Scanner.getNextToken(Scanner.java:1407)
	at org.eclipse.jdt.internal.core.util.PublicScanner.getNextToken(PublicScanner.java:117)
	at de.tobject.findbugs.reporter.MarkerUtil.initScanner(MarkerUtil.java:444)
	at de.tobject.findbugs.reporter.MarkerUtil.getLineStart(MarkerUtil.java:416)
	at de.tobject.findbugs.reporter.MarkerUtil.findInnerClassSourceLine(MarkerUtil.java:500)
	at de.tobject.findbugs.reporter.MarkerUtil.findChildSourceLine(MarkerUtil.java:479)
	at de.tobject.findbugs.reporter.MarkerUtil.completeInnerClassInfo(MarkerUtil.java:396)
	at de.tobject.findbugs.reporter.MarkerUtil.getJavaElement(MarkerUtil.java:316)
	at de.tobject.findbugs.reporter.MarkerUtil.createMarkerParameter(MarkerUtil.java:184)
	at de.tobject.findbugs.reporter.MarkerUtil.createBugParameters(MarkerUtil.java:172)
	at de.tobject.findbugs.reporter.MarkerUtil.createMarkers(MarkerUtil.java:126)
	at de.tobject.findbugs.builder.FindBugsWorker.updateBugCollection(FindBugsWorker.java:378)
	at de.tobject.findbugs.builder.FindBugsWorker.work(FindBugsWorker.java:228)
	at de.tobject.findbugs.builder.FindBugsBuilder$StartedFromBuilderJob.runWithProgress(FindBugsBuilder.java:219)
	at de.tobject.findbugs.FindBugsJob.run(FindBugsJob.java:142)
	at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)

The surprising (at least to me) problem is: in the code we use ToolFactory.createScanner(false, false, true, false) JDT doesn't know which source/target levels are used and just creates an IScanner using some very safe defaults, and not using current project settings!!!
It simply assumes that all the code source level of the parsed code should comply with Java 1.3 JLS specs or, with some luck, 1.4!!!
That was really surprising to me, but there is new API that allows to specify source & target: ToolFactory.createScanner(false, false, true, sourceLevel, complianceLevel);.

We should use that.
I plan to provide a PR.

@iloveeclipse iloveeclipse self-assigned this Aug 5, 2022
iloveeclipse added a commit to iloveeclipse/spotbugs that referenced this issue Aug 5, 2022
This avoids org.eclipse.jdt.core.compiler.InvalidInputException in
MarkerUtil.initScanner() where JDT can't parse code that is perfectly
compilable because the old API used too low default source level.

Fixes spotbugs#2134
iloveeclipse added a commit that referenced this issue Aug 8, 2022
This avoids org.eclipse.jdt.core.compiler.InvalidInputException in
MarkerUtil.initScanner() where JDT can't parse code that is perfectly
compilable because the old API used too low default source level.

Fixes #2134
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant