Skip to content

Commit

Permalink
Remove all version checks for Java 6 and 7 (#14006)
Browse files Browse the repository at this point in the history
Motivation:
Netty 4.2 now requires Java 8 as the minimum supported version, so there
is no need to guard for older JDKs.

Modification:
Remove all the version checks for Java 7 and older, and remove all uses
of `@SuppressJava6Requirement`. Revapi complains these annotation
removals are a potential compatibility issue, so ignores have been
added.

Result:
Less code dealing with JDK versions.
  • Loading branch information
chrisvest committed Apr 27, 2024
1 parent d14633b commit e2aab65
Show file tree
Hide file tree
Showing 75 changed files with 585 additions and 1,050 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/codeql-analysis.yml
Expand Up @@ -22,10 +22,10 @@ name: "CodeQL"

on:
push:
branches: ["4.1", "4.2", main]
branches: ["4.1", "4.2"]
pull_request:
# The branches below must be a subset of the branches above
branches: ["4.1", "4.2", main]
branches: ["4.1", "4.2"]
schedule:
- cron: '0 13 * * 3'

Expand Down Expand Up @@ -89,6 +89,10 @@ jobs:
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language
- uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: '8' # The JDK version to make available on the path.

- name: Compile project
run: ./mvnw -B -ntp clean package -DskipTests=true
Expand Down
Expand Up @@ -22,7 +22,6 @@
import io.netty.util.internal.ObjectPool;
import io.netty.util.internal.ObjectUtil;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.SuppressJava6Requirement;
import io.netty.util.internal.SystemPropertyUtil;
import io.netty.util.internal.UnstableApi;

Expand All @@ -42,8 +41,6 @@
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import java.util.concurrent.locks.StampedLock;

import static io.netty.util.internal.PlatformDependent.javaVersion;

/**
* An auto-tuning pooling allocator, that follows an anti-generational hypothesis.
* <p>
Expand All @@ -70,7 +67,6 @@
* magazine, to be shared with other magazines.
* The {@link #createSharedChunkQueue()} method can be overridden to customize this queue.
*/
@SuppressJava6Requirement(reason = "Guarded by version check")
@UnstableApi
final class AdaptivePoolingAllocator {
private static final int RETIRE_CAPACITY = 4 * 1024;
Expand Down Expand Up @@ -104,10 +100,6 @@ final class AdaptivePoolingAllocator {

AdaptivePoolingAllocator(ChunkAllocator chunkAllocator) {
this.chunkAllocator = chunkAllocator;
if (javaVersion() < 8) {
// The implementation uses StampedLock, which was introduced in Java 8.
throw new IllegalStateException("This allocator require Java 8 or newer.");
}
centralQueue = ObjectUtil.checkNotNull(createSharedChunkQueue(), "centralQueue");
magazineExpandLock = new StampedLock();
Magazine[] mags = new Magazine[4];
Expand Down Expand Up @@ -234,7 +226,6 @@ private boolean offerToQueue(ChunkByteBuf buffer) {
return centralQueue.offer(buffer);
}

@SuppressJava6Requirement(reason = "Guarded by version check")
@SuppressWarnings("checkstyle:finalclass") // Checkstyle mistakenly believes this class should be final.
private static class AllocationStatistics extends StampedLock {
private static final long serialVersionUID = -8319929980932269688L;
Expand Down
24 changes: 8 additions & 16 deletions buffer/src/main/java/io/netty/buffer/PooledUnsafeHeapByteBuf.java
Expand Up @@ -132,26 +132,18 @@ protected void _setLongLE(int index, long value) {

@Override
public ByteBuf setZero(int index, int length) {
if (PlatformDependent.javaVersion() >= 7) {
checkIndex(index, length);
// Only do on java7+ as the needed Unsafe call was only added there.
UnsafeByteBufUtil.setZero(memory, idx(index), length);
return this;
}
return super.setZero(index, length);
checkIndex(index, length);
UnsafeByteBufUtil.setZero(memory, idx(index), length);
return this;
}

@Override
public ByteBuf writeZero(int length) {
if (PlatformDependent.javaVersion() >= 7) {
// Only do on java7+ as the needed Unsafe call was only added there.
ensureWritable(length);
int wIndex = writerIndex;
UnsafeByteBufUtil.setZero(memory, idx(wIndex), length);
writerIndex = wIndex + length;
return this;
}
return super.writeZero(length);
ensureWritable(length);
int wIndex = writerIndex;
UnsafeByteBufUtil.setZero(memory, idx(wIndex), length);
writerIndex = wIndex + length;
return this;
}

@Override
Expand Down
Expand Up @@ -248,26 +248,18 @@ protected void _setLongLE(int index, long value) {

@Override
public ByteBuf setZero(int index, int length) {
if (PlatformDependent.javaVersion() >= 7) {
// Only do on java7+ as the needed Unsafe call was only added there.
checkIndex(index, length);
UnsafeByteBufUtil.setZero(array, index, length);
return this;
}
return super.setZero(index, length);
checkIndex(index, length);
UnsafeByteBufUtil.setZero(array, index, length);
return this;
}

@Override
public ByteBuf writeZero(int length) {
if (PlatformDependent.javaVersion() >= 7) {
// Only do on java7+ as the needed Unsafe call was only added there.
ensureWritable(length);
int wIndex = writerIndex;
UnsafeByteBufUtil.setZero(array, wIndex, length);
writerIndex = wIndex + length;
return this;
}
return super.writeZero(length);
ensureWritable(length);
int wIndex = writerIndex;
UnsafeByteBufUtil.setZero(array, wIndex, length);
writerIndex = wIndex + length;
return this;
}

@Override
Expand Down
Expand Up @@ -15,16 +15,12 @@
*/
package io.netty.handler.codec.http.websocketx;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.base64.Base64;
import io.netty.util.CharsetUtil;
import io.netty.util.concurrent.FastThreadLocal;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.SuppressJava6Requirement;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
* A utility class mainly for use by web sockets
Expand Down Expand Up @@ -95,24 +91,8 @@ private static byte[] digest(FastThreadLocal<MessageDigest> digestFastThreadLoca
* @param data The data to encode
* @return An encoded string containing the data
*/
@SuppressJava6Requirement(reason = "Guarded with java version check")
static String base64(byte[] data) {
if (PlatformDependent.javaVersion() >= 8) {
return java.util.Base64.getEncoder().encodeToString(data);
}
String encodedString;
ByteBuf encodedData = Unpooled.wrappedBuffer(data);
try {
ByteBuf encoded = Base64.encode(encodedData);
try {
encodedString = encoded.toString(CharsetUtil.UTF_8);
} finally {
encoded.release();
}
} finally {
encodedData.release();
}
return encodedString;
return Base64.getEncoder().encodeToString(data);
}

/**
Expand Down
Expand Up @@ -17,7 +17,6 @@

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.util.internal.PlatformDependent;

/**
* Super-class for SPDY header-block encoders.
Expand All @@ -30,14 +29,7 @@ public abstract class SpdyHeaderBlockEncoder {

static SpdyHeaderBlockEncoder newInstance(
SpdyVersion version, int compressionLevel, int windowBits, int memLevel) {

if (PlatformDependent.javaVersion() >= 7) {
return new SpdyHeaderBlockZlibEncoder(
version, compressionLevel);
} else {
return new SpdyHeaderBlockJZlibEncoder(
version, compressionLevel, windowBits, memLevel);
}
return new SpdyHeaderBlockZlibEncoder(version, compressionLevel);
}

abstract ByteBuf encode(ByteBufAllocator alloc, SpdyHeadersFrame frame) throws Exception;
Expand Down
Expand Up @@ -18,12 +18,10 @@
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.Unpooled;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.SuppressJava6Requirement;

import java.util.zip.Deflater;

import static io.netty.handler.codec.spdy.SpdyCodecUtil.*;
import static io.netty.handler.codec.spdy.SpdyCodecUtil.SPDY_DICT;
import static io.netty.util.internal.ObjectUtil.checkNotNullWithIAE;

class SpdyHeaderBlockZlibEncoder extends SpdyHeaderBlockRawEncoder {
Expand Down Expand Up @@ -73,17 +71,11 @@ private ByteBuf encode(ByteBufAllocator alloc, int len) {
}
}

@SuppressJava6Requirement(reason = "Guarded by java version check")
private boolean compressInto(ByteBuf compressed) {
byte[] out = compressed.array();
int off = compressed.arrayOffset() + compressed.writerIndex();
int toWrite = compressed.writableBytes();
final int numBytes;
if (PlatformDependent.javaVersion() >= 7) {
numBytes = compressor.deflate(out, off, toWrite, Deflater.SYNC_FLUSH);
} else {
numBytes = compressor.deflate(out, off, toWrite);
}
final int numBytes = compressor.deflate(out, off, toWrite, Deflater.SYNC_FLUSH);
compressed.writerIndex(compressed.writerIndex() + numBytes);
return numBytes == toWrite;
}
Expand Down
Expand Up @@ -15,8 +15,6 @@
*/
package io.netty.handler.codec.spdy;

import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.SuppressJava6Requirement;
import io.netty.util.internal.ThrowableUtil;

public class SpdyProtocolException extends Exception {
Expand Down Expand Up @@ -50,17 +48,10 @@ public SpdyProtocolException(Throwable cause) {
}

static SpdyProtocolException newStatic(String message, Class<?> clazz, String method) {
final SpdyProtocolException exception;
if (PlatformDependent.javaVersion() >= 7) {
exception = new StacklessSpdyProtocolException(message, true);
} else {
exception = new StacklessSpdyProtocolException(message);
}
final SpdyProtocolException exception = new StacklessSpdyProtocolException(message, true);
return ThrowableUtil.unknownStackTrace(exception, clazz, method);
}

@SuppressJava6Requirement(reason = "uses Java 7+ Exception.<init>(String, Throwable, boolean, boolean)" +
" but is guarded by version checks")
private SpdyProtocolException(String message, boolean shared) {
super(message, null, false, true);
assert shared;
Expand All @@ -69,10 +60,6 @@ private SpdyProtocolException(String message, boolean shared) {
private static final class StacklessSpdyProtocolException extends SpdyProtocolException {
private static final long serialVersionUID = -6302754207557485099L;

StacklessSpdyProtocolException(String message) {
super(message);
}

StacklessSpdyProtocolException(String message, boolean shared) {
super(message, shared);
}
Expand Down
Expand Up @@ -15,8 +15,6 @@

package io.netty.handler.codec.http2;

import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.SuppressJava6Requirement;
import io.netty.util.internal.ThrowableUtil;
import io.netty.util.internal.UnstableApi;

Expand Down Expand Up @@ -68,17 +66,10 @@ public Http2Exception(Http2Error error, String message, Throwable cause, Shutdow

static Http2Exception newStatic(Http2Error error, String message, ShutdownHint shutdownHint,
Class<?> clazz, String method) {
final Http2Exception exception;
if (PlatformDependent.javaVersion() >= 7) {
exception = new StacklessHttp2Exception(error, message, shutdownHint, true);
} else {
exception = new StacklessHttp2Exception(error, message, shutdownHint);
}
final Http2Exception exception = new StacklessHttp2Exception(error, message, shutdownHint, true);
return ThrowableUtil.unknownStackTrace(exception, clazz, method);
}

@SuppressJava6Requirement(reason = "uses Java 7+ Exception.<init>(String, Throwable, boolean, boolean)" +
" but is guarded by version checks")
private Http2Exception(Http2Error error, String message, ShutdownHint shutdownHint, boolean shared) {
super(message, null, false, true);
assert shared;
Expand Down

0 comments on commit e2aab65

Please sign in to comment.