Skip to content

Commit

Permalink
Merge pull request #16518 from geoand/remove-process-reflection
Browse files Browse the repository at this point in the history
Remove unnecessary ProcessHandle related reflection now that we moved to Java 11
  • Loading branch information
geoand authored Apr 14, 2021
2 parents 3d6d3e9 + 29727c4 commit 18ce6c2
Showing 1 changed file with 9 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package io.quarkus.deployment.ide;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
Expand All @@ -16,15 +14,13 @@
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;

import org.jboss.logging.Logger;

import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
import io.quarkus.deployment.pkg.builditem.BuildSystemTargetBuildItem;
import io.quarkus.dev.spi.DevModeType;
import io.quarkus.runtime.util.JavaVersionUtil;

public class IdeProcessor {

Expand Down Expand Up @@ -183,40 +179,17 @@ private static class ProcessUtil {

/**
* Returns a list of running processes
* Only works for Java 11+
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List<ProcessInfo> runningProcesses() {
if (!JavaVersionUtil.isJava11OrHigher()) {
return Collections.emptyList();
}
// we can't use ProcessHandle directly as it is Java 9+, so we need to do reflection to the get the info we need
try {
Class processHandlerClass = Class.forName("java.lang.ProcessHandle");
Method allProcessesMethod = processHandlerClass.getMethod("allProcesses");
Method processHandleInfoMethod = processHandlerClass.getMethod("info");
Class processHandleInfoClass = Class.forName("java.lang.ProcessHandle$Info");
Method processHandleInfoCommandMethod = processHandleInfoClass.getMethod("command");
Method processHandleInfoArgumentsMethod = processHandleInfoClass.getMethod("arguments");
Stream<Object> allProcessesResult = (Stream<Object>) allProcessesMethod.invoke(null);
List<ProcessInfo> result = new ArrayList<>();
allProcessesResult.forEach(o -> {
try {
Object processHandleInfo = processHandleInfoMethod.invoke(o);
Optional<String> command = (Optional<String>) processHandleInfoCommandMethod.invoke(processHandleInfo);
if (command.isPresent()) {
Optional<String[]> arguments = (Optional<String[]>) processHandleInfoArgumentsMethod
.invoke(processHandleInfo);
result.add(new ProcessInfo(command.get(), arguments.orElse(null)));
}
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
});
return result;
} catch (Exception e) {
throw new RuntimeException("Unable to determine running IDE processes", e);
}
List<ProcessInfo> result = new ArrayList<>();
ProcessHandle.allProcesses().forEach(p -> {
ProcessHandle.Info info = p.info();
Optional<String> command = info.command();
if (command.isPresent()) {
result.add(new ProcessInfo(command.get(), info.arguments().orElse(null)));
}
});
return result;
}
}

Expand Down

0 comments on commit 18ce6c2

Please sign in to comment.