aboutsummaryrefslogtreecommitdiff
path: root/dexmaker-mockito
diff options
context:
space:
mode:
authorPhilip P. Moltmann <moltmann@google.com>2018-05-21 19:15:56 +0000
committerPhilip P. Moltmann <moltmann@google.com>2018-05-21 13:09:05 -0700
commit314cb2efb7b1d8d9b584a6e0bd82727168cfd181 (patch)
tree3b848189d1bc1a2238caf1d1285953232c35806a /dexmaker-mockito
parent8738f6764b9f61eb11ad678cad6d9434b4133731 (diff)
downloaddexmaker-pie-dr1-dev.tar.gz
This reverts commit 8738f6764b9f61eb11ad678cad6d9434b4133731. Reason for revert: This was only a temporary revert. Now that the real issue is fixed, we can submit this again Bug: 80041014 Test: vts-tradefed run host --class com.android.tradefed.device.metric.VtsCoverageCollectorTest atest CtsActivityManagerDeviceTestCases atest CtsInputMethodTestCases Change-Id: I8d0ac45e3fe73c1870cf08f989b6d39f09923183
Diffstat (limited to 'dexmaker-mockito')
-rw-r--r--dexmaker-mockito/src/main/java/com/android/dx/mockito/DexmakerMockMaker.java40
1 files changed, 40 insertions, 0 deletions
diff --git a/dexmaker-mockito/src/main/java/com/android/dx/mockito/DexmakerMockMaker.java b/dexmaker-mockito/src/main/java/com/android/dx/mockito/DexmakerMockMaker.java
index b19f7bd..19f371e 100644
--- a/dexmaker-mockito/src/main/java/com/android/dx/mockito/DexmakerMockMaker.java
+++ b/dexmaker-mockito/src/main/java/com/android/dx/mockito/DexmakerMockMaker.java
@@ -16,15 +16,21 @@
package com.android.dx.mockito;
+import android.os.Build;
+import android.util.Log;
+
import com.android.dx.stock.ProxyBuilder;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.exceptions.stacktrace.StackTraceCleaner;
+import org.mockito.internal.util.reflection.LenientCopyTool;
import org.mockito.invocation.MockHandler;
import org.mockito.mock.MockCreationSettings;
import org.mockito.plugins.MockMaker;
import org.mockito.plugins.StackTraceCleanerProvider;
import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import java.util.Set;
@@ -33,8 +39,42 @@ import java.util.Set;
* Generates mock instances on Android's runtime.
*/
public final class DexmakerMockMaker implements MockMaker, StackTraceCleanerProvider {
+ private static final String LOG_TAG = DexmakerMockMaker.class.getSimpleName();
+
private final UnsafeAllocator unsafeAllocator = UnsafeAllocator.create();
+ public DexmakerMockMaker() {
+ if (Build.VERSION.SDK_INT >= 28) {
+ // Blacklisted APIs were introduced in Android P:
+ //
+ // https://android-developers.googleblog.com/2018/02/
+ // improving-stability-by-reducing-usage.html
+ //
+ // This feature prevents access to blacklisted fields and calling of blacklisted APIs
+ // if the calling class is not trusted.
+ Method allowHiddenApiReflectionFromMethod;
+ try {
+ Class vmDebug = Class.forName("dalvik.system.VMDebug");
+ allowHiddenApiReflectionFromMethod = vmDebug.getDeclaredMethod(
+ "allowHiddenApiReflectionFrom", Class.class);
+ } catch (ClassNotFoundException | NoSuchMethodException e) {
+ throw new IllegalStateException(
+ "Cannot find VMDebug#allowHiddenApiReflectionFrom. Method is needed to "
+ + "allow spies to copy blacklisted fields.");
+ }
+
+ // The LenientCopyTool copies the fields to a spy when creating the copy from an
+ // existing object. Some of the fields might be blacklisted. Marking the LenientCopyTool
+ // as trusted allows the tool to copy all fields, including the blacklisted ones.
+ try {
+ allowHiddenApiReflectionFromMethod.invoke(null, LenientCopyTool.class);
+ } catch (InvocationTargetException | IllegalAccessException e) {
+ Log.w(LOG_TAG, "Cannot allow LenientCopyTool to copy spies of blacklisted fields. "
+ + "This might break spying on system classes.");
+ }
+ }
+ }
+
@Override
public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) {
Class<T> typeToMock = settings.getTypeToMock();