From b67f639fae39ab3e0b4b5cfcfba8cf844d7c1a97 Mon Sep 17 00:00:00 2001 From: ATATC Date: Tue, 15 Aug 2023 12:06:10 +0800 Subject: [PATCH] Now `Config` can directly scan classes. --- .../atatctech/hephaestus/config/Config.java | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/atatctech/hephaestus/config/Config.java b/src/main/java/com/atatctech/hephaestus/config/Config.java index 8bf6f07..17a5aa3 100644 --- a/src/main/java/com/atatctech/hephaestus/config/Config.java +++ b/src/main/java/com/atatctech/hephaestus/config/Config.java @@ -28,25 +28,31 @@ public final class Config { private Config() { } + public static void scanClass(@NotNull Class clz) { + if (!Component.class.isAssignableFrom(clz)) return; + ComponentConfig componentConfig = clz.getAnnotation(ComponentConfig.class); + try { + Field field = clz.getDeclaredField("PARSER"); + field.setAccessible(true); + putParser(componentConfig.tagName(), (Parser) field.get(null)); + } catch (NoSuchFieldException ignored) { + throw new MissingFieldException(clz, "PARSER"); + } catch (IllegalAccessException ignored) { + } + if (clz.isAnnotationPresent(Transform.RequireTransform.class)) { + Transform transform = Transform.getTransform(clz); + if (transform != null) putTransform(componentConfig.tagName(), transform); + } + } + + public static void scanClasses(@NotNull Class @NotNull ... classes) { + for (Class clz : classes) scanClass(clz); + } + public static void scanPackage(@NotNull String pkg) { Reflections reflections = new Reflections(pkg); Set> classes = reflections.getTypesAnnotatedWith(ComponentConfig.class); - for (Class clz : classes) { - if (!Component.class.isAssignableFrom(clz)) continue; - ComponentConfig componentConfig = clz.getAnnotation(ComponentConfig.class); - try { - Field field = clz.getDeclaredField("PARSER"); - field.setAccessible(true); - putParser(componentConfig.tagName(), (Parser) field.get(null)); - } catch (NoSuchFieldException ignored) { - throw new MissingFieldException(clz, "PARSER"); - } catch (IllegalAccessException ignored) { - } - if (clz.isAnnotationPresent(Transform.RequireTransform.class)) { - Transform transform = Transform.getTransform(clz); - if (transform != null) putTransform(componentConfig.tagName(), transform); - } - } + classes.forEach(Config::scanClass); } public static void scanPackages(@NotNull String @NotNull ... packages) {