Skip to content

Commit 131c11f

Browse files
[1.13.9.0]由于Java高版本对反射的限制,去除修改final变量的方法
1 parent eb40ce0 commit 131c11f

File tree

2 files changed

+1
-60
lines changed

2 files changed

+1
-60
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.8.0"
4+
rootProject.version = "1.13.9.0"
55
//全项目重构时更新大版本号
66
//添加模块或有较大更改时更新次版本号
77
//有API变动(新增/删除/更改声明)时更新修订号

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

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)