gdxapp4d/gdxapp4d-lib-bassboonyd/src/main/java/love/distributedrebirth/bassboonyd/BãßBȍőnLoad殭屍.java

133 lines
5.4 KiB
Java
Raw Normal View History

2022-10-15 00:34:58 +00:00
package love.distributedrebirth.bassboonyd;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.function.Consumer;
import love.distributedrebirth.bassboonyd.BãßBȍőnSpiderʸᴰ.SpiderSilk;
import love.distributedrebirth.bassboonyd.info.BãßBȍőnAuthor注;
@BãßBȍőnAuthor注(name = "للَّٰهِilLצسُو", copyright = "©Δ∞ 仙上主天")
public final class BãßBȍőnLoad殭屍 {
2022-10-15 04:23:14 +00:00
private static final BãßBȍőnSpiderʸᴰ BLACK_WIDOW = new BãßBȍőnSpiderʸᴰ();
private static final Set<Class<?>> SCANNED = new LinkedHashSet<>();
2022-10-15 00:34:58 +00:00
@SafeVarargs
protected static <T, K extends BãßBȍőnCoffinStoreKeyʸᴰ> void CAST_BONE_MAGIC(BãßBȍőn<T,K> boon, Consumer<BãßBȍőnCoffinOpenʸᴰ<K>>...coffinFillers) {
BãßBȍőnCoffinOpenʸᴰ<K> bbc = BãßBȍőnCoffinOpenʸᴰ.FROM(boon);
try {
for (Consumer<BãßBȍőnCoffinOpenʸᴰ<K>> embalmFactory:coffinFillers) {
embalmFactory.accept(bbc);
}
for (BãßBȍőnꝐŕḯṿªḕInitPhaseʸᴰ phase:BãßBȍőnꝐŕḯṿªḕInitPhaseʸᴰ.values()) {
initPrivate(phase, boon);
}
2022-10-15 04:23:14 +00:00
initSpiderEggs(boon);
initSpiderWires(boon);
2022-10-15 00:34:58 +00:00
} catch (Exception e) {
throw new RuntimeException(e);
}
}
2022-10-15 04:23:14 +00:00
private static void initSpiderWires(Object boon) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
2022-10-15 00:34:58 +00:00
//System.out.println("SCAN-BOON-FIELD: "+boon.getClass().getSimpleName());
for (Field m:boon.getClass().getDeclaredFields()) {
if (!m.isAnnotationPresent(BãßBȍőnSpiderWire注.class)) {
continue;
}
BãßBȍőnSpiderWire注 anno = m.getAnnotation(BãßBȍőnSpiderWire注.class);
2022-10-15 04:23:14 +00:00
SpiderSilk silkRoad = BLACK_WIDOW.silkRoad(m.getType());
2022-10-15 00:34:58 +00:00
Object bean = silkRoad.senseWire(boon, anno.name(), anno.description());
//System.out.println("type: "+ m.getType() +" wth name: "+anno.name()+" of boon: "+boon.getClass());
m.setAccessible(true);
m.set(boon, bean);
}
}
2022-10-15 04:23:14 +00:00
private static <T, K extends BãßBȍőnCoffinStoreKeyʸᴰ> void initSpiderEggs(BãßBȍőn<T,K> boon) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
2022-10-15 00:34:58 +00:00
for (Class<?> interfaceClass:walkInterfaces(boon.getClass(), new LinkedHashSet<>())) {
for (Class<?> declaredClass:((Class<?>) interfaceClass).getDeclaredClasses()) {
initSpiderEggRegistrate(boon, declaredClass);
}
}
for (Class<?> declaredClass:boon.getClass().getDeclaredClasses()) {
initSpiderEggRegistrate(boon, declaredClass);
}
}
private static <T, K extends BãßBȍőnCoffinStoreKeyʸᴰ> void initSpiderEggRegistrate(BãßBȍőn<T,K> boon, Class<?> declaredClass) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
if (!BãßBȍőnꝐŕḯṿª.class.isAssignableFrom(declaredClass)) {
return;
}
2022-10-15 04:23:14 +00:00
if (SCANNED.contains(declaredClass)) {
2022-10-15 00:34:58 +00:00
return;
}
2022-10-15 04:23:14 +00:00
SCANNED.add(declaredClass);
2022-10-15 00:34:58 +00:00
for (Method m:declaredClass.getDeclaredMethods()) {
if (!m.isAnnotationPresent(BãßBȍőnSpiderEgg注.class)) {
continue;
}
//System.out.println("annoEgg: "+boon.getClass().getSimpleName()+"."+boon+" from: "+declaredClass.getName()+" m="+m.toGenericString());
m.setAccessible(true);
2022-10-15 04:23:14 +00:00
BLACK_WIDOW.registrateEgg(m.getReturnType(), m);
2022-10-15 00:34:58 +00:00
}
}
private static <T, K extends BãßBȍőnCoffinStoreKeyʸᴰ> void initPrivate(BãßBȍőnꝐŕḯṿªḕInitPhaseʸᴰ phase, BãßBȍőn<T,K> boon) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
for (Class<?> interfaceClass:walkInterfaces(boon.getClass(), new LinkedHashSet<>())) {
for (Class<?> declaredClass:((Class<?>) interfaceClass).getDeclaredClasses()) {
initPrivateInvoke(phase, boon, declaredClass);
}
}
for (Class<?> declaredClass:boon.getClass().getDeclaredClasses()) {
initPrivateInvoke(phase, boon, declaredClass);
}
}
private static <T, K extends BãßBȍőnCoffinStoreKeyʸᴰ> void initPrivateInvoke(BãßBȍőnꝐŕḯṿªḕInitPhaseʸᴰ phase, BãßBȍőn<T,K> boon, Class<?> declaredClass) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
if (!BãßBȍőnꝐŕḯṿª.class.isAssignableFrom(declaredClass)) {
return;
}
for (Method m:declaredClass.getDeclaredMethods()) {
if (!m.isAnnotationPresent(BãßBȍőnꝐŕḯṿªḕInit注.class)) {
continue;
}
BãßBȍőnꝐŕḯṿªḕInit注 anno = m.getAnnotation(BãßBȍőnꝐŕḯṿªḕInit注.class);
if (phase != anno.phase()) {
continue; // TODO: redo to fast code path
}
//System.out.println("annoInvoke: req="+phase+" of "+boon.getClass().getSimpleName()+"."+boon+" from: "+declaredClass.getName());
m.setAccessible(true);
Class<?>[] para = m.getParameterTypes();
if (para.length == 0) {
m.invoke(null);
return;
}
if (para.length == 1 && BãßBȍőn.class.isAssignableFrom(para[0])) {
m.invoke(null, boon);
return;
}
throw new IllegalStateException("Unknown method parameters on: "+declaredClass.getSimpleName()+"."+m.toGenericString());
}
}
2022-10-15 01:25:34 +00:00
public static Set<Class<?>> walkInterfaces(Class<?> clazz, Set<Class<?>> result) {
2022-10-15 00:34:58 +00:00
while (clazz != null) {
for (Class<?> next : clazz.getInterfaces()) {
if (result.add(next)) {
walkInterfaces(next, result);
}
}
clazz = clazz.getSuperclass();
}
return result;
}
}