diff options
author | Philip P. Moltmann <moltmann@google.com> | 2018-05-21 18:55:46 +0000 |
---|---|---|
committer | Philip P. Moltmann <moltmann@google.com> | 2018-05-21 19:01:12 +0000 |
commit | 8738f6764b9f61eb11ad678cad6d9434b4133731 (patch) | |
tree | f96ee40a9b2c56f710f1d6071ef3c4f40ea5c316 /dexmaker | |
parent | 5529c6ea5fd4cb3959f11c9b4b114361a3db2f73 (diff) | |
download | dexmaker-8738f6764b9f61eb11ad678cad6d9434b4133731.tar.gz |
DO NOT MERGE: Revert "Allow mocks to call blackisted APIs"
This reverts commit 5529c6ea5fd4cb3959f11c9b4b114361a3db2f73.
Bug: b/80041014
Change-Id: I01752919321eb5f7bfca8b069c3b13edb5d6ce95
Diffstat (limited to 'dexmaker')
-rw-r--r-- | dexmaker/src/main/java/com/android/dx/DexMaker.java | 44 | ||||
-rw-r--r-- | dexmaker/src/main/java/com/android/dx/stock/ProxyBuilder.java | 14 |
2 files changed, 0 insertions, 58 deletions
diff --git a/dexmaker/src/main/java/com/android/dx/DexMaker.java b/dexmaker/src/main/java/com/android/dx/DexMaker.java index 755c9fa..02baa9b 100644 --- a/dexmaker/src/main/java/com/android/dx/DexMaker.java +++ b/dexmaker/src/main/java/com/android/dx/DexMaker.java @@ -49,8 +49,6 @@ import static com.android.dx.rop.code.AccessFlags.ACC_CONSTRUCTOR; import static java.lang.reflect.Modifier.PRIVATE; import static java.lang.reflect.Modifier.STATIC; -import android.util.Log; - /** * Generates a <strong>D</strong>alvik <strong>EX</strong>ecutable (dex) * file for execution on Android. Dex files define classes and interfaces, @@ -198,12 +196,9 @@ import android.util.Log; * }</pre> */ public final class DexMaker { - private static final String LOG_TAG = DexMaker.class.getSimpleName(); - private final Map<TypeId<?>, TypeDeclaration> types = new LinkedHashMap<>(); private ClassLoader sharedClassLoader; private DexFile outputDex; - private boolean markAsTrusted; /** * Creates a new {@code DexMaker} instance, which can be used to create a @@ -364,51 +359,12 @@ public final class DexMaker { return "Generated_" + checksum +".jar"; } - /** - * Set shared class loader to use. - * - * <p>If a class wants to call package private methods of another class they need to share a - * class loader. One common case for this requirement is a mock class wanting to mock package - * private methods of the original class. - * - * @param classLoader the class loader the new class should be loaded by - */ public void setSharedClassLoader(ClassLoader classLoader) { this.sharedClassLoader = classLoader; } - public void markAsTrusted() { - this.markAsTrusted = true; - } - private ClassLoader generateClassLoader(File result, File dexCache, ClassLoader parent) { try { - // Try to load the class so that it can call hidden APIs. This is required for spying - // on system classes as real-methods of these classes might call blacklisted APIs - if (markAsTrusted) { - try { - if (sharedClassLoader != null) { - ClassLoader loader = parent != null ? parent : sharedClassLoader; - loader.getClass().getMethod("addDexPath", String.class, - Boolean.TYPE).invoke(loader, result.getPath(), true); - return loader; - } else { - return (ClassLoader) Class.forName("dalvik.system.BaseDexClassLoader") - .getConstructor(String.class, File.class, String.class, - ClassLoader.class, Boolean.TYPE) - .newInstance(result.getPath(), dexCache.getAbsoluteFile(), null, - parent, true); - } - } catch (InvocationTargetException e) { - if (e.getCause() instanceof SecurityException) { - Log.i(LOG_TAG, "Cannot allow to call blacklisted super methods. This might " - + "break spying on system classes.", e.getCause()); - } else { - throw e; - } - } - } - if (sharedClassLoader != null) { ClassLoader loader = parent != null ? parent : sharedClassLoader; loader.getClass().getMethod("addDexPath", String.class).invoke(loader, diff --git a/dexmaker/src/main/java/com/android/dx/stock/ProxyBuilder.java b/dexmaker/src/main/java/com/android/dx/stock/ProxyBuilder.java index 053fb16..1363894 100644 --- a/dexmaker/src/main/java/com/android/dx/stock/ProxyBuilder.java +++ b/dexmaker/src/main/java/com/android/dx/stock/ProxyBuilder.java @@ -47,8 +47,6 @@ import static java.lang.reflect.Modifier.PRIVATE; import static java.lang.reflect.Modifier.PUBLIC; import static java.lang.reflect.Modifier.STATIC; -import android.os.Build; - /** * Creates dynamic proxies of concrete classes. * <p> @@ -303,18 +301,6 @@ public final class ProxyBuilder<T> { if (sharedClassLoader) { dexMaker.setSharedClassLoader(baseClass.getClassLoader()); } - if (Build.VERSION.SDK_INT >= 28) { - // The proxied class might have blacklisted methods. Blacklisting methods (and fields) - // is a new feature of Android P: - // - // https://android-developers.googleblog.com/2018/02/ - // improving-stability-by-reducing-usage.html - // - // The newly generated class might not be allowed to call methods of the proxied class - // if it is not trusted. As it is not clear which classes have blacklisted methods, mark - // all generated classes as trusted. - dexMaker.markAsTrusted(); - } ClassLoader classLoader = dexMaker.generateAndLoad(parentClassLoader, dexCache); try { proxyClass = loadClass(classLoader, generatedName); |