This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-vfs.git

commit ffc3b01ac823ba536bb3d270ac8c364c019f8e9f
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Thu Mar 27 12:39:10 2025 -0400

    Better test feedback on Java 21 and up
---
 .../org/apache/commons/vfs2/AbstractTestSuite.java   | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git 
a/commons-vfs2/src/test/java/org/apache/commons/vfs2/AbstractTestSuite.java 
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/AbstractTestSuite.java
index cea7489d9..12166fb7c 100644
--- a/commons-vfs2/src/test/java/org/apache/commons/vfs2/AbstractTestSuite.java
+++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/AbstractTestSuite.java
@@ -36,7 +36,9 @@ import junit.framework.TestSuite;
 
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang3.ArrayUtils;
+import org.apache.commons.lang3.ClassUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.reflect.FieldUtils;
 import org.apache.commons.vfs2.impl.DefaultFileReplicator;
 import org.apache.commons.vfs2.impl.DefaultFileSystemManager;
 import org.apache.commons.vfs2.impl.PrivilegedFileReplicator;
@@ -171,15 +173,23 @@ public abstract class AbstractTestSuite extends TestSetup 
{
         final StringBuilder sb = new StringBuilder(256);
         sb.append("Threads still running (" + threadSnapshot.length + ") at " 
+ Instant.now() + ", live threads:");
         sb.append(System.lineSeparator());
-
         Field threadTargetField = null;
         try {
-            threadTargetField = Thread.class.getDeclaredField("target");
-            threadTargetField.setAccessible(true);
+            threadTargetField = FieldUtils.getDeclaredField(Thread.class, 
"target", true);
+            if (threadTargetField == null) {
+                // Java 21 and up
+                threadTargetField = FieldUtils.getDeclaredField(Thread.class, 
"holder", true);
+                if (threadTargetField == null) {
+                    System.err.println("Test suite cannot show you a thread 
snapshot (Thread.holder)");
+                }
+                threadTargetField = 
FieldUtils.getDeclaredField(threadTargetField.getClass(), "task", true);
+                if (threadTargetField == null) {
+                    System.err.println("Test suite cannot show you a thread 
snapshot (Thread.holder.task)");
+                }
+            }
         } catch (final Exception e) {
             System.err.println("Test suite cannot show you a thread snapshot: 
" + e);
         }
-
         int liveCount = 0;
         for (int index = 0; index < threadSnapshot.length; index++) {
             final Thread thread = threadSnapshot[index];
@@ -200,7 +210,6 @@ public abstract class AbstractTestSuite extends TestSetup {
                     sb.append("non_");
                 }
                 sb.append("daemon");
-
                 if (threadTargetField != null) {
                     sb.append(",\t");
                     try {
@@ -216,7 +225,6 @@ public abstract class AbstractTestSuite extends TestSetup {
                         sb.append(")");
                     }
                 }
-
                 sb.append(System.lineSeparator());
 //              Stream.of(thread.getStackTrace()).forEach(e -> {
 //                  sb.append('\t');

Reply via email to