Merge "This fixes several of the VM tests." into dalvik-dev
diff --git a/src/class_linker.cc b/src/class_linker.cc
index 5cd300e..5033c24 100644
--- a/src/class_linker.cc
+++ b/src/class_linker.cc
@@ -1147,8 +1147,14 @@
                                                               class_name_object.get()));
     cause.reset(env->ExceptionOccurred());
     if (cause.get() != NULL) {
+      // Throw LinkageErrors unmolested...
       env->ExceptionClear();
-      // Failed to find class, so fall-through to throw NCDFE.
+      static jclass LinkageError_class = CacheClass(env, "java/lang/LinkageError");
+      if (env->IsInstanceOf(cause.get(), LinkageError_class)) {
+        env->Throw(cause.get());
+        return NULL;
+      }
+      // ...otherwise fall through and throw NCDFE.
     } else if (result.get() == NULL) {
       // broken loader - throw NPE to be compatible with Dalvik
       ThrowNullPointerException("ClassLoader.loadClass returned null for %s",
diff --git a/src/dalvik_system_DexFile.cc b/src/dalvik_system_DexFile.cc
index ee36f99..dd91eac 100644
--- a/src/dalvik_system_DexFile.cc
+++ b/src/dalvik_system_DexFile.cc
@@ -181,12 +181,15 @@
   class_linker->RegisterDexFile(*dex_file);
   Class* result = class_linker->DefineClass(descriptor, class_loader, *dex_file, *dex_class_def);
   if (env->ExceptionCheck()) {
-    // If we threw a ClassNotFoundException, stifle it, since the contract in the caller
-    // says we simply return null if the class is not found.
+    // Swallow any ClassNotFoundException or NoClassDefFoundError; the contract with the caller
+    // is that we return null if the class is not found.
     jthrowable exception = env->ExceptionOccurred();
     env->ExceptionClear();
-    ScopedLocalRef<jclass> exception_class(env, env->FindClass("java/lang/ClassNotFoundException"));
-    if (!env->IsInstanceOf(exception, exception_class.get())) {
+
+    static jclass ClassNotFoundException_class = CacheClass(env, "java/lang/ClassNotFoundException");
+    static jclass NoClassDefFoundError_class = CacheClass(env, "java/lang/NoClassDefFoundError");
+
+    if (!env->IsInstanceOf(exception, ClassNotFoundException_class) && !env->IsInstanceOf(exception, NoClassDefFoundError_class)) {
       env->Throw(exception);
     }
     return NULL;
diff --git a/src/java_lang_Class.cc b/src/java_lang_Class.cc
index 996d6a4..29f1d23 100644
--- a/src/java_lang_Class.cc
+++ b/src/java_lang_Class.cc
@@ -51,7 +51,6 @@
   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
   Class* c = class_linker->FindClass(descriptor.c_str(), class_loader);
   if (c == NULL) {
-    // Convert NoClassDefFoundError to ClassNotFoundException.
     ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred());
     env->ExceptionClear();
     static jclass ClassNotFoundException_class = CacheClass(env, "java/lang/ClassNotFoundException");
diff --git a/test/003-omnibus-opcodes/expected.txt b/test/003-omnibus-opcodes/expected.txt
index 746e702..45fbf2c 100644
--- a/test/003-omnibus-opcodes/expected.txt
+++ b/test/003-omnibus-opcodes/expected.txt
@@ -81,19 +81,11 @@
 	at UnresTest2.run(UnresTest2.java:41)
 	at Main.run(Main.java:64)
 	at Main.main(Main.java:26)
-Caused by: java.lang.NoClassDefFoundError: Class "LUnresClass;" not found
-	at dalvik.system.DexFile.defineClass(Native Method)
-	at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:213)
-	at dalvik.system.DexPathList.findClass(DexPathList.java:315)
-	at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:58)
-	at java.lang.ClassLoader.loadClass(ClassLoader.java:509)
-	at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
-	... 3 more
-Caused by: java.lang.ClassNotFoundException: Didn't find class "UnresClass" on path: /data/art-test/003-omnibus-opcodes.jar
+Caused by: java.lang.ClassNotFoundException: Didn't find class "UnresClassSubclass" on path: /data/art-test/003-omnibus-opcodes.jar
 	at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
 	at java.lang.ClassLoader.loadClass(ClassLoader.java:509)
 	at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
-	... 9 more
+	... 3 more
 UnresTest2 done
 InternedString.run
 Done!