Created
July 4, 2019 17:28
-
-
Save gurkanakdeniz/2b2e5a9c8f38a3c0c1229d56d6859975 to your computer and use it in GitHub Desktop.
dynamic code call in java file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.lang.reflect.Method; | |
import java.net.URL; | |
import java.net.URLClassLoader; | |
public class CodeRunner { | |
public static void compile(String file) throws Exception { | |
try { | |
ProcessBuilder pb = new ProcessBuilder("javac", file); | |
Process process = pb.start(); | |
int exitCode = process.waitFor(); | |
if (exitCode > 0) { | |
throw new Exception("Compile Error"); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
throw e; | |
} | |
} | |
public Object run(String folderName, String[] args, String method) throws Exception { | |
File file = new File(FileUtility.getCompileCodeFile(folderName)); | |
URL url = file.toURI().toURL(); | |
URL[] urls = new URL[] { url }; | |
ClassLoader cl = new URLClassLoader(urls); | |
Class<?> c; | |
c = Class.forName("External", false, cl); | |
Object[] param = { getArgs(args) }; | |
String runMethod = method; | |
if (runMethod == null || runMethod.trim().length() <= 0) { | |
runMethod = "ex"; | |
} | |
Method m = c.getMethod(runMethod, param.getClass()); | |
TimeoutCodeRunner timeoutCodeRunner = new TimeoutCodeRunner(m, 8000); | |
timeoutCodeRunner.run(null, param); // m.invoke(null, param); | |
Exception exception = timeoutCodeRunner.getException(); | |
if (exception != null) { | |
throw exception; | |
} | |
Object obj = timeoutCodeRunner.getResult(); | |
return obj; | |
} | |
private static Object[] getArgs(String[] args) { | |
Object[] response = null; | |
if (args != null && args.length > 0) { | |
response = new Object[args.length]; | |
for (int i = 0; i < args.length; i++) { | |
response[i] = args[i]; | |
} | |
} else { | |
response = new Object[] { "" }; | |
} | |
return response; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class External { | |
public static String ex(Object[] args) { | |
return ""; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.Method; | |
public class TimeoutCodeRunner { | |
private Method method; | |
private long timeout = 5000; | |
private Object result; | |
private Exception exception; | |
private Throwable throwable; | |
public TimeoutCodeRunner(Method method) { | |
this.method = method; | |
} | |
public TimeoutCodeRunner(Method method, long timeout) { | |
this.method = method; | |
this.timeout = timeout; | |
} | |
public void run(Object obj, Object... args) throws Exception { | |
ThreadGroup threadGroup = new ThreadGroup("threadGroup"); | |
Thread runThread = new Thread(threadGroup, new Runnable() { | |
public void run() { | |
try { | |
result = method.invoke(obj, args); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
exception = e; | |
} catch (Throwable e) { | |
e.printStackTrace(); | |
throwable = e; | |
} | |
} | |
}); | |
runThread.setDaemon(true); | |
runThread.start(); | |
runThread.join(timeout); | |
try { | |
runThread.stop(); | |
runThread.interrupt(); | |
threadGroup.interrupt(); | |
threadGroup.destroy(); | |
} catch (Exception e) { | |
exception = e; | |
return; | |
} | |
} | |
public Object getResult() { | |
return result; | |
} | |
public Exception getException() { | |
return exception; | |
} | |
public Throwable getThrowable() { | |
return throwable; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment