Fix FastNative/CriticalNative annotation lookup.

Avoid calling ClassLinker::LookupResolvedType() with
a DexCache/ClassLoader mismatch.

Test: ANDROID_COMPILE_WITH_JACK=false testrunner.py --host -t 004-JniTest
Bug: 63089991
Change-Id: I323ece317274d1523586779a37d01c50b787ace4
diff --git a/runtime/dex_file_annotations.cc b/runtime/dex_file_annotations.cc
index 2b81f0a..ee202af 100644
--- a/runtime/dex_file_annotations.cc
+++ b/runtime/dex_file_annotations.cc
@@ -762,15 +762,16 @@
     }
     const uint8_t* annotation = annotation_item->annotation_;
     uint32_t type_index = DecodeUnsignedLeb128(&annotation);
+    ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
+    Thread* self = Thread::Current();
     mirror::Class* resolved_class;
     if (lookup_in_resolved_boot_classes) {
+      // Note: We cannot use ClassLinker::LookupResolvedType() because the current DexCache
+      // may not be registered with the boot class path ClassLoader and we must not pollute
+      // the DexCache with classes that are not in the associated ClassLoader's ClassTable.
+      const char* descriptor = dex_file.StringByTypeIdx(dex::TypeIndex(type_index));
       ObjPtr<mirror::Class> looked_up_class =
-          Runtime::Current()->GetClassLinker()->LookupResolvedType(
-              klass.GetDexFile(),
-              dex::TypeIndex(type_index),
-              klass.GetDexCache(),
-              // Force the use of the bootstrap class loader.
-              static_cast<mirror::ClassLoader*>(nullptr));
+          class_linker->LookupClass(self, descriptor, /* class_loader */ nullptr);
       resolved_class = looked_up_class.Ptr();
       if (resolved_class == nullptr) {
         // If `resolved_class` is null, this is fine: just ignore that
@@ -779,8 +780,8 @@
         continue;
       }
     } else {
-      StackHandleScope<2> hs(Thread::Current());
-      resolved_class = Runtime::Current()->GetClassLinker()->ResolveType(
+      StackHandleScope<2> hs(self);
+      resolved_class = class_linker->ResolveType(
           klass.GetDexFile(),
           dex::TypeIndex(type_index),
           hs.NewHandle(klass.GetDexCache()),
@@ -789,8 +790,8 @@
         std::string temp;
         LOG(WARNING) << StringPrintf("Unable to resolve %s annotation class %d",
                                      klass.GetRealClass()->GetDescriptor(&temp), type_index);
-        CHECK(Thread::Current()->IsExceptionPending());
-        Thread::Current()->ClearException();
+        CHECK(self->IsExceptionPending());
+        self->ClearException();
         continue;
       }
     }
diff --git a/test/knownfailures.json b/test/knownfailures.json
index 09e76fa..5a67fbc 100644
--- a/test/knownfailures.json
+++ b/test/knownfailures.json
@@ -707,13 +707,6 @@
         "variant": "gcstress & jit & target"
     },
     {
-        "tests": ["004-JniTest"],
-        "description": [ "Tests failing with --build-with-javac-dx since the new annotation",
-                         "lookup changes" ],
-        "bug": "b/63089991",
-        "env_vars": {"ANDROID_COMPILE_WITH_JACK": "false"}
-    },
-    {
         "tests": "660-clinit",
         "variant": "no-image | no-dex2oat | no-prebuild",
         "description": ["Tests <clinit> for app images, which --no-image, --no-prebuild and",