changeset 2319:fa004631cf00

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 Mon, 27 Jan 2014 21:05:58 +0000
parents bbbef54e3b30
children afb6642d0603
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, 57 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/comp/Flow.java	Thu Jan 23 06:34:53 2014 -0800
+++ b/src/share/classes/com/sun/tools/javac/comp/Flow.java	Mon Jan 27 21:05:58 2014 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -1461,9 +1461,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);
         }
 
@@ -1476,8 +1486,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
@@ -1731,10 +1750,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;
@@ -1789,6 +1807,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	Mon Jan 27 21:05:58 2014 +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	Mon Jan 27 21:05:58 2014 +0000
@@ -0,0 +1,2 @@
+CompileTimeErrorForNonAssignedStaticFieldTest.java:14:5: compiler.err.var.might.not.have.been.initialized: i
+1 error