-
Notifications
You must be signed in to change notification settings - Fork 11
/
StaticReflect.java
201 lines (153 loc) · 4.93 KB
/
StaticReflect.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
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
package org.hy.common;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* 静态字段(成员属性)的反射。
*
* 1. 可实现 java.sql.Types.VARCHAR 全路径的解释,并获取静态的值
*
* @author ZhengWei(HY)
* @version v1.0
* @createDate 2013-08-01
* 2019-05-27 v2.0 添加:执行静态方法
*/
public final class StaticReflect
{
/** 静态字段(成员属性)所属的Java类 */
private Class<?> classObj;
/** 静态字段(成员属性)的Field对象 */
private Field staticField;
/** 静态字段(成员属性)的值 */
private Object staticValue;
/**
* 获取枚举类型的所有枚举值
*
* @param i_EnumClass 枚举类型
* @return
*/
public static Enum<?> [] getEnums(Class<? extends Enum<?>> i_EnumClass)
{
Enum<?> [] v_Ret = null;
try
{
Method v_Method_Values = i_EnumClass.getMethod("values");
v_Ret = (Enum [])v_Method_Values.invoke(null);
}
catch (Exception exce)
{
v_Ret = new Enum[]{};
}
return v_Ret;
}
public static Object getStaticValue(String i_StaticURL)
{
try
{
StaticReflect v_StaticReflect = new StaticReflect(i_StaticURL);
return v_StaticReflect.getStaticValue();
}
catch (Exception exce)
{
// Nothing.
}
return null;
}
/**
* 执行静态方法
*
* @author ZhengWei(HY)
* @createDate 2019-05-27
* @version v1.0
*
* @param i_StaticMethod 静态方法
* @param i_ParameterTypes 静态方法的参数
* @return 返回静态方法的返回值
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InvocationTargetException
*/
public static Object invoke(Method i_StaticMethod ,Object ... i_Parameters) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
return i_StaticMethod.invoke(null ,i_Parameters);
}
public StaticReflect(String i_StaticURL) throws IllegalAccessException, ClassNotFoundException, IllegalArgumentException, SecurityException, NoSuchFieldException
{
if ( Help.isNull(i_StaticURL) )
{
throw new NullPointerException("Static URL is null.");
}
this.parser(i_StaticURL.trim());
}
/**
* 解释
*
* @param i_StaticURL
* @throws IllegalAccessException
* @throws ClassNotFoundException
* @throws IllegalArgumentException
* @throws SecurityException
* @throws NoSuchFieldException
*/
private void parser(String i_StaticURL) throws IllegalAccessException, ClassNotFoundException, IllegalArgumentException, SecurityException, NoSuchFieldException
{
String [] v_StaticURLArr = i_StaticURL.replace("." ,"@").split("@");
if ( v_StaticURLArr.length < 2 )
{
throw new IllegalAccessException("Static URL[" + i_StaticURL + "] is not Valid.");
}
// 解释出 Class 类型名称及静态字段的名称
StringBuilder v_ClassName = new StringBuilder();
String v_StaticName = v_StaticURLArr[v_StaticURLArr.length-1];
for (int i=0; i<v_StaticURLArr.length-1; i++)
{
if ( i > 0 )
{
v_ClassName.append(".");
}
v_ClassName.append(v_StaticURLArr[i]);
}
this.classObj = Help.forName(v_ClassName.toString());
Field [] v_FieldArr = this.classObj.getFields();
for (int i=0; i<v_FieldArr.length; i++)
{
Field v_Field = v_FieldArr[i];
// (v_Field.getModifiers() & 8) == 8 静态
if ( v_Field.getName().equalsIgnoreCase(v_StaticName) )
{
this.staticField = v_Field;
this.staticValue = v_Field.get(this.classObj);
}
}
}
public Class<?> getClassObj()
{
return this.classObj;
}
public Object getStaticValue()
{
return this.staticValue;
}
public String getStaticURL()
{
if ( this.classObj != null && this.staticField != null )
{
return this.classObj.getName() + "." + this.staticField.getName();
}
else
{
return "";
}
}
public String toString()
{
if ( this.staticValue != null )
{
return this.staticValue.toString();
}
else
{
return "";
}
}
}