提交 e6f86c5c 编写于 作者: J Juergen Hoeller

Nullability refinements and related polishing

上级 cca32a56
...@@ -888,7 +888,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac ...@@ -888,7 +888,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
} }
// No type found for shortcut FactoryBean instance: // No type found for shortcut FactoryBean instance:
// fall back to full creation of the FactoryBean instance. // fall back to full creation of the FactoryBean instance.
return super.getTypeForFactoryBean(beanName, mbd, allowInit); return super.getTypeForFactoryBean(beanName, mbd, true);
} }
} }
...@@ -1990,6 +1990,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac ...@@ -1990,6 +1990,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
} }
} }
/** /**
* {@link MethodCallback} used to find {@link FactoryBean} type information. * {@link MethodCallback} used to find {@link FactoryBean} type information.
*/ */
...@@ -1999,12 +2000,10 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac ...@@ -1999,12 +2000,10 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
private ResolvableType result = ResolvableType.NONE; private ResolvableType result = ResolvableType.NONE;
FactoryBeanMethodTypeFinder(String factoryMethodName) { FactoryBeanMethodTypeFinder(String factoryMethodName) {
this.factoryMethodName = factoryMethodName; this.factoryMethodName = factoryMethodName;
} }
@Override @Override
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
if (isFactoryBeanMethod(method)) { if (isFactoryBeanMethod(method)) {
...@@ -2028,13 +2027,11 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac ...@@ -2028,13 +2027,11 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
FactoryBean.class.isAssignableFrom(method.getReturnType()); FactoryBean.class.isAssignableFrom(method.getReturnType());
} }
ResolvableType getResult() { ResolvableType getResult() {
Class<?> resolved = this.result.resolve(); Class<?> resolved = this.result.resolve();
boolean foundResult = resolved != null && resolved != Object.class; boolean foundResult = resolved != null && resolved != Object.class;
return (foundResult ? this.result : ResolvableType.NONE); return (foundResult ? this.result : ResolvableType.NONE);
} }
} }
} }
...@@ -612,7 +612,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp ...@@ -612,7 +612,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
if (FactoryBean.class.isAssignableFrom(predictedType)) { if (FactoryBean.class.isAssignableFrom(predictedType)) {
if (beanInstance == null && !isFactoryDereference) { if (beanInstance == null && !isFactoryDereference) {
beanType = getTypeForFactoryBean(beanName, mbd, allowFactoryBeanInit); beanType = getTypeForFactoryBean(beanName, mbd, allowFactoryBeanInit);
predictedType = (beanType != null) ? beanType.resolve() : null; predictedType = beanType.resolve();
if (predictedType == null) { if (predictedType == null) {
return false; return false;
} }
...@@ -1377,17 +1377,19 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp ...@@ -1377,17 +1377,19 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
} }
} }
private void copyRelevantMergedBeanDefinitionCaches(RootBeanDefinition previous, private void copyRelevantMergedBeanDefinitionCaches(RootBeanDefinition previous, RootBeanDefinition mbd) {
RootBeanDefinition mbd) {
if (ObjectUtils.nullSafeEquals(mbd.getBeanClassName(), previous.getBeanClassName()) && if (ObjectUtils.nullSafeEquals(mbd.getBeanClassName(), previous.getBeanClassName()) &&
ObjectUtils.nullSafeEquals(mbd.getFactoryBeanName(), previous.getFactoryBeanName()) && ObjectUtils.nullSafeEquals(mbd.getFactoryBeanName(), previous.getFactoryBeanName()) &&
ObjectUtils.nullSafeEquals(mbd.getFactoryMethodName(), previous.getFactoryMethodName()) && ObjectUtils.nullSafeEquals(mbd.getFactoryMethodName(), previous.getFactoryMethodName())) {
(mbd.targetType == null || mbd.targetType.equals(previous.targetType))) { ResolvableType targetType = mbd.targetType;
mbd.targetType = previous.targetType; ResolvableType previousTargetType = previous.targetType;
mbd.isFactoryBean = previous.isFactoryBean; if (targetType == null || targetType.equals(previousTargetType)) {
mbd.resolvedTargetType = previous.resolvedTargetType; mbd.targetType = previousTargetType;
mbd.factoryMethodReturnType = previous.factoryMethodReturnType; mbd.isFactoryBean = previous.isFactoryBean;
mbd.factoryMethodToIntrospect = previous.factoryMethodToIntrospect; mbd.resolvedTargetType = previous.resolvedTargetType;
mbd.factoryMethodReturnType = previous.factoryMethodReturnType;
mbd.factoryMethodToIntrospect = previous.factoryMethodToIntrospect;
}
} }
} }
...@@ -1625,9 +1627,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp ...@@ -1625,9 +1627,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
* @see org.springframework.beans.factory.FactoryBean#getObjectType() * @see org.springframework.beans.factory.FactoryBean#getObjectType()
* @see #getBean(String) * @see #getBean(String)
*/ */
protected ResolvableType getTypeForFactoryBean(String beanName, protected ResolvableType getTypeForFactoryBean(String beanName, RootBeanDefinition mbd, boolean allowInit) {
RootBeanDefinition mbd, boolean allowInit) {
ResolvableType result = getTypeForFactoryBeanFromAttributes(mbd); ResolvableType result = getTypeForFactoryBeanFromAttributes(mbd);
if (result != ResolvableType.NONE) { if (result != ResolvableType.NONE) {
return result; return result;
......
...@@ -583,8 +583,8 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto ...@@ -583,8 +583,8 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
return StringUtils.toStringArray(result); return StringUtils.toStringArray(result);
} }
private boolean isSingleton(String beanName, RootBeanDefinition mbd, BeanDefinitionHolder dbd) { private boolean isSingleton(String beanName, RootBeanDefinition mbd, @Nullable BeanDefinitionHolder dbd) {
return (dbd != null) ? mbd.isSingleton() : isSingleton(beanName); return (dbd != null ? mbd.isSingleton() : isSingleton(beanName));
} }
/** /**
......
...@@ -900,7 +900,7 @@ public abstract class StringUtils { ...@@ -900,7 +900,7 @@ public abstract class StringUtils {
* @return the resulting {@code String} array * @return the resulting {@code String} array
*/ */
public static String[] toStringArray(@Nullable Collection<String> collection) { public static String[] toStringArray(@Nullable Collection<String> collection) {
return (collection != null || collection.isEmpty() ? collection.toArray(EMPTY_STRING_ARRAY) : EMPTY_STRING_ARRAY); return (!CollectionUtils.isEmpty(collection) ? collection.toArray(EMPTY_STRING_ARRAY) : EMPTY_STRING_ARRAY);
} }
/** /**
......
...@@ -142,20 +142,31 @@ public class MessageMappingMessageHandler extends AbstractMethodMessageHandler<C ...@@ -142,20 +142,31 @@ public class MessageMappingMessageHandler extends AbstractMethodMessageHandler<C
* efficiency consider using the {@code PathPatternRouteMatcher} from * efficiency consider using the {@code PathPatternRouteMatcher} from
* {@code spring-web} instead. * {@code spring-web} instead.
*/ */
public void setRouteMatcher(RouteMatcher routeMatcher) { public void setRouteMatcher(@Nullable RouteMatcher routeMatcher) {
Assert.notNull(routeMatcher, "RouteMatcher must not be null");
this.routeMatcher = routeMatcher; this.routeMatcher = routeMatcher;
} }
/** /**
* Return the {@code RouteMatcher} used to map messages to handlers. * Return the {@code RouteMatcher} used to map messages to handlers.
* May be {@code null} before component is initialized. * May be {@code null} before the component is initialized.
*/ */
@Nullable @Nullable
public RouteMatcher getRouteMatcher() { public RouteMatcher getRouteMatcher() {
return this.routeMatcher; return this.routeMatcher;
} }
/**
* Obtain the {@code RouteMatcher} for actual use.
* @return the RouteMatcher (never {@code null})
* @throws IllegalStateException in case of no RouteMatcher set
* @since 5.0
*/
protected RouteMatcher obtainRouteMatcher() {
RouteMatcher routeMatcher = getRouteMatcher();
Assert.state(routeMatcher != null, "No RouteMatcher set");
return routeMatcher;
}
/** /**
* Configure a {@link ConversionService} to use for type conversion of * Configure a {@link ConversionService} to use for type conversion of
* String based values, e.g. in destination variables or headers. * String based values, e.g. in destination variables or headers.
...@@ -245,13 +256,13 @@ public class MessageMappingMessageHandler extends AbstractMethodMessageHandler<C ...@@ -245,13 +256,13 @@ public class MessageMappingMessageHandler extends AbstractMethodMessageHandler<C
*/ */
@Nullable @Nullable
protected CompositeMessageCondition getCondition(AnnotatedElement element) { protected CompositeMessageCondition getCondition(AnnotatedElement element) {
MessageMapping annot = AnnotatedElementUtils.findMergedAnnotation(element, MessageMapping.class); MessageMapping ann = AnnotatedElementUtils.findMergedAnnotation(element, MessageMapping.class);
if (annot == null || annot.value().length == 0) { if (ann == null || ann.value().length == 0) {
return null; return null;
} }
String[] patterns = processDestinations(annot.value()); String[] patterns = processDestinations(ann.value());
return new CompositeMessageCondition( return new CompositeMessageCondition(
new DestinationPatternsMessageCondition(patterns, this.routeMatcher)); new DestinationPatternsMessageCondition(patterns, obtainRouteMatcher()));
} }
/** /**
...@@ -272,7 +283,7 @@ public class MessageMappingMessageHandler extends AbstractMethodMessageHandler<C ...@@ -272,7 +283,7 @@ public class MessageMappingMessageHandler extends AbstractMethodMessageHandler<C
protected Set<String> getDirectLookupMappings(CompositeMessageCondition mapping) { protected Set<String> getDirectLookupMappings(CompositeMessageCondition mapping) {
Set<String> result = new LinkedHashSet<>(); Set<String> result = new LinkedHashSet<>();
for (String pattern : mapping.getCondition(DestinationPatternsMessageCondition.class).getPatterns()) { for (String pattern : mapping.getCondition(DestinationPatternsMessageCondition.class).getPatterns()) {
if (!this.routeMatcher.isPattern(pattern)) { if (!obtainRouteMatcher().isPattern(pattern)) {
result.add(pattern); result.add(pattern);
} }
} }
...@@ -309,7 +320,7 @@ public class MessageMappingMessageHandler extends AbstractMethodMessageHandler<C ...@@ -309,7 +320,7 @@ public class MessageMappingMessageHandler extends AbstractMethodMessageHandler<C
String pattern = patterns.iterator().next(); String pattern = patterns.iterator().next();
RouteMatcher.Route destination = getDestination(message); RouteMatcher.Route destination = getDestination(message);
Assert.state(destination != null, "Missing destination header"); Assert.state(destination != null, "Missing destination header");
Map<String, String> vars = getRouteMatcher().matchAndExtract(pattern, destination); Map<String, String> vars = obtainRouteMatcher().matchAndExtract(pattern, destination);
if (!CollectionUtils.isEmpty(vars)) { if (!CollectionUtils.isEmpty(vars)) {
MessageHeaderAccessor mha = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class); MessageHeaderAccessor mha = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class);
Assert.state(mha != null && mha.isMutable(), "Mutable MessageHeaderAccessor required"); Assert.state(mha != null && mha.isMutable(), "Mutable MessageHeaderAccessor required");
......
...@@ -56,23 +56,23 @@ final class DefaultRSocketStrategies implements RSocketStrategies { ...@@ -56,23 +56,23 @@ final class DefaultRSocketStrategies implements RSocketStrategies {
private final RouteMatcher routeMatcher; private final RouteMatcher routeMatcher;
private final MetadataExtractor metadataExtractor; private final ReactiveAdapterRegistry adapterRegistry;
private final DataBufferFactory bufferFactory; private final DataBufferFactory bufferFactory;
private final ReactiveAdapterRegistry adapterRegistry; private final MetadataExtractor metadataExtractor;
private DefaultRSocketStrategies(List<Encoder<?>> encoders, List<Decoder<?>> decoders, private DefaultRSocketStrategies(List<Encoder<?>> encoders, List<Decoder<?>> decoders,
RouteMatcher routeMatcher, MetadataExtractor metadataExtractor, RouteMatcher routeMatcher, ReactiveAdapterRegistry adapterRegistry,
DataBufferFactory bufferFactory, ReactiveAdapterRegistry adapterRegistry) { DataBufferFactory bufferFactory, MetadataExtractor metadataExtractor) {
this.encoders = Collections.unmodifiableList(encoders); this.encoders = Collections.unmodifiableList(encoders);
this.decoders = Collections.unmodifiableList(decoders); this.decoders = Collections.unmodifiableList(decoders);
this.routeMatcher = routeMatcher; this.routeMatcher = routeMatcher;
this.metadataExtractor = metadataExtractor;
this.bufferFactory = bufferFactory;
this.adapterRegistry = adapterRegistry; this.adapterRegistry = adapterRegistry;
this.bufferFactory = bufferFactory;
this.metadataExtractor = metadataExtractor;
} }
...@@ -92,8 +92,8 @@ final class DefaultRSocketStrategies implements RSocketStrategies { ...@@ -92,8 +92,8 @@ final class DefaultRSocketStrategies implements RSocketStrategies {
} }
@Override @Override
public MetadataExtractor metadataExtractor() { public ReactiveAdapterRegistry reactiveAdapterRegistry() {
return this.metadataExtractor; return this.adapterRegistry;
} }
@Override @Override
...@@ -102,8 +102,8 @@ final class DefaultRSocketStrategies implements RSocketStrategies { ...@@ -102,8 +102,8 @@ final class DefaultRSocketStrategies implements RSocketStrategies {
} }
@Override @Override
public ReactiveAdapterRegistry reactiveAdapterRegistry() { public MetadataExtractor metadataExtractor() {
return this.adapterRegistry; return this.metadataExtractor;
} }
...@@ -119,39 +119,36 @@ final class DefaultRSocketStrategies implements RSocketStrategies { ...@@ -119,39 +119,36 @@ final class DefaultRSocketStrategies implements RSocketStrategies {
@Nullable @Nullable
private RouteMatcher routeMatcher; private RouteMatcher routeMatcher;
@Nullable
private MetadataExtractor metadataExtractor;
@Nullable @Nullable
private ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance(); private ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
@Nullable @Nullable
private DataBufferFactory bufferFactory; private DataBufferFactory bufferFactory;
@Nullable
private MetadataExtractor metadataExtractor;
DefaultRSocketStrategiesBuilder() { DefaultRSocketStrategiesBuilder() {
this.encoders.add(CharSequenceEncoder.allMimeTypes());
this.encoders.add(new ByteBufferEncoder());
this.encoders.add(new ByteArrayEncoder());
this.encoders.add(new DataBufferEncoder());
// Order of decoders may be significant for default data MimeType // Order of decoders may be significant for default data MimeType
// selection in RSocketRequester.Builder // selection in RSocketRequester.Builder
this.decoders.add(StringDecoder.allMimeTypes()); this.decoders.add(StringDecoder.allMimeTypes());
this.decoders.add(new ByteBufferDecoder()); this.decoders.add(new ByteBufferDecoder());
this.decoders.add(new ByteArrayDecoder()); this.decoders.add(new ByteArrayDecoder());
this.decoders.add(new DataBufferDecoder()); this.decoders.add(new DataBufferDecoder());
this.encoders.add(CharSequenceEncoder.allMimeTypes());
this.encoders.add(new ByteBufferEncoder());
this.encoders.add(new ByteArrayEncoder());
this.encoders.add(new DataBufferEncoder());
} }
DefaultRSocketStrategiesBuilder(RSocketStrategies other) { DefaultRSocketStrategiesBuilder(RSocketStrategies other) {
this.encoders.addAll(other.encoders()); this.encoders.addAll(other.encoders());
this.decoders.addAll(other.decoders()); this.decoders.addAll(other.decoders());
this.routeMatcher = other.routeMatcher(); this.routeMatcher = other.routeMatcher();
this.metadataExtractor = other.metadataExtractor();
this.adapterRegistry = other.reactiveAdapterRegistry(); this.adapterRegistry = other.reactiveAdapterRegistry();
this.bufferFactory = other.dataBufferFactory(); this.bufferFactory = other.dataBufferFactory();
this.metadataExtractor = other.metadataExtractor();
} }
...@@ -185,12 +182,6 @@ final class DefaultRSocketStrategies implements RSocketStrategies { ...@@ -185,12 +182,6 @@ final class DefaultRSocketStrategies implements RSocketStrategies {
return this; return this;
} }
@Override
public Builder metadataExtractor(@Nullable MetadataExtractor metadataExtractor) {
this.metadataExtractor = metadataExtractor;
return this;
}
@Override @Override
public Builder reactiveAdapterStrategy(@Nullable ReactiveAdapterRegistry registry) { public Builder reactiveAdapterStrategy(@Nullable ReactiveAdapterRegistry registry) {
this.adapterRegistry = registry; this.adapterRegistry = registry;
...@@ -204,21 +195,26 @@ final class DefaultRSocketStrategies implements RSocketStrategies { ...@@ -204,21 +195,26 @@ final class DefaultRSocketStrategies implements RSocketStrategies {
} }
@Override @Override
public RSocketStrategies build() { public Builder metadataExtractor(@Nullable MetadataExtractor metadataExtractor) {
this.metadataExtractor = metadataExtractor;
return this;
}
RouteMatcher matcher = this.routeMatcher != null ? this.routeMatcher : initRouteMatcher(); @Override
public RSocketStrategies build() {
RouteMatcher matcher = (this.routeMatcher != null ? this.routeMatcher : initRouteMatcher());
MetadataExtractor extractor = this.metadataExtractor != null ? ReactiveAdapterRegistry registry = (this.adapterRegistry != null ?
this.metadataExtractor : new DefaultMetadataExtractor(this.decoders); this.adapterRegistry : ReactiveAdapterRegistry.getSharedInstance());
DataBufferFactory factory = this.bufferFactory != null ? DataBufferFactory factory = (this.bufferFactory != null ?
this.bufferFactory : new NettyDataBufferFactory(PooledByteBufAllocator.DEFAULT); this.bufferFactory : new NettyDataBufferFactory(PooledByteBufAllocator.DEFAULT));
ReactiveAdapterRegistry registry = this.adapterRegistry != null ? MetadataExtractor extractor = (this.metadataExtractor != null ?
this.adapterRegistry : ReactiveAdapterRegistry.getSharedInstance(); this.metadataExtractor : new DefaultMetadataExtractor(this.decoders));
return new DefaultRSocketStrategies( return new DefaultRSocketStrategies(
this.encoders, this.decoders, matcher, extractor, factory, registry); this.encoders, this.decoders, matcher, registry, factory, extractor);
} }
private RouteMatcher initRouteMatcher() { private RouteMatcher initRouteMatcher() {
......
...@@ -98,9 +98,10 @@ public interface RSocketStrategies { ...@@ -98,9 +98,10 @@ public interface RSocketStrategies {
RouteMatcher routeMatcher(); RouteMatcher routeMatcher();
/** /**
* Return the configured {@link Builder#metadataExtractor(MetadataExtractor)}. * Return the configured
* {@link Builder#reactiveAdapterStrategy(ReactiveAdapterRegistry) reactiveAdapterRegistry}.
*/ */
MetadataExtractor metadataExtractor(); ReactiveAdapterRegistry reactiveAdapterRegistry();
/** /**
* Return the configured * Return the configured
...@@ -109,10 +110,9 @@ public interface RSocketStrategies { ...@@ -109,10 +110,9 @@ public interface RSocketStrategies {
DataBufferFactory dataBufferFactory(); DataBufferFactory dataBufferFactory();
/** /**
* Return the configured * Return the configured {@link Builder#metadataExtractor(MetadataExtractor)}.
* {@link Builder#reactiveAdapterStrategy(ReactiveAdapterRegistry) reactiveAdapterRegistry}.
*/ */
ReactiveAdapterRegistry reactiveAdapterRegistry(); MetadataExtractor metadataExtractor();
/** /**
* Return a builder to create a new {@link RSocketStrategies} instance * Return a builder to create a new {@link RSocketStrategies} instance
...@@ -184,16 +184,6 @@ public interface RSocketStrategies { ...@@ -184,16 +184,6 @@ public interface RSocketStrategies {
*/ */
Builder routeMatcher(@Nullable RouteMatcher routeMatcher); Builder routeMatcher(@Nullable RouteMatcher routeMatcher);
/**
* Configure a {@link MetadataExtractor} to extract the route along with
* other metadata. This option is applicable to client or server
* responders.
* <p>By default this is {@link DefaultMetadataExtractor} created with
* the {@link #decoder(Decoder[]) configured} decoders and extracting a
* route from {@code "message/x.rsocket.routing.v0"} metadata.
*/
Builder metadataExtractor(@Nullable MetadataExtractor metadataExtractor);
/** /**
* Configure the registry for reactive type support. This can be used to * Configure the registry for reactive type support. This can be used to
* to adapt to, and/or determine the semantics of a given * to adapt to, and/or determine the semantics of a given
...@@ -219,6 +209,16 @@ public interface RSocketStrategies { ...@@ -219,6 +209,16 @@ public interface RSocketStrategies {
*/ */
Builder dataBufferFactory(@Nullable DataBufferFactory bufferFactory); Builder dataBufferFactory(@Nullable DataBufferFactory bufferFactory);
/**
* Configure a {@link MetadataExtractor} to extract the route along with
* other metadata. This option is applicable to client or server
* responders.
* <p>By default this is {@link DefaultMetadataExtractor} created with
* the {@link #decoder(Decoder[]) configured} decoders and extracting a
* route from {@code "message/x.rsocket.routing.v0"} metadata.
*/
Builder metadataExtractor(@Nullable MetadataExtractor metadataExtractor);
/** /**
* Build the {@code RSocketStrategies} instance. * Build the {@code RSocketStrategies} instance.
*/ */
......
...@@ -77,15 +77,15 @@ class MessagingRSocket extends AbstractRSocket { ...@@ -77,15 +77,15 @@ class MessagingRSocket extends AbstractRSocket {
MessagingRSocket(MimeType dataMimeType, MimeType metadataMimeType, MetadataExtractor metadataExtractor, MessagingRSocket(MimeType dataMimeType, MimeType metadataMimeType, MetadataExtractor metadataExtractor,
RSocketRequester requester, ReactiveMessageHandler messageHandler, RSocketRequester requester, ReactiveMessageHandler messageHandler, RouteMatcher routeMatcher,
RouteMatcher routeMatcher, RSocketStrategies strategies) { RSocketStrategies strategies) {
Assert.notNull(dataMimeType, "'dataMimeType' is required"); Assert.notNull(dataMimeType, "'dataMimeType' is required");
Assert.notNull(metadataMimeType, "'metadataMimeType' is required"); Assert.notNull(metadataMimeType, "'metadataMimeType' is required");
Assert.notNull(metadataExtractor, "'metadataExtractor' is required"); Assert.notNull(metadataExtractor, "MetadataExtractor is required");
Assert.notNull(requester, "'requester' is required"); Assert.notNull(requester, "RSocketRequester is required");
Assert.notNull(messageHandler, "'messageHandler' is required"); Assert.notNull(messageHandler, "ReactiveMessageHandler is required");
Assert.notNull(routeMatcher, "'routeMatcher' is required"); Assert.notNull(routeMatcher, "RouteMatcher is required");
Assert.notNull(strategies, "RSocketStrategies is required"); Assert.notNull(strategies, "RSocketStrategies is required");
this.dataMimeType = dataMimeType; this.dataMimeType = dataMimeType;
......
...@@ -73,9 +73,7 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler { ...@@ -73,9 +73,7 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler {
private final List<Encoder<?>> encoders = new ArrayList<>(); private final List<Encoder<?>> encoders = new ArrayList<>();
private MetadataExtractor metadataExtractor; private RSocketStrategies strategies = RSocketStrategies.create();
private RSocketStrategies strategies;
@Nullable @Nullable
private MimeType defaultDataMimeType; private MimeType defaultDataMimeType;
...@@ -84,29 +82,9 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler { ...@@ -84,29 +82,9 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler {
public RSocketMessageHandler() { public RSocketMessageHandler() {
setRSocketStrategies(RSocketStrategies.create()); setRSocketStrategies(this.strategies);
} }
/**
* {@inheritDoc}
* <p>When {@link #setRSocketStrategies(RSocketStrategies) rsocketStrategies}
* is set, this property is re-initialized with the decoders in it, and
* likewise when this property is set the {@code RSocketStrategies} are
* mutated to change the decoders in them.
* <p>By default this is set to the
* {@link RSocketStrategies.Builder#decoder(Decoder[]) defaults} from
* {@code RSocketStrategies}.
*/
@Override
public void setDecoders(List<? extends Decoder<?>> decoders) {
super.setDecoders(decoders);
this.strategies = this.strategies.mutate()
.decoders(list -> {
list.clear();
list.addAll(decoders);
})
.build();
}
/** /**
* Configure the encoders to use for encoding handler method return values. * Configure the encoders to use for encoding handler method return values.
...@@ -136,6 +114,27 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler { ...@@ -136,6 +114,27 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler {
return this.encoders; return this.encoders;
} }
/**
* {@inheritDoc}
* <p>When {@link #setRSocketStrategies(RSocketStrategies) rsocketStrategies}
* is set, this property is re-initialized with the decoders in it, and
* likewise when this property is set the {@code RSocketStrategies} are
* mutated to change the decoders in them.
* <p>By default this is set to the
* {@link RSocketStrategies.Builder#decoder(Decoder[]) defaults} from
* {@code RSocketStrategies}.
*/
@Override
public void setDecoders(List<? extends Decoder<?>> decoders) {
super.setDecoders(decoders);
this.strategies = this.strategies.mutate()
.decoders(list -> {
list.clear();
list.addAll(decoders);
})
.build();
}
/** /**
* {@inheritDoc} * {@inheritDoc}
* <p>When {@link #setRSocketStrategies(RSocketStrategies) rsocketStrategies} * <p>When {@link #setRSocketStrategies(RSocketStrategies) rsocketStrategies}
...@@ -147,7 +146,7 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler { ...@@ -147,7 +146,7 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler {
* from {@code RSocketStrategies}. * from {@code RSocketStrategies}.
*/ */
@Override @Override
public void setRouteMatcher(RouteMatcher routeMatcher) { public void setRouteMatcher(@Nullable RouteMatcher routeMatcher) {
super.setRouteMatcher(routeMatcher); super.setRouteMatcher(routeMatcher);
this.strategies = this.strategies.mutate().routeMatcher(routeMatcher).build(); this.strategies = this.strategies.mutate().routeMatcher(routeMatcher).build();
} }
...@@ -181,17 +180,14 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler { ...@@ -181,17 +180,14 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler {
* @param extractor the extractor to use * @param extractor the extractor to use
*/ */
public void setMetadataExtractor(MetadataExtractor extractor) { public void setMetadataExtractor(MetadataExtractor extractor) {
this.metadataExtractor = extractor; this.strategies = this.strategies.mutate().metadataExtractor(extractor).build();
this.strategies = this.strategies.mutate().metadataExtractor(this.metadataExtractor).build();
} }
/** /**
* Return the configured {@link #setMetadataExtractor MetadataExtractor}. * Return the configured {@link #setMetadataExtractor MetadataExtractor}.
* This may be {@code null} before {@link #afterPropertiesSet()}.
*/ */
@Nullable
public MetadataExtractor getMetadataExtractor() { public MetadataExtractor getMetadataExtractor() {
return this.metadataExtractor; return this.strategies.metadataExtractor();
} }
/** /**
...@@ -210,11 +206,11 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler { ...@@ -210,11 +206,11 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler {
*/ */
public void setRSocketStrategies(RSocketStrategies rsocketStrategies) { public void setRSocketStrategies(RSocketStrategies rsocketStrategies) {
this.strategies = rsocketStrategies; this.strategies = rsocketStrategies;
setDecoders(this.strategies.decoders()); this.encoders.clear();
setEncoders(this.strategies.encoders()); this.encoders.addAll(this.strategies.encoders());
setRouteMatcher(this.strategies.routeMatcher()); super.setDecoders(this.strategies.decoders());
setMetadataExtractor(this.strategies.metadataExtractor()); super.setRouteMatcher(this.strategies.routeMatcher());
setReactiveAdapterRegistry(this.strategies.reactiveAdapterRegistry()); super.setReactiveAdapterRegistry(this.strategies.reactiveAdapterRegistry());
} }
/** /**
...@@ -284,19 +280,19 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler { ...@@ -284,19 +280,19 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler {
@Override @Override
@Nullable @Nullable
protected CompositeMessageCondition getCondition(AnnotatedElement element) { protected CompositeMessageCondition getCondition(AnnotatedElement element) {
MessageMapping annot1 = AnnotatedElementUtils.findMergedAnnotation(element, MessageMapping.class); MessageMapping ann1 = AnnotatedElementUtils.findMergedAnnotation(element, MessageMapping.class);
if (annot1 != null && annot1.value().length > 0) { if (ann1 != null && ann1.value().length > 0) {
String[] patterns = processDestinations(annot1.value()); String[] patterns = processDestinations(ann1.value());
return new CompositeMessageCondition( return new CompositeMessageCondition(
RSocketFrameTypeMessageCondition.REQUEST_CONDITION, RSocketFrameTypeMessageCondition.REQUEST_CONDITION,
new DestinationPatternsMessageCondition(patterns, getRouteMatcher())); new DestinationPatternsMessageCondition(patterns, obtainRouteMatcher()));
} }
ConnectMapping annot2 = AnnotatedElementUtils.findMergedAnnotation(element, ConnectMapping.class); ConnectMapping ann2 = AnnotatedElementUtils.findMergedAnnotation(element, ConnectMapping.class);
if (annot2 != null) { if (ann2 != null) {
String[] patterns = processDestinations(annot2.value()); String[] patterns = processDestinations(ann2.value());
return new CompositeMessageCondition( return new CompositeMessageCondition(
RSocketFrameTypeMessageCondition.CONNECT_CONDITION, RSocketFrameTypeMessageCondition.CONNECT_CONDITION,
new DestinationPatternsMessageCondition(patterns, getRouteMatcher())); new DestinationPatternsMessageCondition(patterns, obtainRouteMatcher()));
} }
return null; return null;
} }
...@@ -362,22 +358,18 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler { ...@@ -362,22 +358,18 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler {
} }
private MessagingRSocket createResponder(ConnectionSetupPayload setupPayload, RSocket rsocket) { private MessagingRSocket createResponder(ConnectionSetupPayload setupPayload, RSocket rsocket) {
String s = setupPayload.dataMimeType(); String str = setupPayload.dataMimeType();
MimeType dataMimeType = StringUtils.hasText(s) ? MimeTypeUtils.parseMimeType(s) : this.defaultDataMimeType; MimeType dataMimeType = StringUtils.hasText(str) ? MimeTypeUtils.parseMimeType(str) : this.defaultDataMimeType;
Assert.notNull(dataMimeType, "No `dataMimeType` in ConnectionSetupPayload and no default value"); Assert.notNull(dataMimeType, "No `dataMimeType` in ConnectionSetupPayload and no default value");
Assert.isTrue(isDataMimeTypeSupported(dataMimeType), "Data MimeType '" + dataMimeType + "' not supported"); Assert.isTrue(isDataMimeTypeSupported(dataMimeType), "Data MimeType '" + dataMimeType + "' not supported");
s = setupPayload.metadataMimeType(); str = setupPayload.metadataMimeType();
MimeType metaMimeType = StringUtils.hasText(s) ? MimeTypeUtils.parseMimeType(s) : this.defaultMetadataMimeType; MimeType metaMimeType = StringUtils.hasText(str) ? MimeTypeUtils.parseMimeType(str) : this.defaultMetadataMimeType;
Assert.notNull(metaMimeType, "No `metadataMimeType` in ConnectionSetupPayload and no default value"); Assert.notNull(metaMimeType, "No `metadataMimeType` in ConnectionSetupPayload and no default value");
RSocketRequester requester = RSocketRequester.wrap( RSocketRequester requester = RSocketRequester.wrap(rsocket, dataMimeType, metaMimeType, this.strategies);
rsocket, dataMimeType, metaMimeType, this.strategies); return new MessagingRSocket(dataMimeType, metaMimeType, getMetadataExtractor(),
requester, this, obtainRouteMatcher(), this.strategies);
Assert.state(getRouteMatcher() != null, () -> "No RouteMatcher. Was afterPropertiesSet not called?");
return new MessagingRSocket(dataMimeType, metaMimeType, this.metadataExtractor,
requester, this, getRouteMatcher(), this.strategies);
} }
private boolean isDataMimeTypeSupported(MimeType dataMimeType) { private boolean isDataMimeTypeSupported(MimeType dataMimeType) {
......
...@@ -163,6 +163,7 @@ public class SqlScriptsTestExecutionListener extends AbstractTestExecutionListen ...@@ -163,6 +163,7 @@ public class SqlScriptsTestExecutionListener extends AbstractTestExecutionListen
/** /**
* Get the {@code @SqlMergeMode} annotation declared on the supplied {@code element}. * Get the {@code @SqlMergeMode} annotation declared on the supplied {@code element}.
*/ */
@Nullable
private SqlMergeMode getSqlMergeModeFor(AnnotatedElement element) { private SqlMergeMode getSqlMergeModeFor(AnnotatedElement element) {
return AnnotatedElementUtils.findMergedAnnotation(element, SqlMergeMode.class); return AnnotatedElementUtils.findMergedAnnotation(element, SqlMergeMode.class);
} }
......
...@@ -32,6 +32,7 @@ import org.springframework.util.Assert; ...@@ -32,6 +32,7 @@ import org.springframework.util.Assert;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.util.ResourceUtils; import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
/** /**
* {@code TestPropertySourceAttributes} encapsulates attributes declared * {@code TestPropertySourceAttributes} encapsulates attributes declared
...@@ -196,7 +197,7 @@ class TestPropertySourceAttributes { ...@@ -196,7 +197,7 @@ class TestPropertySourceAttributes {
* @see TestPropertySource#locations * @see TestPropertySource#locations
*/ */
String[] getLocations() { String[] getLocations() {
return this.locations.toArray(new String[0]); return StringUtils.toStringArray(this.locations);
} }
/** /**
...@@ -217,7 +218,7 @@ class TestPropertySourceAttributes { ...@@ -217,7 +218,7 @@ class TestPropertySourceAttributes {
* @see TestPropertySource#properties * @see TestPropertySource#properties
*/ */
String[] getProperties() { String[] getProperties() {
return this.properties.toArray(new String[0]); return StringUtils.toStringArray(this.properties);
} }
/** /**
...@@ -245,7 +246,9 @@ class TestPropertySourceAttributes { ...@@ -245,7 +246,9 @@ class TestPropertySourceAttributes {
} }
private static Class<?> declaringClass(MergedAnnotation<?> mergedAnnotation) { private static Class<?> declaringClass(MergedAnnotation<?> mergedAnnotation) {
return (Class<?>) mergedAnnotation.getSource(); Object source = mergedAnnotation.getSource();
Assert.state(source instanceof Class, "No source class available");
return (Class<?>) source;
} }
} }
...@@ -130,9 +130,7 @@ public abstract class TestPropertySourceUtils { ...@@ -130,9 +130,7 @@ public abstract class TestPropertySourceUtils {
logger.trace(String.format("Processing inlined properties for TestPropertySource attributes %s", attrs)); logger.trace(String.format("Processing inlined properties for TestPropertySource attributes %s", attrs));
} }
String[] attrProps = attrs.getProperties(); String[] attrProps = attrs.getProperties();
if (attrProps != null) { properties.addAll(0, Arrays.asList(attrProps));
properties.addAll(0, Arrays.asList(attrProps));
}
if (!attrs.isInheritProperties()) { if (!attrs.isInheritProperties()) {
break; break;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册