Skip to content

Commit eb40ce0

Browse files
[1.13.8.0]ReflectionHelper新增修改变量的方法
1 parent afb15e3 commit eb40ce0

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
java.sourceCompatibility = JavaVersion.VERSION_1_8
22
java.targetCompatibility = JavaVersion.VERSION_1_8
33
rootProject.group = "com.crypticlib"
4-
rootProject.version = "1.13.7.1"
4+
rootProject.version = "1.13.8.0"
55
//全项目重构时更新大版本号
66
//添加模块或有较大更改时更新次版本号
77
//有API变动(新增/删除/更改声明)时更新修订号

platform/common/src/main/java/crypticlib/util/ReflectionHelper.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,17 @@ 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;
2122
private static Object PLUGIN_INSTANCE = null;
2223

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+
2332
public static Field getField(@NotNull Class<?> clazz, @NotNull String fieldName) {
2433
Field cacheField = getFieldCache(clazz, fieldName);
2534
if (cacheField != null)
@@ -87,6 +96,87 @@ public static <T> T getDeclaredFieldObj(@NotNull Field field, @Nullable Object o
8796
}
8897
}
8998

99+
/**
100+
* 修改一个变量的值
101+
* @param field 变量
102+
* @param owner 所属对象
103+
* @param value 新的值
104+
* @param <T> 变量的类型
105+
*/
106+
public static <T> void setFieldObj(@NotNull Field field, @Nullable Object owner, @NotNull T value) {
107+
try {
108+
field.set(owner, value);
109+
} catch (IllegalAccessException e) {
110+
throw new RuntimeException(e);
111+
}
112+
}
113+
114+
/**
115+
* 修改一个私有变量的值
116+
* @param field 变量
117+
* @param owner 所属对象
118+
* @param value 新的值
119+
* @param <T> 变量的类型
120+
*/
121+
public static <T> void setDeclaredFieldObj(@NotNull Field field, @Nullable Object owner, @NotNull T value) {
122+
try {
123+
field.setAccessible(true);
124+
field.set(owner, value);
125+
} catch (IllegalAccessException e) {
126+
throw new RuntimeException(e);
127+
}
128+
}
129+
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+
90180
public static Method getMethod(@NotNull Class<?> clazz, @NotNull String methodName, Class<?>... argClasses) {
91181
try {
92182
return clazz.getMethod(methodName, argClasses);

0 commit comments

Comments
 (0)