Repository navigation
Expand file tree
/
Copy pathHack.java
More file actions
269 lines (225 loc) · 9.49 KB
/
Copy pathHack.java
File metadata and controls
269 lines (225 loc) · 9.49 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package ctrip.android.bundle.hack;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Created by yb.wang on 14/12/31.
* Hack--反射机制反射后包装的形式:类,方法,字段
*/
public class Hack {
private static AssertionFailureHandler sFailureHandler;
public static interface AssertionFailureHandler {
boolean onAssertionFailure(HackDeclaration.HackAssertionException hackAssertionException);
}
public static abstract class HackDeclaration {
public static class HackAssertionException extends Throwable {
private static final long serialVersionUID = 1;
private Class<?> mHackedClass;
private String mHackedFieldName;
private String mHackedMethodName;
public HackAssertionException(String str) {
super(str);
}
public HackAssertionException(Exception exception) {
super(exception);
}
public String toString() {
return getCause() != null ? getClass().getName() + ": " + getCause() : super.toString();
}
public Class<?> getHackedClass() {
return this.mHackedClass;
}
public void setHackedClass(Class<?> cls) {
this.mHackedClass = cls;
}
public String getHackedMethodName() {
return this.mHackedMethodName;
}
public void setHackedMethodName(String str) {
this.mHackedMethodName = str;
}
public String getHackedFieldName() {
return this.mHackedFieldName;
}
public void setHackedFieldName(String str) {
this.mHackedFieldName = str;
}
}
}
public static class HackedClass<C> {
protected Class<C> mClass;
public <T> HackedField<C, T> staticField(String str) throws HackDeclaration.HackAssertionException {
return new HackedField<C, T>(this.mClass, str, 8);
}
public <T> HackedField<C, T> field(String str) throws HackDeclaration.HackAssertionException {
return new HackedField(this.mClass, str, 0);
}
public HackedMethod staticMethod(String str, Class<?>... clsArr) throws HackDeclaration.HackAssertionException {
return new HackedMethod(this.mClass, str, clsArr, 8);
}
public HackedMethod method(String str, Class<?>... clsArr) throws HackDeclaration.HackAssertionException {
return new HackedMethod(this.mClass, str, clsArr, 0);
}
public HackedConstructor constructor(Class<?>... clsArr) throws HackDeclaration.HackAssertionException {
return new HackedConstructor(this.mClass, clsArr);
}
public HackedClass(Class<C> cls) {
this.mClass = cls;
}
public Class<C> getmClass() {
return this.mClass;
}
}
public static class HackedConstructor {
protected Constructor<?> mConstructor;
HackedConstructor(Class<?> cls, Class<?>[] clsArr) throws HackDeclaration.HackAssertionException {
if (cls != null) {
try {
this.mConstructor = cls.getDeclaredConstructor(clsArr);
} catch (Exception e) {
HackDeclaration.HackAssertionException hackAssertionException = new HackDeclaration.HackAssertionException(e);
hackAssertionException.setHackedClass(cls);
Hack.fail(hackAssertionException);
}
}
}
public Object getInstance(Object... objArr) throws IllegalArgumentException {
Object obj = null;
this.mConstructor.setAccessible(true);
try {
obj = this.mConstructor.newInstance(objArr);
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
}
public static class HackedField<C, T> {
private final Field mField;
public HackedField<C, T> ofGenericType(Class<?> cls) throws HackDeclaration.HackAssertionException {
if (!(this.mField == null || cls.isAssignableFrom(this.mField.getType()))) {
Hack.fail(new HackDeclaration.HackAssertionException(new ClassCastException(this.mField + " is not of type " + cls)));
}
return this;
}
public HackedField<C, T> ofType(Class<?> cls) throws HackDeclaration.HackAssertionException {
if (!(this.mField == null || cls.isAssignableFrom(this.mField.getType()))) {
Hack.fail(new HackDeclaration.HackAssertionException(new ClassCastException(this.mField + " is not of type " + cls)));
}
return this;
}
public HackedField<C, T> ofType(String str) throws HackDeclaration.HackAssertionException {
HackedField<C, T> ofType = null;
try {
ofType = ofType((Class<T>) Class.forName(str));
} catch (Exception e) {
Hack.fail(new HackDeclaration.HackAssertionException(e));
}
return ofType;
}
public T get(C c) {
try {
return (T) this.mField.get(c);
} catch (IllegalAccessException e) {
e.printStackTrace();
return null;
}
}
public void set(C c, Object obj) {
try {
this.mField.set(c, obj);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public void hijack(C c, Interception.InterceptionHandler<?> interceptionHandler) {
T obj = get(c);
if (obj == null) {
throw new IllegalStateException("Cannot hijack null");
}
set(c, Interception.proxy(obj, (Interception.InterceptionHandler) interceptionHandler, obj.getClass().getInterfaces()));
}
HackedField(Class<C> cls, String str, int i) throws HackDeclaration.HackAssertionException {
Field field = null;
if (cls == null) {
this.mField = null;
return;
}
try {
field = cls.getDeclaredField(str);
if (i > 0 && (field.getModifiers() & i) != i) {
Hack.fail(new HackDeclaration.HackAssertionException(field + " does not match modifiers: " + i));
}
field.setAccessible(true);
} catch (Exception e) {
HackDeclaration.HackAssertionException hackAssertionException = new HackDeclaration.HackAssertionException(e);
hackAssertionException.setHackedClass(cls);
hackAssertionException.setHackedFieldName(str);
Hack.fail(hackAssertionException);
} finally {
this.mField = field;
}
}
public Field getField() {
return this.mField;
}
}
public static class HackedMethod {
protected final Method mMethod;
public Object invoke(Object obj, Object... objArr) throws IllegalArgumentException, InvocationTargetException {
Object obj2 = null;
try {
obj2 = this.mMethod.invoke(obj, objArr);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return obj2;
}
HackedMethod(Class<?> cls, String str, Class<?>[] clsArr, int i) throws HackDeclaration.HackAssertionException {
Method method = null;
if (cls == null) {
this.mMethod = null;
return;
}
try {
method = cls.getDeclaredMethod(str, clsArr);
if (i > 0 && (method.getModifiers() & i) != i) {
Hack.fail(new HackDeclaration.HackAssertionException(method + " does not match modifiers: " + i));
}
method.setAccessible(true);
} catch (Exception e) {
HackDeclaration.HackAssertionException hackAssertionException = new HackDeclaration.HackAssertionException(e);
hackAssertionException.setHackedClass(cls);
hackAssertionException.setHackedMethodName(str);
Hack.fail(hackAssertionException);
} finally {
this.mMethod = method;
}
}
public Method getMethod() {
return this.mMethod;
}
}
public static <T> HackedClass<T> into(Class<T> cls) {
return new HackedClass(cls);
}
public static <T> HackedClass<T> into(String str) throws HackDeclaration.HackAssertionException {
try {
return new HackedClass(Class.forName(str));
} catch (Exception e) {
fail(new HackDeclaration.HackAssertionException(e));
return new HackedClass(null);
}
}
private static void fail(HackDeclaration.HackAssertionException hackAssertionException) throws HackDeclaration.HackAssertionException {
if (sFailureHandler == null || !sFailureHandler.onAssertionFailure(hackAssertionException)) {
throw hackAssertionException;
}
}
public static void setAssertionFailureHandler(AssertionFailureHandler assertionFailureHandler) {
sFailureHandler = assertionFailureHandler;
}
private Hack() {
}
}