package io.vertx.core.impl.verticle;
import io.vertx.core.impl.logging.Logger;
import io.vertx.core.impl.logging.LoggerFactory;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.JavaFileObject.Kind;
import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
import javax.tools.ToolProvider;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static io.vertx.core.net.impl.URIDecoder.decodeURIComponent;
public class CompilingClassLoader extends ClassLoader {
private static final Logger log = LoggerFactory.getLogger(CompilingClassLoader.class);
private static final String JAVA_COMPILER_OPTIONS_PROP_NAME = "vertx.javaCompilerOptions";
private final static List<String> COMPILER_OPTIONS;
static {
String props = System.getProperty(JAVA_COMPILER_OPTIONS_PROP_NAME);
if (props != null) {
String[] array = props.split(",");
List<String> compilerProps = new ArrayList<>(array.length);
for (String prop :array) {
compilerProps.add(prop.trim());
}
COMPILER_OPTIONS = Collections.unmodifiableList(compilerProps);
} else {
COMPILER_OPTIONS = null;
}
}
private final JavaSourceContext javaSourceContext;
private final MemoryFileManager fileManager;
public CompilingClassLoader(ClassLoader loader, String sourceName) {
super(loader);
URL resource = getResource(sourceName);
if (resource == null) {
throw new RuntimeException("Resource not found: " + sourceName);
}
final File sourceFile = new File(decodeURIComponent(resource.getFile(), false));
if (!sourceFile.canRead()) {
throw new RuntimeException("File not found: " + sourceFile.getAbsolutePath() + " current dir is: " + new File(".").getAbsolutePath());
}
this.javaSourceContext = new JavaSourceContext(sourceFile);
try {
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
if (javaCompiler == null) {
throw new RuntimeException("Unable to detect java compiler, make sure you're using a JDK not a JRE!");
}
StandardJavaFileManager standardFileManager = javaCompiler.getStandardFileManager(null, null, null);
standardFileManager.setLocation(StandardLocation.SOURCE_PATH, Collections.singleton(javaSourceContext.getSourceRoot()));
fileManager = new MemoryFileManager(loader, standardFileManager);
JavaFileObject javaFile = standardFileManager.getJavaFileForInput(StandardLocation.SOURCE_PATH, resolveMainClassName(), Kind.SOURCE);
JavaCompiler.CompilationTask task = javaCompiler.getTask(null, fileManager, diagnostics, COMPILER_OPTIONS, null, Collections.singleton(javaFile));
boolean valid = task.call();
if (valid) {
for (Diagnostic<?> d : diagnostics.getDiagnostics()) {
String code = d.getCode();
if (code == null || (!code.startsWith("compiler.warn.annotation.method.not.found") &&
!"compiler.warn.proc.processor.incompatible.source.version".equals(code))) {
log.info(d);
}
}
} else {
for (Diagnostic<?> d : diagnostics.getDiagnostics()) {
log.warn(d);
}
throw new RuntimeException("Compilation failed!");
}
} catch (Exception e) {
throw new RuntimeException("Compilation failed", e);
}
}
public String resolveMainClassName() {
return javaSourceContext.getClassName();
}
@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 byte[] getClassBytes(String name) {
return fileManager.getCompiledClass(name);
}
}