Polishing

This commit is contained in:
Sam Brannen
2022-09-14 15:27:00 +02:00
parent 911d1f2dea
commit a2445dcb8a
6 changed files with 22 additions and 33 deletions

View File

@@ -158,7 +158,7 @@ public final class JdkProxyHint implements ConditionalHint {
if (!invalidTypes.isEmpty()) { if (!invalidTypes.isEmpty()) {
throw new IllegalArgumentException("The following must be non-sealed interfaces: " + invalidTypes); throw new IllegalArgumentException("The following must be non-sealed interfaces: " + invalidTypes);
} }
return Arrays.stream(proxiedInterfaces).map(TypeReference::of).toList(); return TypeReference.listOf(proxiedInterfaces);
} }
} }

View File

@@ -20,12 +20,10 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.Executable; import java.lang.reflect.Executable;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.springframework.aot.hint.TypeHint.Builder; import org.springframework.aot.hint.TypeHint.Builder;
@@ -255,7 +253,7 @@ public class ReflectionHints {
} }
private List<TypeReference> mapParameters(Executable executable) { private List<TypeReference> mapParameters(Executable executable) {
return Arrays.stream(executable.getParameterTypes()).map(TypeReference::of).collect(Collectors.toList()); return TypeReference.listOf(executable.getParameterTypes());
} }
} }

View File

@@ -312,8 +312,7 @@ public final class TypeHint implements ConditionalHint {
private ExecutableKey(String name, List<TypeReference> parameterTypes) { private ExecutableKey(String name, List<TypeReference> parameterTypes) {
this.name = name; this.name = name;
this.parameterTypes = parameterTypes.stream().map(TypeReference::getCanonicalName) this.parameterTypes = parameterTypes.stream().map(TypeReference::getCanonicalName).toList();
.collect(Collectors.toList());
} }
@Override @Override

View File

@@ -17,6 +17,7 @@
package org.springframework.aot.hint.predicate; package org.springframework.aot.hint.predicate;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate; import java.util.function.Predicate;
import org.springframework.aot.hint.ProxyHints; import org.springframework.aot.hint.ProxyHints;
@@ -59,7 +60,9 @@ public class ProxyHintsPredicates {
*/ */
public Predicate<RuntimeHints> forInterfaces(TypeReference... interfaces) { public Predicate<RuntimeHints> forInterfaces(TypeReference... interfaces) {
Assert.notEmpty(interfaces, "'interfaces' should not be empty"); Assert.notEmpty(interfaces, "'interfaces' should not be empty");
List<TypeReference> interfaceList = Arrays.asList(interfaces);
return hints -> hints.proxies().jdkProxies().anyMatch(proxyHint -> return hints -> hints.proxies().jdkProxies().anyMatch(proxyHint ->
proxyHint.getProxiedInterfaces().equals(Arrays.asList(interfaces))); proxyHint.getProxiedInterfaces().equals(interfaceList));
} }
} }

View File

@@ -49,7 +49,6 @@ import org.springframework.util.ReflectionUtils;
public class ReflectionHintsPredicates { public class ReflectionHintsPredicates {
ReflectionHintsPredicates() { ReflectionHintsPredicates() {
} }
/** /**
@@ -138,10 +137,10 @@ public class ReflectionHintsPredicates {
return methods.iterator().next(); return methods.iterator().next();
} }
else if (methods.size() > 1) { else if (methods.size() > 1) {
throw new IllegalArgumentException(String.format("Found multiple methods named '%s' on class %s", methodName, type.getName())); throw new IllegalArgumentException("Found multiple methods named '%s' on class %s".formatted(methodName, type.getName()));
} }
else { else {
throw new IllegalArgumentException("No method named '" + methodName + "' on class " + type.getName()); throw new IllegalArgumentException("No method named '%s' on class %s".formatted(methodName, type.getName()));
} }
} }
@@ -160,7 +159,7 @@ public class ReflectionHintsPredicates {
Assert.hasText(fieldName, "'fieldName' should not be empty"); Assert.hasText(fieldName, "'fieldName' should not be empty");
Field field = ReflectionUtils.findField(type, fieldName); Field field = ReflectionUtils.findField(type, fieldName);
if (field == null) { if (field == null) {
throw new IllegalArgumentException("No field named '" + fieldName + "' on class " + type.getName()); throw new IllegalArgumentException("No field named '%s' on class %s".formatted(fieldName, type.getName()));
} }
return new FieldHintPredicate(field); return new FieldHintPredicate(field);
} }
@@ -315,15 +314,13 @@ public class ReflectionHintsPredicates {
/** /**
* Indicate whether the specified {@code ExecutableHint} covers the * Indicate whether the specified {@code ExecutableHint} covers the
* reflection needs of the specified executable definition. * reflection needs of the specified executable definition.
* @return {@code true} if the member matches (same type, name and parameters), * @return {@code true} if the member matches (same type, name, and parameters),
* and the configured {@code ExecutableMode} is compatibe * and the configured {@code ExecutableMode} is compatible
*/ */
static boolean includes(ExecutableHint hint, String name, static boolean includes(ExecutableHint hint, String name,
List<TypeReference> parameterTypes, ExecutableMode executableModes) { List<TypeReference> parameterTypes, ExecutableMode executableModes) {
return hint.getName().equals(name) return hint.getName().equals(name) && hint.getParameterTypes().equals(parameterTypes) &&
&& hint.getParameterTypes().equals(parameterTypes) (hint.getMode().equals(ExecutableMode.INVOKE) || !executableModes.equals(ExecutableMode.INVOKE));
&& (hint.getMode().equals(ExecutableMode.INVOKE)
|| !executableModes.equals(ExecutableMode.INVOKE));
} }
} }
@@ -355,10 +352,8 @@ public class ReflectionHintsPredicates {
Predicate<RuntimeHints> exactMatch() { Predicate<RuntimeHints> exactMatch() {
return hints -> (hints.reflection().getTypeHint(this.executable.getDeclaringClass()) != null) && return hints -> (hints.reflection().getTypeHint(this.executable.getDeclaringClass()) != null) &&
hints.reflection().getTypeHint(this.executable.getDeclaringClass()).constructors().anyMatch(executableHint -> { hints.reflection().getTypeHint(this.executable.getDeclaringClass()).constructors().anyMatch(executableHint -> {
List<TypeReference> parameters = Arrays.stream(this.executable.getParameterTypes()) List<TypeReference> parameters = TypeReference.listOf(this.executable.getParameterTypes());
.map(TypeReference::of).toList(); return includes(executableHint, "<init>", parameters, this.executableMode);
return includes(executableHint, "<init>",
parameters, this.executableMode);
}); });
} }
@@ -366,7 +361,6 @@ public class ReflectionHintsPredicates {
public static class MethodHintPredicate extends ExecutableHintPredicate<Method> { public static class MethodHintPredicate extends ExecutableHintPredicate<Method> {
MethodHintPredicate(Method method) { MethodHintPredicate(Method method) {
super(method); super(method);
} }
@@ -394,10 +388,8 @@ public class ReflectionHintsPredicates {
Predicate<RuntimeHints> exactMatch() { Predicate<RuntimeHints> exactMatch() {
return hints -> (hints.reflection().getTypeHint(this.executable.getDeclaringClass()) != null) && return hints -> (hints.reflection().getTypeHint(this.executable.getDeclaringClass()) != null) &&
hints.reflection().getTypeHint(this.executable.getDeclaringClass()).methods().anyMatch(executableHint -> { hints.reflection().getTypeHint(this.executable.getDeclaringClass()).methods().anyMatch(executableHint -> {
List<TypeReference> parameters = Arrays.stream(this.executable.getParameterTypes()) List<TypeReference> parameters = TypeReference.listOf(this.executable.getParameterTypes());
.map(TypeReference::of).toList(); return includes(executableHint, this.executable.getName(), parameters, this.executableMode);
return includes(executableHint, this.executable.getName(),
parameters, this.executableMode);
}); });
} }
@@ -422,8 +414,8 @@ public class ReflectionHintsPredicates {
private boolean memberCategoryMatch(TypeHint typeHint) { private boolean memberCategoryMatch(TypeHint typeHint) {
if (Modifier.isPublic(this.field.getModifiers())) { if (Modifier.isPublic(this.field.getModifiers())) {
return typeHint.getMemberCategories().contains(MemberCategory.PUBLIC_FIELDS) return typeHint.getMemberCategories().contains(MemberCategory.PUBLIC_FIELDS) ||
|| typeHint.getMemberCategories().contains(MemberCategory.DECLARED_FIELDS); typeHint.getMemberCategories().contains(MemberCategory.DECLARED_FIELDS);
} }
else { else {
return typeHint.getMemberCategories().contains(MemberCategory.DECLARED_FIELDS); return typeHint.getMemberCategories().contains(MemberCategory.DECLARED_FIELDS);
@@ -434,6 +426,7 @@ public class ReflectionHintsPredicates {
return typeHint.fields().anyMatch(fieldHint -> return typeHint.fields().anyMatch(fieldHint ->
this.field.getName().equals(fieldHint.getName())); this.field.getName().equals(fieldHint.getName()));
} }
} }
} }

View File

@@ -96,17 +96,13 @@ class ProxyHintsTests {
private static Consumer<JdkProxyHint> proxiedInterfaces(Class<?>... proxiedInterfaces) { private static Consumer<JdkProxyHint> proxiedInterfaces(Class<?>... proxiedInterfaces) {
return jdkProxyHint -> assertThat(jdkProxyHint.getProxiedInterfaces()) return jdkProxyHint -> assertThat(jdkProxyHint.getProxiedInterfaces())
.containsExactly(toTypeReferences(proxiedInterfaces)); .containsExactlyElementsOf(TypeReference.listOf(proxiedInterfaces));
} }
private static TypeReference[] toTypeReferences(String... proxiedInterfaces) { private static TypeReference[] toTypeReferences(String... proxiedInterfaces) {
return Arrays.stream(proxiedInterfaces).map(TypeReference::of).toArray(TypeReference[]::new); return Arrays.stream(proxiedInterfaces).map(TypeReference::of).toArray(TypeReference[]::new);
} }
private static TypeReference[] toTypeReferences(Class<?>... proxiedInterfaces) {
return Arrays.stream(proxiedInterfaces).map(TypeReference::of).toArray(TypeReference[]::new);
}
sealed interface SealedInterface { sealed interface SealedInterface {
} }