From 6d25cf6cbda1b95fb014a3fd22c01a6bdf47725e Mon Sep 17 00:00:00 2001 From: Mathieu Chartier Date: Tue, 12 Apr 2016 16:54:48 -0700 Subject: Fix InsertDexFileInToClassLoader to handle null class loaders Maybe used by legacy apps. This functionality is planned for deprecation. Bug: 27954959 (cherry picked from commit f2bf9d640e37b72be8b4f6016d4aa95a0e27b7b4) Change-Id: I1ae2cf1e33f2f1b237a41c8cc50d2a814a52a95a --- test/068-classloader/src/Main.java | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'test/068-classloader/src/Main.java') diff --git a/test/068-classloader/src/Main.java b/test/068-classloader/src/Main.java index 361e2938e3..b2d843b351 100644 --- a/test/068-classloader/src/Main.java +++ b/test/068-classloader/src/Main.java @@ -14,6 +14,9 @@ * limitations under the License. */ +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; + /** * Class loader test. */ @@ -62,6 +65,28 @@ public class Main { testSeparation(); testClassForName(); + + testNullClassLoader(); + } + + static void testNullClassLoader() { + try { + /* this is the "alternate" DEX/Jar file */ + String DEX_FILE = System.getenv("DEX_LOCATION") + "/068-classloader-ex.jar"; + /* on Dalvik, this is a DexFile; otherwise, it's null */ + Class mDexClass = Class.forName("dalvik.system.DexFile"); + Constructor ctor = mDexClass.getConstructor(new Class[] {String.class}); + Object mDexFile = ctor.newInstance(DEX_FILE); + Method meth = mDexClass.getMethod("loadClass", + new Class[] { String.class, ClassLoader.class }); + Object klass = meth.invoke(mDexFile, "Mutator", null); + if (klass == null) { + throw new AssertionError("loadClass with nullclass loader failed"); + } + } catch (Exception e) { + System.out.println(e); + } + System.out.println("Loaded class into null class loader"); } static void testSeparation() { -- cgit v1.2.3-59-g8ed1b