Update compatibility and fix build

The compatibility baseline is now Java 17, which means we can use new language features and new bytecode version.

The MemSegBuffer implementation has been updated with the latest API changes from netty/main.
This commit is contained in:
Chris Vest 2021-09-23 10:57:22 +02:00
parent a4beebf355
commit 6c9f1cbb4e
5 changed files with 36 additions and 118 deletions

View File

@ -26,7 +26,6 @@ import io.netty.buffer.api.ReadableComponent;
import io.netty.buffer.api.ReadableComponentProcessor; import io.netty.buffer.api.ReadableComponentProcessor;
import io.netty.buffer.api.WritableComponent; import io.netty.buffer.api.WritableComponent;
import io.netty.buffer.api.WritableComponentProcessor; import io.netty.buffer.api.WritableComponentProcessor;
import io.netty.buffer.api.adaptor.BufferIntegratable;
import io.netty.buffer.api.internal.AdaptableBuffer; import io.netty.buffer.api.internal.AdaptableBuffer;
import io.netty.buffer.api.internal.ArcDrop; import io.netty.buffer.api.internal.ArcDrop;
import io.netty.buffer.api.internal.Statics; import io.netty.buffer.api.internal.Statics;
@ -53,8 +52,7 @@ import static jdk.incubator.foreign.MemoryAccess.setIntAtOffset;
import static jdk.incubator.foreign.MemoryAccess.setLongAtOffset; import static jdk.incubator.foreign.MemoryAccess.setLongAtOffset;
import static jdk.incubator.foreign.MemoryAccess.setShortAtOffset; import static jdk.incubator.foreign.MemoryAccess.setShortAtOffset;
class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements Buffer, ReadableComponent, class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComponent, WritableComponent {
WritableComponent, BufferIntegratable {
private static final MemorySegment CLOSED_SEGMENT; private static final MemorySegment CLOSED_SEGMENT;
private static final MemorySegment ZERO_OFFHEAP_SEGMENT; private static final MemorySegment ZERO_OFFHEAP_SEGMENT;
private static final MemorySegment ZERO_ONHEAP_SEGMENT; private static final MemorySegment ZERO_ONHEAP_SEGMENT;
@ -96,7 +94,7 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements Buffer, Read
private boolean constBuffer; private boolean constBuffer;
MemSegBuffer(MemorySegment base, MemorySegment view, Drop<MemSegBuffer> drop, AllocatorControl control) { MemSegBuffer(MemorySegment base, MemorySegment view, Drop<MemSegBuffer> drop, AllocatorControl control) {
super(new MakeInaccisbleOnDrop(ArcDrop.wrap(drop))); super(ArcDrop.wrap(drop));
this.control = control; this.control = control;
this.base = base; this.base = base;
seg = view; seg = view;
@ -108,7 +106,7 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements Buffer, Read
* Constructor for {@linkplain BufferAllocator#constBufferSupplier(byte[]) const buffers}. * Constructor for {@linkplain BufferAllocator#constBufferSupplier(byte[]) const buffers}.
*/ */
MemSegBuffer(MemSegBuffer parent) { MemSegBuffer(MemSegBuffer parent) {
super(new MakeInaccisbleOnDrop(new ArcDrop<>(ArcDrop.acquire(parent.unsafeGetDrop())))); super(new ArcDrop<>(ArcDrop.acquire(parent.unsafeGetDrop())));
control = parent.control; control = parent.control;
base = parent.base; base = parent.base;
seg = parent.seg; seg = parent.seg;
@ -119,44 +117,6 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements Buffer, Read
constBuffer = true; constBuffer = true;
} }
private static final class MakeInaccisbleOnDrop implements Drop<MemSegBuffer> {
final Drop<MemSegBuffer> delegate;
private MakeInaccisbleOnDrop(Drop<MemSegBuffer> delegate) {
this.delegate = delegate;
}
@Override
public void drop(MemSegBuffer buf) {
try {
delegate.drop(buf);
} finally {
buf.makeInaccessible();
}
}
@Override
public void attach(MemSegBuffer buf) {
delegate.attach(buf);
}
@Override
public String toString() {
return "MemSegDrop(" + delegate + ')';
}
}
@Override
protected Drop<MemSegBuffer> unsafeGetDrop() {
MakeInaccisbleOnDrop drop = (MakeInaccisbleOnDrop) super.unsafeGetDrop();
return drop.delegate;
}
@Override
protected void unsafeSetDrop(Drop<MemSegBuffer> replacement) {
super.unsafeSetDrop(new MakeInaccisbleOnDrop(replacement));
}
@Override @Override
public String toString() { public String toString() {
return "Buffer[roff:" + roff + ", woff:" + woff + ", cap:" + seg.byteSize() + ']'; return "Buffer[roff:" + roff + ", woff:" + woff + ", cap:" + seg.byteSize() + ']';
@ -273,8 +233,7 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements Buffer, Read
} }
// </editor-fold> // </editor-fold>
@Override private long nativeAddress() {
public long nativeAddress() {
if (!isAccessible()) { if (!isAccessible()) {
throw bufferIsClosed(this); throw bufferIsClosed(this);
} }
@ -386,22 +345,8 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements Buffer, Read
final MemorySegment segment = seg; final MemorySegment segment = seg;
int index = fromOffset; int index = fromOffset;
final int end = index + length; final int end = index + length;
long longValue = -1;
byte byteValue = -1; byte byteValue = -1;
public boolean readLong() {
if (index + Long.BYTES <= end) {
longValue = getLongAtOffset(segment, index, ByteOrder.BIG_ENDIAN);
index += Long.BYTES;
return true;
}
return false;
}
public long getLong() {
return longValue;
}
@Override @Override
public boolean readByte() { public boolean readByte() {
if (index < end) { if (index < end) {
@ -457,23 +402,8 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements Buffer, Read
final MemorySegment segment = seg; final MemorySegment segment = seg;
int index = fromOffset; int index = fromOffset;
final int end = index - length; final int end = index - length;
long longValue = -1;
byte byteValue = -1; byte byteValue = -1;
public boolean readLong() {
if (index - Long.BYTES >= end) {
index -= 7;
longValue = getLongAtOffset(segment, index, ByteOrder.LITTLE_ENDIAN);
index--;
return true;
}
return false;
}
public long getLong() {
return longValue;
}
@Override @Override
public boolean readByte() { public boolean readByte() {
if (index > end) { if (index > end) {
@ -1139,7 +1069,8 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements Buffer, Read
}; };
} }
void makeInaccessible() { @Override
protected void makeInaccessible() {
base = CLOSED_SEGMENT; base = CLOSED_SEGMENT;
seg = CLOSED_SEGMENT; seg = CLOSED_SEGMENT;
wseg = CLOSED_SEGMENT; wseg = CLOSED_SEGMENT;

View File

@ -228,24 +228,6 @@ public class BufferBulkAccessTest extends BufferTestSupport {
} }
} }
@ParameterizedTest
@MethodSource("heapAllocators")
public void heapBufferMustHaveZeroAddress(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThat(buf.nativeAddress()).isZero();
}
}
@ParameterizedTest
@MethodSource("directAllocators")
public void directBufferMustHaveNonZeroAddress(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThat(buf.nativeAddress()).isNotZero();
}
}
@ParameterizedTest @ParameterizedTest
@MethodSource("allocators") @MethodSource("allocators")
public void writeBytesMustWriteAllBytesFromByteArray(Fixture fixture) { public void writeBytesMustWriteAllBytesFromByteArray(Fixture fixture) {

View File

@ -113,7 +113,7 @@ public class BufferCompositionTest extends BufferTestSupport {
Buffer a = allocator.allocate(8); Buffer a = allocator.allocate(8);
Buffer b = allocator.allocate(8); Buffer b = allocator.allocate(8);
CompositeBuffer composed = CompositeBuffer.compose(allocator, a.send())) { CompositeBuffer composed = CompositeBuffer.compose(allocator, a.send())) {
try (Buffer ignore = acquire(composed)) { try (Buffer ignore = acquire((ResourceSupport<?, ?>) composed)) {
var exc = assertThrows(IllegalStateException.class, () -> composed.extendWith(b.send())); var exc = assertThrows(IllegalStateException.class, () -> composed.extendWith(b.send()));
assertThat(exc).hasMessageContaining("owned"); assertThat(exc).hasMessageContaining("owned");
} }
@ -144,13 +144,13 @@ public class BufferCompositionTest extends BufferTestSupport {
try (BufferAllocator allocator = BufferAllocator.onHeapUnpooled()) { try (BufferAllocator allocator = BufferAllocator.onHeapUnpooled()) {
Buffer a = allocator.allocate(1); Buffer a = allocator.allocate(1);
CompositeBuffer composite = CompositeBuffer.compose(allocator, a.send()); CompositeBuffer composite = CompositeBuffer.compose(allocator, a.send());
assertTrue(isOwned(composite)); assertTrue(isOwned((ResourceSupport<?, ?>) composite));
assertThat(composite.capacity()).isOne(); assertThat(composite.capacity()).isOne();
assertThat(composite.countComponents()).isOne(); assertThat(composite.countComponents()).isOne();
try (Buffer b = CompositeBuffer.compose(allocator)) { try (Buffer b = CompositeBuffer.compose(allocator)) {
composite.extendWith(b.send()); composite.extendWith(b.send());
} }
assertTrue(isOwned(composite)); assertTrue(isOwned((ResourceSupport<?, ?>) composite));
assertThat(composite.capacity()).isOne(); assertThat(composite.capacity()).isOne();
assertThat(composite.countComponents()).isOne(); assertThat(composite.countComponents()).isOne();
} }
@ -393,11 +393,11 @@ public class BufferCompositionTest extends BufferTestSupport {
allocator.allocate(8).send(), allocator.allocate(8).send(),
allocator.allocate(8).send())) { allocator.allocate(8).send())) {
try (CompositeBuffer split = composite.splitComponentsFloor(4)) { try (CompositeBuffer split = composite.splitComponentsFloor(4)) {
assertTrue(isOwned(split)); assertTrue(isOwned((ResourceSupport<?, ?>) split));
assertTrue(split.isAccessible()); assertTrue(split.isAccessible());
assertThat(split.capacity()).isZero(); assertThat(split.capacity()).isZero();
assertTrue(isOwned(composite)); assertTrue(isOwned((ResourceSupport<?, ?>) composite));
assertTrue(composite.isAccessible()); assertTrue(composite.isAccessible());
assertThat(composite.capacity()).isEqualTo(16); assertThat(composite.capacity()).isEqualTo(16);
} }
@ -411,11 +411,11 @@ public class BufferCompositionTest extends BufferTestSupport {
allocator.allocate(8).send(), allocator.allocate(8).send(),
allocator.allocate(8).send())) { allocator.allocate(8).send())) {
try (CompositeBuffer split = composite.splitComponentsFloor(7)) { try (CompositeBuffer split = composite.splitComponentsFloor(7)) {
assertTrue(isOwned(split)); assertTrue(isOwned((ResourceSupport<?, ?>) split));
assertTrue(split.isAccessible()); assertTrue(split.isAccessible());
assertThat(split.capacity()).isZero(); assertThat(split.capacity()).isZero();
assertTrue(isOwned(composite)); assertTrue(isOwned((ResourceSupport<?, ?>) composite));
assertTrue(composite.isAccessible()); assertTrue(composite.isAccessible());
assertThat(composite.capacity()).isEqualTo(16); assertThat(composite.capacity()).isEqualTo(16);
} }
@ -429,11 +429,11 @@ public class BufferCompositionTest extends BufferTestSupport {
allocator.allocate(8).send(), allocator.allocate(8).send(),
allocator.allocate(8).send())) { allocator.allocate(8).send())) {
try (CompositeBuffer split = composite.splitComponentsFloor(12)) { try (CompositeBuffer split = composite.splitComponentsFloor(12)) {
assertTrue(isOwned(split)); assertTrue(isOwned((ResourceSupport<?, ?>) split));
assertTrue(split.isAccessible()); assertTrue(split.isAccessible());
assertThat(split.capacity()).isEqualTo(8); assertThat(split.capacity()).isEqualTo(8);
assertTrue(isOwned(composite)); assertTrue(isOwned((ResourceSupport<?, ?>) composite));
assertTrue(composite.isAccessible()); assertTrue(composite.isAccessible());
assertThat(composite.capacity()).isEqualTo(8); assertThat(composite.capacity()).isEqualTo(8);
} }
@ -447,11 +447,11 @@ public class BufferCompositionTest extends BufferTestSupport {
allocator.allocate(8).send(), allocator.allocate(8).send(),
allocator.allocate(8).send())) { allocator.allocate(8).send())) {
try (CompositeBuffer split = composite.splitComponentsFloor(8)) { try (CompositeBuffer split = composite.splitComponentsFloor(8)) {
assertTrue(isOwned(split)); assertTrue(isOwned((ResourceSupport<?, ?>) split));
assertTrue(split.isAccessible()); assertTrue(split.isAccessible());
assertThat(split.capacity()).isEqualTo(8); assertThat(split.capacity()).isEqualTo(8);
assertTrue(isOwned(composite)); assertTrue(isOwned((ResourceSupport<?, ?>) composite));
assertTrue(composite.isAccessible()); assertTrue(composite.isAccessible());
assertThat(composite.capacity()).isEqualTo(8); assertThat(composite.capacity()).isEqualTo(8);
} }
@ -465,11 +465,11 @@ public class BufferCompositionTest extends BufferTestSupport {
allocator.allocate(8).send(), allocator.allocate(8).send(),
allocator.allocate(8).send())) { allocator.allocate(8).send())) {
try (CompositeBuffer split = composite.splitComponentsCeil(4)) { try (CompositeBuffer split = composite.splitComponentsCeil(4)) {
assertTrue(isOwned(split)); assertTrue(isOwned((ResourceSupport<?, ?>) split));
assertTrue(split.isAccessible()); assertTrue(split.isAccessible());
assertThat(split.capacity()).isEqualTo(8); assertThat(split.capacity()).isEqualTo(8);
assertTrue(isOwned(composite)); assertTrue(isOwned((ResourceSupport<?, ?>) composite));
assertTrue(composite.isAccessible()); assertTrue(composite.isAccessible());
assertThat(composite.capacity()).isEqualTo(8); assertThat(composite.capacity()).isEqualTo(8);
} }
@ -483,11 +483,11 @@ public class BufferCompositionTest extends BufferTestSupport {
allocator.allocate(8).send(), allocator.allocate(8).send(),
allocator.allocate(8).send())) { allocator.allocate(8).send())) {
try (CompositeBuffer split = composite.splitComponentsCeil(7)) { try (CompositeBuffer split = composite.splitComponentsCeil(7)) {
assertTrue(isOwned(split)); assertTrue(isOwned((ResourceSupport<?, ?>) split));
assertTrue(split.isAccessible()); assertTrue(split.isAccessible());
assertThat(split.capacity()).isEqualTo(8); assertThat(split.capacity()).isEqualTo(8);
assertTrue(isOwned(composite)); assertTrue(isOwned((ResourceSupport<?, ?>) composite));
assertTrue(composite.isAccessible()); assertTrue(composite.isAccessible());
assertThat(composite.capacity()).isEqualTo(8); assertThat(composite.capacity()).isEqualTo(8);
} }
@ -501,11 +501,11 @@ public class BufferCompositionTest extends BufferTestSupport {
allocator.allocate(8).send(), allocator.allocate(8).send(),
allocator.allocate(8).send())) { allocator.allocate(8).send())) {
try (CompositeBuffer split = composite.splitComponentsCeil(12)) { try (CompositeBuffer split = composite.splitComponentsCeil(12)) {
assertTrue(isOwned(split)); assertTrue(isOwned((ResourceSupport<?, ?>) split));
assertTrue(split.isAccessible()); assertTrue(split.isAccessible());
assertThat(split.capacity()).isEqualTo(16); assertThat(split.capacity()).isEqualTo(16);
assertTrue(isOwned(composite)); assertTrue(isOwned((ResourceSupport<?, ?>) composite));
assertTrue(composite.isAccessible()); assertTrue(composite.isAccessible());
assertThat(composite.capacity()).isEqualTo(0); assertThat(composite.capacity()).isEqualTo(0);
} }
@ -517,11 +517,11 @@ public class BufferCompositionTest extends BufferTestSupport {
allocator.allocate(8).send(), allocator.allocate(8).send(),
allocator.allocate(8).send())) { allocator.allocate(8).send())) {
try (CompositeBuffer split = composite.splitComponentsCeil(12)) { try (CompositeBuffer split = composite.splitComponentsCeil(12)) {
assertTrue(isOwned(split)); assertTrue(isOwned((ResourceSupport<?, ?>) split));
assertTrue(split.isAccessible()); assertTrue(split.isAccessible());
assertThat(split.capacity()).isEqualTo(16); assertThat(split.capacity()).isEqualTo(16);
assertTrue(isOwned(composite)); assertTrue(isOwned((ResourceSupport<?, ?>) composite));
assertTrue(composite.isAccessible()); assertTrue(composite.isAccessible());
assertThat(composite.capacity()).isEqualTo(8); assertThat(composite.capacity()).isEqualTo(8);
} }
@ -535,11 +535,11 @@ public class BufferCompositionTest extends BufferTestSupport {
allocator.allocate(8).send(), allocator.allocate(8).send(),
allocator.allocate(8).send())) { allocator.allocate(8).send())) {
try (CompositeBuffer split = composite.splitComponentsCeil(7)) { try (CompositeBuffer split = composite.splitComponentsCeil(7)) {
assertTrue(isOwned(split)); assertTrue(isOwned((ResourceSupport<?, ?>) split));
assertTrue(split.isAccessible()); assertTrue(split.isAccessible());
assertThat(split.capacity()).isEqualTo(8); assertThat(split.capacity()).isEqualTo(8);
assertTrue(isOwned(composite)); assertTrue(isOwned((ResourceSupport<?, ?>) composite));
assertTrue(composite.isAccessible()); assertTrue(composite.isAccessible());
assertThat(composite.capacity()).isEqualTo(8); assertThat(composite.capacity()).isEqualTo(8);
} }
@ -553,11 +553,11 @@ public class BufferCompositionTest extends BufferTestSupport {
allocator.allocate(8).send(), allocator.allocate(8).send(),
allocator.allocate(8).send())) { allocator.allocate(8).send())) {
try (CompositeBuffer split = composite.splitComponentsCeil(0)) { try (CompositeBuffer split = composite.splitComponentsCeil(0)) {
assertTrue(isOwned(split)); assertTrue(isOwned((ResourceSupport<?, ?>) split));
assertTrue(split.isAccessible()); assertTrue(split.isAccessible());
assertThat(split.capacity()).isZero(); assertThat(split.capacity()).isZero();
assertTrue(isOwned(composite)); assertTrue(isOwned((ResourceSupport<?, ?>) composite));
assertTrue(composite.isAccessible()); assertTrue(composite.isAccessible());
assertThat(composite.capacity()).isEqualTo(16); assertThat(composite.capacity()).isEqualTo(16);
} }

View File

@ -617,6 +617,11 @@ public abstract class ByteToMessageDecoder extends ChannelHandlerAdapter {
return ctx.alloc(); return ctx.alloc();
} }
@Override
public BufferAllocator bufferAllocator() {
return ctx.bufferAllocator();
}
@Override @Override
@Deprecated @Deprecated
public <T> Attribute<T> attr(AttributeKey<T> key) { public <T> Attribute<T> attr(AttributeKey<T> key) {

View File

@ -70,8 +70,8 @@
<javaModuleName>io.netty.incubator.buffer</javaModuleName> <javaModuleName>io.netty.incubator.buffer</javaModuleName>
<netty.version>5.0.0.Final-SNAPSHOT</netty.version> <netty.version>5.0.0.Final-SNAPSHOT</netty.version>
<netty.build.version>29</netty.build.version> <netty.build.version>29</netty.build.version>
<!-- Java version for source and bytecode compatibility. --> <!-- Java version for bytecode compatibility. -->
<java.compatibility>11</java.compatibility> <java.compatibility>17</java.compatibility>
<!-- Java version actually used to perform the build; compiling and testing. --> <!-- Java version actually used to perform the build; compiling and testing. -->
<java.version>17</java.version> <java.version>17</java.version>
<junit.version>5.7.0</junit.version> <junit.version>5.7.0</junit.version>