Avoid Class.getPackage() in favor of plain Class.getName() checks

Fixes #22306
This commit is contained in:
Juergen Hoeller
2019-01-25 15:35:49 +01:00
parent 4732f83d70
commit 85ec9b9df2
4 changed files with 12 additions and 20 deletions

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -61,15 +61,14 @@ public class DecoderHttpMessageReader<T> implements HttpMessageReader<T> {
*/ */
public DecoderHttpMessageReader(Decoder<T> decoder) { public DecoderHttpMessageReader(Decoder<T> decoder) {
Assert.notNull(decoder, "Decoder is required"); Assert.notNull(decoder, "Decoder is required");
initLogger(decoder);
this.decoder = decoder; this.decoder = decoder;
this.mediaTypes = MediaType.asMediaTypes(decoder.getDecodableMimeTypes()); this.mediaTypes = MediaType.asMediaTypes(decoder.getDecodableMimeTypes());
initLogger(decoder);
} }
private void initLogger(Decoder<T> decoder) { private static void initLogger(Decoder<?> decoder) {
if (decoder instanceof AbstractDecoder && if (decoder instanceof AbstractDecoder &&
decoder.getClass().getPackage().getName().startsWith("org.springframework.core.codec")) { decoder.getClass().getName().startsWith("org.springframework.core.codec")) {
Log logger = HttpLogging.forLog(((AbstractDecoder) decoder).getLogger()); Log logger = HttpLogging.forLog(((AbstractDecoder) decoder).getLogger());
((AbstractDecoder) decoder).setLogger(logger); ((AbstractDecoder) decoder).setLogger(logger);
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -67,16 +67,15 @@ public class EncoderHttpMessageWriter<T> implements HttpMessageWriter<T> {
*/ */
public EncoderHttpMessageWriter(Encoder<T> encoder) { public EncoderHttpMessageWriter(Encoder<T> encoder) {
Assert.notNull(encoder, "Encoder is required"); Assert.notNull(encoder, "Encoder is required");
initLogger(encoder);
this.encoder = encoder; this.encoder = encoder;
this.mediaTypes = MediaType.asMediaTypes(encoder.getEncodableMimeTypes()); this.mediaTypes = MediaType.asMediaTypes(encoder.getEncodableMimeTypes());
this.defaultMediaType = initDefaultMediaType(this.mediaTypes); this.defaultMediaType = initDefaultMediaType(this.mediaTypes);
initLogger(encoder);
} }
private void initLogger(Encoder<T> encoder) { private static void initLogger(Encoder<?> encoder) {
if (encoder instanceof AbstractEncoder && if (encoder instanceof AbstractEncoder &&
encoder.getClass().getPackage().getName().startsWith("org.springframework.core.codec")) { encoder.getClass().getName().startsWith("org.springframework.core.codec")) {
Log logger = HttpLogging.forLog(((AbstractEncoder) encoder).getLogger()); Log logger = HttpLogging.forLog(((AbstractEncoder) encoder).getLogger());
((AbstractEncoder) encoder).setLogger(logger); ((AbstractEncoder) encoder).setLogger(logger);
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -215,15 +215,12 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
} }
private String formatMappings(Class<?> userType, Map<Method, T> methods) { private String formatMappings(Class<?> userType, Map<Method, T> methods) {
String formattedType = Arrays.stream(ClassUtils.getPackageName(userType).split("\\."))
String formattedType = Arrays.stream(userType.getPackage().getName().split("\\."))
.map(p -> p.substring(0, 1)) .map(p -> p.substring(0, 1))
.collect(Collectors.joining(".", "", ".")) + userType.getSimpleName(); .collect(Collectors.joining(".", "", ".")) + userType.getSimpleName();
Function<Method, String> methodFormatter = method -> Arrays.stream(method.getParameterTypes()) Function<Method, String> methodFormatter = method -> Arrays.stream(method.getParameterTypes())
.map(Class::getSimpleName) .map(Class::getSimpleName)
.collect(Collectors.joining(",", "(", ")")); .collect(Collectors.joining(",", "(", ")"));
return methods.entrySet().stream() return methods.entrySet().stream()
.map(e -> { .map(e -> {
Method method = e.getKey(); Method method = e.getKey();

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -285,15 +285,12 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
} }
private String formatMappings(Class<?> userType, Map<Method, T> methods) { private String formatMappings(Class<?> userType, Map<Method, T> methods) {
String formattedType = Arrays.stream(ClassUtils.getPackageName(userType).split("\\."))
String formattedType = Arrays.stream(userType.getPackage().getName().split("\\."))
.map(p -> p.substring(0, 1)) .map(p -> p.substring(0, 1))
.collect(Collectors.joining(".", "", ".")) + userType.getSimpleName(); .collect(Collectors.joining(".", "", ".")) + userType.getSimpleName();
Function<Method, String> methodFormatter = method -> Arrays.stream(method.getParameterTypes()) Function<Method, String> methodFormatter = method -> Arrays.stream(method.getParameterTypes())
.map(Class::getSimpleName) .map(Class::getSimpleName)
.collect(Collectors.joining(",", "(", ")")); .collect(Collectors.joining(",", "(", ")"));
return methods.entrySet().stream() return methods.entrySet().stream()
.map(e -> { .map(e -> {
Method method = e.getKey(); Method method = e.getKey();