| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.testing; |
| 18 |
|
| 19 |
import android.util.Log; |
| 20 |
|
| 21 |
import com.android.internal.annotations.VisibleForTesting; |
| 22 |
|
| 23 |
import libcore.util.SneakyThrow; |
| 24 |
|
| 25 |
import org.junit.rules.TestRule; |
| 26 |
import org.junit.runner.Description; |
| 27 |
import org.junit.runners.model.Statement; |
| 28 |
|
| 29 |
import java.util.ConcurrentModificationException; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class DexmakerShareClassLoaderRule implements TestRule { |
| 40 |
|
| 41 |
private static final String TAG = "ShareClassloaderRule"; |
| 42 |
@VisibleForTesting |
| 43 |
static final String DEXMAKER_SHARE_CLASSLOADER_PROPERTY = "dexmaker.share_classloader"; |
| 44 |
|
| 45 |
private static Thread sOwningThread = null; |
| 46 |
|
| 47 |
@Override |
| 48 |
public Statement apply(Statement base, Description description) { |
| 49 |
return apply(base::evaluate).toStatement(); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public static void runWithDexmakerShareClassLoader(Runnable r) { |
| 60 |
try { |
| 61 |
apply(r::run).run(); |
| 62 |
} catch (Throwable t) { |
| 63 |
SneakyThrow.sneakyThrow(t); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
private static <T extends Throwable> ThrowingRunnable<T> apply(ThrowingRunnable<T> r) { |
| 72 |
return wrapInMutex(wrapInSetAndClearProperty(r)); |
| 73 |
} |
| 74 |
|
| 75 |
private static <T extends Throwable> ThrowingRunnable<T> wrapInSetAndClearProperty( |
| 76 |
ThrowingRunnable<T> r) { |
| 77 |
return () -> { |
| 78 |
final String previousValue = System.getProperty(DEXMAKER_SHARE_CLASSLOADER_PROPERTY); |
| 79 |
try { |
| 80 |
System.setProperty(DEXMAKER_SHARE_CLASSLOADER_PROPERTY, "true"); |
| 81 |
r.run(); |
| 82 |
} finally { |
| 83 |
if (previousValue != null) { |
| 84 |
System.setProperty(DEXMAKER_SHARE_CLASSLOADER_PROPERTY, previousValue); |
| 85 |
} else { |
| 86 |
System.clearProperty(DEXMAKER_SHARE_CLASSLOADER_PROPERTY); |
| 87 |
} |
| 88 |
} |
| 89 |
}; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
private static <T extends Throwable> ThrowingRunnable<T> wrapInMutex(ThrowingRunnable<T> r) { |
| 96 |
return () -> { |
| 97 |
final boolean isOwner; |
| 98 |
synchronized (DexmakerShareClassLoaderRule.class) { |
| 99 |
isOwner = (sOwningThread == null); |
| 100 |
if (isOwner) { |
| 101 |
sOwningThread = Thread.currentThread(); |
| 102 |
} else if (sOwningThread != Thread.currentThread()) { |
| 103 |
final RuntimeException e = new ConcurrentModificationException( |
| 104 |
"Tried to set dexmaker.share_classloader from " + Thread.currentThread() |
| 105 |
+ ", but was already set from " + sOwningThread); |
| 106 |
|
| 107 |
Log.e(TAG, e.getMessage(), e); |
| 108 |
throw e; |
| 109 |
} |
| 110 |
} |
| 111 |
try { |
| 112 |
r.run(); |
| 113 |
} finally { |
| 114 |
synchronized (DexmakerShareClassLoaderRule.class) { |
| 115 |
if (isOwner) { |
| 116 |
sOwningThread = null; |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
}; |
| 121 |
} |
| 122 |
|
| 123 |
private interface ThrowingRunnable<T extends Throwable> { |
| 124 |
void run() throws T; |
| 125 |
|
| 126 |
default Statement toStatement() { |
| 127 |
return new Statement() { |
| 128 |
@Override |
| 129 |
public void evaluate() throws Throwable { |
| 130 |
ThrowingRunnable.this.run(); |
| 131 |
} |
| 132 |
}; |
| 133 |
} |
| 134 |
} |
| 135 |
} |