Fixed optional

This commit is contained in:
Willem Cazander 2022-10-30 09:53:44 +01:00
parent 3009921c16
commit d0bb36c5af
4 changed files with 31 additions and 17 deletions

View file

@ -13,6 +13,7 @@ import java.util.HashSet;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
@ -235,8 +236,8 @@ public final class DefaultBȍőnLoader implements Bãß.𝔅𝔬𝔫𝔢𝔏𝔬
} }
} }
public static Class<?> findInterfaceByAnnotation(Class<?> clazz, Class<? extends Annotation> annoType) { public static Optional<Class<?>> findInterfaceByAnnotation(Class<?> clazz, Class<? extends Annotation> annoType) {
return walkInterfaces(clazz, new LinkedHashSet<>()).stream().filter(v -> v.isAnnotationPresent(annoType)).findFirst().get(); return walkInterfaces(clazz, new LinkedHashSet<>()).stream().filter(v -> v.isAnnotationPresent(annoType)).findFirst();
} }
public static Set<Class<?>> walkInterfaces(Class<?> clazz, Set<Class<?>> result) { public static Set<Class<?>> walkInterfaces(Class<?> clazz, Set<Class<?>> result) {

View file

@ -3,6 +3,7 @@ package love.distributedrebirth.bassboon.jpp.lang;
import java.lang.instrument.ClassFileTransformer; import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException; import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain; import java.security.ProtectionDomain;
import java.util.Optional;
import love.distributedrebirth.bassboon.DefaultBȍőnLoader; import love.distributedrebirth.bassboon.DefaultBȍőnLoader;
import love.distributedrebirth.bassboon.clazz.BãßBȍőnAuthor注; import love.distributedrebirth.bassboon.clazz.BãßBȍőnAuthor注;
@ -21,18 +22,22 @@ public class ObjectScopeLimitΔ邪ᙾ implements ClassFileTransformer {
if (classBeingRedefined.getDeclaringClass() == null) { if (classBeingRedefined.getDeclaringClass() == null) {
return null; return null;
} }
Class<?> checkPrefixInterface = DefaultBȍőnLoader.findInterfaceByAnnotation(classBeingRedefined, ObjectScopeLimitΔ.Ꝑŕḯṿª.DeathMatchPrefix注.class); Optional<Class<?>> checkPrefixInterface = DefaultBȍőnLoader.findInterfaceByAnnotation(classBeingRedefined, ObjectScopeLimitΔ.Ꝑŕḯṿª.DeathMatchPrefix注.class);
ObjectScopeLimitΔ.Ꝑŕḯṿª.DeathMatchPrefix注 checkPrefix = checkPrefixInterface.getAnnotation(ObjectScopeLimitΔ.Ꝑŕḯṿª.DeathMatchPrefix注.class); if (checkPrefixInterface.isPresent()) {
if (checkPrefix != null) { ObjectScopeLimitΔ.Ꝑŕḯṿª.DeathMatchPrefix注 checkPrefix = checkPrefixInterface.get().getAnnotation(ObjectScopeLimitΔ.Ꝑŕḯṿª.DeathMatchPrefix注.class);
if (!classBeingRedefined.getName().startsWith(checkPrefix.match())) { if (checkPrefix != null) {
throw new IllegalClassFormatException(checkPrefix.lastBreath()+checkPrefix.match()); if (!classBeingRedefined.getName().startsWith(checkPrefix.match())) {
throw new IllegalClassFormatException(checkPrefix.lastBreath()+checkPrefix.match());
}
} }
} }
Class<?> checkNameInterface = DefaultBȍőnLoader.findInterfaceByAnnotation(classBeingRedefined, ObjectScopeLimitΔ.Ꝑŕḯṿª.DeathMatchName注.class); Optional<Class<?>> checkNameInterface = DefaultBȍőnLoader.findInterfaceByAnnotation(classBeingRedefined, ObjectScopeLimitΔ.Ꝑŕḯṿª.DeathMatchName注.class);
ObjectScopeLimitΔ.Ꝑŕḯṿª.DeathMatchName注 checkName = checkNameInterface.getAnnotation(ObjectScopeLimitΔ.Ꝑŕḯṿª.DeathMatchName注.class); if (checkNameInterface.isPresent()) {
if (checkName != null) { ObjectScopeLimitΔ.Ꝑŕḯṿª.DeathMatchName注 checkName = checkNameInterface.get().getAnnotation(ObjectScopeLimitΔ.Ꝑŕḯṿª.DeathMatchName注.class);
if (!classBeingRedefined.getName().equals(checkName.match())) { if (checkName != null) {
throw new IllegalClassFormatException(checkName.lastBreath()+checkName.match()); if (!classBeingRedefined.getName().equals(checkName.match())) {
throw new IllegalClassFormatException(checkName.lastBreath()+checkName.match());
}
} }
} }
return null; return null;

View file

@ -5,6 +5,7 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import java.util.Locale; import java.util.Locale;
import java.util.Optional;
import love.distributedrebirth.bassboon.DefaultBȍőnLoader; import love.distributedrebirth.bassboon.DefaultBȍőnLoader;
import love.distributedrebirth.bassboon.clazz.BãßBȍőnAuthor注; import love.distributedrebirth.bassboon.clazz.BãßBȍőnAuthor注;
@ -40,8 +41,11 @@ public interface /* childface */ BabelLocalizer { // JVM: only allow direct chil
} }
default BabelLocalizerᴬᵖᶦ toBabelLocalizerᴬᵖᶦ() { default BabelLocalizerᴬᵖᶦ toBabelLocalizerᴬᵖᶦ() {
Class<?> loc = DefaultBȍőnLoader.findInterfaceByAnnotation(getClass(), Ꝑŕḯṿª.BabelLocalizer注.class); Optional<Class<?>> loc = DefaultBȍőnLoader.findInterfaceByAnnotation(getClass(), Ꝑŕḯṿª.BabelLocalizer注.class);
Ꝑŕḯṿª.BabelLocalizer注 anno = loc.getAnnotation(Ꝑŕḯṿª.BabelLocalizer注.class); if (loc.isEmpty()) {
throw new RuntimeException("Missing required annotation BabelLocalizer注 on interface: "+this);
}
Ꝑŕḯṿª.BabelLocalizer注 anno = loc.get().getAnnotation(Ꝑŕḯṿª.BabelLocalizer注.class);
return BabelLocalizerᴬᵖᶦ.Ꝑŕᱜṫèƈ.instanceᴺᵉʷ(anno.name(), new Locale(anno.locale()), anno.target()); return BabelLocalizerᴬᵖᶦ.Ꝑŕᱜṫèƈ.instanceᴺᵉʷ(anno.name(), new Locale(anno.locale()), anno.target());
} }

View file

@ -4,6 +4,7 @@ import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException; import java.lang.instrument.IllegalClassFormatException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.security.ProtectionDomain; import java.security.ProtectionDomain;
import java.util.Optional;
import love.distributedrebirth.bassboon.DefaultBȍőnLoader; import love.distributedrebirth.bassboon.DefaultBȍőnLoader;
import love.distributedrebirth.bassboon.clazz.BãßBȍőnAuthor注; import love.distributedrebirth.bassboon.clazz.BãßBȍőnAuthor注;
@ -33,10 +34,13 @@ public class BabelLocalizer邪ᙾ implements ClassFileTransformer {
} }
// babel translated object interfaces // babel translated object interfaces
Class<?> checkMethodNameInterface = DefaultBȍőnLoader.findInterfaceByAnnotation(classBeingRedefined, BabelLocalizer.Ꝑŕḯṿª.BabelMethodNameEnforcer注.class); Optional<Class<?>> checkMethodNameInterface = DefaultBȍőnLoader.findInterfaceByAnnotation(classBeingRedefined, BabelLocalizer.Ꝑŕḯṿª.BabelMethodNameEnforcer注.class);
BabelLocalizer.Ꝑŕḯṿª.BabelMethodNameEnforcer注 checkMethodName = checkMethodNameInterface.getAnnotation(BabelLocalizer.Ꝑŕḯṿª.BabelMethodNameEnforcer注.class); if (checkMethodNameInterface.isEmpty()) {
return null;
}
BabelLocalizer.Ꝑŕḯṿª.BabelMethodNameEnforcer注 checkMethodName = checkMethodNameInterface.get().getAnnotation(BabelLocalizer.Ꝑŕḯṿª.BabelMethodNameEnforcer注.class);
if (checkMethodName == null) { if (checkMethodName == null) {
return classfileBuffer; return null;
} }
for (Method m:classBeingRedefined.getMethods()) { for (Method m:classBeingRedefined.getMethods()) {
if (m.getName().startsWith("toBabel")) { if (m.getName().startsWith("toBabel")) {