Skip to content

Commit 733eb40

Browse files
committed
ClassUtils clean & deprecation in JmsOutGateway
* Remove previously deprecated methods with typos from the `ClassUtils` * Optimize `ClassUtils` logic via `KotlinDetector.isKotlinPresent()` condition * Resolve deprecation warnings in the `JmsOutboundGateway` around `TaskScheduler`
1 parent f12248f commit 733eb40

File tree

2 files changed

+60
-75
lines changed

2 files changed

+60
-75
lines changed

spring-integration-core/src/main/java/org/springframework/integration/util/ClassUtils.java

Lines changed: 47 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.function.Function;
2424
import java.util.function.Supplier;
2525

26+
import org.springframework.core.KotlinDetector;
2627
import org.springframework.lang.Nullable;
2728
import org.springframework.util.ReflectionUtils;
2829

@@ -98,12 +99,13 @@ public abstract class ClassUtils {
9899
PRIMITIVE_WRAPPER_TYPE_MAP.put(Long.class, long.class);
99100
PRIMITIVE_WRAPPER_TYPE_MAP.put(Short.class, short.class);
100101

102+
ClassLoader defaultClassLoader = org.springframework.util.ClassUtils.getDefaultClassLoader();
103+
101104
Class<?> genericSelectorClass = null;
102105
try {
103106
genericSelectorClass =
104107
org.springframework.util.ClassUtils.forName(
105-
"org.springframework.integration.core.GenericSelector",
106-
org.springframework.util.ClassUtils.getDefaultClassLoader());
108+
"org.springframework.integration.core.GenericSelector", defaultClassLoader);
107109
}
108110
catch (ClassNotFoundException e) {
109111
ReflectionUtils.rethrowRuntimeException(e);
@@ -115,8 +117,7 @@ public abstract class ClassUtils {
115117
try {
116118
genericTransformerClass =
117119
org.springframework.util.ClassUtils.forName(
118-
"org.springframework.integration.transformer.GenericTransformer",
119-
org.springframework.util.ClassUtils.getDefaultClassLoader());
120+
"org.springframework.integration.transformer.GenericTransformer", defaultClassLoader);
120121
}
121122
catch (ClassNotFoundException e) {
122123
ReflectionUtils.rethrowRuntimeException(e);
@@ -129,54 +130,59 @@ public abstract class ClassUtils {
129130
try {
130131
genericHandlerClass =
131132
org.springframework.util.ClassUtils.forName(
132-
"org.springframework.integration.handler.GenericHandler",
133-
org.springframework.util.ClassUtils.getDefaultClassLoader());
133+
"org.springframework.integration.handler.GenericHandler", defaultClassLoader);
134134
}
135135
catch (ClassNotFoundException e) {
136136
ReflectionUtils.rethrowRuntimeException(e);
137137
}
138138

139139
HANDLER_HANDLE_METHOD = ReflectionUtils.findMethod(genericHandlerClass, "handle", (Class<?>[]) null);
140140

141-
Class<?> kotlinClass = null;
142-
Method kotlinMethod = null;
143-
try {
144-
kotlinClass = org.springframework.util.ClassUtils.forName("kotlin.jvm.functions.Function0",
145-
org.springframework.util.ClassUtils.getDefaultClassLoader());
141+
if (KotlinDetector.isKotlinPresent()) {
142+
Class<?> kotlinClass = null;
143+
Method kotlinMethod = null;
144+
try {
145+
kotlinClass = org.springframework.util.ClassUtils.forName("kotlin.jvm.functions.Function0",
146+
defaultClassLoader);
146147

147-
kotlinMethod = ReflectionUtils.findMethod(kotlinClass, "invoke", (Class<?>[]) null);
148-
}
149-
catch (ClassNotFoundException e) {
150-
//Ignore: assume no Kotlin in classpath
151-
}
152-
finally {
153-
KOTLIN_FUNCTION_0_CLASS = kotlinClass;
154-
KOTLIN_FUNCTION_0_INVOKE_METHOD = kotlinMethod;
155-
}
148+
kotlinMethod = ReflectionUtils.findMethod(kotlinClass, "invoke", (Class<?>[]) null);
149+
}
150+
catch (ClassNotFoundException e) {
151+
//Ignore: assume no Kotlin in classpath
152+
}
153+
finally {
154+
KOTLIN_FUNCTION_0_CLASS = kotlinClass;
155+
KOTLIN_FUNCTION_0_INVOKE_METHOD = kotlinMethod;
156+
}
156157

157-
kotlinClass = null;
158-
kotlinMethod = null;
159-
try {
160-
kotlinClass = org.springframework.util.ClassUtils.forName("kotlin.jvm.functions.Function1",
161-
org.springframework.util.ClassUtils.getDefaultClassLoader());
162-
}
163-
catch (ClassNotFoundException e) {
164-
//Ignore: assume no Kotlin in classpath
165-
}
166-
finally {
167-
KOTLIN_FUNCTION_1_CLASS = kotlinClass;
168-
}
158+
kotlinClass = null;
159+
try {
160+
kotlinClass = org.springframework.util.ClassUtils.forName("kotlin.jvm.functions.Function1",
161+
defaultClassLoader);
162+
}
163+
catch (ClassNotFoundException e) {
164+
//Ignore: assume no Kotlin in classpath
165+
}
166+
finally {
167+
KOTLIN_FUNCTION_1_CLASS = kotlinClass;
168+
}
169169

170-
kotlinClass = null;
171-
try {
172-
kotlinClass = org.springframework.util.ClassUtils.forName("kotlin.Unit",
173-
org.springframework.util.ClassUtils.getDefaultClassLoader());
174-
}
175-
catch (ClassNotFoundException e) {
176-
//Ignore: assume no Kotlin in classpath
170+
kotlinClass = null;
171+
try {
172+
kotlinClass = org.springframework.util.ClassUtils.forName("kotlin.Unit", defaultClassLoader);
173+
}
174+
catch (ClassNotFoundException e) {
175+
//Ignore: assume no Kotlin in classpath
176+
}
177+
finally {
178+
KOTLIN_UNIT_CLASS = kotlinClass;
179+
}
177180
}
178-
finally {
179-
KOTLIN_UNIT_CLASS = kotlinClass;
181+
else {
182+
KOTLIN_FUNCTION_0_CLASS = null;
183+
KOTLIN_FUNCTION_0_INVOKE_METHOD = null;
184+
KOTLIN_FUNCTION_1_CLASS = null;
185+
KOTLIN_UNIT_CLASS = null;
180186
}
181187
}
182188

@@ -245,18 +251,6 @@ public static boolean isLambda(Class<?> aClass) {
245251
|| aClass.getName().contains("$inlined$"); // for Kotlin lambdas
246252
}
247253

248-
/**
249-
* Check if class is {@code kotlin.jvm.functions.Function0}.
250-
* @param aClass the {@link Class} to check.
251-
* @return true if class is a {@code kotlin.jvm.functions.Function0} implementation.
252-
* @since 5.2
253-
* @deprecated since 5.5.14 in favor of {@link #isKotlinFunction0(Class)}
254-
*/
255-
@Deprecated
256-
public static boolean isKotlinFaction0(Class<?> aClass) {
257-
return KOTLIN_FUNCTION_0_CLASS != null && KOTLIN_FUNCTION_0_CLASS.isAssignableFrom(aClass);
258-
}
259-
260254
/**
261255
* Check if class is {@code kotlin.jvm.functions.Function0}.
262256
* @param aClass the {@link Class} to check.
@@ -267,18 +261,6 @@ public static boolean isKotlinFunction0(Class<?> aClass) {
267261
return KOTLIN_FUNCTION_0_CLASS != null && KOTLIN_FUNCTION_0_CLASS.isAssignableFrom(aClass);
268262
}
269263

270-
/**
271-
* Check if class is {@code kotlin.jvm.functions.Function1}.
272-
* @param aClass the {@link Class} to check.
273-
* @return true if class is a {@code kotlin.jvm.functions.Function1} implementation.
274-
* @since 5.2
275-
* @deprecated since 5.5.14 in favor of {@link #isKotlinFunction1(Class)}
276-
*/
277-
@Deprecated
278-
public static boolean isKotlinFaction1(Class<?> aClass) {
279-
return KOTLIN_FUNCTION_1_CLASS != null && KOTLIN_FUNCTION_1_CLASS.isAssignableFrom(aClass);
280-
}
281-
282264
/**
283265
* Check if class is {@code kotlin.jvm.functions.Function1}.
284266
* @param aClass the {@link Class} to check.
@@ -289,7 +271,6 @@ public static boolean isKotlinFunction1(Class<?> aClass) {
289271
return KOTLIN_FUNCTION_1_CLASS != null && KOTLIN_FUNCTION_1_CLASS.isAssignableFrom(aClass);
290272
}
291273

292-
293274
/**
294275
* Check if class is {@code kotlin.Unit}.
295276
* @param aClass the {@link Class} to check.

spring-integration-jms/src/main/java/org/springframework/integration/jms/JmsOutboundGateway.java

Lines changed: 13 additions & 9 deletions
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.
@@ -16,7 +16,8 @@
1616

1717
package org.springframework.integration.jms;
1818

19-
import java.util.Date;
19+
import java.time.Duration;
20+
import java.time.Instant;
2021
import java.util.Iterator;
2122
import java.util.Map;
2223
import java.util.Map.Entry;
@@ -683,7 +684,7 @@ public void start() {
683684
}
684685
if (!isAsync() && this.receiveTimeout >= 0) {
685686
Assert.state(taskScheduler != null, "'taskScheduler' is required.");
686-
this.reaper = taskScheduler.schedule(new LateReplyReaper(), new Date());
687+
this.reaper = taskScheduler.schedule(new LateReplyReaper(), Instant.now());
687688
}
688689
}
689690
this.active = true;
@@ -732,8 +733,10 @@ protected Object handleRequestMessage(final Message<?> requestMessage) {
732733
if (!this.replyContainer.isRunning()) {
733734
logger.debug(() -> getComponentName() + ": Starting reply container.");
734735
this.replyContainer.start();
735-
this.idleTask = getTaskScheduler().scheduleAtFixedRate(new IdleContainerStopper(),
736-
this.idleReplyContainerTimeout / 2);
736+
this.idleTask =
737+
getTaskScheduler()
738+
.scheduleAtFixedRate(new IdleContainerStopper(),
739+
Duration.ofMillis(this.idleReplyContainerTimeout / 2));
737740
}
738741
}
739742
}
@@ -1170,8 +1173,7 @@ private SettableListenableFuture<AbstractIntegrationMessageBuilder<?>> createFut
11701173
SettableListenableFuture<AbstractIntegrationMessageBuilder<?>> future = new SettableListenableFuture<>();
11711174
this.futures.put(correlationId, future);
11721175
if (this.receiveTimeout > 0) {
1173-
getTaskScheduler().schedule(() -> expire(correlationId),
1174-
new Date(System.currentTimeMillis() + this.receiveTimeout));
1176+
getTaskScheduler().schedule(() -> expire(correlationId), Instant.now().plusMillis(this.receiveTimeout));
11751177
}
11761178
return future;
11771179
}
@@ -1448,8 +1450,10 @@ public void run() {
14481450
}
14491451
// reschedule myself
14501452
if (JmsOutboundGateway.this.receiveTimeout >= 0) {
1451-
JmsOutboundGateway.this.reaper = getTaskScheduler().schedule(this,
1452-
new Date(now + JmsOutboundGateway.this.receiveTimeout));
1453+
JmsOutboundGateway.this.reaper =
1454+
getTaskScheduler()
1455+
.schedule(this,
1456+
Instant.ofEpochMilli(now).plusMillis(JmsOutboundGateway.this.receiveTimeout));
14531457
}
14541458
}
14551459

0 commit comments

Comments
 (0)