Skip to content

Commit

Permalink
ClassUtils clean & deprecation in JmsOutGateway
Browse files Browse the repository at this point in the history
* Remove previously deprecated methods with typos from the `ClassUtils`
* Optimize `ClassUtils` logic via `KotlinDetector.isKotlinPresent()` condition
* Resolve deprecation warnings in the `JmsOutboundGateway` around `TaskScheduler`
  • Loading branch information
artembilan committed Jul 9, 2022
1 parent f12248f commit 733eb40
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.function.Function;
import java.util.function.Supplier;

import org.springframework.core.KotlinDetector;
import org.springframework.lang.Nullable;
import org.springframework.util.ReflectionUtils;

Expand Down Expand Up @@ -98,12 +99,13 @@ public abstract class ClassUtils {
PRIMITIVE_WRAPPER_TYPE_MAP.put(Long.class, long.class);
PRIMITIVE_WRAPPER_TYPE_MAP.put(Short.class, short.class);

ClassLoader defaultClassLoader = org.springframework.util.ClassUtils.getDefaultClassLoader();

Class<?> genericSelectorClass = null;
try {
genericSelectorClass =
org.springframework.util.ClassUtils.forName(
"org.springframework.integration.core.GenericSelector",
org.springframework.util.ClassUtils.getDefaultClassLoader());
"org.springframework.integration.core.GenericSelector", defaultClassLoader);
}
catch (ClassNotFoundException e) {
ReflectionUtils.rethrowRuntimeException(e);
Expand All @@ -115,8 +117,7 @@ public abstract class ClassUtils {
try {
genericTransformerClass =
org.springframework.util.ClassUtils.forName(
"org.springframework.integration.transformer.GenericTransformer",
org.springframework.util.ClassUtils.getDefaultClassLoader());
"org.springframework.integration.transformer.GenericTransformer", defaultClassLoader);
}
catch (ClassNotFoundException e) {
ReflectionUtils.rethrowRuntimeException(e);
Expand All @@ -129,54 +130,59 @@ public abstract class ClassUtils {
try {
genericHandlerClass =
org.springframework.util.ClassUtils.forName(
"org.springframework.integration.handler.GenericHandler",
org.springframework.util.ClassUtils.getDefaultClassLoader());
"org.springframework.integration.handler.GenericHandler", defaultClassLoader);
}
catch (ClassNotFoundException e) {
ReflectionUtils.rethrowRuntimeException(e);
}

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

Class<?> kotlinClass = null;
Method kotlinMethod = null;
try {
kotlinClass = org.springframework.util.ClassUtils.forName("kotlin.jvm.functions.Function0",
org.springframework.util.ClassUtils.getDefaultClassLoader());
if (KotlinDetector.isKotlinPresent()) {
Class<?> kotlinClass = null;
Method kotlinMethod = null;
try {
kotlinClass = org.springframework.util.ClassUtils.forName("kotlin.jvm.functions.Function0",
defaultClassLoader);

kotlinMethod = ReflectionUtils.findMethod(kotlinClass, "invoke", (Class<?>[]) null);
}
catch (ClassNotFoundException e) {
//Ignore: assume no Kotlin in classpath
}
finally {
KOTLIN_FUNCTION_0_CLASS = kotlinClass;
KOTLIN_FUNCTION_0_INVOKE_METHOD = kotlinMethod;
}
kotlinMethod = ReflectionUtils.findMethod(kotlinClass, "invoke", (Class<?>[]) null);
}
catch (ClassNotFoundException e) {
//Ignore: assume no Kotlin in classpath
}
finally {
KOTLIN_FUNCTION_0_CLASS = kotlinClass;
KOTLIN_FUNCTION_0_INVOKE_METHOD = kotlinMethod;
}

kotlinClass = null;
kotlinMethod = null;
try {
kotlinClass = org.springframework.util.ClassUtils.forName("kotlin.jvm.functions.Function1",
org.springframework.util.ClassUtils.getDefaultClassLoader());
}
catch (ClassNotFoundException e) {
//Ignore: assume no Kotlin in classpath
}
finally {
KOTLIN_FUNCTION_1_CLASS = kotlinClass;
}
kotlinClass = null;
try {
kotlinClass = org.springframework.util.ClassUtils.forName("kotlin.jvm.functions.Function1",
defaultClassLoader);
}
catch (ClassNotFoundException e) {
//Ignore: assume no Kotlin in classpath
}
finally {
KOTLIN_FUNCTION_1_CLASS = kotlinClass;
}

kotlinClass = null;
try {
kotlinClass = org.springframework.util.ClassUtils.forName("kotlin.Unit",
org.springframework.util.ClassUtils.getDefaultClassLoader());
}
catch (ClassNotFoundException e) {
//Ignore: assume no Kotlin in classpath
kotlinClass = null;
try {
kotlinClass = org.springframework.util.ClassUtils.forName("kotlin.Unit", defaultClassLoader);
}
catch (ClassNotFoundException e) {
//Ignore: assume no Kotlin in classpath
}
finally {
KOTLIN_UNIT_CLASS = kotlinClass;
}
}
finally {
KOTLIN_UNIT_CLASS = kotlinClass;
else {
KOTLIN_FUNCTION_0_CLASS = null;
KOTLIN_FUNCTION_0_INVOKE_METHOD = null;
KOTLIN_FUNCTION_1_CLASS = null;
KOTLIN_UNIT_CLASS = null;
}
}

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

/**
* Check if class is {@code kotlin.jvm.functions.Function0}.
* @param aClass the {@link Class} to check.
* @return true if class is a {@code kotlin.jvm.functions.Function0} implementation.
* @since 5.2
* @deprecated since 5.5.14 in favor of {@link #isKotlinFunction0(Class)}
*/
@Deprecated
public static boolean isKotlinFaction0(Class<?> aClass) {
return KOTLIN_FUNCTION_0_CLASS != null && KOTLIN_FUNCTION_0_CLASS.isAssignableFrom(aClass);
}

/**
* Check if class is {@code kotlin.jvm.functions.Function0}.
* @param aClass the {@link Class} to check.
Expand All @@ -267,18 +261,6 @@ public static boolean isKotlinFunction0(Class<?> aClass) {
return KOTLIN_FUNCTION_0_CLASS != null && KOTLIN_FUNCTION_0_CLASS.isAssignableFrom(aClass);
}

/**
* Check if class is {@code kotlin.jvm.functions.Function1}.
* @param aClass the {@link Class} to check.
* @return true if class is a {@code kotlin.jvm.functions.Function1} implementation.
* @since 5.2
* @deprecated since 5.5.14 in favor of {@link #isKotlinFunction1(Class)}
*/
@Deprecated
public static boolean isKotlinFaction1(Class<?> aClass) {
return KOTLIN_FUNCTION_1_CLASS != null && KOTLIN_FUNCTION_1_CLASS.isAssignableFrom(aClass);
}

/**
* Check if class is {@code kotlin.jvm.functions.Function1}.
* @param aClass the {@link Class} to check.
Expand All @@ -289,7 +271,6 @@ public static boolean isKotlinFunction1(Class<?> aClass) {
return KOTLIN_FUNCTION_1_CLASS != null && KOTLIN_FUNCTION_1_CLASS.isAssignableFrom(aClass);
}


/**
* Check if class is {@code kotlin.Unit}.
* @param aClass the {@link Class} to check.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -16,7 +16,8 @@

package org.springframework.integration.jms;

import java.util.Date;
import java.time.Duration;
import java.time.Instant;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -683,7 +684,7 @@ public void start() {
}
if (!isAsync() && this.receiveTimeout >= 0) {
Assert.state(taskScheduler != null, "'taskScheduler' is required.");
this.reaper = taskScheduler.schedule(new LateReplyReaper(), new Date());
this.reaper = taskScheduler.schedule(new LateReplyReaper(), Instant.now());
}
}
this.active = true;
Expand Down Expand Up @@ -732,8 +733,10 @@ protected Object handleRequestMessage(final Message<?> requestMessage) {
if (!this.replyContainer.isRunning()) {
logger.debug(() -> getComponentName() + ": Starting reply container.");
this.replyContainer.start();
this.idleTask = getTaskScheduler().scheduleAtFixedRate(new IdleContainerStopper(),
this.idleReplyContainerTimeout / 2);
this.idleTask =
getTaskScheduler()
.scheduleAtFixedRate(new IdleContainerStopper(),
Duration.ofMillis(this.idleReplyContainerTimeout / 2));
}
}
}
Expand Down Expand Up @@ -1170,8 +1173,7 @@ private SettableListenableFuture<AbstractIntegrationMessageBuilder<?>> createFut
SettableListenableFuture<AbstractIntegrationMessageBuilder<?>> future = new SettableListenableFuture<>();
this.futures.put(correlationId, future);
if (this.receiveTimeout > 0) {
getTaskScheduler().schedule(() -> expire(correlationId),
new Date(System.currentTimeMillis() + this.receiveTimeout));
getTaskScheduler().schedule(() -> expire(correlationId), Instant.now().plusMillis(this.receiveTimeout));
}
return future;
}
Expand Down Expand Up @@ -1448,8 +1450,10 @@ public void run() {
}
// reschedule myself
if (JmsOutboundGateway.this.receiveTimeout >= 0) {
JmsOutboundGateway.this.reaper = getTaskScheduler().schedule(this,
new Date(now + JmsOutboundGateway.this.receiveTimeout));
JmsOutboundGateway.this.reaper =
getTaskScheduler()
.schedule(this,
Instant.ofEpochMilli(now).plusMillis(JmsOutboundGateway.this.receiveTimeout));
}
}

Expand Down

0 comments on commit 733eb40

Please sign in to comment.