我的Java编译器有问题。我简化了我的代码:
package a;
public class Base {
// compiles if this is made public or an int
protected Integer value = 0;
}
---
package b; // must be in a separate package
import a.Base;
public class Sub extends Base {
public void increment() {
System.out.println(super.value);
value++;
super.value = 1;
super.value = super.value + 1;
// this line crashes the compiler; the others all work
super.value++;
}
}
编译之后获得:
Information:java: An exception has occurred in the compiler (1.8.0_51). Please file a bug at the Java Developer Connection (http://java.sun.com/webapps/bugreport) after checking the Bug Parade for duplicates. Include your program and the following diagnostic in your report. Thank you.
Information:java: java.lang.NullPointerException
Information:java: at com.sun.tools.javac.code.Symbol$ClassSymbol.isSubClass(Symbol.java:1020)
Information:java: at com.sun.tools.javac.comp.Lower.accessClass(Lower.java:1108)
Information:java: at com.sun.tools.javac.comp.Lower.accessSymbol(Lower.java:983)
Information:java: at com.sun.tools.javac.comp.Lower.access(Lower.java:1220)
Information:java: at com.sun.tools.javac.comp.Lower.visitSelect(Lower.java:3855)
Information:java: at com.sun.tools.javac.tree.JCTree$JCFieldAccess.accept(JCTree.java:1897)
Information:java: at com.sun.tools.javac.tree.TreeTranslator.translate(TreeTranslator.java:58)
Information:java: at com.sun.tools.javac.comp.Lower.translate(Lower.java:2371)
Information:java: at com.sun.tools.javac.comp.Lower.translate(Lower.java:2382)
Information:java: at com.sun.tools.javac.comp.Lower.visitVarDef(Lower.java:3547)
Information:java: at com.sun.tools.javac.tree.JCTree$JCVariableDecl.accept(JCTree.java:852)
Information:java: at com.sun.tools.javac.tree.TreeTranslator.translateVarDefs(TreeTranslator.java:78)
Information:java: at com.sun.tools.javac.comp.Lower.visitLetExpr(Lower.java:3859)
Information:java: at com.sun.tools.javac.tree.JCTree$LetExpr.accept(JCTree.java:2426)
Information:java: at com.sun.tools.javac.comp.Lower.visitLetExpr(Lower.java:3860)
Information:java: at com.sun.tools.javac.comp.Lower.visitUnary(Lower.java:3319)
Information:java: at com.sun.tools.javac.tree.JCTree$JCUnary.accept(JCTree.java:1746)
Information:java: at com.sun.tools.javac.tree.TreeTranslator.visitExec(TreeTranslator.java:245)
Information:java: at com.sun.tools.javac.tree.JCTree$JCExpressionStatement.accept(JCTree.java:1296)
Information:java: at com.sun.tools.javac.tree.TreeTranslator.translate(TreeTranslator.java:70)
Information:java: at com.sun.tools.javac.tree.TreeTranslator.visitBlock(TreeTranslator.java:162)
Information:java: at com.sun.tools.javac.comp.Lower.visitBlock(Lower.java:3561)
Information:java: at com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:909)
Information:java: at com.sun.tools.javac.tree.TreeTranslator.visitMethodDef(TreeTranslator.java:145)
Information:java: at com.sun.tools.javac.comp.Lower.visitMethodDefInternal(Lower.java:2828)
Information:java: at com.sun.tools.javac.comp.Lower.visitMethodDef(Lower.java:2737)
Information:java: at com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:778)
Information:java: at com.sun.tools.javac.comp.Lower.visitClassDef(Lower.java:2508)
Information:java: at com.sun.tools.javac.tree.JCTree$JCClassDecl.accept(JCTree.java:693)
Information:java: at com.sun.tools.javac.comp.Lower.translate(Lower.java:2390)
Information:java: at com.sun.tools.javac.comp.Lower.translateTopLevelClass(Lower.java:3932)
Information:java: at com.sun.tools.javac.main.JavaCompiler.desugar(JavaCompiler.java:1512)
Information:java: at com.sun.tools.javac.main.JavaCompiler.desugar(JavaCompiler.java:1356)
Information:java: at com.sun.tools.javac.main.JavaCompiler.compile2(JavaCompiler.java:901)
Information:java: at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:860)
Information:java: at com.sun.tools.javac.main.Main.compile(Main.java:523)
Information:java: at com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:129)
Information:java: at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:138)
Information:java: at org.jetbrains.jps.javac.JavacMain.compile(JavacMain.java:168)
Information:java: at org.jetbrains.jps.incremental.java.JavaBuilder.compileJava(JavaBuilder.java:382)
Information:java: at org.jetbrains.jps.incremental.java.JavaBuilder.compile(JavaBuilder.java:296)
Information:java: at org.jetbrains.jps.incremental.java.JavaBuilder.doBuild(JavaBuilder.java:204)
Information:java: at org.jetbrains.jps.incremental.java.JavaBuilder.build(JavaBuilder.java:176)
Information:java: at org.jetbrains.jps.incremental.IncProjectBuilder.runModuleLevelBuilders(IncProjectBuilder.java:1202)
Information:java: at org.jetbrains.jps.incremental.IncProjectBuilder.runBuildersForChunk(IncProjectBuilder.java:877)
Information:java: at org.jetbrains.jps.incremental.IncProjectBuilder.buildTargetsChunk(IncProjectBuilder.java:948)
Information:java: at org.jetbrains.jps.incremental.IncProjectBuilder.buildChunkIfAffected(IncProjectBuilder.java:840)
Information:java: at org.jetbrains.jps.incremental.IncProjectBuilder.buildChunks(IncProjectBuilder.java:665)
Information:java: at org.jetbrains.jps.incremental.IncProjectBuilder.runBuild(IncProjectBuilder.java:372)
Information:java: at org.jetbrains.jps.incremental.IncProjectBuilder.build(IncProjectBuilder.java:193)
Information:java: at org.jetbrains.jps.cmdline.BuildRunner.runBuild(BuildRunner.java:137)
Information:java: at org.jetbrains.jps.cmdline.BuildSession.runBuild(BuildSession.java:293)
Information:java: at org.jetbrains.jps.cmdline.BuildSession.run(BuildSession.java:124)
Information:java: at org.jetbrains.jps.cmdline.BuildMain$MyMessageHandler$1.run(BuildMain.java:242)
Information:java: at org.jetbrains.jps.service.impl.SharedThreadPoolImpl$1.run(SharedThreadPoolImpl.java:41)
Information:java: at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
Information:java: at java.util.concurrent.FutureTask.run(FutureTask.java:266)
Information:java: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
Information:java: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
Information:java: at java.lang.Thread.run(Thread.java:745)
Information:java: Errors occurred while compiling module 'testAGH'
Information:2015-10-23 14:23 - Compilation completed with 1 error and 0 warnings in 671ms
Error:java: Compilation failed: internal java compiler error
为什么Java无法编译?
答案 0 :(得分:4)
这显然是编译器错误。您应该在输出指示时提交错误,但是由于您要求为什么我会尝试探索它:)
JDK langtools repo包含javac
的代码。我浏览了几个不同的builds,但是找不到与堆栈跟踪相匹配的行号的版本(我不确定如何match a Java version to a build number),所以我和#39;离开最近的版本b132。
如您所见,基本问题是com.sun.tools.javac.code.Symbol
中Symbol.ClassSymbol.isSubClass()
中的NullPointerException
。这就是问题的根源。在堆栈跟踪中看得更高,我们可以在到达access()
之前看到accessSymbol()
来电accessClass()
然后isSubClass()
。因此,我们可以推断编译器正在尝试验证子类在失败时是否允许访问super.value
。
如果不知道NPE在哪一条线上发生就很难找到确切的问题,所以不幸的是,事情变得模糊。由于此故障仅在我们尝试增加并设置(++
或+=
)字段时发生,并且仅当我们将其称为super.value
时,我们才会期望查看与转换,自动装箱,分配或算术相关的一些代码。唉isSubClass()
中没有任何内容真正与任何内容相关。
我的猜测,鉴于isSubClass()
似乎没有任何罪名,Lower.accessClass()
是否将null
传递给isSubClass()
。在此方法中,有三个值传递给isSubClass()
:字段currentClass
(作为c
,这是"当前封闭的类"),sym.owner
(我认为sym
是我们的value
字段,我不知道它的owner
是什么,但可能是它声明的类别),{{1} },看起来始终设置为非空值。所以它是types
或currentClass
,并且sym.owner
的引用会在Lower.access()
之前提到NPE,所以它似乎也是非空的。
因此,我担心罪魁祸首sym.owner
因某种原因而无效。由于Lower.currentClass
是一个包私有非最终字段,因此我不打算在此时确定它是否可以currentClass
,但似乎有可能。当然,null
还有其他几个可能的罪魁祸首,所以我可能错了。
不确定,但我希望这是有用的!如果我了解更多,我肯定会更新。