Skip to content

Commit 85ec9b9

Browse files
committed
Avoid Class.getPackage() in favor of plain Class.getName() checks
Fixes spring-projects#22306
1 parent 4732f83 commit 85ec9b9

File tree

4 files changed

+12
-20
lines changed

4 files changed

+12
-20
lines changed

spring-web/src/main/java/org/springframework/http/codec/DecoderHttpMessageReader.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -61,15 +61,14 @@ public class DecoderHttpMessageReader<T> implements HttpMessageReader<T> {
6161
*/
6262
public DecoderHttpMessageReader(Decoder<T> decoder) {
6363
Assert.notNull(decoder, "Decoder is required");
64+
initLogger(decoder);
6465
this.decoder = decoder;
6566
this.mediaTypes = MediaType.asMediaTypes(decoder.getDecodableMimeTypes());
66-
initLogger(decoder);
6767
}
6868

69-
private void initLogger(Decoder<T> decoder) {
69+
private static void initLogger(Decoder<?> decoder) {
7070
if (decoder instanceof AbstractDecoder &&
71-
decoder.getClass().getPackage().getName().startsWith("org.springframework.core.codec")) {
72-
71+
decoder.getClass().getName().startsWith("org.springframework.core.codec")) {
7372
Log logger = HttpLogging.forLog(((AbstractDecoder) decoder).getLogger());
7473
((AbstractDecoder) decoder).setLogger(logger);
7574
}

spring-web/src/main/java/org/springframework/http/codec/EncoderHttpMessageWriter.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -67,16 +67,15 @@ public class EncoderHttpMessageWriter<T> implements HttpMessageWriter<T> {
6767
*/
6868
public EncoderHttpMessageWriter(Encoder<T> encoder) {
6969
Assert.notNull(encoder, "Encoder is required");
70+
initLogger(encoder);
7071
this.encoder = encoder;
7172
this.mediaTypes = MediaType.asMediaTypes(encoder.getEncodableMimeTypes());
7273
this.defaultMediaType = initDefaultMediaType(this.mediaTypes);
73-
initLogger(encoder);
7474
}
7575

76-
private void initLogger(Encoder<T> encoder) {
76+
private static void initLogger(Encoder<?> encoder) {
7777
if (encoder instanceof AbstractEncoder &&
78-
encoder.getClass().getPackage().getName().startsWith("org.springframework.core.codec")) {
79-
78+
encoder.getClass().getName().startsWith("org.springframework.core.codec")) {
8079
Log logger = HttpLogging.forLog(((AbstractEncoder) encoder).getLogger());
8180
((AbstractEncoder) encoder).setLogger(logger);
8281
}

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/AbstractHandlerMethodMapping.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -215,15 +215,12 @@ protected void detectHandlerMethods(final Object handler) {
215215
}
216216

217217
private String formatMappings(Class<?> userType, Map<Method, T> methods) {
218-
219-
String formattedType = Arrays.stream(userType.getPackage().getName().split("\\."))
218+
String formattedType = Arrays.stream(ClassUtils.getPackageName(userType).split("\\."))
220219
.map(p -> p.substring(0, 1))
221220
.collect(Collectors.joining(".", "", ".")) + userType.getSimpleName();
222-
223221
Function<Method, String> methodFormatter = method -> Arrays.stream(method.getParameterTypes())
224222
.map(Class::getSimpleName)
225223
.collect(Collectors.joining(",", "(", ")"));
226-
227224
return methods.entrySet().stream()
228225
.map(e -> {
229226
Method method = e.getKey();

spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMethodMapping.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -285,15 +285,12 @@ protected void detectHandlerMethods(Object handler) {
285285
}
286286

287287
private String formatMappings(Class<?> userType, Map<Method, T> methods) {
288-
289-
String formattedType = Arrays.stream(userType.getPackage().getName().split("\\."))
288+
String formattedType = Arrays.stream(ClassUtils.getPackageName(userType).split("\\."))
290289
.map(p -> p.substring(0, 1))
291290
.collect(Collectors.joining(".", "", ".")) + userType.getSimpleName();
292-
293291
Function<Method, String> methodFormatter = method -> Arrays.stream(method.getParameterTypes())
294292
.map(Class::getSimpleName)
295293
.collect(Collectors.joining(",", "(", ")"));
296-
297294
return methods.entrySet().stream()
298295
.map(e -> {
299296
Method method = e.getKey();

0 commit comments

Comments
 (0)