Browse Source

Refactor BindingReflectionHintsRegistrar

This commit splits registerReflectionHints in multiple
methods.

See gh-28683
pull/28715/head
Sébastien Deleuze 2 years ago
parent
commit
58aeab3ab6
  1. 52
      spring-core/src/main/java/org/springframework/aot/hint/support/BindingReflectionHintsRegistrar.java

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

@ -97,27 +97,37 @@ public class BindingReflectionHintsRegistrar {
BeanInfo beanInfo = Introspector.getBeanInfo(clazz); BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
Method writeMethod = propertyDescriptor.getWriteMethod(); registerPropertyHints(hints, seen, propertyDescriptor.getWriteMethod(), 0);
if (writeMethod != null && writeMethod.getDeclaringClass() != Object.class registerPropertyHints(hints, seen, propertyDescriptor.getReadMethod(), -1);
&& writeMethod.getDeclaringClass() != Enum.class) { }
hints.registerMethod(writeMethod, INVOKE); }
MethodParameter methodParameter = MethodParameter.forExecutable(writeMethod, 0); catch (IntrospectionException ex) {
Type methodParameterType = methodParameter.getGenericParameterType(); if (logger.isDebugEnabled()) {
if (!seen.contains(methodParameterType)) { logger.debug("Ignoring referenced type [" + clazz.getName() + "]: " + ex.getMessage());
registerReflectionHints(hints, seen, methodParameterType);
} }
} }
Method readMethod = propertyDescriptor.getReadMethod(); }
if (readMethod != null && readMethod.getDeclaringClass() != Object.class registerKotlinSerializationHints(hints, clazz);
&& readMethod.getDeclaringClass() != Enum.class) { });
hints.registerMethod(readMethod, INVOKE); }
MethodParameter methodParameter = MethodParameter.forExecutable(readMethod, -1); Set<Class<?>> referencedTypes = new LinkedHashSet<>();
collectReferencedTypes(seen, referencedTypes, type);
referencedTypes.forEach(referencedType -> registerReflectionHints(hints, seen, referencedType));
}
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(); Type methodParameterType = methodParameter.getGenericParameterType();
if (!seen.contains(methodParameterType)) { if (!seen.contains(methodParameterType)) {
registerReflectionHints(hints, seen, methodParameterType); registerReflectionHints(hints, seen, methodParameterType);
} }
} }
} }
private void registerKotlinSerializationHints(ReflectionHints hints, Class<?> clazz) {
String companionClassName = clazz.getCanonicalName() + KOTLIN_COMPANION_SUFFIX; String companionClassName = clazz.getCanonicalName() + KOTLIN_COMPANION_SUFFIX;
if (KotlinDetector.isKotlinType(clazz) && ClassUtils.isPresent(companionClassName, null)) { if (KotlinDetector.isKotlinType(clazz) && ClassUtils.isPresent(companionClassName, null)) {
Class<?> companionClass = ClassUtils.resolveClassName(companionClassName, null); Class<?> companionClass = ClassUtils.resolveClassName(companionClassName, null);
@ -127,21 +137,9 @@ public class BindingReflectionHintsRegistrar {
} }
} }
} }
catch (IntrospectionException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Ignoring referenced type [" + clazz.getName() + "]: " + ex.getMessage());
}
}
}
});
}
Set<Class<?>> referencedTypes = new LinkedHashSet<>();
collectReferencedTypes(seen, referencedTypes, type);
referencedTypes.forEach(referencedType -> registerReflectionHints(hints, seen, referencedType));
}
private void collectReferencedTypes(Set<Type> seen, Set<Class<?>> types, @Nullable Type type) { private void collectReferencedTypes(Set<Type> seen, Set<Class<?>> types, Type type) {
if (type == null || seen.contains(type)) { if (seen.contains(type)) {
return; return;
} }
ResolvableType resolvableType = ResolvableType.forType(type); ResolvableType resolvableType = ResolvableType.forType(type);

Loading…
Cancel
Save