/
MainActivity.java
117 lines (87 loc) · 4.03 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package top.niunaijun.app;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import top.niunaijun.app.bean.TestReflection;
import top.niunaijun.app.ref.BRActivityThread;
import top.niunaijun.app.ref.BRTestReflection;
import top.niunaijun.blackreflection.BlackReflection;
import top.niunaijun.blackreflection.R;
public class MainActivity extends AppCompatActivity {
public static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BlackReflection.CACHE = true;
TestReflection testReflection = testBConstructor();
Class<?> classReady = BRTestReflection.getRealClass();
Log.d(TAG, "classReady: " + classReady);
Log.d(TAG, "============================");
// testInvokeContext
BRTestReflection.get(testReflection).testContextInvoke("context", 0);
Log.d(TAG, "============================");
// testInvokeStatic
BRTestReflection.get().testStaticInvoke("static", 0);
Log.d(TAG, "============================");
// testGetContextField
String contextValue = BRTestReflection.get(testReflection).mContextValue();
Log.d(TAG, "mContextValue: " + contextValue);
Log.d(TAG, "============================");
// testGetContextField
String staticValue = BRTestReflection.get().sStaticValue();
Log.d(TAG, "sStaticValue: " + staticValue);
Log.d(TAG, "============================");
// checkField
Field field = BRTestReflection.get()._check_fakeField();
Log.d(TAG, "checkField: " + field);
Log.d(TAG, "============================");
// setContextField
BRTestReflection.get(testReflection)._set_mContextValue(contextValue + " changed");
Log.d(TAG, "mContextValue: " + BRTestReflection.get(testReflection).mContextValue());
Log.d(TAG, "============================");
// setStaticField
BRTestReflection.get()._set_sStaticValue(staticValue + " changed");
Log.d(TAG, "sStaticValue: " + BRTestReflection.get().sStaticValue());
Log.d(TAG, "============================");
// use @BParamClassName
BRTestReflection.get().testParamClassName("i am java.lang.String", 0);
findViewById(R.id.btn_start).setOnClickListener(v -> {
try {
int count = 10000;
long l = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
testSystemReflection();
}
Log.d(TAG, "SystemReflection: " + (System.currentTimeMillis() - l) + "ms");
l = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
testBlackReflection();
}
Log.d(TAG, "BlackReflection: " + (System.currentTimeMillis() - l) + "ms");
} catch (Exception e) {
e.printStackTrace();
}
});
}
private void testSystemReflection() throws Exception {
Class<?> clazz = Class.forName("android.app.ActivityThread");
Method currentActivityThread = clazz.getDeclaredMethod("currentActivityThread");
currentActivityThread.setAccessible(true);
Object invoke = currentActivityThread.invoke(null);
// Log.d(TAG, "testSystemReflection: " + invoke);
}
private void testBlackReflection() throws Exception {
Object o = BRActivityThread.get().currentActivityThread();
}
private TestReflection testBConstructor() {
// test BConstructor
TestReflection testReflection = BRTestReflection.get()._new("a");
Log.d(TAG, "TestConstructor: " + testReflection);
testReflection = BRTestReflection.get()._new("a", "b");
Log.d(TAG, "TestConstructor: " + testReflection);
return testReflection;
}
}