Skip to content

Commit

Permalink
Remove checks for Java versions older than 11 (#11787)
Browse files Browse the repository at this point in the history
Motivation:
Netty 5 will require Java 11 or greater, so there is no need to check for, and be compatible with, versions older than this.

Modification:
Remove numerous version checks and compatibility fallbacks.
Remove annotation for suppressing the Java 6 requirement, which was the Netty 4.1 baseline.

Result:
Cleaner code.
  • Loading branch information
chrisvest committed Oct 25, 2021
1 parent b100e28 commit 944a3e5
Show file tree
Hide file tree
Showing 23 changed files with 103 additions and 258 deletions.
Expand Up @@ -15,7 +15,6 @@
*/
package io.netty.handler.codec.marshalling;

import io.netty.util.internal.PlatformDependent;
import org.jboss.marshalling.Marshalling;
import org.junit.jupiter.api.BeforeAll;

Expand All @@ -33,9 +32,6 @@ public static void checkSupported() throws Throwable {
Marshalling.getProvidedMarshallerFactory(SERIAL_FACTORY);
} catch (Throwable cause) {
// This may fail on Java 9+ depending on which command-line arguments are used when building.
if (PlatformDependent.javaVersion() < 9) {
throw cause;
}
error = cause;
}
assumeTrue(error == null, error + " was not null");
Expand Down
Expand Up @@ -312,13 +312,9 @@ private static void loadLibrary(final ClassLoader loader, final String name, fin
}
}

@SuppressJava6Requirement(reason = "Guarded by version check")
private static void rethrowWithMoreDetailsIfPossible(String name, NoSuchMethodError error) {
if (PlatformDependent.javaVersion() >= 7) {
throw new LinkageError(
"Possible multiple incompatible native libraries on the classpath for '" + name + "'?", error);
}
throw error;
throw new LinkageError(
"Possible multiple incompatible native libraries on the classpath for '" + name + "'?", error);
}

private static void loadLibraryByHelper(final Class<?> helper, final String name, final boolean absolute)
Expand Down
17 changes: 3 additions & 14 deletions common/src/main/java/io/netty/util/internal/PlatformDependent.java
Expand Up @@ -154,7 +154,7 @@ public final class PlatformDependent {

int tryAllocateUninitializedArray =
SystemPropertyUtil.getInt("io.netty.uninitializedArrayAllocationThreshold", 1024);
UNINITIALIZED_ARRAY_ALLOCATION_THRESHOLD = javaVersion() >= 9 && PlatformDependent0.hasAllocateArrayMethod() ?
UNINITIALIZED_ARRAY_ALLOCATION_THRESHOLD = PlatformDependent0.hasAllocateArrayMethod() ?
tryAllocateUninitializedArray : -1;
logger.debug("-Dio.netty.uninitializedArrayAllocationThreshold: {}", UNINITIALIZED_ARRAY_ALLOCATION_THRESHOLD);

Expand Down Expand Up @@ -1372,22 +1372,11 @@ public static Set<String> normalizedLinuxClassifiers() {
return LINUX_OS_CLASSIFIERS;
}

@SuppressJava6Requirement(reason = "Guarded by version check")
public static File createTempFile(String prefix, String suffix, File directory) throws IOException {
if (javaVersion() >= 7) {
if (directory == null) {
return Files.createTempFile(prefix, suffix).toFile();
}
return Files.createTempFile(directory.toPath(), prefix, suffix).toFile();
}
if (directory == null) {
return File.createTempFile(prefix, suffix);
return Files.createTempFile(prefix, suffix).toFile();
}
File file = File.createTempFile(prefix, suffix, directory);
// Try to adjust the perms, if this fails there is not much else we can do...
file.setReadable(false, false);
file.setReadable(true, true);
return file;
return Files.createTempFile(directory.toPath(), prefix, suffix).toFile();
}

/**
Expand Down

This file was deleted.

Expand Up @@ -54,7 +54,6 @@ public static String stackTraceToString(Throwable cause) {
}
}

@SuppressJava6Requirement(reason = "Throwable addSuppressed is only available for >= 7. Has check for < 7.")
public static void addSuppressed(Throwable target, Throwable suppressed) {
target.addSuppressed(suppressed);
}
Expand Down
Expand Up @@ -47,7 +47,6 @@ public void testFileNotFoundWithNullClassLoader() {
}
}

@SuppressJava6Requirement(reason = "uses Java 7+ Throwable#getSuppressed but is guarded by version checks")
private static void verifySuppressedException(UnsatisfiedLinkError error,
Class<?> expectedSuppressedExceptionClass) {
try {
Expand Down
6 changes: 2 additions & 4 deletions handler/src/main/java/io/netty/handler/ssl/Conscrypt.java
Expand Up @@ -34,10 +34,8 @@ final class Conscrypt {
static {
MethodHandle isConscryptSSLEngine = null;

if ((PlatformDependent.javaVersion() >= 8 &&
// Only works on Java14 and earlier for now
// See https://github.com/google/conscrypt/issues/838
PlatformDependent.javaVersion() < 15) || PlatformDependent.isAndroid()) {
// Only works on Java14 and earlier for now. See https://github.com/google/conscrypt/issues/838
if (PlatformDependent.javaVersion() < 15 || PlatformDependent.isAndroid()) {
try {
MethodHandles.Lookup lookup = MethodHandles.lookup();
Class<?> providerClass = Class.forName("org.conscrypt.OpenSSLProvider", true,
Expand Down
16 changes: 7 additions & 9 deletions handler/src/main/java/io/netty/handler/ssl/JdkAlpnSslUtils.java
Expand Up @@ -16,6 +16,11 @@
package io.netty.handler.ssl;


import io.netty.util.internal.EmptyArrays;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLParameters;
Expand All @@ -27,11 +32,6 @@
import java.util.List;
import java.util.function.BiFunction;

import io.netty.util.internal.EmptyArrays;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;

final class JdkAlpnSslUtils {
private static final InternalLogger logger = InternalLoggerFactory.getInstance(JdkAlpnSslUtils.class);
private static final MethodHandle SET_APPLICATION_PROTOCOLS;
Expand Down Expand Up @@ -87,11 +87,9 @@ final class JdkAlpnSslUtils {
(BiFunction<SSLEngine, List<String>, String>)
getHandshakeApplicationProtocolSelector.invokeExact(engine);
} catch (Throwable t) {
// This is expected to work since Java 9, so log as error.
int version = PlatformDependent.javaVersion();
if (version >= 9) {
// We only log when run on java9+ as this is expected on some earlier java8 versions
logger.error("Unable to initialize JdkAlpnSslUtils, but the detected java version was: {}", version, t);
}
logger.error("Unable to initialize JdkAlpnSslUtils. Detected java version was: {}", version, t);
getHandshakeApplicationProtocol = null;
getApplicationProtocol = null;
setApplicationProtocols = null;
Expand Down
23 changes: 6 additions & 17 deletions handler/src/main/java/io/netty/handler/ssl/JettyAlpnSslEngine.java
Expand Up @@ -15,21 +15,19 @@
*/
package io.netty.handler.ssl;

import static io.netty.handler.ssl.SslUtils.toSSLHandshakeException;
import static java.util.Objects.requireNonNull;

import io.netty.handler.ssl.JdkApplicationProtocolNegotiator.ProtocolSelectionListener;
import io.netty.handler.ssl.JdkApplicationProtocolNegotiator.ProtocolSelector;

import java.util.LinkedHashSet;
import java.util.List;
import org.eclipse.jetty.alpn.ALPN;

import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLException;
import java.util.LinkedHashSet;
import java.util.List;

import io.netty.util.internal.PlatformDependent;
import org.eclipse.jetty.alpn.ALPN;
import static io.netty.handler.ssl.SslUtils.toSSLHandshakeException;
import static java.util.Objects.requireNonNull;

@Deprecated(forRemoval = true)
abstract class JettyAlpnSslEngine extends JdkSslEngine {
private static final boolean available = initAvailable();

Expand All @@ -38,15 +36,6 @@ static boolean isAvailable() {
}

private static boolean initAvailable() {
if (PlatformDependent.javaVersion() <= 8) {
try {
// Always use bootstrap class loader.
Class.forName("sun.security.ssl.ALPNExtension", true, null);
return true;
} catch (Throwable ignore) {
// alpn-boot was not loaded.
}
}
return false;
}

Expand Down
Expand Up @@ -334,14 +334,9 @@ public List<byte[]> getStatusResponses() {
// Use SNI if peerHost was specified and a valid hostname
// See https://github.com/netty/netty/issues/4746
if (clientMode && SslUtils.isValidHostNameForSNI(peerHost)) {
// If on java8 and later we should do some extra validation to ensure we can construct the
// Since we are on Java 8+, we should do some extra validation to ensure we can construct the
// SNIHostName later again.
if (PlatformDependent.javaVersion() >= 8) {
if (Java8SslUtils.isValidHostNameForSNI(peerHost)) {
SSL.setTlsExtHostName(ssl, peerHost);
sniHostNames = Collections.singletonList(peerHost);
}
} else {
if (Java8SslUtils.isValidHostNameForSNI(peerHost)) {
SSL.setTlsExtHostName(ssl, peerHost);
sniHostNames = Collections.singletonList(peerHost);
}
Expand Down Expand Up @@ -2155,63 +2150,53 @@ public final boolean getEnableSessionCreation() {
public final synchronized SSLParameters getSSLParameters() {
SSLParameters sslParameters = super.getSSLParameters();

int version = PlatformDependent.javaVersion();
if (version >= 7) {
sslParameters.setEndpointIdentificationAlgorithm(endPointIdentificationAlgorithm);
Java7SslParametersUtils.setAlgorithmConstraints(sslParameters, algorithmConstraints);
if (version >= 8) {
if (sniHostNames != null) {
Java8SslUtils.setSniHostNames(sslParameters, sniHostNames);
}
if (!isDestroyed()) {
Java8SslUtils.setUseCipherSuitesOrder(
sslParameters, (SSL.getOptions(ssl) & SSL.SSL_OP_CIPHER_SERVER_PREFERENCE) != 0);
}

Java8SslUtils.setSNIMatchers(sslParameters, matchers);
}
sslParameters.setEndpointIdentificationAlgorithm(endPointIdentificationAlgorithm);
Java7SslParametersUtils.setAlgorithmConstraints(sslParameters, algorithmConstraints);
if (sniHostNames != null) {
Java8SslUtils.setSniHostNames(sslParameters, sniHostNames);
}
if (!isDestroyed()) {
Java8SslUtils.setUseCipherSuitesOrder(
sslParameters, (SSL.getOptions(ssl) & SSL.SSL_OP_CIPHER_SERVER_PREFERENCE) != 0);
}

Java8SslUtils.setSNIMatchers(sslParameters, matchers);
return sslParameters;
}

@Override
public final synchronized void setSSLParameters(SSLParameters sslParameters) {
int version = PlatformDependent.javaVersion();
if (version >= 7) {
if (sslParameters.getAlgorithmConstraints() != null) {
throw new IllegalArgumentException("AlgorithmConstraints are not supported.");
}
if (sslParameters.getAlgorithmConstraints() != null) {
throw new IllegalArgumentException("AlgorithmConstraints are not supported.");
}

boolean isDestroyed = isDestroyed();
if (version >= 8) {
if (!isDestroyed) {
if (clientMode) {
final List<String> sniHostNames = Java8SslUtils.getSniHostNames(sslParameters);
for (String name: sniHostNames) {
SSL.setTlsExtHostName(ssl, name);
}
this.sniHostNames = sniHostNames;
}
if (Java8SslUtils.getUseCipherSuitesOrder(sslParameters)) {
SSL.setOptions(ssl, SSL.SSL_OP_CIPHER_SERVER_PREFERENCE);
} else {
SSL.clearOptions(ssl, SSL.SSL_OP_CIPHER_SERVER_PREFERENCE);
}
boolean isDestroyed = isDestroyed();
if (!isDestroyed) {
if (clientMode) {
final List<String> sniHostNames = Java8SslUtils.getSniHostNames(sslParameters);
for (String name: sniHostNames) {
SSL.setTlsExtHostName(ssl, name);
}
matchers = sslParameters.getSNIMatchers();
this.sniHostNames = sniHostNames;
}
if (Java8SslUtils.getUseCipherSuitesOrder(sslParameters)) {
SSL.setOptions(ssl, SSL.SSL_OP_CIPHER_SERVER_PREFERENCE);
} else {
SSL.clearOptions(ssl, SSL.SSL_OP_CIPHER_SERVER_PREFERENCE);
}
}
matchers = sslParameters.getSNIMatchers();

final String endPointIdentificationAlgorithm = sslParameters.getEndpointIdentificationAlgorithm();
if (!isDestroyed) {
// If the user asks for hostname verification we must ensure we verify the peer.
// If the user disables hostname verification we leave it up to the user to change the mode manually.
if (clientMode && isEndPointVerificationEnabled(endPointIdentificationAlgorithm)) {
SSL.setVerify(ssl, SSL.SSL_CVERIFY_REQUIRED, -1);
}
final String endPointIdentificationAlgorithm = sslParameters.getEndpointIdentificationAlgorithm();
if (!isDestroyed) {
// If the user asks for hostname verification we must ensure we verify the peer.
// If the user disables hostname verification we leave it up to the user to change the mode manually.
if (clientMode && isEndPointVerificationEnabled(endPointIdentificationAlgorithm)) {
SSL.setVerify(ssl, SSL.SSL_CVERIFY_REQUIRED, -1);
}
this.endPointIdentificationAlgorithm = endPointIdentificationAlgorithm;
algorithmConstraints = sslParameters.getAlgorithmConstraints();
}
this.endPointIdentificationAlgorithm = endPointIdentificationAlgorithm;
algorithmConstraints = sslParameters.getAlgorithmConstraints();
super.setSSLParameters(sslParameters);
}

Expand Down
Expand Up @@ -17,20 +17,19 @@
package io.netty.handler.ssl.util;

import io.netty.util.concurrent.FastThreadLocal;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.StringUtil;
import io.netty.util.internal.SuppressJava6Requirement;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.Provider;
import java.util.Objects;
import javax.net.ssl.ManagerFactoryParameters;

import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.KeyManagerFactorySpi;
import javax.net.ssl.ManagerFactoryParameters;
import javax.net.ssl.X509ExtendedKeyManager;
import javax.net.ssl.X509KeyManager;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.Provider;
import java.util.Objects;

/**
* Helps to implement a custom {@link KeyManagerFactory}.
Expand Down Expand Up @@ -133,15 +132,12 @@ protected KeyManager[] engineGetKeyManagers() {
KeyManager[] keyManagers = this.keyManagers;
if (keyManagers == null) {
keyManagers = parent.engineGetKeyManagers();
if (PlatformDependent.javaVersion() >= 7) {
wrapIfNeeded(keyManagers);
}
wrapIfNeeded(keyManagers);
this.keyManagers = keyManagers;
}
return keyManagers.clone();
}

@SuppressJava6Requirement(reason = "Usage guarded by java version check")
private static void wrapIfNeeded(KeyManager[] keyManagers) {
for (int i = 0; i < keyManagers.length; i++) {
final KeyManager tm = keyManagers[i];
Expand Down
Expand Up @@ -24,7 +24,6 @@
import io.netty.internal.tcnative.SSL;
import io.netty.util.CharsetUtil;
import io.netty.util.internal.EmptyArrays;
import io.netty.util.internal.PlatformDependent;
import org.junit.AssumptionViolatedException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -1068,7 +1067,6 @@ private void testWrapDstBigEnough(BufferType type, SSLEngine engine, int srcLen)
@MethodSource("newTestParams")
@ParameterizedTest
public void testSNIMatchersDoesNotThrow(SSLEngineTestParam param) throws Exception {
assumeTrue(PlatformDependent.javaVersion() >= 8);
SelfSignedCertificate ssc = new SelfSignedCertificate();
serverSslCtx = wrapContext(param, SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey())
.sslProvider(sslServerProvider())
Expand All @@ -1090,7 +1088,6 @@ public void testSNIMatchersDoesNotThrow(SSLEngineTestParam param) throws Excepti
@MethodSource("newTestParams")
@ParameterizedTest
public void testSNIMatchersWithSNINameWithUnderscore(SSLEngineTestParam param) throws Exception {
assumeTrue(PlatformDependent.javaVersion() >= 8);
byte[] name = "rb8hx3pww30y3tvw0mwy.v1_1".getBytes(CharsetUtil.UTF_8);
SelfSignedCertificate ssc = new SelfSignedCertificate();
serverSslCtx = wrapContext(param, SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey())
Expand Down

0 comments on commit 944a3e5

Please sign in to comment.