Fix local variables with null constant

When a local variable is typed "KNOWN_NULL" because of a null constant,
its type is then transformed to "java.lang.Object", but
"java.lang.Object" wouldn't be added to the types of the dex.
So in the rare case where java.lang.Object wouldn't be referenced
anywhere else in the compilation unit it would fail with
"java.lang.IllegalArgumentException: not found: Ljava/lang/Object;"
Now the associated CstType is "java.lang.Object" so that it is coherent
with the future transformation, and "java.lang.Object" is added to the
list of types.

Test: 136-null-constant-debug-info
Bug: 36824690
Change-Id: I0e7e3057e03761cdb5adbb8f454e3123b3a64969
diff --git a/dx/src/com/android/dx/dex/code/OutputFinisher.java b/dx/src/com/android/dx/dex/code/OutputFinisher.java
index b506de2..06616b1 100644
--- a/dx/src/com/android/dx/dex/code/OutputFinisher.java
+++ b/dx/src/com/android/dx/dex/code/OutputFinisher.java
@@ -221,6 +221,10 @@
 
         if (type != Type.KNOWN_NULL) {
             result.add(CstType.intern(type));
+        } else {
+            /* If this a "known null", let's use "Object" because that's going to be the
+             * resulting type in {@link LocalList.MakeState#filterSpec} */
+            result.add(CstType.intern(Type.OBJECT));
         }
 
         if (name != null) {
diff --git a/dx/tests/136-null-constant-debug-info/NullConstant.java b/dx/tests/136-null-constant-debug-info/NullConstant.java
new file mode 100644
index 0000000..5e70654
--- /dev/null
+++ b/dx/tests/136-null-constant-debug-info/NullConstant.java
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+public class NullConstant extends Throwable {
+
+  public final void m() {
+    NullConstant t = this;
+    NullConstant f;
+    f = t = null;
+    long lout = 0L;
+  }
+}
diff --git a/dx/tests/136-null-constant-debug-info/expected.txt b/dx/tests/136-null-constant-debug-info/expected.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/dx/tests/136-null-constant-debug-info/expected.txt
diff --git a/dx/tests/136-null-constant-debug-info/info.txt b/dx/tests/136-null-constant-debug-info/info.txt
new file mode 100644
index 0000000..45e4a26
--- /dev/null
+++ b/dx/tests/136-null-constant-debug-info/info.txt
@@ -0,0 +1,9 @@
+This is a non-regression test that reproduces a case where a local variable
+would have the java.lang.Object type because of a null constant.
+The type of the local variable would be transformed from "KNOWN_NULL" to
+"java.lang.Object", but "java.lang.Object" wouldn't be added to the types of
+the dex.
+So in the rare case where java.lang.Object wouldn't be referenced anywhere else
+in the compilation unit it would fail with "java.lang.IllegalArgumentException:
+not found: Ljava/lang/Object;"
+
diff --git a/dx/tests/136-null-constant-debug-info/run b/dx/tests/136-null-constant-debug-info/run
new file mode 100644
index 0000000..286d400
--- /dev/null
+++ b/dx/tests/136-null-constant-debug-info/run
@@ -0,0 +1,19 @@
+#!/bin/bash
+#
+# Copyright (C) 2017 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+${JAVAC} -g -d . NullConstant.java &> /dev/null
+
+dx --debug --dex *.class 2>&1