Skip to content

Commit 2d0bbaa

Browse files
JayBazuzistagg54isidore
committed
- r using reflection for process for java 8
Co-Authored-By: Samuel Taggart <[email protected]> Co-Authored-By: Llewellyn Falco <[email protected]>
1 parent a8dcaf1 commit 2d0bbaa

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

approvaltests/src/main/java/org/approvaltests/reporters/intellij/IntelliJReporter.java

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import org.approvaltests.reporters.DiffInfo;
44
import org.approvaltests.reporters.GenericDiffReporter;
55

6-
import java.lang.ProcessHandle;
6+
import java.lang.reflect.Method;
77
import java.util.HashSet;
88
import java.util.Optional;
99
import java.util.Set;
@@ -35,8 +35,45 @@ public static String findJetBrainsIdes()
3535
}
3636
private static String[] getRunningPrograms()
3737
{
38-
Stream<Optional<String>> processes = ProcessHandle.allProcesses().map(p -> p.info().command());
39-
return processes.filter(Optional::isPresent).map(c -> c.get()).toArray(String[]::new);
38+
try
39+
{
40+
// Use reflection to support Java 8 (ProcessHandle is Java 9+)
41+
Class<?> processHandleClass = Class.forName("java.lang.ProcessHandle");
42+
Method allProcessesMethod = processHandleClass.getMethod("allProcesses");
43+
Method infoMethod = processHandleClass.getMethod("info");
44+
// Get the ProcessHandle.Info interface to access the command() method
45+
Class<?> processInfoClass = Class.forName("java.lang.ProcessHandle$Info");
46+
Method commandMethod = processInfoClass.getMethod("command");
47+
@SuppressWarnings("unchecked")
48+
Stream<Object> processHandles = (Stream<Object>) allProcessesMethod.invoke(null);
49+
try
50+
{
51+
Stream<Optional<String>> processes = processHandles.map(p -> {
52+
try
53+
{
54+
Object processInfo = infoMethod.invoke(p);
55+
@SuppressWarnings("unchecked")
56+
Optional<String> command = (Optional<String>) commandMethod.invoke(processInfo);
57+
return command;
58+
}
59+
catch (Exception e)
60+
{
61+
System.out.println("Failed to get command for process: " + e.getMessage());
62+
return Optional.<String> empty();
63+
}
64+
});
65+
return processes.filter(Optional::isPresent).map(c -> c.get()).toArray(String[]::new);
66+
}
67+
finally
68+
{
69+
processHandles.close();
70+
}
71+
}
72+
catch (Exception e)
73+
{
74+
System.out.println("Failed to get running programs: " + e.getMessage());
75+
return new String[0];
76+
}
4077
}
4178
public static String findJetBrainsIdes(String[] commands)
4279
{

0 commit comments

Comments
 (0)