From cea06e6a9e53f3274671e531eca6435e954b92d9 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 1 Sep 2022 13:42:19 -0700 Subject: [PATCH] Polish ReflectionHints and TypeHint method order --- .../aot/hint/ReflectionHints.java | 69 ++++++++--------- .../springframework/aot/hint/TypeHint.java | 74 +++++++++---------- 2 files changed, 72 insertions(+), 71 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/aot/hint/ReflectionHints.java b/spring-core/src/main/java/org/springframework/aot/hint/ReflectionHints.java index a30046c2cf..8261788e60 100644 --- a/spring-core/src/main/java/org/springframework/aot/hint/ReflectionHints.java +++ b/spring-core/src/main/java/org/springframework/aot/hint/ReflectionHints.java @@ -125,6 +125,16 @@ public class ReflectionHints { return this; } + /** + * Register the need for reflection on the specified {@link Field}, + * enabling write access. + * @param field the field that requires reflection + * @return {@code this}, to facilitate method chaining + */ + public ReflectionHints registerField(Field field) { + return registerField(field, fieldHint -> fieldHint.allowWrite(true)); + } + /** * Register the need for reflection on the specified {@link Field}. * @param field the field that requires reflection @@ -136,14 +146,26 @@ public class ReflectionHints { typeHint -> typeHint.withField(field.getName(), fieldHint)); } + /** - * Register the need for reflection on the specified {@link Field}, - * enabling write access. - * @param field the field that requires reflection + * Register the need for reflection on the specified {@link Constructor}, + * enabling {@link ExecutableMode#INVOKE}. + * @param constructor the constructor that requires reflection * @return {@code this}, to facilitate method chaining */ - public ReflectionHints registerField(Field field) { - return registerField(field, fieldHint -> fieldHint.allowWrite(true)); + public ReflectionHints registerConstructor(Constructor constructor) { + return registerConstructor(constructor, ExecutableMode.INVOKE); + } + + /** + * Register the need for reflection on the specified {@link Constructor}, + * using the specified {@link ExecutableMode}. + * @param constructor the constructor that requires reflection + * @param mode the requested mode + * @return {@code this}, to facilitate method chaining + */ + public ReflectionHints registerConstructor(Constructor constructor, ExecutableMode mode) { + return registerConstructor(constructor, constructorHint -> constructorHint.withMode(mode)); } /** @@ -159,35 +181,13 @@ public class ReflectionHints { } /** - * Register the need for reflection on the specified {@link Constructor}, - * using the specified {@link ExecutableMode}. - * @param constructor the constructor that requires reflection - * @param mode the requested mode - * @return {@code this}, to facilitate method chaining - */ - public ReflectionHints registerConstructor(Constructor constructor, ExecutableMode mode) { - return registerConstructor(constructor, constructorHint -> constructorHint.withMode(mode)); - } - - /** - * Register the need for reflection on the specified {@link Constructor}, + * Register the need for reflection on the specified {@link Method}, * enabling {@link ExecutableMode#INVOKE}. - * @param constructor the constructor that requires reflection - * @return {@code this}, to facilitate method chaining - */ - public ReflectionHints registerConstructor(Constructor constructor) { - return registerConstructor(constructor, ExecutableMode.INVOKE); - } - - /** - * Register the need for reflection on the specified {@link Method}. * @param method the method that requires reflection - * @param methodHint a builder to further customize the hints of this method * @return {@code this}, to facilitate method chaining */ - public ReflectionHints registerMethod(Method method, Consumer methodHint) { - return registerType(TypeReference.of(method.getDeclaringClass()), - typeHint -> typeHint.withMethod(method.getName(), mapParameters(method), methodHint)); + public ReflectionHints registerMethod(Method method) { + return registerMethod(method, ExecutableMode.INVOKE); } /** @@ -202,13 +202,14 @@ public class ReflectionHints { } /** - * Register the need for reflection on the specified {@link Method}, - * enabling {@link ExecutableMode#INVOKE}. + * Register the need for reflection on the specified {@link Method}. * @param method the method that requires reflection + * @param methodHint a builder to further customize the hints of this method * @return {@code this}, to facilitate method chaining */ - public ReflectionHints registerMethod(Method method) { - return registerMethod(method, ExecutableMode.INVOKE); + public ReflectionHints registerMethod(Method method, Consumer methodHint) { + return registerType(TypeReference.of(method.getDeclaringClass()), + typeHint -> typeHint.withMethod(method.getName(), mapParameters(method), methodHint)); } private List mapParameters(Executable executable) { diff --git a/spring-core/src/main/java/org/springframework/aot/hint/TypeHint.java b/spring-core/src/main/java/org/springframework/aot/hint/TypeHint.java index c336b55828..5e55c71238 100644 --- a/spring-core/src/main/java/org/springframework/aot/hint/TypeHint.java +++ b/spring-core/src/main/java/org/springframework/aot/hint/TypeHint.java @@ -173,6 +173,16 @@ public final class TypeHint implements ConditionalHint { return this; } + /** + * Register the need for reflection on the field with the specified name, + * enabling write access. + * @param name the name of the field + * @return {@code this}, to facilitate method chaining + */ + public Builder withField(String name) { + return withField(name, fieldHint -> {}); + } + /** * Register the need for reflection on the field with the specified name. * @param name the name of the field @@ -186,13 +196,24 @@ public final class TypeHint implements ConditionalHint { } /** - * Register the need for reflection on the field with the specified name, - * enabling write access. - * @param name the name of the field + * Register the need for reflection on the constructor with the specified + * parameter types, enabling {@link ExecutableMode#INVOKE}. + * @param parameterTypes the parameter types of the constructor * @return {@code this}, to facilitate method chaining */ - public Builder withField(String name) { - return withField(name, fieldHint -> {}); + public Builder withConstructor(List parameterTypes) { + return withConstructor(parameterTypes, ExecutableMode.INVOKE); + } + + /** + * Register the need for reflection on the constructor with the specified + * parameter types, using the specified {@link ExecutableMode}. + * @param parameterTypes the parameter types of the constructor + * @param mode the requested mode + * @return {@code this}, to facilitate method chaining + */ + public Builder withConstructor(List parameterTypes, ExecutableMode mode) { + return withConstructor(parameterTypes, constructorHint -> constructorHint.withMode(mode)); } /** @@ -211,41 +232,15 @@ public final class TypeHint implements ConditionalHint { return this; } - /** - * Register the need for reflection on the constructor with the specified - * parameter types, using the specified {@link ExecutableMode}. - * @param parameterTypes the parameter types of the constructor - * @param mode the requested mode - * @return {@code this}, to facilitate method chaining - */ - public Builder withConstructor(List parameterTypes, ExecutableMode mode) { - return withConstructor(parameterTypes, constructorHint -> constructorHint.withMode(mode)); - } - - /** - * Register the need for reflection on the constructor with the specified - * parameter types, enabling {@link ExecutableMode#INVOKE}. - * @param parameterTypes the parameter types of the constructor - * @return {@code this}, to facilitate method chaining - */ - public Builder withConstructor(List parameterTypes) { - return withConstructor(parameterTypes, ExecutableMode.INVOKE); - } - /** * Register the need for reflection on the method with the specified name - * and parameter types. + * and parameter types, enabling {@link ExecutableMode#INVOKE}. * @param name the name of the method * @param parameterTypes the parameter types of the constructor - * @param methodHint a builder to further customize the hints of this method * @return {@code this}, to facilitate method chaining */ - public Builder withMethod(String name, List parameterTypes, Consumer methodHint) { - ExecutableKey key = new ExecutableKey(name, parameterTypes); - ExecutableHint.Builder builder = this.methods.computeIfAbsent(key, - k -> ExecutableHint.ofMethod(name, parameterTypes)); - methodHint.accept(builder); - return this; + public Builder withMethod(String name, List parameterTypes) { + return withMethod(name, parameterTypes, ExecutableMode.INVOKE); } /** @@ -262,13 +257,18 @@ public final class TypeHint implements ConditionalHint { /** * Register the need for reflection on the method with the specified name - * and parameter types, enabling {@link ExecutableMode#INVOKE}. + * and parameter types. * @param name the name of the method * @param parameterTypes the parameter types of the constructor + * @param methodHint a builder to further customize the hints of this method * @return {@code this}, to facilitate method chaining */ - public Builder withMethod(String name, List parameterTypes) { - return withMethod(name, parameterTypes, ExecutableMode.INVOKE); + public Builder withMethod(String name, List parameterTypes, Consumer methodHint) { + ExecutableKey key = new ExecutableKey(name, parameterTypes); + ExecutableHint.Builder builder = this.methods.computeIfAbsent(key, + k -> ExecutableHint.ofMethod(name, parameterTypes)); + methodHint.accept(builder); + return this; } /**