Skip to content

Commit

Permalink
#11 Get rid of reflection for Java8Process.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rein Raudjärv committed Dec 8, 2017
1 parent 9ce0c0d commit a8a8230
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 57 deletions.
30 changes: 3 additions & 27 deletions java8/src/main/java/org/zeroturnaround/process/Java8Process.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.zeroturnaround.process;

import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
Expand All @@ -20,25 +19,6 @@ public class Java8Process extends JavaProcess {

private static final Logger log = LoggerFactory.getLogger(Java8Process.class);

/**
* <code>public Process destroyForcibly()</code>
*/
private static final Method METHOD_DESTROY_FORCIBLY = getMethod("destroyForcibly");

/**
* <code>public boolean isAlive()</code>
*/
private static final Method METHOD_IS_ALIVE = getMethod("isAlive");

/**
* <code>public boolean waitFor(long timeout, TimeUnit unit) throws InterruptedException</code>
*/
private static final Method METHOD_WAIT_FOR_TIMEOUT = getMethod("waitFor", long.class, TimeUnit.class);

public static boolean isSupported() {
return METHOD_DESTROY_FORCIBLY != null;
}

public Java8Process(Process process) {
super(process);
}
Expand Down Expand Up @@ -69,19 +49,15 @@ protected void invokeDestroy(boolean forceful) {
// Process methods in Java 8

public Process invokeDestroyForcibly() {
return (Process) ReflectionUtil.invokeWithoutDeclaredExceptions(METHOD_DESTROY_FORCIBLY, process);
return process.destroyForcibly();
}

public boolean isAlive() {
return (Boolean) ReflectionUtil.invokeWithoutDeclaredExceptions(METHOD_IS_ALIVE, process);
return process.isAlive();
}

public boolean waitFor(long timeout, TimeUnit unit) throws InterruptedException {
return (Boolean) ReflectionUtil.invokeWithInterruptedException(METHOD_WAIT_FOR_TIMEOUT, process, timeout, unit);
}

private static Method getMethod(String name, Class<?>... parameterTypes) {
return ReflectionUtil.getMethodOrNull(Process.class, name, parameterTypes);
return process.waitFor(timeout, unit);
}

}
4 changes: 3 additions & 1 deletion src/main/java/org/zeroturnaround/process/Processes.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Arrays;

import org.apache.commons.lang3.JavaVersion;
import org.apache.commons.lang3.SystemUtils;

/**
Expand Down Expand Up @@ -41,8 +42,9 @@ public static org.zeroturnaround.process.SystemProcess newStandardProcess(Proces
* @return system process that represents the given input as described above.
*/
public static JavaProcess newJavaProcess(Process process) {
if (Java8Process.isSupported())
if (SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_1_8)) {
return new Java8Process(process);
}
return new JavaProcess(process);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@
import java.lang.reflect.Method;
import java.lang.reflect.UndeclaredThrowableException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Helper methods for Reflection API.
*/
class ReflectionUtil {

private static final Logger log = LoggerFactory.getLogger(ReflectionUtil.class);

public static Object invokeWithoutDeclaredExceptions(Method method, Object target, Object... args) {
try {
return doInvoke(method, target, args);
Expand All @@ -23,18 +18,6 @@ public static Object invokeWithoutDeclaredExceptions(Method method, Object targe
}
}

public static Object invokeWithInterruptedException(Method method, Object target, Object... args) throws InterruptedException {
try {
return doInvoke(method, target, args);
}
catch (InterruptedException e) {
throw e;
}
catch (Throwable t) {
throw uncheck(t);
}
}

private static Object doInvoke(Method method, Object target, Object... args) throws Throwable {
try {
return method.invoke(target, args);
Expand All @@ -54,14 +37,4 @@ private static RuntimeException uncheck(Throwable t) {
throw new UndeclaredThrowableException(t);
}

public static Method getMethodOrNull(Class<?> klass, String name, Class<?>... parameterTypes) {
try {
return klass.getMethod(name, parameterTypes);
}
catch (Exception e) {
log.trace("Could not find {} method {}", klass, name);
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.apache.commons.lang3.JavaVersion;
import org.apache.commons.lang3.SystemUtils;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.rules.TestRule;
import org.zeroturnaround.process.Java8Process;
import org.zeroturnaround.process.JavaProcess;
import org.zeroturnaround.process.SystemProcess;

Expand All @@ -28,7 +28,7 @@ protected static boolean isDestroyGracefullySupported(SystemProcess process) {
}

protected static boolean isDestroyForcefullySupported(SystemProcess process) {
return SystemUtils.IS_OS_WINDOWS || !(process instanceof JavaProcess) || Java8Process.isSupported();
return SystemUtils.IS_OS_WINDOWS || !(process instanceof JavaProcess) || SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_1_8);
}

/**
Expand Down

0 comments on commit a8a8230

Please sign in to comment.