Browse Source

Ensure classpath checks can be evaluated at build-time

Closes gh-29352
pull/29364/head
Sébastien Deleuze 3 years ago
parent
commit
aa776e40f9
  1. 7
      spring-messaging/src/main/java/org/springframework/messaging/converter/ProtobufMessageConverter.java
  2. 16
      spring-web/src/main/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverter.java
  3. 6
      spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurationSupport.java
  4. 4
      spring-websocket/src/main/java/org/springframework/web/socket/config/WebSocketNamespaceHandler.java

7
spring-messaging/src/main/java/org/springframework/messaging/converter/ProtobufMessageConverter.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 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.
@ -67,9 +67,12 @@ public class ProtobufMessageConverter extends AbstractMessageConverter { @@ -67,9 +67,12 @@ public class ProtobufMessageConverter extends AbstractMessageConverter {
*/
public static final MimeType PROTOBUF = new MimeType("application", "x-protobuf", DEFAULT_CHARSET);
private static final boolean protobufJsonFormatPresent =
ClassUtils.isPresent("com.google.protobuf.util.JsonFormat", ProtobufMessageConverter.class.getClassLoader());
private static final Map<Class<?>, Method> methodCache = new ConcurrentReferenceHashMap<>();
final ExtensionRegistry extensionRegistry;
@Nullable
@ -98,7 +101,7 @@ public class ProtobufMessageConverter extends AbstractMessageConverter { @@ -98,7 +101,7 @@ public class ProtobufMessageConverter extends AbstractMessageConverter {
if (formatSupport != null) {
this.protobufFormatSupport = formatSupport;
}
else if (ClassUtils.isPresent("com.google.protobuf.util.JsonFormat", getClass().getClassLoader())) {
else if (protobufJsonFormatPresent) {
this.protobufFormatSupport = new ProtobufJavaUtilSupport(null, null);
}
else {

16
spring-web/src/main/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverter.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 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.
@ -105,9 +105,19 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M @@ -105,9 +105,19 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M
*/
public static final String X_PROTOBUF_MESSAGE_HEADER = "X-Protobuf-Message";
private static final boolean protobufFormatFactoryPresent;
private static final boolean protobufJsonFormatPresent;
private static final Map<Class<?>, Method> methodCache = new ConcurrentReferenceHashMap<>();
static {
ClassLoader classLoader = ProtobufHttpMessageConverter.class.getClassLoader();
protobufFormatFactoryPresent = ClassUtils.isPresent("com.googlecode.protobuf.format.FormatFactory", classLoader);
protobufJsonFormatPresent = ClassUtils.isPresent("com.google.protobuf.util.JsonFormat", classLoader);
}
final ExtensionRegistry extensionRegistry;
@Nullable
@ -136,10 +146,10 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M @@ -136,10 +146,10 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M
if (formatSupport != null) {
this.protobufFormatSupport = formatSupport;
}
else if (ClassUtils.isPresent("com.googlecode.protobuf.format.FormatFactory", getClass().getClassLoader())) {
else if (protobufFormatFactoryPresent) {
this.protobufFormatSupport = new ProtobufJavaFormatSupport();
}
else if (ClassUtils.isPresent("com.google.protobuf.util.JsonFormat", getClass().getClassLoader())) {
else if (protobufJsonFormatPresent) {
this.protobufFormatSupport = new ProtobufJavaUtilSupport(null, null);
}
else {

6
spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurationSupport.java

@ -85,6 +85,10 @@ import org.springframework.web.server.i18n.LocaleContextResolver; @@ -85,6 +85,10 @@ import org.springframework.web.server.i18n.LocaleContextResolver;
*/
public class WebFluxConfigurationSupport implements ApplicationContextAware {
private static final boolean jakartaValidatorPresent =
ClassUtils.isPresent("jakarta.validation.Validator", WebFluxConfigurationSupport.class.getClassLoader());
@Nullable
private Map<String, CorsConfiguration> corsConfigurations;
@ -384,7 +388,7 @@ public class WebFluxConfigurationSupport implements ApplicationContextAware { @@ -384,7 +388,7 @@ public class WebFluxConfigurationSupport implements ApplicationContextAware {
public Validator webFluxValidator() {
Validator validator = getValidator();
if (validator == null) {
if (ClassUtils.isPresent("jakarta.validation.Validator", getClass().getClassLoader())) {
if (jakartaValidatorPresent) {
try {
validator = new OptionalValidatorFactoryBean();
}

4
spring-websocket/src/main/java/org/springframework/web/socket/config/WebSocketNamespaceHandler.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 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.
@ -28,7 +28,7 @@ import org.springframework.util.ClassUtils; @@ -28,7 +28,7 @@ import org.springframework.util.ClassUtils;
*/
public class WebSocketNamespaceHandler extends NamespaceHandlerSupport {
private static boolean isSpringMessagingPresent = ClassUtils.isPresent(
private static final boolean isSpringMessagingPresent = ClassUtils.isPresent(
"org.springframework.messaging.Message", WebSocketNamespaceHandler.class.getClassLoader());

Loading…
Cancel
Save