[gwt-contrib] Fix initialization of non-final fields (issue1618807)

2011-12-23 Thread stephen . haberman

Reviewers: cromwellian,



Please review this at http://gwt-code-reviews.appspot.com/1618807/

Affected files:
  M dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaScriptAST.java


Index: dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaScriptAST.java
diff --git  
a/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaScriptAST.java  
b/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaScriptAST.java
index  
b059bd8440f05e2c8193124d6a5565ff7e3ef192..91e991c77b1d725348d6d951eef448647692f4ce  
100644

--- a/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaScriptAST.java
+++ b/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaScriptAST.java
@@ -829,10 +829,13 @@ public class GenerateJavaScriptAST {
   JsNameRef localRef = (JsNameRef) pop(); // localRef

   JVariable target = x.getVariableRef().getTarget();
-  if (target instanceof JField  ((JField)  
target).getLiteralInitializer() != null) {

-// Will initialize at top scope; no need to double-initialize.
-push(null);
-return;
+  if (target instanceof JField) {
+JField field = (JField) target;
+if (field.isFinal()  field.getLiteralInitializer() != null) {
+  // Will initialize at top scope; no need to double-initialize.
+  push(null);
+  return;
+}
   }

   JsBinaryOperation binOp =
@@ -863,8 +866,13 @@ public class GenerateJavaScriptAST {
 public void endVisit(JField x, Context ctx) {
   // if we need an initial value, create an assignment
   if (x.getLiteralInitializer() != null) {
-// setup the constant value
-accept(x.getLiteralInitializer());
+if (x.isFinal()) {
+  // setup the constant value
+  accept(x.getLiteralInitializer());
+} else {
+  // setup the default value, see Issue 380
+  accept(x.getType().getDefaultValue());
+}
   } else if (!x.hasInitializer()  x.getEnclosingType() !=  
program.getTypeJavaLangObject()) {

 // setup a default value
 accept(x.getType().getDefaultValue());


--
http://groups.google.com/group/Google-Web-Toolkit-Contributors


[gwt-contrib] Re: Fix initialization of non-final fields (issue1618807)

2011-12-23 Thread cromwellian


http://gwt-code-reviews.appspot.com/1618807/diff/1/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaScriptAST.java
File dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaScriptAST.java
(right):

http://gwt-code-reviews.appspot.com/1618807/diff/1/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaScriptAST.java#newcode873
dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaScriptAST.java:873:
// setup the default value, see Issue 380
LGTM. One TODO for future reference: I wonder if we could actually
tighten this up a bit, that is, if every possible constructor writes to
this field, then the default value isn't needed, since it will always be
written. This may be moot if the Closure Compiler handles it.

http://gwt-code-reviews.appspot.com/1618807/

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors