changeset 22148:dd5777a77f91

8020216: javac, compile time error isn't shown when final static field is not assigned Reviewed-by: jjg
author vromero
date Mon, 16 Dec 2013 14:32:12 +0000
parents c75c0b9b5743
children c7e024d637bf
files langtools/src/share/classes/com/sun/tools/javac/comp/Flow.java langtools/test/tools/javac/flow/T8020216/CompileTimeErrorForNonAssignedStaticFieldTest.java langtools/test/tools/javac/flow/T8020216/CompileTimeErrorForNonAssignedStaticFieldTest.out
diffstat 3 files changed, 54 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Flow.java	Fri Dec 13 09:36:24 2013 -0800
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Flow.java	Mon Dec 16 14:32:12 2013 +0000
@@ -1461,9 +1461,19 @@
             this.names = names;
         }
 
+        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);
         }
 
@@ -1476,8 +1486,16 @@
             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.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
@@ -1733,8 +1751,7 @@
             Assert.check(pendingExits.isEmpty());
 
             try {
-                boolean isInitialConstructor =
-                    TreeInfo.isInitialConstructor(tree);
+                isInitialConstructor = TreeInfo.isInitialConstructor(tree);
 
                 if (!isInitialConstructor) {
                     firstadr = nextadr;
@@ -1789,6 +1806,7 @@
                 nextadr = nextadrPrev;
                 firstadr = firstadrPrev;
                 returnadr = returnadrPrev;
+                isInitialConstructor = false;
             }
         }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/flow/T8020216/CompileTimeErrorForNonAssignedStaticFieldTest.java	Mon Dec 16 14:32:12 2013 +0000
@@ -0,0 +1,29 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 8020216
+ * @summary javac, compile time error isn't shown when final static field is not assigned
+ * @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/T8020216/CompileTimeErrorForNonAssignedStaticFieldTest.out	Mon Dec 16 14:32:12 2013 +0000
@@ -0,0 +1,2 @@
+CompileTimeErrorForNonAssignedStaticFieldTest.java:14:5: compiler.err.var.might.not.have.been.initialized: i
+1 error