Merge changes from topic "aospMockitoBackport"
* changes:
(Resubmit) Do not read Build.VERSION to allow non-standard Android distributions
(Resubmit) Allow to share classloader via dexmaker.share_classloader system property
Update dexmaker to current master (018d62e74446b2939a905fbc116cf816d4235846)
diff --git a/README.version b/README.version
index 5a8b61a..4d54161 100644
--- a/README.version
+++ b/README.version
@@ -1,5 +1,5 @@
URL: https://github.com/linkedin/dexmaker/
-Version: 2.19.0 (8532233e653b5178ce1e70016987ff776e7149f4)
+Version: master (018d62e74446b2939a905fbc116cf816d4235846)
License: Apache 2.0
Description:
Dexmaker is a Java-language API for doing compile time or runtime code generation targeting the Dalvik VM. Unlike cglib or ASM, this library creates Dalvik .dex files instead of Java .class files.
@@ -10,6 +10,5 @@
Local Modifications:
Add ability to run dexmaker tests from within the source tree (I1b146841099b54f64d4a7dfe743b88717793619a)
- Allow to share classloader via dexmaker.share_classloader system property (Ia73198937e2e505f3baa96486f378fb8dc62d6d5)
- Do not read Build.VERSION to allow non-standard Android distributions (I0b647514a306da979f7fdf96d5e5f8ae5e7ec945)
- Fix caching of shared class loader proxies (needs upstream, I33c12311cb17366b936f4716c1bf89a5d2e18074)
+ Allow to share classloader via dexmaker.share_classloader system property (I324cddd644610eef811c620a1fccf6a24b2b9406)
+ Do not read Build.VERSION to allow non-standard Android distributions (Ia8c4ba4c82cd6f193c565f1bfe48faffc4aac08f)
diff --git a/dexmaker-mockito-inline-dispatcher/build.gradle b/dexmaker-mockito-inline-dispatcher/build.gradle
index 9ea6567..2251364 100644
--- a/dexmaker-mockito-inline-dispatcher/build.gradle
+++ b/dexmaker-mockito-inline-dispatcher/build.gradle
@@ -2,7 +2,7 @@
android {
compileSdkVersion 28
- buildToolsVersion '28.0.0'
+ buildToolsVersion '28.0.1'
defaultConfig {
applicationId 'com.android.dexmaker.mockito.inline.dispatcher'
@@ -11,3 +11,8 @@
versionName VERSION_NAME
}
}
+
+repositories {
+ jcenter()
+ google()
+}
diff --git a/dexmaker-mockito-inline-extended-tests/build.gradle b/dexmaker-mockito-inline-extended-tests/build.gradle
index 20a8e16..8695cde 100644
--- a/dexmaker-mockito-inline-extended-tests/build.gradle
+++ b/dexmaker-mockito-inline-extended-tests/build.gradle
@@ -14,6 +14,7 @@
android {
compileSdkVersion 28
+ buildToolsVersion '28.0.1'
android {
lintOptions {
@@ -46,8 +47,8 @@
androidTestImplementation project(':dexmaker-mockito-inline-extended')
implementation 'junit:junit:4.12'
- implementation 'com.android.support.test:runner:1.0.1'
+ implementation 'com.android.support.test:runner:1.0.2'
implementation 'com.android.support.test:rules:1.0.2'
- api 'org.mockito:mockito-core:2.19.0', { exclude group: 'net.bytebuddy' }
+ api 'org.mockito:mockito-core:2.21.0', { exclude group: 'net.bytebuddy' }
}
diff --git a/dexmaker-mockito-inline-extended-tests/src/main/java/com/android/dx/mockito/inline/extended/tests/MockStatic.java b/dexmaker-mockito-inline-extended-tests/src/main/java/com/android/dx/mockito/inline/extended/tests/MockStatic.java
index 3d1671e..aed493b 100644
--- a/dexmaker-mockito-inline-extended-tests/src/main/java/com/android/dx/mockito/inline/extended/tests/MockStatic.java
+++ b/dexmaker-mockito-inline-extended-tests/src/main/java/com/android/dx/mockito/inline/extended/tests/MockStatic.java
@@ -73,6 +73,18 @@
}
}
+ private static class NoDefaultConstructorClass {
+ private static int mLastId;
+
+ NoDefaultConstructorClass(int id) {
+ mLastId = id;
+ }
+
+ static int getLastId() {
+ return mLastId;
+ }
+ }
+
@Test
public void spyStatic() throws Exception {
ContentResolver resolver = InstrumentationRegistry.getTargetContext().getContentResolver();
@@ -101,6 +113,23 @@
}
@Test
+ public void spyStaticOnObjectWithNoDefaultConstructor() throws Exception {
+ new NoDefaultConstructorClass(23);
+
+ MockitoSession session = mockitoSession().spyStatic(NoDefaultConstructorClass.class)
+ .startMocking();
+ try {
+ // Starting the spying hasn't change the static state of the class.
+ assertEquals(23, NoDefaultConstructorClass.getLastId());
+
+ when(NoDefaultConstructorClass.getLastId()).thenReturn(42);
+ assertEquals(42, NoDefaultConstructorClass.getLastId());
+ } finally {
+ session.finishMocking();
+ }
+ }
+
+ @Test
public void mockStatic() throws Exception {
ContentResolver resolver = InstrumentationRegistry.getTargetContext().getContentResolver();
String deviceName = Settings.Global.getString(resolver, DEVICE_NAME);
diff --git a/dexmaker-mockito-inline-extended/build.gradle b/dexmaker-mockito-inline-extended/build.gradle
index 055168b..b5e42cb 100644
--- a/dexmaker-mockito-inline-extended/build.gradle
+++ b/dexmaker-mockito-inline-extended/build.gradle
@@ -19,7 +19,7 @@
android {
compileSdkVersion 28
- buildToolsVersion '28.0.0'
+ buildToolsVersion '28.0.1'
android {
lintOptions {
@@ -119,5 +119,5 @@
dependencies {
implementation project(':dexmaker-mockito-inline')
- implementation 'org.mockito:mockito-core:2.19.0', { exclude group: 'net.bytebuddy' }
+ implementation 'org.mockito:mockito-core:2.21.0', { exclude group: 'net.bytebuddy' }
}
diff --git a/dexmaker-mockito-inline-extended/src/main/java/com/android/dx/mockito/inline/InlineStaticMockMaker.java b/dexmaker-mockito-inline-extended/src/main/java/com/android/dx/mockito/inline/InlineStaticMockMaker.java
index cf6a95b..f764e07 100644
--- a/dexmaker-mockito-inline-extended/src/main/java/com/android/dx/mockito/inline/InlineStaticMockMaker.java
+++ b/dexmaker-mockito-inline-extended/src/main/java/com/android/dx/mockito/inline/InlineStaticMockMaker.java
@@ -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;
@@ -72,6 +73,31 @@
+ "\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;
diff --git a/dexmaker-mockito-inline-extended/src/main/java/com/android/dx/mockito/inline/extended/ExtendedMockito.java b/dexmaker-mockito-inline-extended/src/main/java/com/android/dx/mockito/inline/extended/ExtendedMockito.java
index 2d8acc8..19da75b 100644
--- a/dexmaker-mockito-inline-extended/src/main/java/com/android/dx/mockito/inline/extended/ExtendedMockito.java
+++ b/dexmaker-mockito-inline-extended/src/main/java/com/android/dx/mockito/inline/extended/ExtendedMockito.java
@@ -202,19 +202,19 @@
* converted by this method all references to the already existing object will be affected by
* the stubbing.
*
- * @param toMock The existing object to convert into a spy
+ * @param toSpy The existing object to convert into a spy
*/
@UnstableApi
@SuppressWarnings("CheckReturnValue")
- public static void spyOn(Object toMock) {
+ public static void spyOn(Object toSpy) {
if (onSpyInProgressInstance.get() != null) {
throw new IllegalStateException("Cannot set up spying on an existing object while "
+ "setting up spying for another existing object");
}
- onSpyInProgressInstance.set(toMock);
+ onSpyInProgressInstance.set(toSpy);
try {
- spy(toMock);
+ spy(toSpy);
} finally {
onSpyInProgressInstance.remove();
}
diff --git a/dexmaker-mockito-inline-extended/src/main/java/com/android/dx/mockito/inline/extended/StaticMockitoSessionBuilder.java b/dexmaker-mockito-inline-extended/src/main/java/com/android/dx/mockito/inline/extended/StaticMockitoSessionBuilder.java
index 89d619f..c40fc89 100644
--- a/dexmaker-mockito-inline-extended/src/main/java/com/android/dx/mockito/inline/extended/StaticMockitoSessionBuilder.java
+++ b/dexmaker-mockito-inline-extended/src/main/java/com/android/dx/mockito/inline/extended/StaticMockitoSessionBuilder.java
@@ -26,6 +26,9 @@
import java.util.ArrayList;
+import static org.mockito.Mockito.CALLS_REAL_METHODS;
+import static org.mockito.Mockito.withSettings;
+
/**
* Same as {@link MockitoSessionBuilder} but adds the ability to stub static methods
* calls via {@link #mockStatic(Class)}, {@link #mockStatic(Class, Answer)}, and
@@ -97,7 +100,8 @@
*/
@UnstableApi
public <T> StaticMockitoSessionBuilder spyStatic(Class<T> clazz) {
- staticMockings.add(new StaticMocking<>(clazz, () -> Mockito.spy(clazz)));
+ staticMockings.add(new StaticMocking<>(clazz, () -> Mockito.mock(clazz, withSettings()
+ .defaultAnswer(CALLS_REAL_METHODS))));
return this;
}
diff --git a/dexmaker-mockito-inline-tests/build.gradle b/dexmaker-mockito-inline-tests/build.gradle
index 5c86f89..27df02b 100644
--- a/dexmaker-mockito-inline-tests/build.gradle
+++ b/dexmaker-mockito-inline-tests/build.gradle
@@ -14,6 +14,7 @@
android {
compileSdkVersion 28
+ buildToolsVersion '28.0.1'
android {
lintOptions {
@@ -48,6 +49,6 @@
androidTestImplementation project(':dexmaker-mockito-inline')
implementation 'junit:junit:4.12'
- implementation 'com.android.support.test:runner:1.0.1'
- api 'org.mockito:mockito-core:2.19.0', { exclude group: 'net.bytebuddy' }
+ implementation 'com.android.support.test:runner:1.0.2'
+ api 'org.mockito:mockito-core:2.21.0', { exclude group: 'net.bytebuddy' }
}
diff --git a/dexmaker-mockito-inline/build.gradle b/dexmaker-mockito-inline/build.gradle
index 96ad295..8f7cfa2 100644
--- a/dexmaker-mockito-inline/build.gradle
+++ b/dexmaker-mockito-inline/build.gradle
@@ -19,7 +19,7 @@
android {
compileSdkVersion 28
- buildToolsVersion '28.0.0'
+ buildToolsVersion '28.0.1'
android {
lintOptions {
@@ -113,6 +113,6 @@
dependencies {
implementation project(':dexmaker')
- implementation 'org.mockito:mockito-core:2.19.0', { exclude group: 'net.bytebuddy' }
+ implementation 'org.mockito:mockito-core:2.21.0', { exclude group: 'net.bytebuddy' }
}
diff --git a/dexmaker-mockito-tests/build.gradle b/dexmaker-mockito-tests/build.gradle
index 70f3dfa..82aeaaf 100644
--- a/dexmaker-mockito-tests/build.gradle
+++ b/dexmaker-mockito-tests/build.gradle
@@ -42,5 +42,5 @@
implementation 'com.android.support.test:runner:0.5'
implementation 'junit:junit:4.12'
- api 'org.mockito:mockito-core:2.19.0', { exclude group: 'net.bytebuddy' }
+ api 'org.mockito:mockito-core:2.21.0', { exclude group: 'net.bytebuddy' }
}
diff --git a/dexmaker-mockito/build.gradle b/dexmaker-mockito/build.gradle
index 60cb77c..46cdd8f 100644
--- a/dexmaker-mockito/build.gradle
+++ b/dexmaker-mockito/build.gradle
@@ -26,5 +26,5 @@
dependencies {
implementation project(':dexmaker')
- implementation 'org.mockito:mockito-core:2.19.0', { exclude group: 'net.bytebuddy' }
+ implementation 'org.mockito:mockito-core:2.21.0', { exclude group: 'net.bytebuddy' }
}
diff --git a/dexmaker-tests/build.gradle b/dexmaker-tests/build.gradle
index 6cf2958..223f2f1 100644
--- a/dexmaker-tests/build.gradle
+++ b/dexmaker-tests/build.gradle
@@ -14,7 +14,7 @@
android {
compileSdkVersion 28
- buildToolsVersion '28.0.0'
+ buildToolsVersion '28.0.1'
defaultConfig {
applicationId 'com.linkedin.dexmaker'