@@ -18,17 +18,8 @@ public class ReflectionHelper {
1818
1919 private final static Map <String , Map <String , Field >> fieldCaches = new ConcurrentHashMap <>();
2020 private final static Map <Class <?>, Object > singletonObjectMap = new ConcurrentHashMap <>();
21- private final static Field FIELD_CLASS_MODIFIERS_FIELD ;
2221 private static Object PLUGIN_INSTANCE = null ;
2322
24- static {
25- try {
26- FIELD_CLASS_MODIFIERS_FIELD = Field .class .getDeclaredField ("modifiers" );
27- } catch (NoSuchFieldException e ) {
28- throw new RuntimeException (e );
29- }
30- }
31-
3223 public static Field getField (@ NotNull Class <?> clazz , @ NotNull String fieldName ) {
3324 Field cacheField = getFieldCache (clazz , fieldName );
3425 if (cacheField != null )
@@ -127,56 +118,6 @@ public static <T> void setDeclaredFieldObj(@NotNull Field field, @Nullable Objec
127118 }
128119 }
129120
130- /**
131- * 修改一个final变量的值
132- * @param field 变量
133- * @param owner 所属对象
134- * @param value 新的值
135- * @param <T> 变量的类型
136- */
137- public static <T > void setFinalFieldObj (@ NotNull Field field , @ Nullable Object owner , @ NotNull T value ) {
138- try {
139- int originalModifiers = field .getModifiers ();
140- removeFinalModifier (field );
141- field .set (owner , value );
142- restoreFinalModifier (field , originalModifiers );
143- } catch (IllegalAccessException e ) {
144- throw new RuntimeException (e );
145- }
146- }
147-
148- /**
149- * 修改一个私有final变量的值
150- * @param field 变量
151- * @param owner 所属对象
152- * @param value 新的值
153- * @param <T> 变量的类型
154- */
155- public static <T > void setDeclaredFinalFieldObj (@ NotNull Field field , @ Nullable Object owner , @ NotNull T value ) {
156- try {
157- int originalModifiers = field .getModifiers ();
158- removeFinalModifier (field );
159- field .setAccessible (true );
160- field .set (owner , value );
161- restoreFinalModifier (field , originalModifiers );
162- } catch (IllegalAccessException e ) {
163- throw new RuntimeException (e );
164- }
165- }
166-
167- private static void removeFinalModifier (Field field ) throws IllegalAccessException {
168- FIELD_CLASS_MODIFIERS_FIELD .setAccessible (true );
169- // 移除 final 标志位
170- int currentModifiers = FIELD_CLASS_MODIFIERS_FIELD .getInt (field );
171- FIELD_CLASS_MODIFIERS_FIELD .setInt (field , currentModifiers & ~Modifier .FINAL );
172- }
173-
174- private static void restoreFinalModifier (Field field , int originalModifiers ) throws IllegalAccessException {
175- FIELD_CLASS_MODIFIERS_FIELD .setAccessible (true );
176- // 恢复原始修饰符
177- FIELD_CLASS_MODIFIERS_FIELD .setInt (field , originalModifiers );
178- }
179-
180121 public static Method getMethod (@ NotNull Class <?> clazz , @ NotNull String methodName , Class <?>... argClasses ) {
181122 try {
182123 return clazz .getMethod (methodName , argClasses );
0 commit comments