|
3 | 3 | import org.approvaltests.reporters.DiffInfo; |
4 | 4 | import org.approvaltests.reporters.GenericDiffReporter; |
5 | 5 |
|
6 | | -import java.lang.ProcessHandle; |
| 6 | +import java.lang.reflect.Method; |
7 | 7 | import java.util.HashSet; |
8 | 8 | import java.util.Optional; |
9 | 9 | import java.util.Set; |
@@ -35,8 +35,45 @@ public static String findJetBrainsIdes() |
35 | 35 | } |
36 | 36 | private static String[] getRunningPrograms() |
37 | 37 | { |
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 | + } |
40 | 77 | } |
41 | 78 | public static String findJetBrainsIdes(String[] commands) |
42 | 79 | { |
|
0 commit comments