Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance PMD rules #2753

Merged
merged 10 commits into from
Dec 4, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* CountDownLatchBenchmark.getWithShield thrpt 5 452938674.099 ± 8088381.004 ops/s
* CountDownLatchBenchmark.getWithoutShield thrpt 5 340168846.799 ± 4901839.746 ops/s
*/
@Fork(value = 1)
@Fork(1)
@State(Scope.Benchmark)
@Warmup(iterations = 5, time = 3)
@Measurement(iterations = 5, time = 3)
Expand All @@ -44,7 +44,7 @@ public class CountDownLatchBenchmark {

private static final Object NULL = new Object();

private CountDownLatch latch = new CountDownLatch(0);
private final CountDownLatch latch = new CountDownLatch(0);

@Nullable
private volatile Object value = NULL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
public final class AsyncContextFriendlyExecutor implements ExecutorService {
private final ExecutorService executor;

@SuppressWarnings("PMD.UnusedFormalParameter")
public AsyncContextFriendlyExecutor(int maxThread, String prefix) {
executor = Executors.newCachedThreadPool(new DefaultThreadFactory(prefix, false, NORM_PRIORITY));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;

@Fork(value = 1)
@Fork(1)
@State(Scope.Benchmark)
@Warmup(iterations = 5, time = 3)
@Measurement(iterations = 5, time = 3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

import javax.annotation.Nullable;

@Fork(value = 1)
@Fork(1)
@State(Scope.Benchmark)
@Warmup(iterations = 5, time = 3)
@Measurement(iterations = 5, time = 3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
* group:requestN 1000000 false thrpt 3 93466190.240 ± 24680358.208 ops/s
* group:write 1000000 false thrpt 3 1675.382 ± 1185.913 ops/s * </pre>
*/
@Fork(value = 1)
@Fork(1)
@State(Scope.Benchmark)
@Warmup(iterations = 2, time = 3)
@Measurement(iterations = 3, time = 5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
* ofStringConstantLookup PATCH thrpt 5 145321385.794 ± 4382738.681 ops/s
* ofStringConstantLookup UNKNOWN thrpt 5 189249897.729 ± 10985411.431 ops/s
*/
@Fork(value = 1)
@Fork(1)
@State(Scope.Benchmark)
@Warmup(iterations = 5, time = 3)
@Measurement(iterations = 5, time = 3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
* javaParseLongAsciiString 8192 thrpt 5 75324292.593 ± 592329.213 ops/s
* stParseLongAsciiString 8192 thrpt 5 80748587.669 ± 1076241.647 ops/s
*/
@Fork(value = 1)
@Fork(1)
@State(Scope.Benchmark)
@Warmup(iterations = 5, time = 3)
@Measurement(iterations = 5, time = 3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
* JacksonStreamingSerializerBenchmark.deserializeSmallBackedByArray thrpt 5 1092601,832 ± 3884,520 ops/s
* JacksonStreamingSerializerBenchmark.deserializeSmallBackedByDirect thrpt 5 727287,804 ± 2357,798 ops/s
*/
@Fork(value = 1)
@Fork(1)
@State(Scope.Benchmark)
@Warmup(iterations = 5, time = 3)
@Measurement(iterations = 5, time = 3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* HttpResponseStatusBenchmark.toStringConcatenation thrpt 5 63931127.209 ± 1936778.214 ops/s
* HttpResponseStatusBenchmark.toStringConvertBuffer thrpt 5 21250378.081 ± 234963.626 ops/s
*/
@Fork(value = 1)
@Fork(1)
@State(Scope.Benchmark)
@Warmup(iterations = 5, time = 5)
@Measurement(iterations = 5, time = 10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import static io.servicetalk.http.api.HttpProtocolVersion.HTTP_1_1;
import static io.servicetalk.http.api.HttpRequestMethod.GET;

@Fork(value = 1)
@Fork(1)
@State(Scope.Benchmark)
@Warmup(iterations = 5, time = 5)
@Measurement(iterations = 5, time = 5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import static io.servicetalk.http.api.HttpRequestMethod.GET;
import static java.nio.charset.StandardCharsets.UTF_8;

@Fork(value = 1)
@Fork(1)
@State(Scope.Benchmark)
@Warmup(iterations = 5, time = 5)
@Measurement(iterations = 5, time = 5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
* HttpResponseDecoderBenchmark.initialLine 600 thrpt 5 965798.581 ± 13396.336 ops/s
* HttpResponseDecoderBenchmark.initialLine 700 thrpt 5 811948.162 ± 14176.711 ops/s
*/
@Fork(value = 1)
@Fork(1)
@State(Scope.Benchmark)
@Warmup(iterations = 5, time = 3)
@Measurement(iterations = 5, time = 3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
* Benchmark Mode Cnt Score Error Units
* HttpResponseEncoderBenchmarkFullResponse.fullResponse thrpt 5 669406.100 ± 6113.671 ops/s
*/
@Fork(value = 1)
@Fork(1)
@State(Scope.Benchmark)
@Warmup(iterations = 5, time = 5)
@Measurement(iterations = 5, time = 10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
* HttpResponseEncoderInitialLineBenchmark.initialLine 600 thrpt 5 1271252.139 ± 17791.329 ops/s
* HttpResponseEncoderInitialLineBenchmark.initialLine 700 thrpt 5 1220163.698 ± 43243.342 ops/s
*/
@Fork(value = 1)
@Fork(1)
@State(Scope.Benchmark)
@Warmup(iterations = 5, time = 3)
@Measurement(iterations = 5, time = 3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
import static io.servicetalk.concurrent.Cancellable.IGNORE_CANCEL;
import static io.servicetalk.concurrent.api.SourceAdapters.toSource;

@Fork(value = 1)
@Fork(1)
@State(Scope.Benchmark)
@Warmup(iterations = 5, time = 3)
@Measurement(iterations = 5, time = 3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ public boolean process(final byte value) {
if (!sign) {
sign = true;
negative = value == '-';
if ((negative || value == '+')) {
if (negative || value == '+') {
if (buffer.readableBytes() == 1) {
throw illegalInput(buffer);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

final class UnreleasableReadOnlyByteBufferBuf extends AbstractReferenceCountedByteBuf {

protected final ByteBuffer buffer;
private final ByteBuffer buffer;
private final ByteBufAllocator allocator;
@Nullable
private ByteBuffer tmpNioBuf;
Expand Down Expand Up @@ -477,8 +477,9 @@ public ByteBuf copy(int index, int length) {
ByteBuffer src;
try {
src = (ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length);
} catch (IllegalArgumentException ignored) {
throw new IndexOutOfBoundsException("Too many bytes to read - Need " + (index + length));
} catch (IllegalArgumentException cause) {
throw (IndexOutOfBoundsException) new IndexOutOfBoundsException(
"Too many bytes to read - Need " + (index + length)).initCause(cause);
}

ByteBuf dst = src.isDirect() ? alloc().directBuffer(length) : alloc().heapBuffer(length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void writeBytesInputStreamZeroLength(boolean write) throws IOException {
}

@ParameterizedTest(name = "{displayName} [{index}] heapBuffer={0} limitRead={1} write={2}")
@CsvSource(value = {"false,false,false", "false,false,true", "false,true,false", "false,true,true",
@CsvSource({"false,false,false", "false,false,true", "false,true,false", "false,true,true",
"true,false,false", "true,false,true", "true,true,false", "true,true,true"})
void writeBytesInputStreamExactLength(boolean heapBuffer, boolean limitRead, boolean write) throws IOException {
Buffer buffer = buffer(heapBuffer);
Expand All @@ -65,7 +65,7 @@ void writeBytesInputStreamExactLength(boolean heapBuffer, boolean limitRead, boo
}

@ParameterizedTest(name = "{displayName} [{index}] heapBuffer={0} limitRead={1} write={2}")
@CsvSource(value = {"false,false,false", "false,false,true", "false,true,false", "false,true,true",
@CsvSource({"false,false,false", "false,false,true", "false,true,false", "false,true,true",
"true,false,false", "true,false,true", "true,true,false", "true,true,true"})
void writeBytesInputStreamHalfAvailable(boolean heapBuffer, boolean limitRead, boolean write) throws IOException {
Buffer buffer = buffer(heapBuffer);
Expand All @@ -76,7 +76,7 @@ void writeBytesInputStreamHalfAvailable(boolean heapBuffer, boolean limitRead, b
}

@ParameterizedTest(name = "{displayName} [{index}] heapBuffer={0} limitRead={1} write={2}")
@CsvSource(value = {"false,false,false", "false,false,true", "false,true,false", "false,true,true",
@CsvSource({"false,false,false", "false,false,true", "false,true,false", "false,true,true",
"true,false,false", "true,false,true", "true,true,false", "true,true,true"})
void writeBytesInputStreamDoubleLength(boolean heapBuffer, boolean limitRead, boolean write) throws IOException {
Buffer buffer = buffer(heapBuffer);
Expand All @@ -100,7 +100,7 @@ private static void writeOrSetBytes(Buffer buffer, InputStream is, int length, b
}

@ParameterizedTest(name = "{displayName} [{index}] heapBuffer={0} limitRead={1} write={2}")
@CsvSource(value = {"false,false,false", "false,false,true", "false,true,false", "false,true,true",
@CsvSource({"false,false,false", "false,false,true", "false,true,false", "false,true,true",
"true,false,false", "true,false,true", "true,true,false", "true,true,true"})
void writeBytesUntilEndStream(boolean heapBuffer, boolean limitRead, boolean write) throws IOException {
Buffer buffer = buffer(heapBuffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ private static void heapSort(Object[] keyValueArray) {
Key childKey = (Key) keyValueArray[childKeyIndex];
final int rightChildKeyIndex = (keyIndex << 1) + 4;
final Key rightChildKey;
if (rightChildKeyIndex <= endKeyIndex && childKey.compareTo((rightChildKey =
(Key) keyValueArray[rightChildKeyIndex])) < 0) {
if (rightChildKeyIndex <= endKeyIndex && childKey.compareTo(rightChildKey =
(Key) keyValueArray[rightChildKeyIndex]) < 0) {
childKey = rightChildKey;
childKeyIndex = rightChildKeyIndex;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

class PowerSetPartitionMapTest {
private static final Key<Integer> DC_ID = Key.newKey("dc");
Expand Down Expand Up @@ -230,12 +230,7 @@ void testResolveFourElements() {
void testAddEmptyPartitionAttributesThrows() {
PowerSetPartitionMap<ListenableAsyncCloseable> map = new PowerSetPartitionMap<>(address -> VALUE);
PartitionAttributes emptyAttributes = new DefaultPartitionAttributesBuilder(0).build();
try {
map.add(emptyAttributes);
fail();
} catch (IllegalArgumentException expected) {
// expected
}
assertThrows(IllegalArgumentException.class, () -> map.add(emptyAttributes));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.junit.jupiter.api.Test;

import static io.servicetalk.concurrent.api.AsyncCloseables.emptyAsyncCloseable;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertThrows;

class DefaultClientGroupTest {

Expand All @@ -30,18 +30,8 @@ void requestingClientFromClosedClientGroupShouldNotHang() throws Exception {
new DefaultClientGroup<>(s -> emptyAsyncCloseable());
cg.closeAsync().toFuture().get();

try {
cg.get("foo");
fail("ClientGroup is closed, cg.get() should throw");
} catch (IllegalStateException e) {
// Expected
}

try {
cg.get("foo"); // Ensure this doesn't hang
fail("ClientGroup is closed, cg.get() should throw");
} catch (IllegalStateException e) {
// Expected
}
assertThrows(IllegalStateException.class, () -> cg.get("foo"));
// Ensure this doesn't hang
assertThrows(IllegalStateException.class, () -> cg.get("foo"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ private void processClosed() throws IOException {

private void processClosed(TerminalNotification currClosed) throws IOException {
Object currState = stateUpdater.getAndSet(this, State.TERMINATED);
if (currState instanceof Subscriber && currClosed.cause() != ConnectedPublisher.CONNECTED_PUBLISHER_CANCELLED) {
if (currState instanceof Subscriber &&
!ConnectedPublisher.CONNECTED_PUBLISHER_CANCELLED.equals(currClosed.cause())) {
currClosed.terminate((Subscriber<?>) currState);
}
throw newAlreadyClosed(currClosed.cause());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,12 +403,7 @@ public void onComplete() {
} catch (RuntimeException cause) {
assertSame(DELIBERATE_EXCEPTION, cause);
}
try {
cbos.flush();
fail();
} catch (IOException ignored) {
// expected
}
assertThrows(IOException.class, () -> cbos.flush());
cbos.close();
onError.await();
toSource(cbos.connect()).subscribe(subscriber);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,12 +407,7 @@ public void onComplete() {
} catch (RuntimeException cause) {
assertSame(DELIBERATE_EXCEPTION, cause);
}
try {
cpw.flush();
fail();
} catch (IOException ignored) {
// expected
}
assertThrows(IOException.class, () -> cpw.flush());
cpw.close();
onError.await();
toSource(cpw.connect()).subscribe(subscriber);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void onNext(@Nullable T t) {
event = checkNoSignalsExpectation("onNext", t, event);
if (event instanceof OnNextEvent) {
@SuppressWarnings("unchecked")
final OnNextEvent<T> castEvent = ((OnNextEvent<T>) event);
final OnNextEvent<T> castEvent = (OnNextEvent<T>) event;
try {
castEvent.onNext(t);
event = pollNextEvent();
Expand All @@ -172,7 +172,7 @@ public void onNext(@Nullable T t) {
}
} else if (event instanceof OnNextAggregateEvent) {
@SuppressWarnings("unchecked")
final OnNextAggregateEvent<T> castEvent = ((OnNextAggregateEvent<T>) event);
final OnNextAggregateEvent<T> castEvent = (OnNextAggregateEvent<T>) event;
assert currAggregateSignals != null;
currAggregateSignals.add(t);
if (currAggregateSignals.size() == castEvent.maxOnNext()) {
Expand Down Expand Up @@ -274,7 +274,7 @@ private PublisherEvent currEvent() {
private PublisherEvent checkOnNextEventsFromTerminal(String signalName, @Nullable PublisherEvent event) {
if (event instanceof OnNextAggregateEvent) {
@SuppressWarnings("unchecked")
final OnNextAggregateEvent<T> castEvent = ((OnNextAggregateEvent<T>) event);
final OnNextAggregateEvent<T> castEvent = (OnNextAggregateEvent<T>) event;
assert currAggregateSignals != null;
if (currAggregateSignals.size() >= castEvent.minOnNext()) {
try {
Expand Down Expand Up @@ -306,7 +306,7 @@ private PublisherEvent checkOnNextEventsFromTerminal(String signalName, @Nullabl
private PublisherEvent pollNextEvent() {
Queue<VerifyThreadEvent> verifyThreadEvents = null;
int subscriptionBeginIndex = -1;
do {
while (true) {
final int eventIndex = eventIndexUpdater.incrementAndGet(this);
if (eventIndex >= events.size()) {
// try to execute any remaining events before completing
Expand Down Expand Up @@ -363,7 +363,7 @@ private PublisherEvent pollNextEvent() {
}
break;
}
} while (true);
}

processVerifyThreadAggregate(verifyThreadEvents);

Expand Down Expand Up @@ -512,6 +512,7 @@ static final class OnNextIterableEvent<T> extends PublisherEvent {
this.iterable = requireNonNull(iterable);
}

@Override
String description() {
return "expectNext(" + iterable + ")";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public final Duration verify(Duration duration) throws AssertionError {
return timeSource.timeElapsed(startTime);
}

private void processRuntimeException(RuntimeException t) {
private static void processRuntimeException(RuntimeException t) {
Throwable cause = t.getCause();
if (cause instanceof AssertionError) {
throw (AssertionError) cause;
Expand Down Expand Up @@ -143,6 +143,7 @@ static <T> boolean notEqualsOnNext(@Nullable final T expected, @Nullable final T
abstract String description();

static final class StepAssertionError extends AssertionError {
private static final long serialVersionUID = -5220083330558844374L;
private final PublisherEvent event;

private StepAssertionError(PublisherEvent event, String message, Throwable cause) {
Expand Down Expand Up @@ -188,7 +189,7 @@ private static StackTraceElement[] filterStackTrace(String stepClassName, StackT
public String toString() {
String s = StepAssertionError.class.getSimpleName();
String message = getLocalizedMessage();
return (message != null) ? (s + ": " + message) : s;
return message != null ? (s + ": " + message) : s;
}
}
}
Expand Down
Loading
Loading