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

135 lines
5.6 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殭屍 {
public static class Ꝑŕḯṿª implements BãßBȍőnꝐŕḯṿª {
private static final BãßBȍőnSpiderʸᴰ BLACK_WIDOW = new BãßBȍőnSpiderʸᴰ();
private static final Set<Class<?>> SCANNED = new LinkedHashSet<>();
}
@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);
}
initSpiderEggs(boon, bbc);
initSpiderWires(boon, bbc);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static void initSpiderWires(Object boon, BãßBȍőnCoffinOpenʸᴰ<?> bbc) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
//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);
SpiderSilk silkRoad = Ꝑŕḯṿª.BLACK_WIDOW.silkRoad(m.getType());
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);
}
}
private static <T, K extends BãßBȍőnCoffinStoreKeyʸᴰ> void initSpiderEggs(BãßBȍőn<T,K> boon, BãßBȍőnCoffinOpenʸᴰ<?> bbc) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
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;
}
if (Ꝑŕḯṿª.SCANNED.contains(declaredClass)) {
return;
}
Ꝑŕḯṿª.SCANNED.add(declaredClass);
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);
Ꝑŕḯṿª.BLACK_WIDOW.registrateEgg(m.getReturnType(), m);
}
}
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());
}
}
private static Set<Class<?>> walkInterfaces(Class<?> clazz, Set<Class<?>> result) {
while (clazz != null) {
for (Class<?> next : clazz.getInterfaces()) {
if (result.add(next)) {
walkInterfaces(next, result);
}
}
clazz = clazz.getSuperclass();
}
return result;
}
}