Skip to content

Commit aa776e4

Browse files
committed
Ensure classpath checks can be evaluated at build-time
Closes gh-29352
1 parent 6b6cf1b commit aa776e4

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 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,9 +67,12 @@ public class ProtobufMessageConverter extends AbstractMessageConverter {
6767
*/
6868
public static final MimeType PROTOBUF = new MimeType("application", "x-protobuf", DEFAULT_CHARSET);
6969

70+
private static final boolean protobufJsonFormatPresent =
71+
ClassUtils.isPresent("com.google.protobuf.util.JsonFormat", ProtobufMessageConverter.class.getClassLoader());
7072

7173
private static final Map<Class<?>, Method> methodCache = new ConcurrentReferenceHashMap<>();
7274

75+
7376
final ExtensionRegistry extensionRegistry;
7477

7578
@Nullable
@@ -98,7 +101,7 @@ public ProtobufMessageConverter(ExtensionRegistry extensionRegistry) {
98101
if (formatSupport != null) {
99102
this.protobufFormatSupport = formatSupport;
100103
}
101-
else if (ClassUtils.isPresent("com.google.protobuf.util.JsonFormat", getClass().getClassLoader())) {
104+
else if (protobufJsonFormatPresent) {
102105
this.protobufFormatSupport = new ProtobufJavaUtilSupport(null, null);
103106
}
104107
else {

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

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -105,9 +105,19 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M
105105
*/
106106
public static final String X_PROTOBUF_MESSAGE_HEADER = "X-Protobuf-Message";
107107

108+
private static final boolean protobufFormatFactoryPresent;
109+
110+
private static final boolean protobufJsonFormatPresent;
108111

109112
private static final Map<Class<?>, Method> methodCache = new ConcurrentReferenceHashMap<>();
110113

114+
static {
115+
ClassLoader classLoader = ProtobufHttpMessageConverter.class.getClassLoader();
116+
protobufFormatFactoryPresent = ClassUtils.isPresent("com.googlecode.protobuf.format.FormatFactory", classLoader);
117+
protobufJsonFormatPresent = ClassUtils.isPresent("com.google.protobuf.util.JsonFormat", classLoader);
118+
}
119+
120+
111121
final ExtensionRegistry extensionRegistry;
112122

113123
@Nullable
@@ -136,10 +146,10 @@ public ProtobufHttpMessageConverter(ExtensionRegistry extensionRegistry) {
136146
if (formatSupport != null) {
137147
this.protobufFormatSupport = formatSupport;
138148
}
139-
else if (ClassUtils.isPresent("com.googlecode.protobuf.format.FormatFactory", getClass().getClassLoader())) {
149+
else if (protobufFormatFactoryPresent) {
140150
this.protobufFormatSupport = new ProtobufJavaFormatSupport();
141151
}
142-
else if (ClassUtils.isPresent("com.google.protobuf.util.JsonFormat", getClass().getClassLoader())) {
152+
else if (protobufJsonFormatPresent) {
143153
this.protobufFormatSupport = new ProtobufJavaUtilSupport(null, null);
144154
}
145155
else {

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@
8585
*/
8686
public class WebFluxConfigurationSupport implements ApplicationContextAware {
8787

88+
private static final boolean jakartaValidatorPresent =
89+
ClassUtils.isPresent("jakarta.validation.Validator", WebFluxConfigurationSupport.class.getClassLoader());
90+
91+
8892
@Nullable
8993
private Map<String, CorsConfiguration> corsConfigurations;
9094

@@ -384,7 +388,7 @@ public ReactiveAdapterRegistry webFluxAdapterRegistry() {
384388
public Validator webFluxValidator() {
385389
Validator validator = getValidator();
386390
if (validator == null) {
387-
if (ClassUtils.isPresent("jakarta.validation.Validator", getClass().getClassLoader())) {
391+
if (jakartaValidatorPresent) {
388392
try {
389393
validator = new OptionalValidatorFactoryBean();
390394
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -28,7 +28,7 @@
2828
*/
2929
public class WebSocketNamespaceHandler extends NamespaceHandlerSupport {
3030

31-
private static boolean isSpringMessagingPresent = ClassUtils.isPresent(
31+
private static final boolean isSpringMessagingPresent = ClassUtils.isPresent(
3232
"org.springframework.messaging.Message", WebSocketNamespaceHandler.class.getClassLoader());
3333

3434

0 commit comments

Comments
 (0)