Fix native annotations returning TypeNotPresentException.

Also adds test cases for inner classes and TypeNotPresentException.

Change-Id: I28041af455f09b43fcf0b07b79b5a21d4741079b
diff --git a/runtime/dex_file.cc b/runtime/dex_file.cc
index 29413bf..5526883 100644
--- a/runtime/dex_file.cc
+++ b/runtime/dex_file.cc
@@ -1787,8 +1787,15 @@
             klass->GetDexFile(), index, klass.Get());
         set_object = true;
         if (element_object == nullptr) {
-          // TODO: Put a TypeNotFoundExceptionProxy instead of throwing here.
-          return false;
+          CHECK(self->IsExceptionPending());
+          if (result_style == kAllObjects) {
+            const char* msg = StringByTypeIdx(index);
+            self->ThrowNewWrappedException("Ljava/lang/TypeNotPresentException;", msg);
+            element_object = self->GetException();
+            self->ClearException();
+          } else {
+            return false;
+          }
         }
       }
       break;
diff --git a/test/005-annotations/expected.txt b/test/005-annotations/expected.txt
index 36b3868..e1c3dad 100644
--- a/test/005-annotations/expected.txt
+++ b/test/005-annotations/expected.txt
@@ -101,3 +101,10 @@
 Package declared annotations:
       @android.test.anno.AnnoSimplePackage()
         interface android.test.anno.AnnoSimplePackage
+
+Inner Classes:
+Canonical:android.test.anno.ClassWithInnerClasses.InnerClass Simple:InnerClass
+Canonical:null Simple:
+
+Get annotation with missing class should not throw
+Got expected TypeNotPresentException
diff --git a/test/005-annotations/src/android/test/anno/AnnoMissingClass.java b/test/005-annotations/src/android/test/anno/AnnoMissingClass.java
new file mode 100644
index 0000000..c32e9a2
--- /dev/null
+++ b/test/005-annotations/src/android/test/anno/AnnoMissingClass.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+
+package android.test.anno;
+
+import java.lang.annotation.*;
+
+@Retention(RetentionPolicy.RUNTIME)
+public @interface AnnoMissingClass {
+    Class value();
+}
diff --git a/test/005-annotations/src/android/test/anno/ClassWithInnerClasses.java b/test/005-annotations/src/android/test/anno/ClassWithInnerClasses.java
new file mode 100644
index 0000000..e151f1a
--- /dev/null
+++ b/test/005-annotations/src/android/test/anno/ClassWithInnerClasses.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+
+package android.test.anno;
+
+public class ClassWithInnerClasses {
+  public class InnerClass {
+    public String toString() {
+      return "Canonical:" + getClass().getCanonicalName() + " Simple:" + getClass().getSimpleName();
+    }
+  }
+  Object anonymousClass = new Object() {
+    public String toString() {
+      return "Canonical:" + getClass().getCanonicalName() + " Simple:" + getClass().getSimpleName();
+    }
+  };
+
+  public void print() {
+    System.out.println(new InnerClass());
+    System.out.println(anonymousClass);
+  }
+}
diff --git a/test/005-annotations/src/android/test/anno/ClassWithMissingAnnotation.java b/test/005-annotations/src/android/test/anno/ClassWithMissingAnnotation.java
new file mode 100644
index 0000000..8cfdd8c
--- /dev/null
+++ b/test/005-annotations/src/android/test/anno/ClassWithMissingAnnotation.java
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+
+package android.test.anno;
+
+// Add annotation for missing type to cause TypeNotPresentException.
+@AnnoMissingClass(MissingAnnotation.class)
+public class ClassWithMissingAnnotation {
+}
diff --git a/test/005-annotations/src/android/test/anno/TestAnnotations.java b/test/005-annotations/src/android/test/anno/TestAnnotations.java
index 1deff33..7b74a73 100644
--- a/test/005-annotations/src/android/test/anno/TestAnnotations.java
+++ b/test/005-annotations/src/android/test/anno/TestAnnotations.java
@@ -180,5 +180,24 @@
         printAnnotationArray("    ", TestAnnotations.class.getPackage().getAnnotations());
         System.out.println("Package declared annotations:");
         printAnnotationArray("    ", TestAnnotations.class.getPackage().getDeclaredAnnotations());
+
+        System.out.println();
+
+        // Test inner classes.
+        System.out.println("Inner Classes:");
+        new ClassWithInnerClasses().print();
+
+        System.out.println();
+
+        // Test TypeNotPresentException.
+        try {
+            AnnoMissingClass missingAnno =
+                ClassWithMissingAnnotation.class.getAnnotation(AnnoMissingClass.class);
+            System.out.println("Get annotation with missing class should not throw");
+            System.out.println(missingAnno.value());
+            System.out.println("Getting value of missing annotaton should have thrown");
+        } catch (TypeNotPresentException expected) {
+            System.out.println("Got expected TypeNotPresentException");
+        }
     }
 }