Skip to content

Commit c4f28ee

Browse files
author
Blankj
committed
see 12/17 log
1 parent 190051a commit c4f28ee

File tree

1 file changed

+242
-0
lines changed

1 file changed

+242
-0
lines changed
Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
package com.blankj.utilcode.util;
2+
3+
import java.lang.reflect.AccessibleObject;
4+
import java.lang.reflect.Constructor;
5+
import java.lang.reflect.Field;
6+
import java.lang.reflect.Member;
7+
import java.lang.reflect.Modifier;
8+
9+
/**
10+
* <pre>
11+
* author: Blankj
12+
* blog : http://blankj.com
13+
* time : 2017/12/15
14+
* desc : 反射相关工具类
15+
* </pre>
16+
*/
17+
public final class ReflectUtils {
18+
19+
private final Class<?> type;
20+
21+
private final Object object;
22+
23+
private ReflectUtils(Class<?> type) {
24+
this(type, type);
25+
}
26+
27+
private ReflectUtils(Class<?> type, Object object) {
28+
this.type = type;
29+
this.object = object;
30+
}
31+
32+
///////////////////////////////////////////////////////////////////////////
33+
// reflect
34+
///////////////////////////////////////////////////////////////////////////
35+
36+
public static ReflectUtils reflect(String className) {
37+
return reflect(forName(className));
38+
}
39+
40+
public static ReflectUtils reflect(String name, ClassLoader classLoader) {
41+
return reflect(forName(name, classLoader));
42+
}
43+
44+
public static ReflectUtils reflect(Class<?> clazz) {
45+
return new ReflectUtils(clazz);
46+
}
47+
48+
public static ReflectUtils reflect(Object object) {
49+
return new ReflectUtils(object == null ? Object.class : object.getClass(), object);
50+
}
51+
52+
private ReflectUtils reflect(Class<?> type, Object object) {
53+
return new ReflectUtils(type, object);
54+
}
55+
56+
private ReflectUtils reflect(Constructor<?> constructor, Object... args) {
57+
try {
58+
return new ReflectUtils(constructor.getDeclaringClass(), accessible(constructor).newInstance(args));
59+
} catch (Exception e) {
60+
throw new ReflectException(e);
61+
}
62+
}
63+
64+
private static Class<?> forName(String className) {
65+
try {
66+
return Class.forName(className);
67+
} catch (ClassNotFoundException e) {
68+
throw new ReflectException(e);
69+
}
70+
}
71+
72+
private static Class<?> forName(String name, ClassLoader classLoader) {
73+
try {
74+
return Class.forName(name, true, classLoader);
75+
} catch (ClassNotFoundException e) {
76+
throw new ReflectException(e);
77+
}
78+
}
79+
80+
@SuppressWarnings("unchecked")
81+
public <T> T get() {
82+
return (T) object;
83+
}
84+
85+
public ReflectUtils field(String name) {
86+
try {
87+
Field field = field0(name);
88+
return reflect(field.getType(), field.get(object));
89+
} catch (IllegalAccessException e) {
90+
throw new ReflectException(e);
91+
}
92+
}
93+
94+
private Field field0(String name) {
95+
Class<?> t = type();
96+
try {
97+
return accessible(t.getField(name));
98+
} catch (NoSuchFieldException e) {
99+
do {
100+
try {
101+
return accessible(t.getDeclaredField(name));
102+
} catch (NoSuchFieldException ignore) {
103+
}
104+
t = t.getSuperclass();
105+
}
106+
while (t != null);
107+
throw new ReflectException(e);
108+
}
109+
}
110+
111+
public ReflectUtils create() {
112+
return create(new Object[0]);
113+
}
114+
115+
public ReflectUtils create(Object... args) {
116+
Class<?>[] types = types(args);
117+
try {
118+
Constructor<?> constructor = type().getDeclaredConstructor(types);
119+
return reflect(constructor, args);
120+
} catch (NoSuchMethodException e) {
121+
for (Constructor<?> constructor : type().getDeclaredConstructors()) {
122+
if (match(constructor.getParameterTypes(), types)) {
123+
return reflect(constructor, args);
124+
}
125+
}
126+
throw new ReflectException(e);
127+
}
128+
}
129+
130+
private boolean match(Class<?>[] declaredTypes, Class<?>[] actualTypes) {
131+
if (declaredTypes.length == actualTypes.length) {
132+
for (int i = 0; i < actualTypes.length; i++) {
133+
if (actualTypes[i] == NULL.class
134+
|| wrapper(declaredTypes[i]).isAssignableFrom(wrapper(actualTypes[i]))) {
135+
continue;
136+
}
137+
return false;
138+
}
139+
return true;
140+
} else {
141+
return false;
142+
}
143+
}
144+
145+
private static Class<?>[] types(Object... values) {
146+
if (values == null) return new Class[0];
147+
148+
Class<?>[] result = new Class[values.length];
149+
150+
for (int i = 0; i < values.length; i++) {
151+
Object value = values[i];
152+
result[i] = value == null ? NULL.class : value.getClass();
153+
}
154+
155+
return result;
156+
}
157+
158+
public Class<?> type() {
159+
return type;
160+
}
161+
162+
public static Class<?> wrapper(Class<?> type) {
163+
if (type == null) {
164+
return null;
165+
} else if (type.isPrimitive()) {
166+
if (boolean.class == type) {
167+
return Boolean.class;
168+
} else if (int.class == type) {
169+
return Integer.class;
170+
} else if (long.class == type) {
171+
return Long.class;
172+
} else if (short.class == type) {
173+
return Short.class;
174+
} else if (byte.class == type) {
175+
return Byte.class;
176+
} else if (double.class == type) {
177+
return Double.class;
178+
} else if (float.class == type) {
179+
return Float.class;
180+
} else if (char.class == type) {
181+
return Character.class;
182+
} else if (void.class == type) {
183+
return Void.class;
184+
}
185+
}
186+
return type;
187+
}
188+
189+
190+
@Override
191+
public int hashCode() {
192+
return object.hashCode();
193+
}
194+
195+
@Override
196+
public boolean equals(Object obj) {
197+
return obj instanceof ReflectUtils && object.equals(((ReflectUtils) obj).get());
198+
}
199+
200+
@Override
201+
public String toString() {
202+
return object.toString();
203+
}
204+
205+
206+
/**
207+
* @param accessible
208+
* @param <T>
209+
* @return
210+
*/
211+
public static <T extends AccessibleObject> T accessible(T accessible) {
212+
if (accessible == null) return null;
213+
if (accessible instanceof Member) {
214+
Member member = (Member) accessible;
215+
if (Modifier.isPublic(member.getModifiers()) &&
216+
Modifier.isPublic(member.getDeclaringClass().getModifiers())) {
217+
return accessible;
218+
}
219+
}
220+
if (!accessible.isAccessible()) accessible.setAccessible(true);
221+
return accessible;
222+
}
223+
224+
225+
private static class NULL {
226+
}
227+
228+
public static class ReflectException extends RuntimeException {
229+
230+
public ReflectException(String message) {
231+
super(message);
232+
}
233+
234+
public ReflectException(String message, Throwable cause) {
235+
super(message, cause);
236+
}
237+
238+
public ReflectException(Throwable cause) {
239+
super(cause);
240+
}
241+
}
242+
}

0 commit comments

Comments
 (0)