Skip to content

Commit

Permalink
Remove JDK 9 workarounds etc
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Sep 17, 2021
1 parent e0a4b05 commit b74e938
Show file tree
Hide file tree
Showing 15 changed files with 36 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;

/**
* Utility methods for AOP proxy factories.
Expand All @@ -48,11 +47,6 @@
*/
public abstract class AopProxyUtils {

// JDK 17 Class.isSealed() method available?
@Nullable
private static final Method isSealedMethod = ClassUtils.getMethodIfAvailable(Class.class, "isSealed");


/**
* Obtain the singleton target object behind the given proxy, if any.
* @param candidate the (potential) proxy to check
Expand Down Expand Up @@ -142,7 +136,7 @@ else if (Proxy.isProxyClass(targetClass)) {
List<Class<?>> proxiedInterfaces = new ArrayList<>(specifiedInterfaces.length + 3);
for (Class<?> ifc : specifiedInterfaces) {
// Only non-sealed interfaces are actually eligible for JDK proxying (on JDK 17)
if (isSealedMethod == null || Boolean.FALSE.equals(ReflectionUtils.invokeMethod(isSealedMethod, ifc))) {
if (!ifc.isSealed()) {
proxiedInterfaces.add(ifc);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public static Object newInstance(Class type, Class[] parameterTypes, Object[] ar
return newInstance(getConstructor(type, parameterTypes), args);
}

@SuppressWarnings("deprecation") // on JDK 9
@SuppressWarnings("deprecation")
public static Object newInstance(final Constructor cstruct, final Object[] args) {
boolean flag = cstruct.isAccessible();
try {
Expand Down Expand Up @@ -439,7 +439,7 @@ public static Class defineClass(String className, byte[] b, ClassLoader loader,
return defineClass(className, b, loader, protectionDomain, null);
}

@SuppressWarnings("deprecation") // on JDK 9
@SuppressWarnings("deprecation")
public static Class defineClass(String className, byte[] b, ClassLoader loader,
ProtectionDomain protectionDomain, Class<?> contextClass) throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
*
* <p>As this repackaging happens at the class file level, sources
* and javadocs are not available here... except for a few files
* that have been patched for Spring's purposes on JDK 9/10/11.
* that have been patched for Spring's purposes on JDK 9-17.
*/
package org.springframework.cglib.core;
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
*
* <p>As this repackaging happens at the class file level, sources
* and javadocs are not available here... except for a few files
* that have been patched for Spring's purposes on JDK 9/10/11.
* that have been patched for Spring's purposes on JDK 9-17.
*/
package org.springframework.cglib.proxy;
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@

package org.springframework.core;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Flow;
import java.util.function.Function;

import kotlinx.coroutines.CompletableDeferredKt;
import kotlinx.coroutines.Deferred;
import org.reactivestreams.Publisher;
import reactor.adapter.JdkFlowAdapter;
import reactor.blockhound.BlockHound;
import reactor.blockhound.integration.BlockHoundIntegration;
import reactor.core.publisher.Flux;
Expand All @@ -36,7 +37,6 @@
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
import org.springframework.util.ConcurrentReferenceHashMap;
import org.springframework.util.ReflectionUtils;

/**
* A registry of adapters to adapt Reactive Streams {@link Publisher} to/from
Expand Down Expand Up @@ -350,28 +350,12 @@ void registerAdapters(ReactiveAdapterRegistry registry) {
private static class ReactorJdkFlowAdapterRegistrar {

void registerAdapter(ReactiveAdapterRegistry registry) {
// TODO: remove reflection when build requires JDK 9+

try {
String publisherName = "java.util.concurrent.Flow.Publisher";
Class<?> publisherClass = ClassUtils.forName(publisherName, getClass().getClassLoader());

String adapterName = "reactor.adapter.JdkFlowAdapter";
Class<?> flowAdapterClass = ClassUtils.forName(adapterName, getClass().getClassLoader());

Method toFluxMethod = flowAdapterClass.getMethod("flowPublisherToFlux", publisherClass);
Method toFlowMethod = flowAdapterClass.getMethod("publisherToFlowPublisher", Publisher.class);
Object emptyFlow = ReflectionUtils.invokeMethod(toFlowMethod, null, Flux.empty());

registry.registerReactiveType(
ReactiveTypeDescriptor.multiValue(publisherClass, () -> emptyFlow),
source -> (Publisher<?>) ReflectionUtils.invokeMethod(toFluxMethod, null, source),
publisher -> ReflectionUtils.invokeMethod(toFlowMethod, null, publisher)
);
}
catch (Throwable ex) {
// Ignore
}
Flow.Publisher<?> emptyFlow = JdkFlowAdapter.publisherToFlowPublisher(Flux.empty());
registry.registerReactiveType(
ReactiveTypeDescriptor.multiValue(Flow.Publisher.class, () -> emptyFlow),
source -> JdkFlowAdapter.flowPublisherToFlux((Flow.Publisher<?>) source),
JdkFlowAdapter::publisherToFlowPublisher
);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2021 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,6 @@

package org.springframework.core.convert.support;

import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -122,10 +121,7 @@ private Object convertToByteBuffer(@Nullable Object source, TypeDescriptor sourc
ByteBuffer byteBuffer = ByteBuffer.allocate(bytes.length);
byteBuffer.put(bytes);

// Extra cast necessary for compiling on JDK 9 plus running on JDK 8, since
// otherwise the overridden ByteBuffer-returning rewind method would be chosen
// which isn't available on JDK 8.
return ((Buffer) byteBuffer).rewind();
return byteBuffer.rewind();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 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 @@ -19,7 +19,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Arrays;
Expand Down Expand Up @@ -333,18 +332,14 @@ private void write(ByteBuffer source) {
public DefaultDataBuffer slice(int index, int length) {
checkIndex(index, length);
int oldPosition = this.byteBuffer.position();
// Explicit access via Buffer base type for compatibility
// with covariant return type on JDK 9's ByteBuffer...
Buffer buffer = this.byteBuffer;
try {
buffer.position(index);
this.byteBuffer.position(index);
ByteBuffer slice = this.byteBuffer.slice();
// Explicit cast for compatibility with covariant return type on JDK 9's ByteBuffer
slice.limit(length);
return new SlicedDefaultDataBuffer(slice, this.dataBufferFactory, length);
}
finally {
buffer.position(oldPosition);
this.byteBuffer.position(oldPosition);
}
}

Expand All @@ -358,11 +353,8 @@ public ByteBuffer asByteBuffer(int index, int length) {
checkIndex(index, length);

ByteBuffer duplicate = this.byteBuffer.duplicate();
// Explicit access via Buffer base type for compatibility
// with covariant return type on JDK 9's ByteBuffer...
Buffer buffer = duplicate;
buffer.position(index);
buffer.limit(index + length);
duplicate.position(index);
duplicate.limit(index + length);
return duplicate.slice();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ public static Set<Class<?>> getAllInterfacesForClassAsSet(Class<?> clazz, @Nulla
* conflicting method signatures (or a similar constraint is violated)
* @see java.lang.reflect.Proxy#getProxyClass
*/
@SuppressWarnings("deprecation") // on JDK 9
@SuppressWarnings("deprecation")
public static Class<?> createCompositeInterface(Class<?>[] interfaces, @Nullable ClassLoader classLoader) {
Assert.notEmpty(interfaces, "Interface array must not be empty");
return Proxy.getProxyClass(classLoader, interfaces);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public static <T> Constructor<T> accessibleConstructor(Class<T> clazz, Class<?>.
* @param ctor the constructor to make accessible
* @see java.lang.reflect.Constructor#setAccessible
*/
@SuppressWarnings("deprecation") // on JDK 9
@SuppressWarnings("deprecation")
public static void makeAccessible(Constructor<?> ctor) {
if ((!Modifier.isPublic(ctor.getModifiers()) ||
!Modifier.isPublic(ctor.getDeclaringClass().getModifiers())) && !ctor.isAccessible()) {
Expand Down Expand Up @@ -563,7 +563,7 @@ public static boolean isCglibRenamedMethod(Method renamedMethod) {
* @param method the method to make accessible
* @see java.lang.reflect.Method#setAccessible
*/
@SuppressWarnings("deprecation") // on JDK 9
@SuppressWarnings("deprecation")
public static void makeAccessible(Method method) {
if ((!Modifier.isPublic(method.getModifiers()) ||
!Modifier.isPublic(method.getDeclaringClass().getModifiers())) && !method.isAccessible()) {
Expand Down Expand Up @@ -775,7 +775,7 @@ public static boolean isPublicStaticFinal(Field field) {
* @param field the field to make accessible
* @see java.lang.reflect.Field#setAccessible
*/
@SuppressWarnings("deprecation") // on JDK 9
@SuppressWarnings("deprecation")
public static void makeAccessible(Field field) {
if ((!Modifier.isPublic(field.getModifiers()) ||
!Modifier.isPublic(field.getDeclaringClass().getModifiers()) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ void hashCodeWithBooleanTrue() {
@Deprecated
void hashCodeWithDouble() {
double dbl = 9830.43;
int expected = (new Double(dbl)).hashCode();
int expected = Double.valueOf(dbl).hashCode();
assertThat(ObjectUtils.hashCode(dbl)).isEqualTo(expected);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ else if (found) {
setSourceMethodName(sourceMethodName);
}

@SuppressWarnings("deprecation") // setMillis is deprecated in JDK 9
@SuppressWarnings("deprecation")
protected Object writeReplace() {
LogRecord serialized = new LogRecord(getLevel(), getMessage());
serialized.setLoggerName(getLoggerName());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 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 @@ -17,7 +17,6 @@
package org.springframework.messaging.simp.stomp;

import java.io.ByteArrayOutputStream;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
Expand Down Expand Up @@ -134,11 +133,7 @@ public List<Message<byte[]>> decode(ByteBuffer byteBuffer,
private Message<byte[]> decodeMessage(ByteBuffer byteBuffer, @Nullable MultiValueMap<String, String> headers) {
Message<byte[]> decodedMessage = null;
skipEol(byteBuffer);

// Explicit mark/reset access via Buffer base type for compatibility
// with covariant return type on JDK 9's ByteBuffer...
Buffer buffer = byteBuffer;
buffer.mark();
byteBuffer.mark();

String command = readCommand(byteBuffer);
if (command.length() > 0) {
Expand Down Expand Up @@ -176,7 +171,7 @@ private Message<byte[]> decodeMessage(ByteBuffer byteBuffer, @Nullable MultiValu
headers.putAll(map);
}
}
buffer.reset();
byteBuffer.reset();
}
}
else {
Expand Down Expand Up @@ -357,8 +352,7 @@ else if (b == '\r') {
throw new StompConversionException("'\\r' must be followed by '\\n'");
}
}
// Explicit cast for compatibility with covariant return type on JDK 9's ByteBuffer
((Buffer) byteBuffer).position(byteBuffer.position() - 1);
byteBuffer.position(byteBuffer.position() - 1);
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 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 Down Expand Up @@ -189,7 +189,7 @@ protected DocumentBuilder createDocumentBuilder(DocumentBuilderFactory factory)
* @return the XMLReader
* @throws SAXException if thrown by JAXP methods
*/
@SuppressWarnings("deprecation") // on JDK 9
@SuppressWarnings("deprecation")
protected XMLReader createXmlReader() throws SAXException {
XMLReader xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 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 Down Expand Up @@ -122,7 +122,6 @@ public int getMaxInMemorySize() {


@Override
@SuppressWarnings({"rawtypes", "unchecked", "cast"}) // XMLEventReader is Iterator<Object> on JDK 9
public Flux<XMLEvent> decode(Publisher<DataBuffer> input, ResolvableType elementType,
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {

Expand All @@ -137,7 +136,7 @@ public Flux<XMLEvent> decode(Publisher<DataBuffer> input, ResolvableType element
.flatMapIterable(buffer -> {
try {
InputStream is = buffer.asInputStream();
Iterator eventReader = inputFactory.createXMLEventReader(is);
Iterator<Object> eventReader = inputFactory.createXMLEventReader(is);
List<XMLEvent> result = new ArrayList<>();
eventReader.forEachRemaining(event -> result.add((XMLEvent) event));
return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 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 Down Expand Up @@ -197,7 +197,7 @@ private DOMSource readDOMSource(InputStream body, HttpInputMessage inputMessage)
}
}

@SuppressWarnings("deprecation") // on JDK 9
@SuppressWarnings("deprecation")
private SAXSource readSAXSource(InputStream body, HttpInputMessage inputMessage) throws IOException {
try {
XMLReader xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
Expand Down

0 comments on commit b74e938

Please sign in to comment.