Skip to content

Commit f4ac7cc

Browse files
committed
Clean up warnings and polish
1 parent 3c0c32f commit f4ac7cc

File tree

5 files changed

+34
-33
lines changed

5 files changed

+34
-33
lines changed

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/AbstractExtensionContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private NamespacedHierarchicalStore.CloseAction<org.junit.platform.engine.suppor
9595
return (__, ___, value) -> {
9696
boolean isAutoCloseEnabled = this.configuration.isClosingStoredAutoCloseablesEnabled();
9797

98-
if (value instanceof AutoCloseable closeable && isAutoCloseEnabled) {
98+
if (value instanceof @SuppressWarnings("resource") AutoCloseable closeable && isAutoCloseEnabled) {
9999
closeable.close();
100100
return;
101101
}

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/execution/JupiterEngineExecutionContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private JupiterEngineExecutionContext(State state) {
4747

4848
public void close() throws Exception {
4949
ExtensionContext extensionContext = getExtensionContext();
50-
if (extensionContext instanceof AutoCloseable closeable) {
50+
if (extensionContext instanceof @SuppressWarnings("resource") AutoCloseable closeable) {
5151
try {
5252
closeable.close();
5353
}

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/AutoCloseExtension.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ public void afterAll(ExtensionContext context) {
6262

6363
private static void closeFields(Class<?> testClass, @Nullable Object testInstance,
6464
ThrowableCollector throwableCollector) {
65+
6566
Predicate<Field> predicate = (testInstance == null ? ModifierSupport::isStatic : ModifierSupport::isNotStatic);
66-
AnnotationSupport.findAnnotatedFields(testClass, AutoClose.class, predicate, BOTTOM_UP).forEach(
67-
field -> throwableCollector.execute(() -> closeField(field, testInstance)));
67+
AnnotationSupport.findAnnotatedFields(testClass, AutoClose.class, predicate, BOTTOM_UP)//
68+
.forEach(field -> throwableCollector.execute(() -> closeField(field, testInstance)));
6869
}
6970

7071
private static void closeField(Field field, @Nullable Object testInstance) throws Exception {
@@ -86,16 +87,16 @@ private static void closeField(Field field, @Nullable Object testInstance) throw
8687

8788
private static void invokeCloseMethod(Field field, Object target, String methodName) throws Exception {
8889
// Avoid reflection if we can directly invoke close() via AutoCloseable.
89-
if (target instanceof AutoCloseable closeable && "close".equals(methodName)) {
90+
if (target instanceof @SuppressWarnings("resource") AutoCloseable closeable && "close".equals(methodName)) {
9091
closeable.close();
9192
return;
9293
}
9394

9495
Class<?> targetType = target.getClass();
9596
Method closeMethod = ReflectionSupport.findMethod(targetType, methodName).orElseThrow(
9697
() -> new ExtensionConfigurationException(
97-
"Cannot @AutoClose field %s because %s does not define method %s().".formatted(getQualifiedName(field),
98-
targetType.getName(), methodName)));
98+
"Cannot @AutoClose field %s because %s does not define method %s()."//
99+
.formatted(getQualifiedName(field), targetType.getName(), methodName)));
99100

100101
closeMethod = ReflectionUtils.getInterfaceMethodIfPossible(closeMethod, targetType);
101102
ReflectionSupport.invokeMethod(closeMethod, target);

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/PreInterruptThreadDumpPrinter.java

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,23 @@
1818
import org.junit.jupiter.engine.Constants;
1919

2020
/**
21-
* The default implementation for {@link PreInterruptCallback},
22-
* which will print the stacks of all {@link Thread}s to {@code System.out}.
21+
* Default implementation of {@link PreInterruptCallback}, which prints the stacks
22+
* of all {@link Thread}s to {@code System.out}.
2323
*
24-
* <p>Note: This is disabled by default, and must be enabled with
25-
* {@link Constants#EXTENSIONS_TIMEOUT_THREAD_DUMP_ENABLED_PROPERTY_NAME}
24+
* <p>Note: This is disabled by default and must be enabled via
25+
* {@link Constants#EXTENSIONS_TIMEOUT_THREAD_DUMP_ENABLED_PROPERTY_NAME}.
2626
*
2727
* @since 5.12
2828
*/
2929
final class PreInterruptThreadDumpPrinter implements PreInterruptCallback {
30+
3031
private static final String NL = "\n";
3132

3233
@Override
3334
public void beforeThreadInterrupt(PreInterruptContext preInterruptContext, ExtensionContext extensionContext) {
34-
3535
Map<Thread, StackTraceElement[]> stackTraces = Thread.getAllStackTraces();
3636

37-
StringBuilder sb = new StringBuilder();
38-
sb.append("Thread ");
37+
StringBuilder sb = new StringBuilder("Thread ");
3938
appendThreadName(sb, preInterruptContext.getThreadToInterrupt());
4039
sb.append(" will be interrupted.");
4140
sb.append(NL);
@@ -48,10 +47,9 @@ public void beforeThreadInterrupt(PreInterruptContext preInterruptContext, Exten
4847
appendThreadName(sb, thread);
4948
for (StackTraceElement stackTraceElement : stack) {
5049
sb.append(NL);
51-
//Do the same prefix as java.lang.Throwable.printStackTrace(java.lang.Throwable.PrintStreamOrWriter)
50+
// Use the same prefix as java.lang.Throwable.printStackTrace(PrintStreamOrWriter)
5251
sb.append("\tat ");
5352
sb.append(stackTraceElement.toString());
54-
5553
}
5654
sb.append(NL);
5755
}
@@ -61,23 +59,25 @@ public void beforeThreadInterrupt(PreInterruptContext preInterruptContext, Exten
6159
}
6260

6361
/**
64-
* Appends the {@link Thread} name and ID in a similar fashion as {@code jstack}.
65-
* @param sb the buffer
66-
* @param th the thread to append
62+
* Append the {@link Thread} name and ID in a similar fashion as {@code jstack}.
63+
* @param builder the builder to append to
64+
* @param thread the thread whose information should be appended
6765
*/
68-
private void appendThreadName(StringBuilder sb, Thread th) {
69-
// Use same format as java.lang.management.ThreadInfo.toString
70-
sb.append("\"");
71-
sb.append(th.getName());
72-
sb.append("\"");
73-
if (th.isDaemon()) {
74-
sb.append(" daemon");
66+
@SuppressWarnings("deprecation") // Thread.getId() is deprecated on JDK 19+
67+
private static void appendThreadName(StringBuilder builder, Thread thread) {
68+
// Use same format as java.lang.management.ThreadInfo.toString()
69+
builder.append("\"");
70+
builder.append(thread.getName());
71+
builder.append("\"");
72+
if (thread.isDaemon()) {
73+
builder.append(" daemon");
7574
}
76-
sb.append(" prio=");
77-
sb.append(th.getPriority());
78-
sb.append(" Id=");
79-
sb.append(th.getId());
80-
sb.append(" ");
81-
sb.append(th.getState());
75+
builder.append(" prio=");
76+
builder.append(thread.getPriority());
77+
builder.append(" Id=");
78+
builder.append(thread.getId());
79+
builder.append(" ");
80+
builder.append(thread.getState());
8281
}
82+
8383
}

junit-jupiter-migrationsupport/src/main/java/org/junit/jupiter/migrationsupport/rules/TestRuleSupport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ private List<TestRuleAnnotatedMember> getRuleAnnotatedMembers(ExtensionContext c
147147
Object testInstance = context.getRequiredTestInstance();
148148
Namespace namespace = Namespace.create(TestRuleSupport.class, context.getRequiredTestClass());
149149
// @formatter:off
150-
return new ArrayList<TestRuleAnnotatedMember>(requireNonNull(context.getStore(namespace)
150+
return new ArrayList<>(requireNonNull(context.getStore(namespace)
151151
.getOrComputeIfAbsent("rule-annotated-members", key -> findRuleAnnotatedMembers(testInstance), List.class)));
152152
// @formatter:on
153153
}

0 commit comments

Comments
 (0)