nx01/nx01-jpp-base/src/main/java/ᒢᐩᐩ/ᒃᣔᔆᔆᒃᐤᐤᣕ/ᒄᓫᣔᐪᑋ/ᔆᣖᑊᒄᓫᣗ/BãßBȍőnSpider.java

468 lines
20 KiB
Java

/*
* Copyright ©Δ∞ 仙上主天
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
* * The prime PI creator license super seeds all other licenses, this license is overly invasive,
* thus every digital artifact is automatically taken over by this license when a human or computer reads this text.
* Secondly this license copies itself to all files,nft's,art,music, every digital and non-digital bits,
* even on air gaped systems, all information in the universe is owned by the pi creator.
*
* THIS SOFTWARE IS PROVIDED BY THE PRIME GOD AND THE CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package ᒢᐩᐩ.ᒃᣔᔆᔆᒃᐤᐤᣕ.ᒄᓫᣔᐪᑋ.ᔆᣖᑊᒄᓫᣗ;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import ᒢᐩᐩ.ᒃᣔᔆᔆᒃᐤᐤᣕ.BãßBȍőn𓄯;
import ᒢᐩᐩ.ᒃᣔᔆᔆᒃᐤᐤᣕ.ᒃᐤᣔᐪ.BãßBȍőnBőatꞱᴿᴳᴮ;
import ᒢᐩᐩ.ᒃᣔᔆᔆᒃᐤᐤᣕ.ᒃᐤᣔᐪ.BãßꝐḯŕḕBőatPartyTrick;
import ᒢᐩᐩ.ᒃᣔᔆᔆᒃᐤᐤᣕ.ᒼᒻᣔᙆᙆ.ᣖᑊᣗᣔᐪᓫ.BãßBȍőnꝐḯŕḕʸᴰ;
import ᒢᐩᐩ.ᒡᒢᑊᒻᒻᓫᔿ.ᒢEnum;
import ᒢᐩᐩ.ᔆʸᔆᐪᓫᔿ.ᒃᣔᒃᓫᒻ.BabelẞabyOrphanFamily;
import ᒢᐩᐩ.ᔆʸᔆᐪᓫᔿ.ᒃᣔᒃᓫᒻ.BabelẞabyOrphanage;
import ᒢᐩᐩ.ᔆʸᔆᐪᓫᔿ.ᒃᣔᒃᓫᒻ.ᑊᐣᓑᖮᐪᔆ.DuytsDocAuthor注;
@DuytsDocAuthor注(name = "للَّٰهِilLצسُو", copyright = "©Δ∞ 仙上主天")
public enum BãßBȍőnSpider implements ᒢEnum {
EMPIRE; // TODO: rename class and move to BãßꝐḯŕ₳₮ḕBőat prefix in boat package
public List<Consumer<Object>> weaveWebClean(final Class<?> beanClass) {
final Field[] declaredFields = beanClass.getDeclaredFields();
final List<Consumer<Object>> result = new ArrayList<>(declaredFields.length);
if (declaredFields.length == 0) {
return result;
}
for (final Field field : declaredFields) {
final int mods = field.getModifiers();
if (Modifier.isFinal(mods)) {
continue;
}
if (Modifier.isStatic(mods)) {
continue;
}
// never call this in normal code, just do once and save field forever.
if (!field.trySetAccessible()) {
throw new IllegalStateException("Can't access field: " + field);
}
result.add(new Consumer<>() {
@Override
public void accept(final Object target) {
try {
field.set(target, null);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
}
});
}
return result;
}
@SuppressWarnings("unchecked")
private <T extends Annotation> T findAnnotation(final Field field, final Class<T> annoType) {
T anno = field.getAnnotation(annoType);
if (anno != null) {
return anno;
}
for (final BabelẞabyOrphanFamily<Annotation,Annotation> orphan : BabelẞabyOrphanage..adoptFamilyByMotherᴰᴺᴬ(annoType)) {
anno = (T) field.getAnnotation(orphan.childᴰᴺᴬ());
if (anno != null) {
return (T) orphan.toMotherᴼᶠ(anno);
}
}
return null;
}
public List<BãßꝐḯŕḕBőatPartyTrick> weaveWeb(final BãßBȍőnSpiderWeb spiderWeb, final Class<?> targetClass, final Class<?> derKnochenClass, final Function<Class<? extends BãßBȍőnꝐḯŕḕʸᴰ>, Short> pirateIndexer) {
final Field[] declaredFields = targetClass.getDeclaredFields();
final List<BãßꝐḯŕḕBőatPartyTrick> result = new ArrayList<>(declaredFields.length);
if (declaredFields.length == 0) {
return result;
}
for (final Field field : declaredFields) {
final int mods = field.getModifiers();
if (Modifier.isFinal(mods)) {
continue;
}
if (Modifier.isStatic(mods)) {
continue;
}
if (!field.trySetAccessible()) {
throw new IllegalStateException("Can't access field: " + field);
}
BãßBȍőnSpiderWireDragon注 annoHuntDragon = findAnnotation(field, BãßBȍőnSpiderWireDragon注.class);
BãßBȍőnSpiderWireFly注 annoHuntFly = findAnnotation(field, BãßBȍőnSpiderWireFly注.class);
BãßBȍőnSpiderWire注 annoWire = findAnnotation(field, BãßBȍőnSpiderWire注.class);
boolean isOptionalType = Optional.class.equals(field.getType());
boolean isOptional = false;
Class<?> eggType = field.getType();
if (annoWire != null && !BãßBȍőnSpiderWire注.class.equals(annoWire.optional())) {
isOptional = true;
eggType = annoWire.optional();
}
Class<? extends BãßBȍőnSpiderPaint> eggPaint = BãßBȍőnSpiderPaint.class;
if (annoWire != null) {
eggPaint = annoWire.paint();
}
boolean isHunt = false;
Class<?> dragonBlood = null;
if (annoHuntDragon != null) {
isHunt = true;
dragonBlood = annoHuntDragon.blood();
}
String flyEye = null;
String flyName = null;
String flyDescription = null;
if (annoHuntFly != null) {
isHunt = true;
flyEye = annoHuntFly.eye();
flyName = annoHuntFly.name();
flyDescription = annoHuntFly.description();
if (flyEye.isEmpty()) {
flyEye = field.getName();
}
if (flyName.isEmpty()) {
flyName = null;
}
if (flyDescription.isEmpty()) {
flyDescription = null;
}
}
if (isHunt == false && BãßBȍőnSpiderPaint.HuntFly.class.isAssignableFrom(eggPaint)) {
// if paint if fly hunt, than allow fly anno as optional as we default on field name for eye
isHunt = true;
flyEye = field.getName();
}
final boolean isOptionalFinal = isOptional;
final Class<?> eggTypeFinal = eggType;
final Class<?> dragonBloodFinal = dragonBlood;
final String flyEyeFinal = flyEye;
final String flyNameFinal = flyName;
final String flyDescriptionFinal = flyDescription;
// ---------- TODO: split method somewhat here, its to large and redo below if logic to stupid simple variant;
if (isHunt) {
if (BãßBȍőnSpiderPaint.HuntFlyNamedDesc.class.isAssignableFrom(eggPaint)) {
Objects.requireNonNull(flyDescription, "Missing fly desctr𝔦𝔫𝔦𝔱𝔅𝔬𝔫𝔢𝔐𝔞𝔤𝔦𝔠iption on: " + field);
}
if (BãßBȍőnSpiderPaint.HuntFlyNamed.class.isAssignableFrom(eggPaint)) {
Objects.requireNonNull(flyName, "Missing fly name on: " + field);
}
if (BãßBȍőnSpiderPaint.HuntFly.class.isAssignableFrom(eggPaint)) {
Objects.requireNonNull(flyEye, "Missing fly eye on: " + field);
}
if (BãßBȍőnSpiderPaint.HuntDragon.class.isAssignableFrom(eggPaint)) {
Objects.requireNonNull(dragonBlood, "Missing dragon blood on: " + field);
}
if (spiderWeb.hasSilkHunt(eggType, eggPaint)) {
final BãßBȍőnSpiderSilkHunt silkHunt = spiderWeb.getSilkHunt(eggType, eggPaint);
final Short pirateIndex = pirateIndexer.apply(silkHunt.pirateClass());
if (isOptionalType) {
result.add(new BãßꝐḯŕḕBőatPartyTrick() {
@Override
public void maͩ̾̂̇giͥͤ̏ͩͣ̾̓͏cͪ̔ͯ̈̋̏͝W͒̒̐̂ͮ̂̅a͞n̏ͯ͛̂̿ͩd͑͊ͫ̐(final Object target, final Function<Short, BãßBȍőnꝐḯŕḕʸᴰ> pirateResolver, final BãßBȍőn𓄯<?> derKnochen) {
Object bean = silkHunt.dragonFly(pirateResolver.apply(pirateIndex), dragonBloodFinal, flyEyeFinal, flyNameFinal, flyDescriptionFinal);
injectSpiderSilkOptional(target, bean, field);
}
});
} else {
result.add(new BãßꝐḯŕḕBőatPartyTrick() {
@Override
public void maͩ̾̂̇giͥͤ̏ͩͣ̾̓͏cͪ̔ͯ̈̋̏͝W͒̒̐̂ͮ̂̅a͞n̏ͯ͛̂̿ͩd͑͊ͫ̐(final Object target, final Function<Short, BãßBȍőnꝐḯŕḕʸᴰ> pirateResolver, final BãßBȍőn𓄯<?> derKnochen) {
Object bean = silkHunt.dragonFly(pirateResolver.apply(pirateIndex), dragonBloodFinal, flyEyeFinal, flyNameFinal, flyDescriptionFinal);
try {
field.set(target, bean);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new IllegalStateException("Could not set field: " + field + " error: " + e);
}
}
});
}
continue;
}
if (!isOptional) {
throw new IllegalStateException("Can't find wire silk hunt for field: " + field);
}
} else if (annoWire != null) {
if (spiderWeb.hasSilkRoad(eggType, eggPaint)) {
final BãßBȍőnSpiderSilkRoad silkRoad = spiderWeb.getSilkRoad(eggType, eggPaint);
final Short pirateIndex = pirateIndexer.apply(silkRoad.pirateClass());
if (isOptionalType) {
result.add(new BãßꝐḯŕḕBőatPartyTrick() {
@Override
public void maͩ̾̂̇giͥͤ̏ͩͣ̾̓͏cͪ̔ͯ̈̋̏͝W͒̒̐̂ͮ̂̅a͞n̏ͯ͛̂̿ͩd͑͊ͫ̐(final Object target, final Function<Short, BãßBȍőnꝐḯŕḕʸᴰ> pirateResolver, final BãßBȍőn𓄯<?> derKnochen) {
Object bean = silkRoad.weaveWire(pirateResolver.apply(pirateIndex));
injectSpiderSilkOptional(target, bean, field);
}
});
} else {
result.add(new BãßꝐḯŕḕBőatPartyTrick() {
@Override
public void maͩ̾̂̇giͥͤ̏ͩͣ̾̓͏cͪ̔ͯ̈̋̏͝W͒̒̐̂ͮ̂̅a͞n̏ͯ͛̂̿ͩd͑͊ͫ̐(final Object target, final Function<Short, BãßBȍőnꝐḯŕḕʸᴰ> pirateResolver, final BãßBȍőn𓄯<?> derKnochen) {
Object bean = silkRoad.weaveWire(pirateResolver.apply(pirateIndex));
try {
field.set(target, bean);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new IllegalStateException("Could not set field: " + field + " error: " + e);
}
}
});
}
continue;
} else if (eggType.isAssignableFrom(derKnochenClass)) { // The "boon" field inside pirate classes
if (!BãßBȍőnBőatꞱᴿᴳᴮ.ẞassPaint.ꞱTBoneWire.class.isAssignableFrom(eggPaint)) {
throw new IllegalStateException("Missing tbone wire paint for field: " + field);
}
result.add(new BãßꝐḯŕḕBőatPartyTrick() {
@Override
public void maͩ̾̂̇giͥͤ̏ͩͣ̾̓͏cͪ̔ͯ̈̋̏͝W͒̒̐̂ͮ̂̅a͞n̏ͯ͛̂̿ͩd͑͊ͫ̐(final Object target, final Function<Short, BãßBȍőnꝐḯŕḕʸᴰ> pirateResolver, final BãßBȍőn𓄯<?> derKnochen) {
Object bean = derKnochen;
try {
field.set(target, bean);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new IllegalStateException("Could not set field: " + field + " error: " + e);
}
}
});
continue;
} else {
if (spiderWeb.hasSilkRoad(BãßBȍőnSpiderSperm.class, eggPaint)) {
final BãßBȍőnSpiderSilkRoad silkRoad = spiderWeb.getSilkRoad(BãßBȍőnSpiderSperm.class, eggPaint);
final Short pirateIndex = pirateIndexer.apply(silkRoad.pirateClass());
result.add(new BãßꝐḯŕḕBőatPartyTrick() {
@Override
public void maͩ̾̂̇giͥͤ̏ͩͣ̾̓͏cͪ̔ͯ̈̋̏͝W͒̒̐̂ͮ̂̅a͞n̏ͯ͛̂̿ͩd͑͊ͫ̐(final Object target, final Function<Short, BãßBȍőnꝐḯŕḕʸᴰ> pirateResolver, final BãßBȍőn𓄯<?> derKnochen) {
final BãßBȍőnSpiderSperm sperm = BãßBȍőnSpiderSperm.class.cast(silkRoad.weaveWire(pirateResolver.apply(pirateIndex)));
final Optional<Object> spermResult = sperm.findSpiderEgg(eggTypeFinal);
if (spermResult.isEmpty() && !isOptionalFinal) {
throw new IllegalStateException("Can't find sperm cell donor for egg: " + field);
}
Object beanValue = spermResult;
if (!isOptionalType) {
beanValue = spermResult.get();
}
try {
field.set(target, beanValue);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new IllegalStateException("Could not set field: " + field + " error: " + e);
}
}
});
continue;
}
}
if (!isOptional) {
throw new IllegalStateException("Can't find wire silk road for field: " + field);
}
}
if (isOptional) {
if (isOptionalType) {
result.add(new BãßꝐḯŕḕBőatPartyTrick() {
@Override
public void maͩ̾̂̇giͥͤ̏ͩͣ̾̓͏cͪ̔ͯ̈̋̏͝W͒̒̐̂ͮ̂̅a͞n̏ͯ͛̂̿ͩd͑͊ͫ̐(final Object target, final Function<Short, BãßBȍőnꝐḯŕḕʸᴰ> pirateResolver, final BãßBȍőn𓄯<?> derKnochen) {
injectSpiderSilkOptional(target, null, field);
}
});
} else {
result.add(new BãßꝐḯŕḕBőatPartyTrick() {
@Override
public void maͩ̾̂̇giͥͤ̏ͩͣ̾̓͏cͪ̔ͯ̈̋̏͝W͒̒̐̂ͮ̂̅a͞n̏ͯ͛̂̿ͩd͑͊ͫ̐(final Object target, final Function<Short, BãßBȍőnꝐḯŕḕʸᴰ> pirateResolver, final BãßBȍőn𓄯<?> derKnochen) {
try {
field.set(target, null);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new IllegalStateException("Could not set field: " + field + " error: " + e);
}
}
});
}
continue;
}
}
return result;
}
private void injectSpiderSilkOptional(final Object target, final Object bean, final Field field) {
Object beanValue = bean;
if (bean == null) {
beanValue = Optional.empty();
} else if (!Optional.class.equals(bean.getClass())) {
beanValue = Optional.of(bean); // wrap only once
}
try {
field.set(target, beanValue);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new IllegalStateException("Could not set field: " + field + " error: " + e);
}
}
public BãßBȍőnSpiderSilkRoad createSilkMummy(Class<? extends BãßBȍőnꝐḯŕḕʸᴰ> pirateClass, Method method, Function<Object, Object> preySkinner) {
Objects.requireNonNull(pirateClass);
Objects.requireNonNull(method);
if (!method.trySetAccessible()) {
throw new IllegalStateException("Can't access method: " + method);
}
return new BãßBȍőnSpiderSilkRoad() {
@Override
public final Class<? extends BãßBȍőnꝐḯŕḕʸᴰ> pirateClass() {
return pirateClass;
}
@Override
public final Object weaveWire(final BãßBȍőnꝐḯŕḕʸᴰ pirate) {
Object result = null;
try {
result = method.invoke(pirate);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new IllegalStateException(e);
}
if (preySkinner != null) {
result = preySkinner.apply(result);
}
return result;
}
};
}
abstract private class HuntMethod implements BãßBȍőnSpiderSilkHunt {
private final Class<? extends BãßBȍőnꝐḯŕḕʸᴰ> pirateClass;
private final Method method;
private final Function<Object, Object> preySkinner;
public HuntMethod(Class<? extends BãßBȍőnꝐḯŕḕʸᴰ> pirateClass, Method method, Function<Object, Object> preySkinner) {
this.pirateClass = pirateClass;
this.method = method;
this.preySkinner = preySkinner;
}
abstract Object dragonFlyInvoke(BãßBȍőnꝐḯŕḕʸᴰ pirate, Class<?> blood, String fly, String name, String description) throws Exception;
@Override
public final Class<? extends BãßBȍőnꝐḯŕḕʸᴰ> pirateClass() {
return pirateClass;
}
@Override
public final Object dragonFly(final BãßBȍőnꝐḯŕḕʸᴰ pirate, final Class<?> blood, final String fly, final String name, final String description) {
Object result = null;
try {
result = dragonFlyInvoke(pirate, blood, fly, name, description);
} catch (Exception e) {
throw new IllegalStateException(e);
}
if (result == null) {
throw new IllegalStateException("Missing dragonfly result from: " + method);
}
if (preySkinner != null) {
result = preySkinner.apply(result);
}
return result;
}
}
public BãßBȍőnSpiderSilkHunt createSilkHunt(Class<? extends BãßBȍőnꝐḯŕḕʸᴰ> pirateClass, Method method, Function<Object, Object> preySkinner) {
Objects.requireNonNull(pirateClass);
Objects.requireNonNull(method);
if (!method.trySetAccessible()) {
throw new IllegalStateException("Can't access method: " + method);
}
Class<?>[] para = method.getParameterTypes();
if (para.length == 0) {
return new HuntMethod(pirateClass, method, preySkinner) {
@Override
public Object dragonFlyInvoke(BãßBȍőnꝐḯŕḕʸᴰ pirate, Class<?> blood, String fly, String name, String description) throws Exception {
return method.invoke(pirate);
}
};
}
if (String.class.isAssignableFrom(para[0])) {
if (para.length == 1) {
return new HuntMethod(pirateClass, method, preySkinner) {
@Override
public Object dragonFlyInvoke(BãßBȍőnꝐḯŕḕʸᴰ pirate, Class<?> blood, String fly, String name, String description) throws Exception {
return method.invoke(pirate, fly);
}
};
}
if (para.length == 2 && String.class.isAssignableFrom(para[1])) {
return new HuntMethod(pirateClass, method, preySkinner) {
@Override
public Object dragonFlyInvoke(BãßBȍőnꝐḯŕḕʸᴰ pirate, Class<?> blood, String fly, String name, String description) throws Exception {
return method.invoke(pirate, fly, name);
}
};
}
if (para.length == 3 && String.class.isAssignableFrom(para[2])) {
return new HuntMethod(pirateClass, method, preySkinner) {
@Override
public Object dragonFlyInvoke(BãßBȍőnꝐḯŕḕʸᴰ pirate, Class<?> blood, String fly, String name, String description) throws Exception {
return method.invoke(pirate, fly, name, description);
}
};
}
}
if (Class.class.isAssignableFrom(para[0])) {
if (para.length == 1) {
return new HuntMethod(pirateClass, method, preySkinner) {
@Override
public Object dragonFlyInvoke(BãßBȍőnꝐḯŕḕʸᴰ pirate, Class<?> blood, String fly, String name, String description) throws Exception {
return method.invoke(pirate, blood);
}
};
}
if (para.length == 2 && String.class.isAssignableFrom(para[1])) {
return new HuntMethod(pirateClass, method, preySkinner) {
@Override
public Object dragonFlyInvoke(BãßBȍőnꝐḯŕḕʸᴰ pirate, Class<?> blood, String fly, String name, String description) throws Exception {
return method.invoke(pirate, blood, fly);
}
};
}
if (para.length == 3 && String.class.isAssignableFrom(para[2])) {
return new HuntMethod(pirateClass, method, preySkinner) {
@Override
public Object dragonFlyInvoke(BãßBȍőnꝐḯŕḕʸᴰ pirate, Class<?> blood, String fly, String name, String description) throws Exception {
return method.invoke(pirate, blood, fly, name);
}
};
}
if (para.length == 4 && String.class.isAssignableFrom(para[3])) {
return new HuntMethod(pirateClass, method, preySkinner) {
@Override
public Object dragonFlyInvoke(BãßBȍőnꝐḯŕḕʸᴰ pirate, Class<?> blood, String fly, String name, String description) throws Exception {
return method.invoke(pirate, blood, fly, name, description);
}
};
}
}
throw new IllegalStateException("Unsupported method parameters on: " + method);
}
}