in java-local/src/main/java/openwhisk/java/local/JavaCompilerLoader.java [50:99]
public JavaCompilerLoader(ClassLoader loader, Path sourcePath) {
super(loader);
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null) {
throw new RuntimeException("No java compiler. Please use a JDK not a JRE!");
}
try {
Path resolvedPath = sourcePath.toAbsolutePath();
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());
if(!packageName.isEmpty())
{
className = packageName + '.' + className;
}
File sourceRoot = findSourceRoot(packageName, resolvedPath);
standardFileManager.setLocation(StandardLocation.SOURCE_PATH, Collections.singleton(sourceRoot));
JavaFileObject javaFile = standardFileManager.getJavaFileForInput(StandardLocation.SOURCE_PATH, className, Kind.SOURCE);
if(javaFile == null ){
System.err.printf("java file for classname does not exist file: %s\n", className);
return;
}
fileManager = new MemoryFileManager(loader, standardFileManager);
CompilationTask task = compiler.getTask(null, fileManager, diagnostics, Collections.singleton("-g"), null, Collections.singleton(javaFile));
boolean valid = task.call();
if (valid) {
diagnostics.getDiagnostics().forEach(System.out::println);
} else {
diagnostics.getDiagnostics().forEach(System.err::println);
throw new RuntimeException(String.format("Compilation for the file %s failed!\n",sourcePath.toString()));
}
this.mainClass = loadClass(className);
Method m = mainClass.getMethod("main", 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;
} catch (IOException | ClassNotFoundException | NoSuchMethodException | SecurityException e) {
e.printStackTrace();
}
}