Skip to content

Commit 5eadef2

Browse files
committed
Static Call Cache
1 parent 33628f5 commit 5eadef2

File tree

3 files changed

+71
-30
lines changed

3 files changed

+71
-30
lines changed

app/src/main/java/top/niunaijun/app/MainActivity.java

+38-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package top.niunaijun.app;
22

3-
import androidx.appcompat.app.AppCompatActivity;
4-
53
import android.os.Bundle;
64
import android.util.Log;
75

6+
import androidx.appcompat.app.AppCompatActivity;
7+
88
import java.lang.reflect.Field;
9+
import java.lang.reflect.Method;
910

1011
import top.niunaijun.app.bean.TestReflection;
12+
import top.niunaijun.app.ref.BRActivityThread;
1113
import top.niunaijun.app.ref.BRTestReflection;
14+
import top.niunaijun.blackreflection.BlackReflection;
1215
import top.niunaijun.blackreflection.R;
1316

1417

@@ -19,6 +22,7 @@ public class MainActivity extends AppCompatActivity {
1922
protected void onCreate(Bundle savedInstanceState) {
2023
super.onCreate(savedInstanceState);
2124
setContentView(R.layout.activity_main);
25+
BlackReflection.CACHE = true;
2226

2327
TestReflection testReflection = testBConstructor();
2428
Class<?> classReady = BRTestReflection.getRealClass();
@@ -67,8 +71,40 @@ protected void onCreate(Bundle savedInstanceState) {
6771

6872
// use @BParamClassName
6973
BRTestReflection.get().testParamClassName("i am java.lang.String", 0);
74+
75+
findViewById(R.id.btn_start).setOnClickListener(v -> {
76+
try {
77+
int count = 10000;
78+
long l = System.currentTimeMillis();
79+
for (int i = 0; i < count; i++) {
80+
testSystemReflection();
81+
}
82+
Log.d(TAG, "SystemReflection: " + (System.currentTimeMillis() - l) + "ms");
83+
84+
l = System.currentTimeMillis();
85+
for (int i = 0; i < count; i++) {
86+
testBlackReflection();
87+
}
88+
Log.d(TAG, "BlackReflection: " + (System.currentTimeMillis() - l) + "ms");
89+
} catch (Exception e) {
90+
e.printStackTrace();
91+
}
92+
});
7093
}
7194

95+
private void testSystemReflection() throws Exception {
96+
Class<?> clazz = Class.forName("android.app.ActivityThread");
97+
Method currentActivityThread = clazz.getDeclaredMethod("currentActivityThread");
98+
currentActivityThread.setAccessible(true);
99+
Object invoke = currentActivityThread.invoke(null);
100+
// Log.d(TAG, "testSystemReflection: " + invoke);
101+
}
102+
103+
private void testBlackReflection() throws Exception {
104+
Object o = BRActivityThread.get().currentActivityThread();
105+
}
106+
107+
72108
private TestReflection testBConstructor() {
73109
// test BConstructor
74110
TestReflection testReflection = BRTestReflection.get()._new("a");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package top.niunaijun.app.ref;
2+
3+
import top.niunaijun.blackreflection.annotation.BClassName;
4+
import top.niunaijun.blackreflection.annotation.BStaticMethod;
5+
6+
/**
7+
* Created by Milk on 2022/5/28.
8+
* * ∧_∧
9+
* (`・ω・∥
10+
* 丶 つ0
11+
* しーJ
12+
* 此处无Bug
13+
*/
14+
@BClassName("android.app.ActivityThread")
15+
public interface ActivityThread {
16+
@BStaticMethod
17+
Object currentActivityThread();
18+
}

core/src/main/java/top/niunaijun/blackreflection/BlackReflection.java

+15-28
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import java.util.WeakHashMap;
1111

1212
import top.niunaijun.blackreflection.annotation.BClass;
13+
import top.niunaijun.blackreflection.annotation.BClassName;
14+
import top.niunaijun.blackreflection.annotation.BClassNameNotProcess;
1315
import top.niunaijun.blackreflection.annotation.BConstructor;
1416
import top.niunaijun.blackreflection.annotation.BConstructorNotProcess;
1517
import top.niunaijun.blackreflection.annotation.BField;
@@ -18,8 +20,6 @@
1820
import top.niunaijun.blackreflection.annotation.BFieldSetNotProcess;
1921
import top.niunaijun.blackreflection.annotation.BMethodCheckNotProcess;
2022
import top.niunaijun.blackreflection.annotation.BParamClass;
21-
import top.niunaijun.blackreflection.annotation.BClassName;
22-
import top.niunaijun.blackreflection.annotation.BClassNameNotProcess;
2323
import top.niunaijun.blackreflection.annotation.BParamClassName;
2424
import top.niunaijun.blackreflection.utils.Reflector;
2525

@@ -36,6 +36,7 @@ public class BlackReflection {
3636
public static boolean DEBUG = false;
3737
public static boolean CACHE = false;
3838
private static final Map<Class<?>, Object> sProxyCache = new HashMap<>();
39+
private static final Map<Class<?>, Object> sProxyWithExceptionCache = new HashMap<>();
3940

4041
// key caller
4142
private static final WeakHashMap<Object, Map<Class<?>, Object>> sCallerProxyCache = new WeakHashMap<>();
@@ -163,16 +164,11 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
163164
}
164165
});
165166

166-
if (CACHE) {
167-
if (caller == null) {
168-
sProxyCache.put(clazz, o);
167+
if (caller == null) {
168+
if (withException) {
169+
sProxyWithExceptionCache.put(clazz, o);
169170
} else {
170-
Map<Class<?>, Object> callerClassMap = sCallerProxyCache.get(caller);
171-
if (callerClassMap == null) {
172-
callerClassMap = new HashMap<>();
173-
sCallerProxyCache.put(caller, callerClassMap);
174-
}
175-
callerClassMap.put(clazz, o);
171+
sProxyCache.put(clazz, o);
176172
}
177173
}
178174
return (T) o;
@@ -183,25 +179,16 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
183179
}
184180

185181
private static <T> T getProxy(Class<T> clazz, final Object caller, boolean withException) {
186-
if (!CACHE) {
187-
return null;
188-
}
189182
try {
190-
if (!withException) {
191-
if (caller == null) {
192-
Object o = sProxyCache.get(clazz);
193-
if (o != null) {
194-
return (T) o;
195-
}
183+
if (caller == null) {
184+
Object o;
185+
if (withException) {
186+
o = sProxyWithExceptionCache.get(clazz);
187+
} else {
188+
o = sProxyCache.get(clazz);
196189
}
197-
else {
198-
Map<Class<?>, Object> callerClassMap = sCallerProxyCache.get(caller);
199-
if (callerClassMap != null) {
200-
Object o = callerClassMap.get(clazz);
201-
if (o != null) {
202-
return (T) o;
203-
}
204-
}
190+
if (o != null) {
191+
return (T) o;
205192
}
206193
}
207194
} catch (Throwable ignore) {

0 commit comments

Comments
 (0)