Only emit constant values for fields of primitive or string type
Boxed primitive fields can't be constants even if they are initialized
with a valid constant expression.
MOE_MIGRATED_REVID=137066492
diff --git a/java/com/google/turbine/binder/ConstBinder.java b/java/com/google/turbine/binder/ConstBinder.java
index 4b82e57..9d6a4db 100644
--- a/java/com/google/turbine/binder/ConstBinder.java
+++ b/java/com/google/turbine/binder/ConstBinder.java
@@ -30,6 +30,7 @@
import com.google.turbine.model.Const.Value;
import com.google.turbine.model.TurbineFlag;
import com.google.turbine.model.TurbineTyKind;
+import com.google.turbine.type.Type;
import java.lang.annotation.RetentionPolicy;
import javax.annotation.Nullable;
@@ -166,6 +167,17 @@
if ((base.access() & TurbineFlag.ACC_FINAL) == 0) {
return null;
}
+ switch (base.type().tyKind()) {
+ case PRIM_TY:
+ break;
+ case CLASS_TY:
+ if (((Type.ClassTy) base.type()).sym().equals(ClassSymbol.STRING)) {
+ break;
+ }
+ // falls through
+ default:
+ return null;
+ }
Value value = constantEnv.get(base.sym());
if (value != null) {
value = (Value) ConstEvaluator.cast(base.type(), value);
diff --git a/javatests/com/google/turbine/lower/LowerIntegrationTest.java b/javatests/com/google/turbine/lower/LowerIntegrationTest.java
index 76a43f5..9739d4f 100644
--- a/javatests/com/google/turbine/lower/LowerIntegrationTest.java
+++ b/javatests/com/google/turbine/lower/LowerIntegrationTest.java
@@ -226,6 +226,7 @@
"interface_method.test",
"raw_canon.test",
"float_exponent.test",
+ "boxed_const.test",
};
List<Object[]> tests =
ImmutableList.copyOf(testCases).stream().map(x -> new Object[] {x}).collect(toList());
diff --git a/javatests/com/google/turbine/lower/testdata/boxed_const.test b/javatests/com/google/turbine/lower/testdata/boxed_const.test
new file mode 100644
index 0000000..b55ae8e
--- /dev/null
+++ b/javatests/com/google/turbine/lower/testdata/boxed_const.test
@@ -0,0 +1,12 @@
+=== Test.java ===
+class Test {
+ public static final Integer A = 42;
+ public final Integer B = 42;
+ public static Integer C = 42;
+ public Integer D = 42;
+
+ public static final int E = 42;
+ public final int F = 42;
+ public static int G = 42;
+ public int H = 42;
+}