Add additional shortcuts for hint registration

Add `MemberCategory` and `FieldMode` shortcuts for type registration.
Helper `builtWith` methods have also been extracted to the Hint types
to allow general reuse (for example with `registerTypes`).

See gh-29011
This commit is contained in:
Phillip Webb
2022-09-01 16:32:35 -07:00
parent da1005cd66
commit bc0bf1fac3
8 changed files with 199 additions and 6 deletions

View File

@@ -20,6 +20,7 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import java.lang.reflect.Method;
import java.util.List;
import java.util.function.Consumer;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -80,6 +81,15 @@ public final class ExecutableHint extends MemberHint {
return this.mode;
}
/**
* Return a {@link Consumer} that applies the given {@link ExecutableMode}
* to the accepted {@link Builder}.
* @param mode the mode to apply
* @return a consumer to apply the mode
*/
public static Consumer<Builder> builtWith(ExecutableMode mode) {
return builder -> builder.withMode(mode);
}
/**
* Builder for {@link ExecutableHint}.

View File

@@ -17,6 +17,7 @@
package org.springframework.aot.hint;
import java.lang.reflect.Field;
import java.util.function.Consumer;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -66,6 +67,16 @@ public final class FieldHint extends MemberHint {
return this.allowUnsafeAccess;
}
/**
* Return a {@link Consumer} that applies the given {@link FieldMode}
* to the accepted {@link Builder}.
* @param mode the mode to apply
* @return a consumer to apply the mode
*/
public static Consumer<Builder> builtWith(FieldMode mode) {
return builder -> builder.withMode(mode);
}
/**
* Builder for {@link FieldHint}.

View File

@@ -86,6 +86,28 @@ public class ReflectionHints {
return this;
}
/**
* Register or customize reflection hints for the specified type
* using the specified {@link MemberCategory MemberCategories}.
* @param type the type to customize
* @param memberCategories the member categories to apply
* @return {@code this}, to facilitate method chaining
*/
public ReflectionHints registerType(Class<?> type, MemberCategory... memberCategories) {
return registerType(TypeReference.of(type), memberCategories);
}
/**
* Register or customize reflection hints for the specified type
* using the specified {@link MemberCategory MemberCategories}.
* @param type the type to customize
* @param memberCategories the member categories to apply
* @return {@code this}, to facilitate method chaining
*/
public ReflectionHints registerType(TypeReference type , MemberCategory... memberCategories) {
return registerType(type, TypeHint.builtWith(memberCategories));
}
/**
* Register or customize reflection hints for the specified type.
* @param type the type to customize
@@ -132,7 +154,17 @@ public class ReflectionHints {
* @return {@code this}, to facilitate method chaining
*/
public ReflectionHints registerField(Field field) {
return registerField(field, fieldHint -> fieldHint.withMode(FieldMode.WRITE));
return registerField(field, FieldMode.WRITE);
}
/**
* Register the need for reflection on the specified {@link Field}
* using the specified {@link FieldMode}.
* @param field the field that requires reflection
* @return {@code this}, to facilitate method chaining
*/
public ReflectionHints registerField(Field field, FieldMode mode) {
return registerField(field, FieldHint.builtWith(mode));
}
/**
@@ -164,7 +196,7 @@ public class ReflectionHints {
* @return {@code this}, to facilitate method chaining
*/
public ReflectionHints registerConstructor(Constructor<?> constructor, ExecutableMode mode) {
return registerConstructor(constructor, constructorHint -> constructorHint.withMode(mode));
return registerConstructor(constructor, ExecutableHint.builtWith(mode));
}
/**
@@ -197,7 +229,7 @@ public class ReflectionHints {
* @return {@code this}, to facilitate method chaining
*/
public ReflectionHints registerMethod(Method method, ExecutableMode mode) {
return registerMethod(method, methodHint -> methodHint.withMode(mode));
return registerMethod(method, ExecutableHint.builtWith(mode));
}
/**

View File

@@ -126,6 +126,17 @@ public final class TypeHint implements ConditionalHint {
.toString();
}
/**
* Return a {@link Consumer} that applies the given {@link MemberCategory
* MemberCategories} to the accepted {@link Builder}.
* @param memberCategories the memberCategories to apply
* @return a consumer to apply the member categories
*/
public static Consumer<Builder> builtWith(MemberCategory... memberCategories) {
return builder -> builder.withMembers(memberCategories);
}
/**
* Builder for {@link TypeHint}.
*/
@@ -180,7 +191,18 @@ public final class TypeHint implements ConditionalHint {
* @return {@code this}, to facilitate method chaining
*/
public Builder withField(String name) {
return withField(name, fieldHint -> {});
return withField(name, FieldMode.WRITE);
}
/**
* Register the need for reflection on the field with the specified name
* using the specified {@link FieldMode}.
* @param name the name of the field
* @param mode the requested mode
* @return {@code this}, to facilitate method chaining
*/
public Builder withField(String name, FieldMode mode) {
return withField(name, FieldHint.builtWith(mode));
}
/**
@@ -213,7 +235,7 @@ public final class TypeHint implements ConditionalHint {
* @return {@code this}, to facilitate method chaining
*/
public Builder withConstructor(List<TypeReference> parameterTypes, ExecutableMode mode) {
return withConstructor(parameterTypes, constructorHint -> constructorHint.withMode(mode));
return withConstructor(parameterTypes, ExecutableHint.builtWith(mode));
}
/**
@@ -252,7 +274,7 @@ public final class TypeHint implements ConditionalHint {
* @return {@code this}, to facilitate method chaining
*/
public Builder withMethod(String name, List<TypeReference> parameterTypes, ExecutableMode mode) {
return withMethod(name, parameterTypes, methodHint -> methodHint.withMode(mode));
return withMethod(name, parameterTypes, ExecutableHint.builtWith(mode));
}
/**