diff --git a/spring-core/src/main/java/org/springframework/aot/hint/FieldHint.java b/spring-core/src/main/java/org/springframework/aot/hint/FieldHint.java deleted file mode 100644 index 0587700ccbc4..000000000000 --- a/spring-core/src/main/java/org/springframework/aot/hint/FieldHint.java +++ /dev/null @@ -1,144 +0,0 @@ -/* - * Copyright 2002-2022 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -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; - -/** - * A hint that describes the need for reflection on a {@link Field}. - * - * @author Stephane Nicoll - * @since 6.0 - */ -public final class FieldHint extends MemberHint { - - private final FieldMode mode; - - private final boolean allowUnsafeAccess; - - - private FieldHint(Builder builder) { - super(builder.name); - this.mode = (builder.mode != null ? builder.mode : FieldMode.WRITE); - this.allowUnsafeAccess = builder.allowUnsafeAccess; - } - - /** - * Return whether setting the value of the field should be allowed. - * @return {@code true} to allow {@link Field#set(Object, Object)}. - * @deprecated in favor of {@link #getMode()} - */ - @Deprecated - public boolean isAllowWrite() { - return this.mode == FieldMode.WRITE; - } - - /** - * Return the {@linkplain FieldMode mode} that applies to this hint. - * @return the mode - */ - public FieldMode getMode() { - return this.mode; - } - - /** - * Return whether using {@code Unsafe} on the field should be allowed. - * @return {@code true} to allow unsafe access - */ - public boolean isAllowUnsafeAccess() { - 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 builtWith(FieldMode mode) { - return builder -> builder.withMode(mode); - } - - - /** - * Builder for {@link FieldHint}. - */ - public static class Builder { - - private final String name; - - @Nullable - private FieldMode mode; - - private boolean allowUnsafeAccess; - - - Builder(String name) { - this.name = name; - } - - /** - * Specify if setting the value of the field should be allowed. - * @param allowWrite {@code true} to allow {@link Field#set(Object, Object)} - * @return {@code this}, to facilitate method chaining - * @deprecated in favor of {@link #withMode(FieldMode)} - */ - @Deprecated - public Builder allowWrite(boolean allowWrite) { - if (allowWrite) { - return withMode(FieldMode.WRITE); - } - return this; - } - - /** - * Specify that the {@linkplain FieldMode mode} is required. - * @param mode the required mode - * @return {@code this}, to facilitate method chaining - */ - public Builder withMode(FieldMode mode) { - Assert.notNull(mode, "'mode' must not be null"); - if ((this.mode == null || !this.mode.includes(mode))) { - this.mode = mode; - } - return this; - } - - /** - * Specify whether using {@code Unsafe} on the field should be allowed. - * @param allowUnsafeAccess {@code true} to allow unsafe access - * @return {@code this}, to facilitate method chaining - */ - public Builder allowUnsafeAccess(boolean allowUnsafeAccess) { - this.allowUnsafeAccess = allowUnsafeAccess; - return this; - } - - /** - * Create a {@link FieldHint} based on the state of this builder. - * @return a field hint - */ - FieldHint build() { - return new FieldHint(this); - } - - } -} 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 95432587adda..3de974f2e98b 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 @@ -44,7 +44,7 @@ public final class TypeHint implements ConditionalHint { @Nullable private final TypeReference reachableType; - private final Set fields; + private final Set fields; private final Set constructors; @@ -57,7 +57,7 @@ private TypeHint(Builder builder) { this.type = builder.type; this.reachableType = builder.reachableType; this.memberCategories = Set.copyOf(builder.memberCategories); - this.fields = builder.fields.values().stream().map(FieldHint.Builder::build).collect(Collectors.toSet()); + this.fields = builder.fields; this.constructors = builder.constructors.values().stream().map(ExecutableHint.Builder::build).collect(Collectors.toSet()); this.methods = builder.methods.values().stream().map(ExecutableHint.Builder::build).collect(Collectors.toSet()); } @@ -89,10 +89,10 @@ public TypeReference getReachableType() { /** * Return the fields that require reflection. - * @return a stream of {@link FieldHint} + * @return a stream of Strings */ - public Stream fields() { - return this.fields.stream(); + public Set fields() { + return this.fields; } /** @@ -147,7 +147,7 @@ public static class Builder { @Nullable private TypeReference reachableType; - private final Map fields = new HashMap<>(); + private final Set fields = new HashSet<>(); private final Map constructors = new HashMap<>(); @@ -191,31 +191,9 @@ public Builder onReachableType(Class reachableType) { * @return {@code this}, to facilitate method chaining */ public Builder withField(String name) { - return withField(name, FieldMode.WRITE); + return withField(name); } - /** - * 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)); - } - - /** - * Register the need for reflection on the field with the specified name. - * @param name the name of the field - * @param fieldHint a builder to further customize the hints of this field - * @return {@code this}, to facilitate method chaining - */ - public Builder withField(String name, Consumer fieldHint) { - FieldHint.Builder builder = this.fields.computeIfAbsent(name, FieldHint.Builder::new); - fieldHint.accept(builder); - return this; - } /** * Register the need for reflection on the constructor with the specified