133 lines
5.4 KiB
Java
133 lines
5.4 KiB
Java
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殭屍 {
|
||
|
||
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);
|
||
initSpiderWires(boon);
|
||
} catch (Exception e) {
|
||
throw new RuntimeException(e);
|
||
}
|
||
}
|
||
|
||
private static void initSpiderWires(Object boon) 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) 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());
|
||
}
|
||
}
|
||
|
||
public 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;
|
||
}
|
||
}
|