onEnable 之前加载插件
修复 NoClassDefFoundError 的问题
This commit is contained in:
59
src/main/java/com/ilummc/tlib/util/Ref.java
Normal file
59
src/main/java/com/ilummc/tlib/util/Ref.java
Normal file
@@ -0,0 +1,59 @@
|
||||
package com.ilummc.tlib.util;
|
||||
|
||||
import com.ilummc.tlib.util.asm.AsmAnalyser;
|
||||
import org.objectweb.asm.ClassReader;
|
||||
import org.objectweb.asm.ClassWriter;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ThreadSafe
|
||||
public class Ref {
|
||||
|
||||
private static final Map<String, List<Field>> cachedFields = new ConcurrentHashMap<>();
|
||||
|
||||
public static final int ACC_BRIDGE = 0x0040;
|
||||
public static final int ACC_SYNTHETIC = 0x1000;
|
||||
|
||||
public static List<Field> getDeclaredFields(Class<?> clazz) {
|
||||
return getDeclaredFields(clazz, 0, false);
|
||||
}
|
||||
|
||||
public static List<Field> getDeclaredFields(String clazz, int excludeModifiers, boolean cache) {
|
||||
try {
|
||||
return getDeclaredFields(Class.forName(clazz), excludeModifiers, cache);
|
||||
} catch (ClassNotFoundException e) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
public static List<Field> getDeclaredFields(Class<?> clazz, int excludeModifiers, boolean cache) {
|
||||
try {
|
||||
Class.forName("org.objectweb.asm.ClassVisitor");
|
||||
List<Field> fields;
|
||||
if ((fields = cachedFields.get(clazz.getName())) != null) return fields;
|
||||
ClassReader classReader = new ClassReader(clazz.getResourceAsStream("/" + clazz.getName().replace('.', '/') + ".class"));
|
||||
AsmAnalyser analyser = new AsmAnalyser(new ClassWriter(ClassWriter.COMPUTE_MAXS), excludeModifiers);
|
||||
classReader.accept(analyser, ClassReader.SKIP_DEBUG);
|
||||
fields = analyser.getFields().stream().map(name -> {
|
||||
try {
|
||||
return clazz.getDeclaredField(name);
|
||||
} catch (Throwable ignored) {
|
||||
}
|
||||
return null;
|
||||
}).filter(Objects::nonNull).collect(Collectors.toList());
|
||||
if (cache) cachedFields.putIfAbsent(clazz.getName(), fields);
|
||||
return fields;
|
||||
} catch (Exception e) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user