8030218: javac, compile time error isn't shown when final static field is not assigned, follow-up
authorvromero
Thu, 19 Dec 2013 21:58:50 +0000
changeset 22166 adc980c0dbcc
parent 22165 ec53c8946fc2
child 22167 e0ba35f27975
8030218: javac, compile time error isn't shown when final static field is not assigned, follow-up Reviewed-by: jjg, jfranck, sundar
langtools/src/share/classes/com/sun/tools/javac/comp/Flow.java
langtools/test/tools/javac/flow/T8030218/CompileTimeErrorForNonAssignedStaticFieldTest.java
langtools/test/tools/javac/flow/T8030218/CompileTimeErrorForNonAssignedStaticFieldTest.out
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Flow.java	Thu Dec 19 20:16:44 2013 +0000
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Flow.java	Thu Dec 19 21:58:50 2013 +0000
@@ -1460,9 +1460,19 @@
             this.names = names;
         }
 
+        private boolean isInitialConstructor = false;
+
         @Override
         protected void markDead(JCTree tree) {
-            inits.inclRange(returnadr, nextadr);
+            if (!isInitialConstructor) {
+                inits.inclRange(returnadr, nextadr);
+            } else {
+                for (int address = returnadr; address < nextadr; address++) {
+                    if (!(isFinalUninitializedStaticField(vardecls[address].sym))) {
+                        inits.incl(address);
+                    }
+                }
+            }
             uninits.inclRange(returnadr, nextadr);
         }
 
@@ -1475,8 +1485,17 @@
             return
                 sym.pos >= startPos &&
                 ((sym.owner.kind == MTH ||
-                 ((sym.flags() & (FINAL | HASINIT | PARAMETER)) == FINAL &&
-                  classDef.sym.isEnclosedBy((ClassSymbol)sym.owner))));
+                isFinalUninitializedField(sym)));
+        }
+
+        boolean isFinalUninitializedField(VarSymbol sym) {
+            return sym.owner.kind == TYP &&
+                   ((sym.flags() & (FINAL | HASINIT | PARAMETER)) == FINAL &&
+                   classDef.sym.isEnclosedBy((ClassSymbol)sym.owner));
+        }
+
+        boolean isFinalUninitializedStaticField(VarSymbol sym) {
+            return isFinalUninitializedField(sym) && sym.isStatic();
         }
 
         /** Initialize new trackable variable by setting its address field
@@ -1730,10 +1749,9 @@
             int returnadrPrev = returnadr;
 
             Assert.check(pendingExits.isEmpty());
-
+            boolean lastInitialConstructor = isInitialConstructor;
             try {
-                boolean isInitialConstructor =
-                    TreeInfo.isInitialConstructor(tree);
+                isInitialConstructor = TreeInfo.isInitialConstructor(tree);
 
                 if (!isInitialConstructor) {
                     firstadr = nextadr;
@@ -1788,6 +1806,7 @@
                 nextadr = nextadrPrev;
                 firstadr = firstadrPrev;
                 returnadr = returnadrPrev;
+                isInitialConstructor = lastInitialConstructor;
             }
         }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/flow/T8030218/CompileTimeErrorForNonAssignedStaticFieldTest.java	Thu Dec 19 21:58:50 2013 +0000
@@ -0,0 +1,29 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 8030218
+ * @summary javac, compile time error isn't shown when final static field is not assigned, follow-up
+ * @compile/fail/ref=CompileTimeErrorForNonAssignedStaticFieldTest.out -XDrawDiagnostics CompileTimeErrorForNonAssignedStaticFieldTest.java
+ */
+
+public class CompileTimeErrorForNonAssignedStaticFieldTest {
+    private final static int i;
+
+    public CompileTimeErrorForNonAssignedStaticFieldTest()
+            throws InstantiationException {
+        throw new InstantiationException("Can't instantiate");
+    }
+
+    static class Inner {
+        private final int j;
+        public Inner(int x)
+                throws InstantiationException {
+            if (x == 0) {
+                throw new InstantiationException("Can't instantiate");
+            } else {
+                j = 1;
+            }
+            System.out.println(j);
+        }
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/flow/T8030218/CompileTimeErrorForNonAssignedStaticFieldTest.out	Thu Dec 19 21:58:50 2013 +0000
@@ -0,0 +1,2 @@
+CompileTimeErrorForNonAssignedStaticFieldTest.java:14:5: compiler.err.var.might.not.have.been.initialized: i
+1 error