2015-10-23 10 views
6

Ich habe ein Problem mit dem Java-Compiler. Ich vereinfacht meinen Code:Java-Compiler schlägt beim Kompilieren eines einfachen Programms fehl

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++; 
    } 
} 

nach dem Kompilieren erhalte ich:

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 

Warum Java scheitern diese zu kompilieren?

+2

Gleiches Verhalten mit 1.8.0_60. Unabhängig davon, ob Sie Compiles programmieren oder nicht, sollte der Compiler keine Ausnahme auslösen, deshalb sollten Sie wahrscheinlich einen Fehler ablegen. – assylias

+2

* Warum Java kompiliert *: Die Fehlermeldung sagt warum: Es gibt einen Fehler im Compiler. –

+2

Ich habe auf 1.8.0_66 aktualisiert und der Java-Compiler stürzt immer noch ab.Ich werde einen Fehler melden. – Yozer

Antwort

4

Dies ist eindeutig ein Compiler-Fehler. Sie sollten einen Fehler wie die Ausgabe anweist, Datei aber da Sie gefragt, warum Ich werde versuchen, dass :) für javac

Die JDK langtools repo enthält den Code zu erkunden. Ich blätterte durch verschiedene builds, aber war nicht in der Lage, eine Version mit Zeilennummern zu finden, die mit dem Stack-Trace übereinstimmten (ich bin mir nicht sicher, wie), so gehe ich von der neuesten Build, b132. Wie Sie sehen, ist das zugrunde liegende Problem ein NullPointerException in com.sun.tools.javac.code.Symbol in Symbol.ClassSymbol.isSubClass(). Das ist also die Ursache des Problems. Wenn wir in der Stack-Kurve höher schauen, können wir access() Aufrufe accessSymbol() dann accessClass() vor erreichen isSubClass() sehen. Daraus können wir schließen, dass der Compiler versucht zu verifizieren, dass die Unterklasse zum Zeitpunkt des Fehlschlagens auf super.value zugreifen darf.

Ohne zu wissen, auf welcher Linie die NPE auftritt, ist es schwierig, das genaue Problem zu lokalisieren, so dass hier leider alles unscharf wird. Da dieser Fehler nur auftritt, wenn wir versuchen, das Feld zu inkrementieren (oder +=), und nur wenn wir es als super.value bezeichnen, erwarten wir Code, der sich auf Casting, Autoboxing, Zuweisung oder Arithmetik bezieht . Ach nichts in isSubClass() scheint wirklich etwas davon verwandt zu sein.

Meine Vermutung, da es nichts Belastendes in isSubClass() zu sein scheint, ist, dass Lower.accessClass() ein null in isSubClass() passiert. Es gibt drei Werte isSubClass() in dieser Methode übergeben: das Feld currentClass (als c, ist dies „die zur Zeit umschließenden Klasse“), sym.owner (ich glaube, sym unser value Feld ist, ich weiß nicht, was seine owner ist aber vermutlich die Klasse, in der es deklariert ist) und types, die immer auf einen Nicht-Null-Wert festgelegt werden soll. So ist es entweder currentClass oder sym.owner, und es gibt Verweise auf sym.owner, die NPE in Lower.access() früher wäre, so dass auch scheint, nicht null zu sein.

Also werde ich riskieren, dass der Schuldige ist Lower.currentClass ist aus irgendeinem Grund Null. Da currentClass ist ein Paket-private nicht-endgültigen Feld, werde ich nicht versuchen zu bestimmen, ob es zu diesem Zeitpunkt null sein kann oder nicht, aber es scheint möglich. Es gibt natürlich mehrere andere mögliche Täter in isSubClass(), also könnte ich wohl falsch liegen.

Inconclusive, aber ich hoffe, das war informativ! Ich werde sicherlich updaten, wenn ich mehr lerne.

Verwandte Themen