public byte[] getClassBytes(String name) { return fileManager.getCompiledClass(name); }
@Override protected Class<?> findClass(String name) throws ClassNotFoundException { byte[] bytecode = getClassBytes(name); if (bytecode == null) { throw new ClassNotFoundException(name); } return defineClass(name, bytecode, 0, bytecode.length); }
public JsonObject launch() throws Exception { Invoker invoker; ClassLoader loader; if (JAVA_FILE_MATCHER.matches(this.binaryPath)) { loader = new JavaCompilerLoader(Thread.currentThread().getContextClassLoader(), this.getBinaryPath()); invoker = (Invoker) loader; } else { if (getEntryClassName() == null) { throw new IllegalStateException("Main class name is required to execute .jar"); } loader = new JarLoader(this.getBinaryPath(), getEntryClassName()); invoker = (Invoker) loader; } Thread.currentThread().setContextClassLoader(loader); JsonObject args = getParameter(); if (args == null) { args = new JsonObject(); //always pass an argument } return invoker.invokeMain(args); }
@Test public void testLauncherParameters(){ Launcher launcher = new Launcher(); final Path binaryPath = Paths.get("/"); assertNull(launcher.getBinaryPath()); launcher.setBinaryPath(binaryPath); assertTrue(launcher.getBinaryPath().isAbsolute()); final String className = "clazzname"; assertNull(launcher.getEntryClassName()); launcher.setEntryClassName(className); assertSame(className, launcher.getEntryClassName()); final JsonObject param = new JsonObject(); assertNull(launcher.getParameter()); launcher.setParameter(param); assertSame(param, launcher.getParameter()); }
public static void main(String[] args) { CLI cli = new CLI(); try { CommandLine.populateCommand(cli, args); Launcher launcher = new Launcher(); launcher.setBinaryPath(path); launcher.setEntryClassName(cli.mainClassName); launcher.setParameter(readParameters(cli)); JsonObject o = launcher.launch(); Gson gson = new Gson(); System.out.println(gson.toJson(o));
@Test public void testRelativePathForJarLaunch() throws Exception{ Launcher launcher = new Launcher(); Path jarPath = Paths.get("./src/test/resources/serverlessJava.jar"); launcher.setBinaryPath(jarPath); launcher.setEntryClassName("aproject.App"); JsonObject result = launcher.launch(); assertNotNull(result); } }
@Test public void testRelativePathForJavaFile() throws Exception{ Launcher launcher = new Launcher(); Path filePath = Paths.get("./src/test/resources/aproject/App.java"); launcher.setBinaryPath(filePath); JsonObject result = launcher.launch(); assertNotNull(result); }
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>(); StandardJavaFileManager standardFileManager = compiler.getStandardFileManager(null, null, null); String packageName = findPackageName(resolvedPath); String fileName = sourcePath.getFileName().toString(); String className = fileName.substring(0,fileName.length()-Kind.SOURCE.extension.length()); File sourceRoot = findSourceRoot(packageName, resolvedPath); standardFileManager.setLocation(StandardLocation.SOURCE_PATH, Collections.singleton(sourceRoot)); return; fileManager = new MemoryFileManager(loader, standardFileManager); CompilationTask task = compiler.getTask(null, fileManager, diagnostics, Collections.singleton("-g"), null, Collections.singleton(javaFile)); boolean valid = task.call(); this.mainClass = loadClass(className);
public JarLoader(Path jarPath, String entrypoint) throws MalformedURLException, ClassNotFoundException, NoSuchMethodException, SecurityException { super(new URL[] { jarPath.toUri().toURL() }); final String[] splittedEntrypoint = entrypoint.split("#"); final String entrypointClassName = splittedEntrypoint[0]; final String entrypointMethodName = splittedEntrypoint.length > 1 ? splittedEntrypoint[1] : "main"; this.mainClass = loadClass(entrypointClassName); Method m = mainClass.getMethod(entrypointMethodName, new Class[] { JsonObject.class }); m.setAccessible(true); int modifiers = m.getModifiers(); if (m.getReturnType() != JsonObject.class || !Modifier.isStatic(modifiers) || !Modifier.isPublic(modifiers)) { throw new NoSuchMethodException("main"); } this.mainMethod = m; }
@Test public void testAbsolutePathForJarLaunch() throws Exception{ Launcher launcher = new Launcher(); Path jarPath = Paths.get("./src/test/resources/serverlessJava.jar"); launcher.setBinaryPath(jarPath.toRealPath()); launcher.setEntryClassName("aproject.App"); JsonObject result = launcher.launch(); assertNotNull(result); }
@Test public void testAbsolutePathForJavaFile() throws Exception{ Launcher launcher = new Launcher(); Path filePath = Paths.get("./src/test/resources/aproject/App.java"); launcher.setBinaryPath(filePath.toRealPath()); JsonObject result = launcher.launch(); assertNotNull(result); }
@Test(expected=IllegalStateException.class) public void testLaunchJarNoMain() throws Exception{ Launcher launcher = new Launcher(); Path jarPath = Paths.get("someJar.jar"); System.out.println(jarPath.toAbsolutePath().toString()); launcher.setBinaryPath(jarPath); launcher.launch(); fail(); }