Fix remaining tests and make the build work on Java 11

This commit is contained in:
Chris Vest 2021-05-21 17:28:07 +02:00
parent 1c3b27f9e0
commit 9dc1d533e3
7 changed files with 169 additions and 5 deletions

View File

@ -18,7 +18,10 @@ package io.netty.buffer.api.internal;
import io.netty.buffer.api.Buffer; import io.netty.buffer.api.Buffer;
import io.netty.buffer.api.Drop; import io.netty.buffer.api.Drop;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodHandles.Lookup; import java.lang.invoke.MethodHandles.Lookup;
import java.lang.invoke.MethodType;
import java.lang.invoke.VarHandle; import java.lang.invoke.VarHandle;
import java.lang.ref.Cleaner; import java.lang.ref.Cleaner;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
@ -38,6 +41,28 @@ public interface Statics {
return "NO_OP_DROP"; return "NO_OP_DROP";
} }
}; };
MethodHandle BB_SLICE_OFFSETS = getByteBufferSliceOffsetsMethodHandle();
MethodHandle BB_PUT_OFFSETS = getByteBufferPutOffsetsMethodHandle();
static MethodHandle getByteBufferSliceOffsetsMethodHandle() {
try {
Lookup lookup = MethodHandles.lookup();
MethodType type = MethodType.methodType(ByteBuffer.class, int.class, int.class);
return lookup.findVirtual(ByteBuffer.class, "slice", type);
} catch (Exception ignore) {
return null;
}
}
static MethodHandle getByteBufferPutOffsetsMethodHandle() {
try {
Lookup lookup = MethodHandles.lookup();
MethodType type = MethodType.methodType(ByteBuffer.class, int.class, ByteBuffer.class, int.class, int.class);
return lookup.findVirtual(ByteBuffer.class, "put", type);
} catch (Exception ignore) {
return null;
}
}
static VarHandle findVarHandle(Lookup lookup, Class<?> recv, String name, Class<?> type) { static VarHandle findVarHandle(Lookup lookup, Class<?> recv, String name, Class<?> type) {
try { try {
@ -77,16 +102,62 @@ public interface Statics {
* support Java 11. * support Java 11.
*/ */
static ByteBuffer bbslice(ByteBuffer buffer, int fromOffset, int length) { static ByteBuffer bbslice(ByteBuffer buffer, int fromOffset, int length) {
return buffer.slice(fromOffset, length); if (BB_SLICE_OFFSETS != null) {
// return buffer.clear().position(fromOffset).limit(fromOffset + length).slice(); return bbsliceJdk13(buffer, fromOffset, length);
}
return bbsliceFallback(buffer, fromOffset, length);
}
private static ByteBuffer bbsliceJdk13(ByteBuffer buffer, int fromOffset, int length) {
try {
return (ByteBuffer) BB_SLICE_OFFSETS.invokeExact(buffer, fromOffset, length);
} catch (RuntimeException re) {
throw re;
} catch (Throwable throwable) {
throw new LinkageError("Unexpected exception from ByteBuffer.slice(int,int).", throwable);
}
}
private static ByteBuffer bbsliceFallback(ByteBuffer buffer, int fromOffset, int length) {
if (fromOffset < 0) {
throw new IndexOutOfBoundsException("The fromOffset must be positive: " + fromOffset + '.');
}
int newLimit = fromOffset + length;
if (newLimit > buffer.capacity()) {
throw new IndexOutOfBoundsException(
"The limit of " + newLimit + " would be greater than capacity: " + buffer.capacity() + '.');
}
try {
return buffer.position(fromOffset).limit(newLimit).slice();
} finally {
buffer.clear();
}
} }
/** /**
* The ByteBuffer put-buffer-with-offset-and-length method is not available in Java 11. * The ByteBuffer put-buffer-with-offset-and-length method is not available in Java 11.
*/ */
static void bbput(ByteBuffer dest, int destPos, ByteBuffer src, int srcPos, int length) { static void bbput(ByteBuffer dest, int destPos, ByteBuffer src, int srcPos, int length) {
dest.put(destPos, src, srcPos, length); if (BB_PUT_OFFSETS != null) {
// dest.position(destPos).put(bbslice(src, srcPos, length)); bbputJdk16(dest, destPos, src, srcPos, length);
} else {
bbputFallback(dest, destPos, src, srcPos, length);
}
}
private static void bbputJdk16(ByteBuffer dest, int destPos, ByteBuffer src, int srcPos, int length) {
try {
@SuppressWarnings("unused") // We need to cast the return type in order to invokeExact.
ByteBuffer ignore = (ByteBuffer) BB_PUT_OFFSETS.invokeExact(dest, destPos, src, srcPos, length);
} catch (RuntimeException re) {
throw re;
} catch (Throwable throwable) {
throw new LinkageError("Unexpected exception from ByteBuffer.put(int,ByteBuffer,int,int).", throwable);
}
}
private static void bbputFallback(ByteBuffer dest, int destPos, ByteBuffer src, int srcPos, int length) {
dest.position(destPos).put(bbslice(src, srcPos, length));
} }
static IllegalStateException bufferIsClosed() { static IllegalStateException bufferIsClosed() {

View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2021 The Netty Project
~
~ The Netty Project licenses this file to you under the Apache License,
~ version 2.0 (the "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at:
~
~ https://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
~ WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
~ License for the specific language governing permissions and limitations
~ under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.netty.incubator</groupId>
<artifactId>netty-incubator-buffer-parent</artifactId>
<version>0.0.1.Final-SNAPSHOT</version>
</parent>
<artifactId>netty-incubator-buffer-memseg-dummy</artifactId>
<version>0.0.1.Final-SNAPSHOT</version>
</project>

View File

@ -0,0 +1,19 @@
/*
* Copyright 2021 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
module netty.incubator.buffer.memseg {
// Java 11 compatible stand-in module for the memory segment implementation.
// We need this module in order for the tests module to pull in the memseg module.
}

View File

@ -1,4 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2021 The Netty Project
~
~ The Netty Project licenses this file to you under the Apache License,
~ version 2.0 (the "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at:
~
~ https://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
~ WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
~ License for the specific language governing permissions and limitations
~ under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
@ -92,5 +107,17 @@
</dependency> </dependency>
</dependencies> </dependencies>
</profile> </profile>
<profile>
<id>Java 11 support for tests</id>
<activation>
<jdk>!17</jdk>
</activation>
<dependencies>
<dependency>
<groupId>io.netty.incubator</groupId>
<artifactId>netty-incubator-buffer-memseg-dummy</artifactId>
</dependency>
</dependencies>
</profile>
</profiles> </profiles>
</project> </project>

View File

@ -21,5 +21,7 @@ open module netty.incubator.buffer.tests {
requires static java.logging; requires static java.logging;
requires netty.incubator.buffer; requires netty.incubator.buffer;
requires static netty.incubator.buffer.memseg; // We need to require memseg in order for its implementation to be service loaded.
// Just having it on the module path is not enough.
requires netty.incubator.buffer.memseg;
} }

View File

@ -266,6 +266,7 @@ public class BufferReferenceCountingTest extends BufferTestSupport {
try (BufferAllocator allocator = fixture.createAllocator(); try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) { Buffer buf = allocator.allocate(8)) {
assertThrows(IllegalArgumentException.class, () -> buf.slice(0, -1)); assertThrows(IllegalArgumentException.class, () -> buf.slice(0, -1));
assertThrows(IllegalArgumentException.class, () -> buf.slice(2, -1));
// Verify that the slice is closed properly afterwards. // Verify that the slice is closed properly afterwards.
assertTrue(buf.isOwned()); assertTrue(buf.isOwned());
} }

14
pom.xml
View File

@ -105,6 +105,15 @@
<module>buffer-memseg</module> <module>buffer-memseg</module>
</modules> </modules>
</profile> </profile>
<profile>
<id>Java 11 support for tests</id>
<activation>
<jdk>!17</jdk>
</activation>
<modules>
<module>buffer-memseg-dummy</module>
</modules>
</profile>
</profiles> </profiles>
<build> <build>
@ -383,6 +392,11 @@
<artifactId>netty-incubator-buffer-memseg</artifactId> <artifactId>netty-incubator-buffer-memseg</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>io.netty.incubator</groupId>
<artifactId>netty-incubator-buffer-memseg-dummy</artifactId>
<version>${project.version}</version>
</dependency>
<dependency> <dependency>
<groupId>io.netty</groupId> <groupId>io.netty</groupId>