Refactor BindingReflectionHintsRegistrar

This commit splits registerReflectionHints in multiple
methods.

See gh-28683
This commit is contained in:
Sébastien Deleuze
2022-06-27 12:20:29 +02:00
parent fb1aa4f5d5
commit 58aeab3ab6

View File

@@ -97,34 +97,8 @@ public class BindingReflectionHintsRegistrar {
BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
Method writeMethod = propertyDescriptor.getWriteMethod();
if (writeMethod != null && writeMethod.getDeclaringClass() != Object.class
&& writeMethod.getDeclaringClass() != Enum.class) {
hints.registerMethod(writeMethod, INVOKE);
MethodParameter methodParameter = MethodParameter.forExecutable(writeMethod, 0);
Type methodParameterType = methodParameter.getGenericParameterType();
if (!seen.contains(methodParameterType)) {
registerReflectionHints(hints, seen, methodParameterType);
}
}
Method readMethod = propertyDescriptor.getReadMethod();
if (readMethod != null && readMethod.getDeclaringClass() != Object.class
&& readMethod.getDeclaringClass() != Enum.class) {
hints.registerMethod(readMethod, INVOKE);
MethodParameter methodParameter = MethodParameter.forExecutable(readMethod, -1);
Type methodParameterType = methodParameter.getGenericParameterType();
if (!seen.contains(methodParameterType)) {
registerReflectionHints(hints, seen, methodParameterType);
}
}
}
String companionClassName = clazz.getCanonicalName() + KOTLIN_COMPANION_SUFFIX;
if (KotlinDetector.isKotlinType(clazz) && ClassUtils.isPresent(companionClassName, null)) {
Class<?> companionClass = ClassUtils.resolveClassName(companionClassName, null);
Method serializerMethod = ClassUtils.getMethodIfAvailable(companionClass, "serializer");
if (serializerMethod != null) {
hints.registerMethod(serializerMethod);
}
registerPropertyHints(hints, seen, propertyDescriptor.getWriteMethod(), 0);
registerPropertyHints(hints, seen, propertyDescriptor.getReadMethod(), -1);
}
}
catch (IntrospectionException ex) {
@@ -133,6 +107,7 @@ public class BindingReflectionHintsRegistrar {
}
}
}
registerKotlinSerializationHints(hints, clazz);
});
}
Set<Class<?>> referencedTypes = new LinkedHashSet<>();
@@ -140,8 +115,31 @@ public class BindingReflectionHintsRegistrar {
referencedTypes.forEach(referencedType -> registerReflectionHints(hints, seen, referencedType));
}
private void collectReferencedTypes(Set<Type> seen, Set<Class<?>> types, @Nullable Type type) {
if (type == null || seen.contains(type)) {
private void registerPropertyHints(ReflectionHints hints, Set<Type> seen, @Nullable Method method, int parameterIndex) {
if (method != null && method.getDeclaringClass() != Object.class
&& method.getDeclaringClass() != Enum.class) {
hints.registerMethod(method, INVOKE);
MethodParameter methodParameter = MethodParameter.forExecutable(method, parameterIndex);
Type methodParameterType = methodParameter.getGenericParameterType();
if (!seen.contains(methodParameterType)) {
registerReflectionHints(hints, seen, methodParameterType);
}
}
}
private void registerKotlinSerializationHints(ReflectionHints hints, Class<?> clazz) {
String companionClassName = clazz.getCanonicalName() + KOTLIN_COMPANION_SUFFIX;
if (KotlinDetector.isKotlinType(clazz) && ClassUtils.isPresent(companionClassName, null)) {
Class<?> companionClass = ClassUtils.resolveClassName(companionClassName, null);
Method serializerMethod = ClassUtils.getMethodIfAvailable(companionClass, "serializer");
if (serializerMethod != null) {
hints.registerMethod(serializerMethod);
}
}
}
private void collectReferencedTypes(Set<Type> seen, Set<Class<?>> types, Type type) {
if (seen.contains(type)) {
return;
}
ResolvableType resolvableType = ResolvableType.forType(type);