From d0bb36c5af11e553df94de72dbbc31ea47386743 Mon Sep 17 00:00:00 2001 From: Willem Date: Sun, 30 Oct 2022 09:53:44 +0100 Subject: [PATCH] Fixed optional --- .../bassboon/DefaultBȍőnLoader.java | 5 ++-- .../jpp/lang/ObjectScopeLimitΔ邪ᙾ.java | 25 +++++++++++-------- .../jpp/lang/babel/BabelLocalizer.java | 8 ++++-- .../jpp/lang/babel/BabelLocalizer邪ᙾ.java | 10 +++++--- 4 files changed, 31 insertions(+), 17 deletions(-) diff --git a/gdxapp4d-lib-bassboonyd/src/main/java/love/distributedrebirth/bassboon/DefaultBȍőnLoader.java b/gdxapp4d-lib-bassboonyd/src/main/java/love/distributedrebirth/bassboon/DefaultBȍőnLoader.java index 8313d478..8619461b 100644 --- a/gdxapp4d-lib-bassboonyd/src/main/java/love/distributedrebirth/bassboon/DefaultBȍőnLoader.java +++ b/gdxapp4d-lib-bassboonyd/src/main/java/love/distributedrebirth/bassboon/DefaultBȍőnLoader.java @@ -13,6 +13,7 @@ import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.function.Consumer; import java.util.function.Function; @@ -235,8 +236,8 @@ public final class DefaultBȍőnLoader implements Bãß.𝔅𝔬𝔫𝔢𝔏𝔬 } } - public static Class findInterfaceByAnnotation(Class clazz, Class annoType) { - return walkInterfaces(clazz, new LinkedHashSet<>()).stream().filter(v -> v.isAnnotationPresent(annoType)).findFirst().get(); + public static Optional> findInterfaceByAnnotation(Class clazz, Class annoType) { + return walkInterfaces(clazz, new LinkedHashSet<>()).stream().filter(v -> v.isAnnotationPresent(annoType)).findFirst(); } public static Set> walkInterfaces(Class clazz, Set> result) { diff --git a/gdxapp4d-lib-bassboonyd/src/main/java/love/distributedrebirth/bassboon/jpp/lang/ObjectScopeLimitΔ邪ᙾ.java b/gdxapp4d-lib-bassboonyd/src/main/java/love/distributedrebirth/bassboon/jpp/lang/ObjectScopeLimitΔ邪ᙾ.java index eb862b17..28e718de 100644 --- a/gdxapp4d-lib-bassboonyd/src/main/java/love/distributedrebirth/bassboon/jpp/lang/ObjectScopeLimitΔ邪ᙾ.java +++ b/gdxapp4d-lib-bassboonyd/src/main/java/love/distributedrebirth/bassboon/jpp/lang/ObjectScopeLimitΔ邪ᙾ.java @@ -3,6 +3,7 @@ package love.distributedrebirth.bassboon.jpp.lang; import java.lang.instrument.ClassFileTransformer; import java.lang.instrument.IllegalClassFormatException; import java.security.ProtectionDomain; +import java.util.Optional; import love.distributedrebirth.bassboon.DefaultBȍőnLoader; import love.distributedrebirth.bassboon.clazz.BãßBȍőnAuthor注; @@ -21,18 +22,22 @@ public class ObjectScopeLimitΔ邪ᙾ implements ClassFileTransformer { if (classBeingRedefined.getDeclaringClass() == null) { return null; } - Class checkPrefixInterface = DefaultBȍőnLoader.findInterfaceByAnnotation(classBeingRedefined, ObjectScopeLimitΔ.Ꝑŕḯṿª₮ḕ.DeathMatchPrefix注.class); - ObjectScopeLimitΔ.Ꝑŕḯṿª₮ḕ.DeathMatchPrefix注 checkPrefix = checkPrefixInterface.getAnnotation(ObjectScopeLimitΔ.Ꝑŕḯṿª₮ḕ.DeathMatchPrefix注.class); - if (checkPrefix != null) { - if (!classBeingRedefined.getName().startsWith(checkPrefix.match())) { - throw new IllegalClassFormatException(checkPrefix.lastBreath()+checkPrefix.match()); + Optional> checkPrefixInterface = DefaultBȍőnLoader.findInterfaceByAnnotation(classBeingRedefined, ObjectScopeLimitΔ.Ꝑŕḯṿª₮ḕ.DeathMatchPrefix注.class); + if (checkPrefixInterface.isPresent()) { + ObjectScopeLimitΔ.Ꝑŕḯṿª₮ḕ.DeathMatchPrefix注 checkPrefix = checkPrefixInterface.get().getAnnotation(ObjectScopeLimitΔ.Ꝑŕḯṿª₮ḕ.DeathMatchPrefix注.class); + if (checkPrefix != null) { + if (!classBeingRedefined.getName().startsWith(checkPrefix.match())) { + throw new IllegalClassFormatException(checkPrefix.lastBreath()+checkPrefix.match()); + } } } - Class checkNameInterface = DefaultBȍőnLoader.findInterfaceByAnnotation(classBeingRedefined, ObjectScopeLimitΔ.Ꝑŕḯṿª₮ḕ.DeathMatchName注.class); - ObjectScopeLimitΔ.Ꝑŕḯṿª₮ḕ.DeathMatchName注 checkName = checkNameInterface.getAnnotation(ObjectScopeLimitΔ.Ꝑŕḯṿª₮ḕ.DeathMatchName注.class); - if (checkName != null) { - if (!classBeingRedefined.getName().equals(checkName.match())) { - throw new IllegalClassFormatException(checkName.lastBreath()+checkName.match()); + Optional> checkNameInterface = DefaultBȍőnLoader.findInterfaceByAnnotation(classBeingRedefined, ObjectScopeLimitΔ.Ꝑŕḯṿª₮ḕ.DeathMatchName注.class); + if (checkNameInterface.isPresent()) { + ObjectScopeLimitΔ.Ꝑŕḯṿª₮ḕ.DeathMatchName注 checkName = checkNameInterface.get().getAnnotation(ObjectScopeLimitΔ.Ꝑŕḯṿª₮ḕ.DeathMatchName注.class); + if (checkName != null) { + if (!classBeingRedefined.getName().equals(checkName.match())) { + throw new IllegalClassFormatException(checkName.lastBreath()+checkName.match()); + } } } return null; diff --git a/gdxapp4d-lib-bassboonyd/src/main/java/love/distributedrebirth/bassboon/jpp/lang/babel/BabelLocalizer.java b/gdxapp4d-lib-bassboonyd/src/main/java/love/distributedrebirth/bassboon/jpp/lang/babel/BabelLocalizer.java index ced459d7..17746250 100644 --- a/gdxapp4d-lib-bassboonyd/src/main/java/love/distributedrebirth/bassboon/jpp/lang/babel/BabelLocalizer.java +++ b/gdxapp4d-lib-bassboonyd/src/main/java/love/distributedrebirth/bassboon/jpp/lang/babel/BabelLocalizer.java @@ -5,6 +5,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.util.Locale; +import java.util.Optional; import love.distributedrebirth.bassboon.DefaultBȍőnLoader; import love.distributedrebirth.bassboon.clazz.BãßBȍőnAuthor注; @@ -40,8 +41,11 @@ public interface /* childface */ BabelLocalizer { // JVM: only allow direct chil } default BabelLocalizerᴬᵖᶦ toBabelLocalizerᴬᵖᶦ() { - Class loc = DefaultBȍőnLoader.findInterfaceByAnnotation(getClass(), Ꝑŕḯṿª₮ḕ.BabelLocalizer注.class); - Ꝑŕḯṿª₮ḕ.BabelLocalizer注 anno = loc.getAnnotation(Ꝑŕḯṿª₮ḕ.BabelLocalizer注.class); + Optional> loc = DefaultBȍőnLoader.findInterfaceByAnnotation(getClass(), Ꝑŕḯṿª₮ḕ.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()); } diff --git a/gdxapp4d-lib-bassboonyd/src/main/java/love/distributedrebirth/bassboon/jpp/lang/babel/BabelLocalizer邪ᙾ.java b/gdxapp4d-lib-bassboonyd/src/main/java/love/distributedrebirth/bassboon/jpp/lang/babel/BabelLocalizer邪ᙾ.java index 4657d2ee..4a26ec16 100644 --- a/gdxapp4d-lib-bassboonyd/src/main/java/love/distributedrebirth/bassboon/jpp/lang/babel/BabelLocalizer邪ᙾ.java +++ b/gdxapp4d-lib-bassboonyd/src/main/java/love/distributedrebirth/bassboon/jpp/lang/babel/BabelLocalizer邪ᙾ.java @@ -4,6 +4,7 @@ import java.lang.instrument.ClassFileTransformer; import java.lang.instrument.IllegalClassFormatException; import java.lang.reflect.Method; import java.security.ProtectionDomain; +import java.util.Optional; import love.distributedrebirth.bassboon.DefaultBȍőnLoader; import love.distributedrebirth.bassboon.clazz.BãßBȍőnAuthor注; @@ -33,10 +34,13 @@ public class BabelLocalizer邪ᙾ implements ClassFileTransformer { } // babel translated object interfaces - Class checkMethodNameInterface = DefaultBȍőnLoader.findInterfaceByAnnotation(classBeingRedefined, BabelLocalizer.Ꝑŕḯṿª₮ḕ.BabelMethodNameEnforcer注.class); - BabelLocalizer.Ꝑŕḯṿª₮ḕ.BabelMethodNameEnforcer注 checkMethodName = checkMethodNameInterface.getAnnotation(BabelLocalizer.Ꝑŕḯṿª₮ḕ.BabelMethodNameEnforcer注.class); + Optional> checkMethodNameInterface = DefaultBȍőnLoader.findInterfaceByAnnotation(classBeingRedefined, BabelLocalizer.Ꝑŕḯṿª₮ḕ.BabelMethodNameEnforcer注.class); + if (checkMethodNameInterface.isEmpty()) { + return null; + } + BabelLocalizer.Ꝑŕḯṿª₮ḕ.BabelMethodNameEnforcer注 checkMethodName = checkMethodNameInterface.get().getAnnotation(BabelLocalizer.Ꝑŕḯṿª₮ḕ.BabelMethodNameEnforcer注.class); if (checkMethodName == null) { - return classfileBuffer; + return null; } for (Method m:classBeingRedefined.getMethods()) { if (m.getName().startsWith("toBabel")) {