Skip to content

Commit

Permalink
Now Config can directly scan classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
ATATC committed Aug 15, 2023
1 parent 0d1c20f commit b67f639
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions src/main/java/com/atatctech/hephaestus/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Class<?>> 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) {
Expand Down

0 comments on commit b67f639

Please sign in to comment.