Skip to content

Commit 58aeab3

Browse files
committed
Refactor BindingReflectionHintsRegistrar
This commit splits registerReflectionHints in multiple methods. See spring-projectsgh-28683
1 parent fb1aa4f commit 58aeab3

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

spring-core/src/main/java/org/springframework/aot/hint/support/BindingReflectionHintsRegistrar.java

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -97,34 +97,8 @@ private void registerReflectionHints(ReflectionHints hints, Set<Type> seen, Type
9797
BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
9898
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
9999
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
100-
Method writeMethod = propertyDescriptor.getWriteMethod();
101-
if (writeMethod != null && writeMethod.getDeclaringClass() != Object.class
102-
&& writeMethod.getDeclaringClass() != Enum.class) {
103-
hints.registerMethod(writeMethod, INVOKE);
104-
MethodParameter methodParameter = MethodParameter.forExecutable(writeMethod, 0);
105-
Type methodParameterType = methodParameter.getGenericParameterType();
106-
if (!seen.contains(methodParameterType)) {
107-
registerReflectionHints(hints, seen, methodParameterType);
108-
}
109-
}
110-
Method readMethod = propertyDescriptor.getReadMethod();
111-
if (readMethod != null && readMethod.getDeclaringClass() != Object.class
112-
&& readMethod.getDeclaringClass() != Enum.class) {
113-
hints.registerMethod(readMethod, INVOKE);
114-
MethodParameter methodParameter = MethodParameter.forExecutable(readMethod, -1);
115-
Type methodParameterType = methodParameter.getGenericParameterType();
116-
if (!seen.contains(methodParameterType)) {
117-
registerReflectionHints(hints, seen, methodParameterType);
118-
}
119-
}
120-
}
121-
String companionClassName = clazz.getCanonicalName() + KOTLIN_COMPANION_SUFFIX;
122-
if (KotlinDetector.isKotlinType(clazz) && ClassUtils.isPresent(companionClassName, null)) {
123-
Class<?> companionClass = ClassUtils.resolveClassName(companionClassName, null);
124-
Method serializerMethod = ClassUtils.getMethodIfAvailable(companionClass, "serializer");
125-
if (serializerMethod != null) {
126-
hints.registerMethod(serializerMethod);
127-
}
100+
registerPropertyHints(hints, seen, propertyDescriptor.getWriteMethod(), 0);
101+
registerPropertyHints(hints, seen, propertyDescriptor.getReadMethod(), -1);
128102
}
129103
}
130104
catch (IntrospectionException ex) {
@@ -133,15 +107,39 @@ private void registerReflectionHints(ReflectionHints hints, Set<Type> seen, Type
133107
}
134108
}
135109
}
110+
registerKotlinSerializationHints(hints, clazz);
136111
});
137112
}
138113
Set<Class<?>> referencedTypes = new LinkedHashSet<>();
139114
collectReferencedTypes(seen, referencedTypes, type);
140115
referencedTypes.forEach(referencedType -> registerReflectionHints(hints, seen, referencedType));
141116
}
142117

143-
private void collectReferencedTypes(Set<Type> seen, Set<Class<?>> types, @Nullable Type type) {
144-
if (type == null || seen.contains(type)) {
118+
private void registerPropertyHints(ReflectionHints hints, Set<Type> seen, @Nullable Method method, int parameterIndex) {
119+
if (method != null && method.getDeclaringClass() != Object.class
120+
&& method.getDeclaringClass() != Enum.class) {
121+
hints.registerMethod(method, INVOKE);
122+
MethodParameter methodParameter = MethodParameter.forExecutable(method, parameterIndex);
123+
Type methodParameterType = methodParameter.getGenericParameterType();
124+
if (!seen.contains(methodParameterType)) {
125+
registerReflectionHints(hints, seen, methodParameterType);
126+
}
127+
}
128+
}
129+
130+
private void registerKotlinSerializationHints(ReflectionHints hints, Class<?> clazz) {
131+
String companionClassName = clazz.getCanonicalName() + KOTLIN_COMPANION_SUFFIX;
132+
if (KotlinDetector.isKotlinType(clazz) && ClassUtils.isPresent(companionClassName, null)) {
133+
Class<?> companionClass = ClassUtils.resolveClassName(companionClassName, null);
134+
Method serializerMethod = ClassUtils.getMethodIfAvailable(companionClass, "serializer");
135+
if (serializerMethod != null) {
136+
hints.registerMethod(serializerMethod);
137+
}
138+
}
139+
}
140+
141+
private void collectReferencedTypes(Set<Type> seen, Set<Class<?>> types, Type type) {
142+
if (seen.contains(type)) {
145143
return;
146144
}
147145
ResolvableType resolvableType = ResolvableType.forType(type);

0 commit comments

Comments
 (0)