From 84a790358b95e0f79be3a6e69ae9a43dc113f921 Mon Sep 17 00:00:00 2001 From: Andreas Gampe Date: Mon, 21 Sep 2015 11:25:10 -0700 Subject: [PATCH] ART: PathClassLoader test Add a test checking that subclassing PathClassLoader works as expected. Change-Id: Ic6bb733b748df5e29b21df021f302ed4237b0f99 --- test/142-classloader2/expected.txt | 1 + test/142-classloader2/info.txt | 1 + .../142-classloader2/smali/MyPathClassLoader.smali | 13 ++++ test/142-classloader2/src-ex/A.java | 22 +++++++ test/142-classloader2/src/A.java | 22 +++++++ test/142-classloader2/src/Main.java | 76 ++++++++++++++++++++++ 6 files changed, 135 insertions(+) create mode 100644 test/142-classloader2/expected.txt create mode 100644 test/142-classloader2/info.txt create mode 100644 test/142-classloader2/smali/MyPathClassLoader.smali create mode 100644 test/142-classloader2/src-ex/A.java create mode 100644 test/142-classloader2/src/A.java create mode 100644 test/142-classloader2/src/Main.java diff --git a/test/142-classloader2/expected.txt b/test/142-classloader2/expected.txt new file mode 100644 index 000000000..86f5e220e --- /dev/null +++ b/test/142-classloader2/expected.txt @@ -0,0 +1 @@ +Everything OK. diff --git a/test/142-classloader2/info.txt b/test/142-classloader2/info.txt new file mode 100644 index 000000000..eb821a8dd --- /dev/null +++ b/test/142-classloader2/info.txt @@ -0,0 +1 @@ +Check sub-classing of PathClassLoader. diff --git a/test/142-classloader2/smali/MyPathClassLoader.smali b/test/142-classloader2/smali/MyPathClassLoader.smali new file mode 100644 index 000000000..553abd46c --- /dev/null +++ b/test/142-classloader2/smali/MyPathClassLoader.smali @@ -0,0 +1,13 @@ +# Simple subclass of PathClassLoader with methods overridden. +# We need to use smali right now to subclass a libcore class, see b/24304298. + +.class public LMyPathClassLoader; + +.super Ldalvik/system/PathClassLoader; + +# Simple forwarding constructor. +.method public constructor (Ljava/lang/String;Ljava/lang/ClassLoader;)V + .registers 3 + invoke-direct {p0, p1, p2}, Ldalvik/system/PathClassLoader;->(Ljava/lang/String;Ljava/lang/ClassLoader;)V + return-void +.end method diff --git a/test/142-classloader2/src-ex/A.java b/test/142-classloader2/src-ex/A.java new file mode 100644 index 000000000..d5fa1f9df --- /dev/null +++ b/test/142-classloader2/src-ex/A.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. + */ + +/** + * Identical class to the main src, except with a different value, so we can distinguish them. + */ +public class A { + public static String value = "Ex-A"; +} diff --git a/test/142-classloader2/src/A.java b/test/142-classloader2/src/A.java new file mode 100644 index 000000000..532df5187 --- /dev/null +++ b/test/142-classloader2/src/A.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. + */ + +/** + * Main class, with a simple value. + */ +public class A { + public static String value = "Src-A"; +} diff --git a/test/142-classloader2/src/Main.java b/test/142-classloader2/src/Main.java new file mode 100644 index 000000000..86c61ebc3 --- /dev/null +++ b/test/142-classloader2/src/Main.java @@ -0,0 +1,76 @@ +/* + * 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. + */ + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; + +/** + * PathClassLoader test. + */ +public class Main { + + private static ClassLoader createClassLoader(String dexPath, ClassLoader parent) { + try { + Class myClassLoaderClass = Class.forName("MyPathClassLoader"); + Constructor constructor = myClassLoaderClass.getConstructor(String.class, + ClassLoader.class); + return (ClassLoader)constructor.newInstance(dexPath, parent); + } catch (Exception e) { + // Ups, not available?!?! + throw new RuntimeException(e); + } + } + + /** + * Main entry point. + */ + public static void main(String[] args) throws Exception { + // Check the class-path for the second file. We'll use that one as the source of the + // new classloader. + String cp = System.getProperty("java.class.path"); + if (cp.split(System.getProperty("path.separator")).length != 1) { + throw new IllegalStateException("Didn't find exactly one classpath element in " + cp); + } + if (!cp.endsWith("classloader2.jar")) { + throw new IllegalStateException("Don't understand classpath " + cp); + } + cp = cp.replace("classloader2.jar", "classloader2-ex.jar"); + + ClassLoader myClassLoader = createClassLoader( + cp, ClassLoader.getSystemClassLoader().getParent()); + + // Now load our test class. + Class srcClass = A.class; + Class exClass = myClassLoader.loadClass("A"); + + // First check: classes should be different. + if (srcClass == exClass) { + throw new IllegalStateException("Loaded class instances are the same"); + } + + // Secondary checks: get the static field values and make sure they aren't the same. + String srcValue = (String)srcClass.getDeclaredField("value").get(null); + if (!"Src-A".equals(srcValue)) { + throw new IllegalStateException("Expected Src-A, found " + srcValue); + } + String exValue = (String)exClass.getDeclaredField("value").get(null); + if (!"Ex-A".equals(exValue)) { + throw new IllegalStateException("Expected Ex-A, found " + exValue); + } + + System.out.println("Everything OK."); + } +} -- 2.11.0