Skip to content

Commit 00c9a40

Browse files
committed
Fix Spring AOT model relfection
1 parent d889d63 commit 00c9a40

File tree

1 file changed

+43
-21
lines changed

1 file changed

+43
-21
lines changed

spring-aot/src/main/java/io/kubernetes/client/spring/aot/KubernetesBeanFactoryInitializationAotProcessor.java

+43-21
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Set;
2222
import org.jetbrains.annotations.NotNull;
2323
import org.reflections.Reflections;
24+
import org.reflections.scanners.Scanners;
2425
import org.slf4j.Logger;
2526
import org.slf4j.LoggerFactory;
2627
import org.springframework.aot.hint.MemberCategory;
@@ -55,38 +56,52 @@ public BeanFactoryInitializationAotContribution processAheadOfTime(
5556
@NotNull ConfigurableListableBeanFactory beanFactory) {
5657
return (generationContext, beanFactoryInitializationCode) -> {
5758
RuntimeHints hints = generationContext.getRuntimeHints();
58-
String[] classNames =
59-
new String[] {
60-
"com.google.gson.JsonElement", //
61-
"io.kubernetes.client.informer.cache.ProcessorListener", //
62-
"io.kubernetes.client.extended.controller.Controller", //
63-
"io.kubernetes.client.util.generic.GenericKubernetesApi$StatusPatch", //
64-
"io.kubernetes.client.util.Watch$Response" //
65-
};
66-
for (String className : classNames) {
67-
LOGGER.info("registering {} for reflection", className);
68-
hints.reflection().registerType(TypeReference.of(className), allMemberCategories);
69-
}
59+
registerStaticClasses(hints);
60+
registerModels(hints);
7061
registerForPackage("io.kubernetes", hints);
71-
Collection<String> packages = AutoConfigurationPackages.get(beanFactory);
72-
for (String packageName : packages) {
73-
registerForPackage(packageName, hints);
74-
}
62+
registerAutoconfigurationPackages(beanFactory, hints);
7563
};
7664
}
7765

66+
private void registerStaticClasses(RuntimeHints hints) {
67+
Set<String> classNames = Set.of(
68+
"com.google.gson.JsonElement",
69+
"io.kubernetes.client.informer.cache.ProcessorListener",
70+
"io.kubernetes.client.extended.controller.Controller",
71+
"io.kubernetes.client.util.generic.GenericKubernetesApi$StatusPatch",
72+
"io.kubernetes.client.util.Watch$Response"
73+
);
74+
for (String className : classNames) {
75+
LOGGER.info("registering {} for reflection", className);
76+
hints.reflection().registerType(TypeReference.of(className), allMemberCategories);
77+
}
78+
}
79+
80+
private void registerModels(RuntimeHints hints) {
81+
Reflections reflections = new Reflections("io.kubernetes.client.openapi.models",
82+
Scanners.SubTypes.filterResultsBy(s -> true));
83+
Set<Class<?>> models = reflections.getSubTypesOf(Object.class);
84+
LOGGER.info("Found {} models", models.size());
85+
registerClassesForReflection(models, hints);
86+
}
87+
7888
private void registerForPackage(String packageName, RuntimeHints hints) {
7989
Reflections reflections = new Reflections(packageName);
80-
Set<Class<?>> apiModels = reflections.getTypesAnnotatedWith(ApiModel.class);
8190
Set<Class<? extends Controller>> controllers = reflections.getSubTypesOf(Controller.class);
91+
LOGGER.info("Found {} controllers", controllers.size());
8292
Set<Class<?>> jsonAdapters = findJsonAdapters(reflections);
93+
LOGGER.info("Found {} jsonAdapters", jsonAdapters.size());
8394
Set<Class<?>> all = new HashSet<>();
8495
all.addAll(jsonAdapters);
8596
all.addAll(controllers);
86-
all.addAll(apiModels);
87-
for (Class<?> clazz : all) {
88-
LOGGER.info("registering {} for reflection", clazz.getName());
89-
hints.reflection().registerType(clazz, allMemberCategories);
97+
registerClassesForReflection(all, hints);
98+
}
99+
100+
private void registerAutoconfigurationPackages(ConfigurableListableBeanFactory beanFactory,
101+
RuntimeHints hints) {
102+
Collection<String> packages = AutoConfigurationPackages.get(beanFactory);
103+
for (String packageName : packages) {
104+
registerForPackage(packageName, hints);
90105
}
91106
}
92107

@@ -102,4 +117,11 @@ private <R extends Annotation> Set<Class<?>> findJsonAdapters(Reflections reflec
102117
}
103118
return classes;
104119
}
120+
121+
private void registerClassesForReflection(Set<Class<?>> classes, RuntimeHints hints) {
122+
for (Class<?> clazz : classes) {
123+
LOGGER.info("registering {} for reflection", clazz);
124+
hints.reflection().registerType(clazz, allMemberCategories);
125+
}
126+
}
105127
}

0 commit comments

Comments
 (0)