changeset 3133:8af87c6ebafc

8030218: javac, compile time error isn't shown when final static field is not assigned, follow-up Reviewed-by: jjg, jfranck, sundar
author vromero
date Thu, 19 Dec 2013 21:58:50 +0000
parents a10c5a27b7be
children 41773f3d520b
files src/share/classes/com/sun/tools/javac/comp/Flow.java test/tools/javac/flow/T8030218/CompileTimeErrorForNonAssignedStaticFieldTest.java test/tools/javac/flow/T8030218/CompileTimeErrorForNonAssignedStaticFieldTest.out
diffstat 3 files changed, 56 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/comp/Flow.java	Thu Dec 19 20:16:44 2013 +0000
+++ b/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/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/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