Skip to content

Commit

Permalink
project: Adapt code to new Java language aspects
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Veit <[email protected]>
  • Loading branch information
sebveit committed Aug 28, 2024
1 parent 56e1ebd commit fca0ada
Show file tree
Hide file tree
Showing 20 changed files with 57 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public void setBody(final ByteBuf body) {

@Override
public String toString() {
@SuppressWarnings("StringBufferReplaceableByString")
final StringBuilder builder = new StringBuilder();
builder.append("Frame{");
builder.append("byteOrder=").append(byteOrder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private static InboundError mapToError(final Frame frame) {
final UInt32 replySerial = getReplySerialFromHeader(headerFields);
final DBusString sender = getSenderFromHeader(headerFields);
final Optional<DBusString> optionalName = getErrorNameFromHeader(headerFields);
if (!optionalName.isPresent()) {
if (optionalName.isEmpty()) {
final String msg = "Missing error name in message header.";
throw new CorruptedFrameException(msg);
}
Expand Down Expand Up @@ -227,7 +227,7 @@ private static InboundSignal mapToSignal(final Frame frame) {
final DBusString sender = getSenderFromHeader(headerFields);
final ObjectPath path = getObjectPathFromHeader(headerFields);
final Optional<DBusString> optionalInterface = getInterfaceNameFromHeader(headerFields);
if (!optionalInterface.isPresent()) {
if (optionalInterface.isEmpty()) {
final String msg = "Missing interface name in message header.";
throw new CorruptedFrameException(msg);
}
Expand Down Expand Up @@ -287,8 +287,6 @@ public void decode(final ChannelHandlerContext ctx, final Frame frame, final Lis

private InboundMessage mapFrameToInboundMessage(final Frame frame) {
switch (frame.getType()) {
default:
return null;
case ERROR:
return mapToError(frame);
case METHOD_CALL:
Expand All @@ -297,6 +295,8 @@ private InboundMessage mapFrameToInboundMessage(final Frame frame) {
return mapToMethodReturn(frame);
case SIGNAL:
return mapToSignal(frame);
default:
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ private static ByteBuf encodeByteOrder(final ByteBufAllocator allocator, final B
final byte bigEndian = 0x42;
final byte littleEndian = 0x6C;
switch (order) {
default:
throw new EncoderException("unknown byte order");
case BIG_ENDIAN:
buffer.writeByte(bigEndian);
break;
case LITTLE_ENDIAN:
buffer.writeByte(littleEndian);
break;
default:
throw new EncoderException("unknown byte order");
}
return buffer;
}
Expand All @@ -61,13 +62,14 @@ private static ByteBuf encodeMessageType(final ByteBufAllocator allocator, final
final int capacity = 1;
final ByteBuf buffer = allocator.buffer(capacity, capacity);
switch (type) {
default:
throw new EncoderException("unknown message type");
case ERROR:
case METHOD_CALL:
case METHOD_RETURN:
case SIGNAL:
buffer.writeByte(type.getDecimalCode());
break;
default:
throw new EncoderException("unknown message type");
}
return buffer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@ static MessageType decodeType(final ByteBuf buffer) {
final byte ub = buffer.readByte();
final int decimalCode = Byte.toUnsignedInt(ub);
final MessageType messageType = MessageType.fromDecimalCode(decimalCode);
if (messageType == null) {
return MessageType.INVALID;
} else {
return messageType;
}
return Objects.requireNonNullElse(messageType, MessageType.INVALID);
}

static Set<MessageFlag> decodeFlags(final ByteBuf buffer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class SaslStringEncoder extends MessageToByteEncoder<String> {
@Override
protected void encode(final ChannelHandlerContext ctx, final String msg, final ByteBuf out) {
LoggerUtils.trace(LOGGER, MARKER, () -> "Encoding SASL string to bytes.");
if (msg.length() != 0) {
if (!msg.isEmpty()) {
final CharBuffer charBuffer = CharBuffer.wrap(msg + CRLF);
final ByteBuf byteBuffer = ByteBufUtil.encodeString(ctx.alloc(), charBuffer, StandardCharsets.US_ASCII);
out.writeBytes(byteBuffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ static <R extends DBusBasicType> DecoderResult<R> decodeBasicType(final TypeCode
final int offset, final ByteOrder order)
throws DecoderException {
switch (code) {
default:
throw new DecoderException("Unsupported basic type: " + code);
case BOOLEAN:
return (DecoderResult<R>) new BooleanDecoder(order).decode(buffer, offset);
case BYTE:
Expand All @@ -146,6 +144,8 @@ static <R extends DBusBasicType> DecoderResult<R> decodeBasicType(final TypeCode
return (DecoderResult<R>) new UInt64Decoder(order).decode(buffer, offset);
case UNIX_FD:
return (DecoderResult<R>) new UnixFdDecoder(order).decode(buffer, offset);
default:
throw new DecoderException("Unsupported basic type: " + code);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ public EncoderResult<ByteBuf> encode(final DBusBoolean value, final int offset)
final int padding = EncoderUtils.applyPadding(buffer, offset, Type.BOOLEAN);
producedBytes += padding;
switch (order) {
default:
throw new Exception("Unknown byte order");
case BIG_ENDIAN:
buffer.writeInt(value.getDelegate() ? 1 : 0);
break;
case LITTLE_ENDIAN:
buffer.writeIntLE(value.getDelegate() ? 1 : 0);
break;
default:
throw new Exception("Unknown byte order");
}
producedBytes += TYPE_SIZE;
final EncoderResult<ByteBuf> result = new EncoderResultImpl<>(producedBytes, buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ public EncoderResult<ByteBuf> encode(final DBusDouble value, final int offset) t
try {
final int padding = EncoderUtils.applyPadding(buffer, offset, Type.DOUBLE);
switch (order) {
default:
throw new Exception("unknown byte order");
case BIG_ENDIAN:
buffer.writeDouble(value.getDelegate());
break;
case LITTLE_ENDIAN:
buffer.writeDoubleLE(value.getDelegate());
break;
default:
throw new Exception("unknown byte order");
}
final int producedBytes = TYPE_SIZE + padding;
final EncoderResult<ByteBuf> result = new EncoderResultImpl<>(producedBytes, buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ public EncoderResult<ByteBuf> encode(final Int16 value, final int offset) throws
final int padding = EncoderUtils.applyPadding(buffer, offset, Type.INT16);
producedBytes += padding;
switch (order) {
default:
throw new Exception("unknown byte order");
case BIG_ENDIAN:
buffer.writeShort(value.getDelegate());
break;
case LITTLE_ENDIAN:
buffer.writeShortLE(value.getDelegate());
break;
default:
throw new Exception("unknown byte order");
}
producedBytes += TYPE_SIZE;
final EncoderResult<ByteBuf> result = new EncoderResultImpl<>(producedBytes, buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ public EncoderResult<ByteBuf> encode(final Int32 value, final int offset) throws
final int padding = EncoderUtils.applyPadding(buffer, offset, Type.INT32);
producedBytes += padding;
switch (order) {
default:
throw new Exception("unknown byte order");
case BIG_ENDIAN:
buffer.writeInt(value.getDelegate());
break;
case LITTLE_ENDIAN:
buffer.writeIntLE(value.getDelegate());
break;
default:
throw new Exception("unknown byte order");
}
producedBytes += TYPE_SIZE;
final EncoderResult<ByteBuf> result = new EncoderResultImpl<>(producedBytes, buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ public EncoderResult<ByteBuf> encode(final Int64 value, final int offset) throws
final int padding = EncoderUtils.applyPadding(buffer, offset, Type.INT64);
producedBytes += padding;
switch (order) {
default:
throw new Exception("unknown byte order");
case BIG_ENDIAN:
buffer.writeLong(value.getDelegate());
break;
case LITTLE_ENDIAN:
buffer.writeLongLE(value.getDelegate());
break;
default:
throw new Exception("unknown byte order");
}
producedBytes += TYPE_SIZE;
final EncoderResult<ByteBuf> result = new EncoderResultImpl<>(producedBytes, buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ public EncoderResult<ByteBuf> encode(final UInt16 value, final int offset) throw
try {
final int padding = EncoderUtils.applyPadding(buffer, offset, Type.UINT16);
switch (order) {
default:
throw new Exception("unknown byte order");
case BIG_ENDIAN:
buffer.writeShort(value.getDelegate());
break;
case LITTLE_ENDIAN:
buffer.writeShortLE(value.getDelegate());
break;
default:
throw new Exception("unknown byte order");
}
final int producedBytes = padding + TYPE_SIZE;
final EncoderResult<ByteBuf> result = new EncoderResultImpl<>(producedBytes, buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ public EncoderResult<ByteBuf> encode(final UInt32 value, final int offset) throw
try {
final int padding = EncoderUtils.applyPadding(buffer, offset, Type.UINT32);
switch (order) {
default:
throw new Exception("unknown byte order");
case BIG_ENDIAN:
buffer.writeInt(value.getDelegate());
break;
case LITTLE_ENDIAN:
buffer.writeIntLE(value.getDelegate());
break;
default:
throw new Exception("unknown byte order");
}
final int producedBytes = padding + TYPE_SIZE;
final EncoderResult<ByteBuf> result = new EncoderResultImpl<>(producedBytes, buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ public EncoderResult<ByteBuf> encode(final UInt64 value, final int offset) throw
try {
final int padding = EncoderUtils.applyPadding(buffer, offset, Type.UINT64);
switch (order) {
default:
throw new Exception("unknown byte order");
case BIG_ENDIAN:
buffer.writeLong(value.getDelegate());
break;
case LITTLE_ENDIAN:
buffer.writeLongLE(value.getDelegate());
break;
default:
throw new Exception("unknown byte order");
}
final int producedBytes = padding + TYPE_SIZE;
final EncoderResult<ByteBuf> result = new EncoderResultImpl<>(producedBytes, buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,14 @@ public EncoderResult<ByteBuf> encode(final UnixFd value, final int offset)
try {
final int padding = EncoderUtils.applyPadding(buffer, offset, Type.UNIX_FD);
switch (order) {
default:
throw new Exception("unknown byte order");
case BIG_ENDIAN:
buffer.writeInt(value.getDelegate());
break;
case LITTLE_ENDIAN:
buffer.writeIntLE(value.getDelegate());
break;
default:
throw new Exception("unknown byte order");
}
final int producedBytes = padding + TYPE_SIZE;
final EncoderResult<ByteBuf> result = new EncoderResultImpl<>(producedBytes, buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
package com.lucimber.dbus.standard;

import com.lucimber.dbus.exception.AccessDeniedException;
import com.lucimber.dbus.exception.NotSupportedException;
import com.lucimber.dbus.exception.PropertyReadOnlyException;
import com.lucimber.dbus.exception.UnknownInterfaceException;
import com.lucimber.dbus.exception.UnknownPropertyException;
import com.lucimber.dbus.type.DBusString;
import com.lucimber.dbus.type.Dict;
import com.lucimber.dbus.type.Variant;

import java.util.Optional;

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/src/main/java/com/lucimber/dbus/type/Dict.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public Set<Entry<KeyT, ValueT>> entrySet() {
*/
public Set<DictEntry<KeyT, ValueT>> dictionaryEntrySet() {
final Signature subSig = signature.subContainer();
return delegate.entrySet().stream().sequential()
return delegate.entrySet().stream()
.map(e -> new DictEntry<>(subSig, e.getKey(), e.getValue()))
.collect(Collectors.toSet());
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/main/java/com/lucimber/dbus/type/Signature.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private static Node parseStructEndTypeCode(final Node node, final int idx) {
+ " Missing opening parenthesis of struct.";
throw new SignatureException(String.format(msg, idx));
}
if (node.children == null || node.children.size() == 0) {
if (node.children == null || node.children.isEmpty()) {
final String msg = "Error at position %d."
+ " Empty structures are not allowed.";
throw new SignatureException(String.format(msg, idx));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@

package com.lucimber.dbus.impl.netty.connection;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.lucimber.dbus.connection.Handler;
import com.lucimber.dbus.connection.HandlerContext;
import com.lucimber.dbus.connection.PipelineFactory;
Expand All @@ -25,6 +22,8 @@
import java.util.concurrent.atomic.AtomicReference;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

final class ConnectionWriteOutboundMessageTest {

private static final int TIMEOUT_IN_SECONDS = 5;
Expand Down Expand Up @@ -67,7 +66,7 @@ public void onInboundMessage(final HandlerContext ctx, final InboundMessage msg)
// Verify method return
countDownLatch.await(TIMEOUT_IN_SECONDS, TimeUnit.SECONDS);
final InboundMessage inboundMessage = atomicInboundMessage.get();
assertTrue(inboundMessage instanceof InboundMethodReturn);
assertInstanceOf(InboundMethodReturn.class, inboundMessage);
final InboundMethodReturn methodReturn = (InboundMethodReturn) inboundMessage;
assertEquals(destination, methodReturn.getSender(), "Sender");
assertEquals(methodCall.getSerial(), methodReturn.getReplySerial(), "Serial number");
Expand Down Expand Up @@ -115,7 +114,7 @@ public void onInboundMessage(final HandlerContext ctx, final InboundMessage msg)
// Verify method return
countDownLatch.await(TIMEOUT_IN_SECONDS, TimeUnit.SECONDS);
final InboundMessage inboundMessage = atomicInboundMessage.get();
assertTrue(inboundMessage instanceof InboundError);
assertInstanceOf(InboundError.class, inboundMessage);
final InboundError inboundError = (InboundError) inboundMessage;
assertEquals(destination, inboundError.getSender(), "Sender");
assertEquals(methodCall.getSerial(), inboundError.getReplySerial());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ final class StringDecoderTest {
void decodeString(final ByteOrder byteOrder) {
final ByteBuf buffer = Unpooled.buffer();
// UINT32 bytes
final byte[] lengthBytes;
if (byteOrder == ByteOrder.BIG_ENDIAN) {
final byte[] lengthBytes = {0x00, 0x00, 0x00, 0x07};
buffer.writeBytes(lengthBytes);
lengthBytes = new byte[]{0x00, 0x00, 0x00, 0x07};
} else {
final byte[] lengthBytes = {0x07, 0x00, 0x00, 0x00};
buffer.writeBytes(lengthBytes);
lengthBytes = new byte[]{0x07, 0x00, 0x00, 0x00};
}

buffer.writeBytes(lengthBytes);
// UTF-8 bytes (7 bytes)
buffer.writeBytes(VALID_STRING.getBytes(StandardCharsets.UTF_8));
// Trailing NUL byte
Expand All @@ -52,13 +51,13 @@ void decodeString(final ByteOrder byteOrder) {
void failDueToIndexLimitation(final ByteOrder byteOrder) {
final ByteBuf buffer = Unpooled.buffer();
// UINT32 bytes (Integer.MAX_VALUE + 1 = 2147483648)
final byte[] lengthBytes;
if (byteOrder == ByteOrder.BIG_ENDIAN) {
final byte[] lengthBytes = {(byte) 0x80, 0x00, 0x00, 0x00};
buffer.writeBytes(lengthBytes);
lengthBytes = new byte[]{(byte) 0x80, 0x00, 0x00, 0x00};
} else {
final byte[] lengthBytes = {0x00, 0x00, 0x00, (byte) 0x80};
buffer.writeBytes(lengthBytes);
lengthBytes = new byte[]{0x00, 0x00, 0x00, (byte) 0x80};
}
buffer.writeBytes(lengthBytes);
// UTF-8 bytes (7 bytes)
buffer.writeBytes(VALID_STRING.getBytes(StandardCharsets.UTF_8));
// Trailing NUL byte
Expand Down

0 comments on commit fca0ada

Please sign in to comment.