Skip to content

Commit e6f86c5

Browse files
committed
Nullability refinements and related polishing
1 parent cca32a5 commit e6f86c5

File tree

12 files changed

+144
-146
lines changed

12 files changed

+144
-146
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ protected ResolvableType getTypeForFactoryBean(String beanName,
888888
}
889889
// No type found for shortcut FactoryBean instance:
890890
// fall back to full creation of the FactoryBean instance.
891-
return super.getTypeForFactoryBean(beanName, mbd, allowInit);
891+
return super.getTypeForFactoryBean(beanName, mbd, true);
892892
}
893893
}
894894

@@ -1990,6 +1990,7 @@ public String getDependencyName() {
19901990
}
19911991
}
19921992

1993+
19931994
/**
19941995
* {@link MethodCallback} used to find {@link FactoryBean} type information.
19951996
*/
@@ -1999,12 +2000,10 @@ private static class FactoryBeanMethodTypeFinder implements MethodCallback {
19992000

20002001
private ResolvableType result = ResolvableType.NONE;
20012002

2002-
20032003
FactoryBeanMethodTypeFinder(String factoryMethodName) {
20042004
this.factoryMethodName = factoryMethodName;
20052005
}
20062006

2007-
20082007
@Override
20092008
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
20102009
if (isFactoryBeanMethod(method)) {
@@ -2028,13 +2027,11 @@ private boolean isFactoryBeanMethod(Method method) {
20282027
FactoryBean.class.isAssignableFrom(method.getReturnType());
20292028
}
20302029

2031-
20322030
ResolvableType getResult() {
20332031
Class<?> resolved = this.result.resolve();
20342032
boolean foundResult = resolved != null && resolved != Object.class;
20352033
return (foundResult ? this.result : ResolvableType.NONE);
20362034
}
2037-
20382035
}
20392036

20402037
}

spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ else if (containsSingleton(beanName) && !containsBeanDefinition(beanName)) {
612612
if (FactoryBean.class.isAssignableFrom(predictedType)) {
613613
if (beanInstance == null && !isFactoryDereference) {
614614
beanType = getTypeForFactoryBean(beanName, mbd, allowFactoryBeanInit);
615-
predictedType = (beanType != null) ? beanType.resolve() : null;
615+
predictedType = beanType.resolve();
616616
if (predictedType == null) {
617617
return false;
618618
}
@@ -1377,17 +1377,19 @@ protected RootBeanDefinition getMergedBeanDefinition(
13771377
}
13781378
}
13791379

1380-
private void copyRelevantMergedBeanDefinitionCaches(RootBeanDefinition previous,
1381-
RootBeanDefinition mbd) {
1380+
private void copyRelevantMergedBeanDefinitionCaches(RootBeanDefinition previous, RootBeanDefinition mbd) {
13821381
if (ObjectUtils.nullSafeEquals(mbd.getBeanClassName(), previous.getBeanClassName()) &&
13831382
ObjectUtils.nullSafeEquals(mbd.getFactoryBeanName(), previous.getFactoryBeanName()) &&
1384-
ObjectUtils.nullSafeEquals(mbd.getFactoryMethodName(), previous.getFactoryMethodName()) &&
1385-
(mbd.targetType == null || mbd.targetType.equals(previous.targetType))) {
1386-
mbd.targetType = previous.targetType;
1387-
mbd.isFactoryBean = previous.isFactoryBean;
1388-
mbd.resolvedTargetType = previous.resolvedTargetType;
1389-
mbd.factoryMethodReturnType = previous.factoryMethodReturnType;
1390-
mbd.factoryMethodToIntrospect = previous.factoryMethodToIntrospect;
1383+
ObjectUtils.nullSafeEquals(mbd.getFactoryMethodName(), previous.getFactoryMethodName())) {
1384+
ResolvableType targetType = mbd.targetType;
1385+
ResolvableType previousTargetType = previous.targetType;
1386+
if (targetType == null || targetType.equals(previousTargetType)) {
1387+
mbd.targetType = previousTargetType;
1388+
mbd.isFactoryBean = previous.isFactoryBean;
1389+
mbd.resolvedTargetType = previous.resolvedTargetType;
1390+
mbd.factoryMethodReturnType = previous.factoryMethodReturnType;
1391+
mbd.factoryMethodToIntrospect = previous.factoryMethodToIntrospect;
1392+
}
13911393
}
13921394
}
13931395

@@ -1625,9 +1627,7 @@ protected boolean isFactoryBean(String beanName, RootBeanDefinition mbd) {
16251627
* @see org.springframework.beans.factory.FactoryBean#getObjectType()
16261628
* @see #getBean(String)
16271629
*/
1628-
protected ResolvableType getTypeForFactoryBean(String beanName,
1629-
RootBeanDefinition mbd, boolean allowInit) {
1630-
1630+
protected ResolvableType getTypeForFactoryBean(String beanName, RootBeanDefinition mbd, boolean allowInit) {
16311631
ResolvableType result = getTypeForFactoryBeanFromAttributes(mbd);
16321632
if (result != ResolvableType.NONE) {
16331633
return result;

spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,8 +583,8 @@ private String[] doGetBeanNamesForType(ResolvableType type, boolean includeNonSi
583583
return StringUtils.toStringArray(result);
584584
}
585585

586-
private boolean isSingleton(String beanName, RootBeanDefinition mbd, BeanDefinitionHolder dbd) {
587-
return (dbd != null) ? mbd.isSingleton() : isSingleton(beanName);
586+
private boolean isSingleton(String beanName, RootBeanDefinition mbd, @Nullable BeanDefinitionHolder dbd) {
587+
return (dbd != null ? mbd.isSingleton() : isSingleton(beanName));
588588
}
589589

590590
/**

spring-core/src/main/java/org/springframework/util/StringUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ public static TimeZone parseTimeZoneString(String timeZoneString) {
900900
* @return the resulting {@code String} array
901901
*/
902902
public static String[] toStringArray(@Nullable Collection<String> collection) {
903-
return (collection != null || collection.isEmpty() ? collection.toArray(EMPTY_STRING_ARRAY) : EMPTY_STRING_ARRAY);
903+
return (!CollectionUtils.isEmpty(collection) ? collection.toArray(EMPTY_STRING_ARRAY) : EMPTY_STRING_ARRAY);
904904
}
905905

906906
/**

spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/reactive/MessageMappingMessageHandler.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,20 +142,31 @@ public Validator getValidator() {
142142
* efficiency consider using the {@code PathPatternRouteMatcher} from
143143
* {@code spring-web} instead.
144144
*/
145-
public void setRouteMatcher(RouteMatcher routeMatcher) {
146-
Assert.notNull(routeMatcher, "RouteMatcher must not be null");
145+
public void setRouteMatcher(@Nullable RouteMatcher routeMatcher) {
147146
this.routeMatcher = routeMatcher;
148147
}
149148

150149
/**
151150
* Return the {@code RouteMatcher} used to map messages to handlers.
152-
* May be {@code null} before component is initialized.
151+
* May be {@code null} before the component is initialized.
153152
*/
154153
@Nullable
155154
public RouteMatcher getRouteMatcher() {
156155
return this.routeMatcher;
157156
}
158157

158+
/**
159+
* Obtain the {@code RouteMatcher} for actual use.
160+
* @return the RouteMatcher (never {@code null})
161+
* @throws IllegalStateException in case of no RouteMatcher set
162+
* @since 5.0
163+
*/
164+
protected RouteMatcher obtainRouteMatcher() {
165+
RouteMatcher routeMatcher = getRouteMatcher();
166+
Assert.state(routeMatcher != null, "No RouteMatcher set");
167+
return routeMatcher;
168+
}
169+
159170
/**
160171
* Configure a {@link ConversionService} to use for type conversion of
161172
* String based values, e.g. in destination variables or headers.
@@ -245,13 +256,13 @@ protected CompositeMessageCondition getMappingForMethod(Method method, Class<?>
245256
*/
246257
@Nullable
247258
protected CompositeMessageCondition getCondition(AnnotatedElement element) {
248-
MessageMapping annot = AnnotatedElementUtils.findMergedAnnotation(element, MessageMapping.class);
249-
if (annot == null || annot.value().length == 0) {
259+
MessageMapping ann = AnnotatedElementUtils.findMergedAnnotation(element, MessageMapping.class);
260+
if (ann == null || ann.value().length == 0) {
250261
return null;
251262
}
252-
String[] patterns = processDestinations(annot.value());
263+
String[] patterns = processDestinations(ann.value());
253264
return new CompositeMessageCondition(
254-
new DestinationPatternsMessageCondition(patterns, this.routeMatcher));
265+
new DestinationPatternsMessageCondition(patterns, obtainRouteMatcher()));
255266
}
256267

257268
/**
@@ -272,7 +283,7 @@ protected String[] processDestinations(String[] destinations) {
272283
protected Set<String> getDirectLookupMappings(CompositeMessageCondition mapping) {
273284
Set<String> result = new LinkedHashSet<>();
274285
for (String pattern : mapping.getCondition(DestinationPatternsMessageCondition.class).getPatterns()) {
275-
if (!this.routeMatcher.isPattern(pattern)) {
286+
if (!obtainRouteMatcher().isPattern(pattern)) {
276287
result.add(pattern);
277288
}
278289
}
@@ -309,7 +320,7 @@ protected Mono<Void> handleMatch(
309320
String pattern = patterns.iterator().next();
310321
RouteMatcher.Route destination = getDestination(message);
311322
Assert.state(destination != null, "Missing destination header");
312-
Map<String, String> vars = getRouteMatcher().matchAndExtract(pattern, destination);
323+
Map<String, String> vars = obtainRouteMatcher().matchAndExtract(pattern, destination);
313324
if (!CollectionUtils.isEmpty(vars)) {
314325
MessageHeaderAccessor mha = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class);
315326
Assert.state(mha != null && mha.isMutable(), "Mutable MessageHeaderAccessor required");

spring-messaging/src/main/java/org/springframework/messaging/rsocket/DefaultRSocketStrategies.java

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,23 @@ final class DefaultRSocketStrategies implements RSocketStrategies {
5656

5757
private final RouteMatcher routeMatcher;
5858

59-
private final MetadataExtractor metadataExtractor;
59+
private final ReactiveAdapterRegistry adapterRegistry;
6060

6161
private final DataBufferFactory bufferFactory;
6262

63-
private final ReactiveAdapterRegistry adapterRegistry;
63+
private final MetadataExtractor metadataExtractor;
6464

6565

6666
private DefaultRSocketStrategies(List<Encoder<?>> encoders, List<Decoder<?>> decoders,
67-
RouteMatcher routeMatcher, MetadataExtractor metadataExtractor,
68-
DataBufferFactory bufferFactory, ReactiveAdapterRegistry adapterRegistry) {
67+
RouteMatcher routeMatcher, ReactiveAdapterRegistry adapterRegistry,
68+
DataBufferFactory bufferFactory, MetadataExtractor metadataExtractor) {
6969

7070
this.encoders = Collections.unmodifiableList(encoders);
7171
this.decoders = Collections.unmodifiableList(decoders);
7272
this.routeMatcher = routeMatcher;
73-
this.metadataExtractor = metadataExtractor;
74-
this.bufferFactory = bufferFactory;
7573
this.adapterRegistry = adapterRegistry;
74+
this.bufferFactory = bufferFactory;
75+
this.metadataExtractor = metadataExtractor;
7676
}
7777

7878

@@ -92,8 +92,8 @@ public RouteMatcher routeMatcher() {
9292
}
9393

9494
@Override
95-
public MetadataExtractor metadataExtractor() {
96-
return this.metadataExtractor;
95+
public ReactiveAdapterRegistry reactiveAdapterRegistry() {
96+
return this.adapterRegistry;
9797
}
9898

9999
@Override
@@ -102,8 +102,8 @@ public DataBufferFactory dataBufferFactory() {
102102
}
103103

104104
@Override
105-
public ReactiveAdapterRegistry reactiveAdapterRegistry() {
106-
return this.adapterRegistry;
105+
public MetadataExtractor metadataExtractor() {
106+
return this.metadataExtractor;
107107
}
108108

109109

@@ -119,39 +119,36 @@ static class DefaultRSocketStrategiesBuilder implements RSocketStrategies.Builde
119119
@Nullable
120120
private RouteMatcher routeMatcher;
121121

122-
@Nullable
123-
private MetadataExtractor metadataExtractor;
124-
125122
@Nullable
126123
private ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
127124

128125
@Nullable
129126
private DataBufferFactory bufferFactory;
130127

128+
@Nullable
129+
private MetadataExtractor metadataExtractor;
131130

132131
DefaultRSocketStrategiesBuilder() {
132+
this.encoders.add(CharSequenceEncoder.allMimeTypes());
133+
this.encoders.add(new ByteBufferEncoder());
134+
this.encoders.add(new ByteArrayEncoder());
135+
this.encoders.add(new DataBufferEncoder());
133136

134137
// Order of decoders may be significant for default data MimeType
135138
// selection in RSocketRequester.Builder
136-
137139
this.decoders.add(StringDecoder.allMimeTypes());
138140
this.decoders.add(new ByteBufferDecoder());
139141
this.decoders.add(new ByteArrayDecoder());
140142
this.decoders.add(new DataBufferDecoder());
141-
142-
this.encoders.add(CharSequenceEncoder.allMimeTypes());
143-
this.encoders.add(new ByteBufferEncoder());
144-
this.encoders.add(new ByteArrayEncoder());
145-
this.encoders.add(new DataBufferEncoder());
146143
}
147144

148145
DefaultRSocketStrategiesBuilder(RSocketStrategies other) {
149146
this.encoders.addAll(other.encoders());
150147
this.decoders.addAll(other.decoders());
151148
this.routeMatcher = other.routeMatcher();
152-
this.metadataExtractor = other.metadataExtractor();
153149
this.adapterRegistry = other.reactiveAdapterRegistry();
154150
this.bufferFactory = other.dataBufferFactory();
151+
this.metadataExtractor = other.metadataExtractor();
155152
}
156153

157154

@@ -185,12 +182,6 @@ public Builder routeMatcher(@Nullable RouteMatcher routeMatcher) {
185182
return this;
186183
}
187184

188-
@Override
189-
public Builder metadataExtractor(@Nullable MetadataExtractor metadataExtractor) {
190-
this.metadataExtractor = metadataExtractor;
191-
return this;
192-
}
193-
194185
@Override
195186
public Builder reactiveAdapterStrategy(@Nullable ReactiveAdapterRegistry registry) {
196187
this.adapterRegistry = registry;
@@ -204,21 +195,26 @@ public Builder dataBufferFactory(@Nullable DataBufferFactory bufferFactory) {
204195
}
205196

206197
@Override
207-
public RSocketStrategies build() {
198+
public Builder metadataExtractor(@Nullable MetadataExtractor metadataExtractor) {
199+
this.metadataExtractor = metadataExtractor;
200+
return this;
201+
}
208202

209-
RouteMatcher matcher = this.routeMatcher != null ? this.routeMatcher : initRouteMatcher();
203+
@Override
204+
public RSocketStrategies build() {
205+
RouteMatcher matcher = (this.routeMatcher != null ? this.routeMatcher : initRouteMatcher());
210206

211-
MetadataExtractor extractor = this.metadataExtractor != null ?
212-
this.metadataExtractor : new DefaultMetadataExtractor(this.decoders);
207+
ReactiveAdapterRegistry registry = (this.adapterRegistry != null ?
208+
this.adapterRegistry : ReactiveAdapterRegistry.getSharedInstance());
213209

214-
DataBufferFactory factory = this.bufferFactory != null ?
215-
this.bufferFactory : new NettyDataBufferFactory(PooledByteBufAllocator.DEFAULT);
210+
DataBufferFactory factory = (this.bufferFactory != null ?
211+
this.bufferFactory : new NettyDataBufferFactory(PooledByteBufAllocator.DEFAULT));
216212

217-
ReactiveAdapterRegistry registry = this.adapterRegistry != null ?
218-
this.adapterRegistry : ReactiveAdapterRegistry.getSharedInstance();
213+
MetadataExtractor extractor = (this.metadataExtractor != null ?
214+
this.metadataExtractor : new DefaultMetadataExtractor(this.decoders));
219215

220216
return new DefaultRSocketStrategies(
221-
this.encoders, this.decoders, matcher, extractor, factory, registry);
217+
this.encoders, this.decoders, matcher, registry, factory, extractor);
222218
}
223219

224220
private RouteMatcher initRouteMatcher() {

spring-messaging/src/main/java/org/springframework/messaging/rsocket/RSocketStrategies.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,10 @@ default <T> Decoder<T> decoder(ResolvableType elementType, @Nullable MimeType mi
9898
RouteMatcher routeMatcher();
9999

100100
/**
101-
* Return the configured {@link Builder#metadataExtractor(MetadataExtractor)}.
101+
* Return the configured
102+
* {@link Builder#reactiveAdapterStrategy(ReactiveAdapterRegistry) reactiveAdapterRegistry}.
102103
*/
103-
MetadataExtractor metadataExtractor();
104+
ReactiveAdapterRegistry reactiveAdapterRegistry();
104105

105106
/**
106107
* Return the configured
@@ -109,10 +110,9 @@ default <T> Decoder<T> decoder(ResolvableType elementType, @Nullable MimeType mi
109110
DataBufferFactory dataBufferFactory();
110111

111112
/**
112-
* Return the configured
113-
* {@link Builder#reactiveAdapterStrategy(ReactiveAdapterRegistry) reactiveAdapterRegistry}.
113+
* Return the configured {@link Builder#metadataExtractor(MetadataExtractor)}.
114114
*/
115-
ReactiveAdapterRegistry reactiveAdapterRegistry();
115+
MetadataExtractor metadataExtractor();
116116

117117
/**
118118
* Return a builder to create a new {@link RSocketStrategies} instance
@@ -184,16 +184,6 @@ interface Builder {
184184
*/
185185
Builder routeMatcher(@Nullable RouteMatcher routeMatcher);
186186

187-
/**
188-
* Configure a {@link MetadataExtractor} to extract the route along with
189-
* other metadata. This option is applicable to client or server
190-
* responders.
191-
* <p>By default this is {@link DefaultMetadataExtractor} created with
192-
* the {@link #decoder(Decoder[]) configured} decoders and extracting a
193-
* route from {@code "message/x.rsocket.routing.v0"} metadata.
194-
*/
195-
Builder metadataExtractor(@Nullable MetadataExtractor metadataExtractor);
196-
197187
/**
198188
* Configure the registry for reactive type support. This can be used to
199189
* to adapt to, and/or determine the semantics of a given
@@ -219,6 +209,16 @@ interface Builder {
219209
*/
220210
Builder dataBufferFactory(@Nullable DataBufferFactory bufferFactory);
221211

212+
/**
213+
* Configure a {@link MetadataExtractor} to extract the route along with
214+
* other metadata. This option is applicable to client or server
215+
* responders.
216+
* <p>By default this is {@link DefaultMetadataExtractor} created with
217+
* the {@link #decoder(Decoder[]) configured} decoders and extracting a
218+
* route from {@code "message/x.rsocket.routing.v0"} metadata.
219+
*/
220+
Builder metadataExtractor(@Nullable MetadataExtractor metadataExtractor);
221+
222222
/**
223223
* Build the {@code RSocketStrategies} instance.
224224
*/

0 commit comments

Comments
 (0)