Skip to content

Commit

Permalink
Allow static advice to see hidden APIs
Browse files Browse the repository at this point in the history
This is necessary as the advice might be called from such a hidden
method and it need to investigate the properties of this method.
  • Loading branch information
moltmann authored and drewhannay committed Jul 13, 2018
1 parent b69e0c4 commit b1aac83
Showing 1 changed file with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.mockito.plugins.MockMaker;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
Expand Down Expand Up @@ -72,6 +73,31 @@ public final class InlineStaticMockMaker implements MockMaker {
+ "\n\nPotentially, the current VM does not support the jvmti API " +
"correctly", ioe);
}

// 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 allowHiddenApiReflectionFrom;
try {
Class vmDebug = Class.forName("dalvik.system.VMDebug");
allowHiddenApiReflectionFrom = vmDebug.getDeclaredMethod(
"allowHiddenApiReflectionFrom", Class.class);
} catch (ClassNotFoundException | NoSuchMethodException e) {
throw new IllegalStateException("Cannot find "
+ "VMDebug#allowHiddenApiReflectionFrom.");
}

// The StaticMockMethodAdvice is used by methods of spies to call the real methods. As
// the real methods might be blacklisted, this class needs to be marked as trusted.
try {
allowHiddenApiReflectionFrom.invoke(null, StaticMockMethodAdvice.class);
} catch (InvocationTargetException e) {
throw e.getCause();
}
} catch (Throwable throwable) {
agent = null;
initializationError = throwable;
Expand Down

0 comments on commit b1aac83

Please sign in to comment.