|
| 1 | +package utils; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.lang.reflect.Constructor; |
| 5 | +import java.lang.reflect.InvocationTargetException; |
| 6 | +import java.lang.reflect.Method; |
| 7 | +import java.lang.reflect.Modifier; |
| 8 | +import java.net.URL; |
| 9 | +import java.net.URLClassLoader; |
| 10 | +import java.nio.charset.StandardCharsets; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +public class MainRunner { |
| 15 | + |
| 16 | + private final String className; |
| 17 | + private final Class<?> reloadClazz; |
| 18 | + private final List<URL> extraUrls; |
| 19 | + |
| 20 | + public MainRunner(String className, Class<?> reloadClazz, List<URL> extraUrls) { |
| 21 | + this.className = className; |
| 22 | + this.reloadClazz = reloadClazz; |
| 23 | + this.extraUrls = extraUrls; |
| 24 | + } |
| 25 | + |
| 26 | + public ExecutionResult runJavaClass(String input, String... args) throws IOException { |
| 27 | + |
| 28 | + InputStream origIn = System.in; |
| 29 | + PrintStream origOut = System.out; |
| 30 | + PrintStream origErr = System.err; |
| 31 | + |
| 32 | + ByteArrayInputStream in = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8)); |
| 33 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 34 | + ByteArrayOutputStream err = new ByteArrayOutputStream(); |
| 35 | + |
| 36 | + // https://stackoverflow.com/a/20094513/854540 |
| 37 | + ArrayList<URL> allUrls = new ArrayList<>(extraUrls); |
| 38 | + allUrls.add(reloadClazz.getProtectionDomain().getCodeSource().getLocation()); |
| 39 | + URL[] urls = allUrls.toArray(new URL[0]); |
| 40 | + |
| 41 | + try (URLClassLoader classLoader = new URLClassLoader(urls, reloadClazz.getClassLoader().getParent())){ |
| 42 | + |
| 43 | + // https://stackoverflow.com/a/1119559/854540 |
| 44 | + System.setIn(in); |
| 45 | + System.setOut(new PrintStream(out)); |
| 46 | + System.setErr(new PrintStream(err)); |
| 47 | + |
| 48 | + Class<?> clazz = classLoader.loadClass(className); |
| 49 | + |
| 50 | + Method mainMethod; |
| 51 | + Object[] invokeArgs; |
| 52 | + try { |
| 53 | + mainMethod = getMainMethod(clazz, String[].class); |
| 54 | + invokeArgs = new Object[]{args}; |
| 55 | + } |
| 56 | + catch (NoSuchMethodException _) { |
| 57 | + mainMethod = getMainMethod(clazz); |
| 58 | + invokeArgs = new Object[0]; |
| 59 | + } |
| 60 | + |
| 61 | + Object invokeObj; |
| 62 | + if (Modifier.isStatic(mainMethod.getModifiers())) { |
| 63 | + invokeObj = null; |
| 64 | + } |
| 65 | + else { |
| 66 | + Constructor<?> constructor = clazz.getDeclaredConstructor(); |
| 67 | + constructor.setAccessible(true); |
| 68 | + invokeObj = constructor.newInstance(); |
| 69 | + } |
| 70 | + |
| 71 | + mainMethod.setAccessible(true); |
| 72 | + mainMethod.invoke(invokeObj, invokeArgs); |
| 73 | + |
| 74 | + return new ExecutionResult(out.toString(StandardCharsets.UTF_8), err.toString(StandardCharsets.UTF_8)); |
| 75 | + } |
| 76 | + catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InstantiationException e) { |
| 77 | + throw new RuntimeException(e); // avoid changing all throws signatures although ideally should |
| 78 | + } |
| 79 | + catch (InvocationTargetException e) { |
| 80 | + e.getTargetException().printStackTrace(); // ugh |
| 81 | + return new ExecutionResult(out.toString(StandardCharsets.UTF_8), err.toString(StandardCharsets.UTF_8)); |
| 82 | + } |
| 83 | + finally { |
| 84 | + System.setIn(origIn); |
| 85 | + System.setOut(origOut); |
| 86 | + System.setErr(origErr); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private Method getMainMethod(Class<?> clazz, Class<?>... parameterTypes) throws NoSuchMethodException { |
| 91 | + while (clazz != null) { |
| 92 | + try { |
| 93 | + return clazz.getDeclaredMethod("main", parameterTypes); |
| 94 | + } |
| 95 | + catch (NoSuchMethodException e) { |
| 96 | + clazz = clazz.getSuperclass(); |
| 97 | + } |
| 98 | + } |
| 99 | + throw new NoSuchMethodException(); |
| 100 | + } |
| 101 | + |
| 102 | + public record ExecutionResult(String out, String err) { |
| 103 | + } |
| 104 | +} |
0 commit comments