You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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.
The text was updated successfully, but these errors were encountered:
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.
Fixesspotbugs#2134
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
Consider following innocent code in a Java project configured with Java 8 source/target compatibility level
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:spotbugs/eclipsePlugin/src/de/tobject/findbugs/reporter/MarkerUtil.java
Line 440 in 7d6894a
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!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.
The text was updated successfully, but these errors were encountered: