mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 18:48:20 +08:00
放弃Unsafe包操作
This commit is contained in:
@@ -1,30 +1,22 @@
|
||||
package config;
|
||||
|
||||
import config.offset.FieldOffset;
|
||||
import config.offset.OffsetUtil;
|
||||
import config.source.ComplexConfiguration;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class OffsetTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
|
||||
List<FieldOffset> fieldOffsets = OffsetUtil.getClassMemberOffset(ComplexConfiguration.class);
|
||||
|
||||
output(fieldOffsets);
|
||||
|
||||
}
|
||||
// @Test
|
||||
// public void test() {
|
||||
//
|
||||
// output(OffsetUtil.getClassMemberOffset(ComplexConfiguration.class));
|
||||
// output(OffsetUtil.getClassMemberOffset(ComplexConfiguration.Sub.class));
|
||||
//
|
||||
// }
|
||||
|
||||
protected static void output(List<FieldOffset> fieldOffsets) {
|
||||
for (FieldOffset fieldOffset : fieldOffsets) {
|
||||
System.out.println(fieldOffset.getOffsetValue() + " -> " + fieldOffset.getField().getName());
|
||||
if (!fieldOffset.getSubFieldOffsetList().isEmpty()) {
|
||||
output(fieldOffset.getSubFieldOffsetList());
|
||||
}
|
||||
System.out.println(fieldOffset.getOffsetValue() + " -> " + fieldOffset);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ public class SomeModel extends AbstractModel implements ConfigurationSerializabl
|
||||
|
||||
@TestOnly
|
||||
public static SomeModel deserialize(Map<String, ?> args) {
|
||||
return new SomeModel((String) args.get("name"), (int) args.get("num"));
|
||||
return new SomeModel((String) args.get("name"), (Integer) args.get("num"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -59,4 +59,6 @@ public class FieldOffset implements Comparable<FieldOffset> {
|
||||
public int compareTo(@NotNull FieldOffset that) {
|
||||
return this.offsetValue.compareTo(that.offsetValue);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
package config.offset;
|
||||
|
||||
import jdk.internal.misc.Unsafe;
|
||||
|
||||
import sun.misc.Unsafe;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedExceptionAction;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
@@ -18,14 +17,23 @@ public class OffsetUtil {
|
||||
|
||||
static {
|
||||
try {
|
||||
unsafe = AccessController.doPrivileged((PrivilegedExceptionAction<Unsafe>) () -> {
|
||||
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
|
||||
theUnsafe.setAccessible(true);
|
||||
return (Unsafe) theUnsafe.get(null);
|
||||
});
|
||||
} catch (Throwable e) {
|
||||
System.err.println("Unable to load unsafe");
|
||||
// 获取 Unsafe 内部的私有的实例化单例对象
|
||||
Field field = Unsafe.class.getDeclaredField("theUnsafe");
|
||||
// 无视权限
|
||||
field.setAccessible(true);
|
||||
unsafe = (Unsafe) field.get(null);
|
||||
} catch (NoSuchFieldException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// try {
|
||||
// unsafe = AccessController.doPrivileged((PrivilegedExceptionAction<Unsafe>) () -> {
|
||||
// Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
|
||||
// theUnsafe.setAccessible(true);
|
||||
// return (Unsafe) theUnsafe.get(null);
|
||||
// });
|
||||
// } catch (Throwable e) {
|
||||
// System.err.println("Unable to load unsafe");
|
||||
// }
|
||||
}
|
||||
|
||||
public static List<FieldOffset> getClassMemberOffset(Class<?> beanClass) {
|
||||
|
||||
Reference in New Issue
Block a user