Skip to content

Commit 3113917

Browse files
committed
Update ReactiveAdapterRegistry to do classpath checks during init
To improve GraalVM native image footprint and compatibility. Closes gh-26295
1 parent 994a35d commit 3113917

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

spring-core/src/main/java/org/springframework/core/ReactiveAdapterRegistry.java

Lines changed: 31 additions & 17 deletions
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-2021 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.
@@ -60,7 +60,28 @@ public class ReactiveAdapterRegistry {
6060
@Nullable
6161
private static volatile ReactiveAdapterRegistry sharedInstance;
6262

63-
private final boolean reactorPresent;
63+
private static final boolean reactorPresent;
64+
65+
private static final boolean rxjava1Present;
66+
67+
private static final boolean rxjava2Present;
68+
69+
private static final boolean rxjava3Present;
70+
71+
private static final boolean flowPublisherPresent;
72+
73+
private static final boolean kotlinCoroutinesPresent;
74+
75+
static {
76+
ClassLoader classLoader = ReactiveAdapterRegistry.class.getClassLoader();
77+
reactorPresent = ClassUtils.isPresent("reactor.core.publisher.Flux", classLoader);
78+
rxjava1Present = ClassUtils.isPresent("rx.Observable", classLoader) &&
79+
ClassUtils.isPresent("rx.RxReactiveStreams", classLoader);
80+
rxjava2Present = ClassUtils.isPresent("io.reactivex.Flowable", classLoader);
81+
rxjava3Present = ClassUtils.isPresent("io.reactivex.rxjava3.core.Flowable", classLoader);
82+
flowPublisherPresent = ClassUtils.isPresent("java.util.concurrent.Flow.Publisher", classLoader);
83+
kotlinCoroutinesPresent = ClassUtils.isPresent("kotlinx.coroutines.reactor.MonoKt", classLoader);
84+
}
6485

6586
private final List<ReactiveAdapter> adapters = new ArrayList<>();
6687

@@ -70,41 +91,34 @@ public class ReactiveAdapterRegistry {
7091
* @see #getSharedInstance()
7192
*/
7293
public ReactiveAdapterRegistry() {
73-
ClassLoader classLoader = ReactiveAdapterRegistry.class.getClassLoader();
74-
7594
// Reactor
76-
boolean reactorRegistered = false;
77-
if (ClassUtils.isPresent("reactor.core.publisher.Flux", classLoader)) {
95+
if (reactorPresent) {
7896
new ReactorRegistrar().registerAdapters(this);
79-
reactorRegistered = true;
8097
}
81-
this.reactorPresent = reactorRegistered;
8298

8399
// RxJava1 (deprecated)
84-
if (ClassUtils.isPresent("rx.Observable", classLoader) &&
85-
ClassUtils.isPresent("rx.RxReactiveStreams", classLoader)) {
100+
if (rxjava1Present) {
86101
new RxJava1Registrar().registerAdapters(this);
87102
}
88103

89104
// RxJava2
90-
if (ClassUtils.isPresent("io.reactivex.Flowable", classLoader)) {
105+
if (rxjava2Present) {
91106
new RxJava2Registrar().registerAdapters(this);
92107
}
93-
94108
// RxJava3
95-
if (ClassUtils.isPresent("io.reactivex.rxjava3.core.Flowable", classLoader)) {
109+
if (rxjava3Present) {
96110
new RxJava3Registrar().registerAdapters(this);
97111
}
98112

99113
// Java 9+ Flow.Publisher
100-
if (ClassUtils.isPresent("java.util.concurrent.Flow.Publisher", classLoader)) {
114+
if (flowPublisherPresent) {
101115
new ReactorJdkFlowAdapterRegistrar().registerAdapter(this);
102116
}
103117
// If not present, do nothing for the time being...
104118
// We can fall back on "reactive-streams-flow-bridge" (once released)
105119

106-
// Coroutines
107-
if (this.reactorPresent && ClassUtils.isPresent("kotlinx.coroutines.reactor.MonoKt", classLoader)) {
120+
// Kotlin Coroutines
121+
if (reactorPresent && kotlinCoroutinesPresent) {
108122
new CoroutinesRegistrar().registerAdapters(this);
109123
}
110124
}
@@ -125,7 +139,7 @@ public boolean hasAdapters() {
125139
public void registerReactiveType(ReactiveTypeDescriptor descriptor,
126140
Function<Object, Publisher<?>> toAdapter, Function<Publisher<?>, Object> fromAdapter) {
127141

128-
if (this.reactorPresent) {
142+
if (reactorPresent) {
129143
this.adapters.add(new ReactorAdapter(descriptor, toAdapter, fromAdapter));
130144
}
131145
else {

0 commit comments

Comments
 (0)