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 { @@ -97,27 +97,37 @@ 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);
registerPropertyHints(hints, seen, propertyDescriptor.getWriteMethod(), 0);
registerPropertyHints(hints, seen, propertyDescriptor.getReadMethod(), -1);
}
}
catch (IntrospectionException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Ignoring referenced type [" + clazz.getName() + "]: " + ex.getMessage());
}
}
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);
}
registerKotlinSerializationHints(hints, clazz);
});
}
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();
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);
@ -127,21 +137,9 @@ public class BindingReflectionHintsRegistrar { @@ -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) {
if (type == null || seen.contains(type)) {
private void collectReferencedTypes(Set<Type> seen, Set<Class<?>> types, Type type) {
if (seen.contains(type)) {
return;
}
ResolvableType resolvableType = ResolvableType.forType(type);

Loading…
Cancel
Save