Polishing

This commit is contained in:
Sam Brannen 2022-10-08 13:35:48 +02:00
parent ced37d53b4
commit c98ed17728
4 changed files with 25 additions and 35 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* 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.
@ -48,7 +48,8 @@ import org.springframework.util.StringUtils;
@SuppressWarnings("serial")
public class SpringCacheAnnotationParser implements CacheAnnotationParser, Serializable {
private static final Set<Class<? extends Annotation>> CACHE_OPERATION_ANNOTATIONS = Set.of(Cacheable.class, CacheEvict.class, CachePut.class, Caching.class);
private static final Set<Class<? extends Annotation>> CACHE_OPERATION_ANNOTATIONS =
Set.of(Cacheable.class, CacheEvict.class, CachePut.class, Caching.class);
@Override

View File

@ -25,7 +25,6 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Stream;
import org.springframework.aot.hint.ReflectionHints;
import org.springframework.aot.hint.RuntimeHints;
@ -33,8 +32,10 @@ import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import static org.springframework.core.annotation.MergedAnnotations.SearchStrategy.TYPE_HIERARCHY;
/**
* Process {@link Reflective} annotated elements.
* Process {@link Reflective @Reflective} annotated elements.
*
* @author Stephane Nicoll
* @author Andy Wilkinson
@ -53,12 +54,12 @@ public class ReflectiveRuntimeHintsRegistrar {
*/
public void registerRuntimeHints(RuntimeHints runtimeHints, Class<?>... types) {
Set<Entry> entries = new HashSet<>();
Arrays.stream(types).forEach(type -> {
for (Class<?> type : types) {
processType(entries, type);
for (Class<?> implementedInterface : ClassUtils.getAllInterfacesForClass(type)) {
processType(entries, implementedInterface);
}
});
}
entries.forEach(entry -> {
AnnotatedElement element = entry.element();
entry.processor().registerReflectionHints(runtimeHints.reflection(), element);
@ -86,20 +87,21 @@ public class ReflectiveRuntimeHintsRegistrar {
}
private boolean isReflective(AnnotatedElement element) {
return MergedAnnotations.from(element, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY).isPresent(Reflective.class);
return MergedAnnotations.from(element, TYPE_HIERARCHY).isPresent(Reflective.class);
}
@SuppressWarnings("unchecked")
private Entry createEntry(AnnotatedElement element) {
List<ReflectiveProcessor> processors =
MergedAnnotations.from(element, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY)
.stream(Reflective.class).flatMap(annotation -> Stream.of(annotation.getClassArray("value")))
.map(type -> (Class<? extends ReflectiveProcessor>) type)
.distinct()
.map(processorClass -> this.processors.computeIfAbsent(processorClass, this::instantiateClass))
.toList();
List<ReflectiveProcessor> processors = MergedAnnotations.from(element, TYPE_HIERARCHY)
.stream(Reflective.class)
.map(annotation -> annotation.getClassArray("value"))
.flatMap(Arrays::stream)
.map(type -> (Class<? extends ReflectiveProcessor>) type)
.distinct()
.map(processorClass -> this.processors.computeIfAbsent(processorClass, this::instantiateClass))
.toList();
ReflectiveProcessor processorToUse = (processors.size() == 1 ? processors.get(0)
: new DelegateReflectiveProcessor(processors));
: new DelegatingReflectiveProcessor(processors));
return new Entry(element, processorToUse);
}
@ -114,11 +116,11 @@ public class ReflectiveRuntimeHintsRegistrar {
}
}
static class DelegateReflectiveProcessor implements ReflectiveProcessor {
private static class DelegatingReflectiveProcessor implements ReflectiveProcessor {
private final Iterable<ReflectiveProcessor> processors;
public DelegateReflectiveProcessor(Iterable<ReflectiveProcessor> processors) {
DelegatingReflectiveProcessor(Iterable<ReflectiveProcessor> processors) {
this.processors = processors;
}

View File

@ -132,6 +132,7 @@ class ReflectiveRuntimeHintsRegistrarTests {
this.registrar.registerRuntimeHints(this.runtimeHints, beanClass);
}
@Reflective
@SuppressWarnings("unused")
static class SampleTypeAnnotatedBean {
@ -139,7 +140,6 @@ class ReflectiveRuntimeHintsRegistrarTests {
private String notManaged;
public void notManaged() {
}
}
@ -148,13 +148,10 @@ class ReflectiveRuntimeHintsRegistrarTests {
@Reflective
SampleConstructorAnnotatedBean(String name) {
}
SampleConstructorAnnotatedBean(Integer nameAsNumber) {
}
}
@SuppressWarnings("unused")
@ -176,7 +173,6 @@ class ReflectiveRuntimeHintsRegistrarTests {
void notManaged() {
}
}
@SuppressWarnings("unused")
@ -188,7 +184,6 @@ class ReflectiveRuntimeHintsRegistrarTests {
void notManaged() {
}
}
@SuppressWarnings("unused")
@ -200,7 +195,6 @@ class ReflectiveRuntimeHintsRegistrarTests {
void notManaged() {
}
}
static class SampleMethodAnnotatedBeanWithInterface implements SampleInterface {
@ -211,7 +205,6 @@ class ReflectiveRuntimeHintsRegistrarTests {
public void notManaged() {
}
}
static class SampleMethodAnnotatedBeanWithInheritance extends SampleInheritedClass {
@ -222,7 +215,6 @@ class ReflectiveRuntimeHintsRegistrarTests {
public void notManaged() {
}
}
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@ -251,7 +243,6 @@ class ReflectiveRuntimeHintsRegistrarTests {
@Documented
@Reflective(TestTypeHintReflectiveProcessor.class)
@interface ReflectiveWithCustomProcessor {
}
interface SampleInterface {
@ -273,7 +264,6 @@ class ReflectiveRuntimeHintsRegistrarTests {
public String managed() {
return "test";
}
}
@Reflective
@ -283,7 +273,6 @@ class ReflectiveRuntimeHintsRegistrarTests {
public String managed() {
return "test";
}
}
private static class TestMethodHintReflectiveProcessor extends SimpleReflectiveProcessor {
@ -293,7 +282,6 @@ class ReflectiveRuntimeHintsRegistrarTests {
super.registerMethodHint(hints, method);
hints.registerType(method.getReturnType(), MemberCategory.INVOKE_DECLARED_METHODS);
}
}
private static class TestTypeHintReflectiveProcessor extends SimpleReflectiveProcessor {
@ -303,7 +291,6 @@ class ReflectiveRuntimeHintsRegistrarTests {
super.registerTypeHint(hints, type);
hints.registerType(type, MemberCategory.INVOKE_DECLARED_METHODS);
}
}
}

View File

@ -61,7 +61,7 @@ import org.springframework.util.MimeType;
/**
* Base class providing support methods for Jackson 2.9 encoding. For non-streaming use
* cases, {@link Flux} elements are collected into a {@link List} before serialization for
* performance reason.
* performance reasons.
*
* @author Sebastien Deleuze
* @author Arjen Poutsma
@ -347,13 +347,13 @@ public abstract class AbstractJackson2Encoder extends Jackson2CodecSupport imple
}
/**
* Subclasses can use this method to customize {@link ObjectWriter} used
* Subclasses can use this method to customize the {@link ObjectWriter} used
* for writing values.
* @param writer the writer instance to customize
* @param mimeType the selected MIME type
* @param elementType the type of element values to write
* @param hints a map with serialization hints;
* the Reactor Context, when available, may be accessed under the key
* @param hints a map with serialization hints; the Reactor Context, when
* available, may be accessed under the key
* {@code ContextView.class.getName()}
* @return the customized {@code ObjectWriter} to use
*/