use of org.abstractmeta.toolbox.compilation.compiler.JavaSourceCompiler in project Denizen-For-Bukkit by DenizenScript.
the class RuntimeCompiler method loader.
@SuppressWarnings("unchecked")
public void loader() {
dB.log("Loading external dependencies for run-time compiler.");
dependencies = new ArrayList<String>();
try {
File file = new File(denizen.getDataFolder() + File.separator + "externals" + File.separator + "dependencies");
for (File f : file.listFiles(jarFilter)) {
dependencies.add(f.getPath());
dB.log("Loaded " + f.getName());
}
} catch (Exception error) {
dB.log("No dependencies to load or error loading dependencies: " + error.getMessage());
}
dB.log("Loading plugins as dependencies for run-time compiler.");
for (File f : pluginsFolder.listFiles(jarFilter)) {
dependencies.add(f.getPath());
dB.log("Loaded " + f.getName());
}
dB.log("Attempting to add CraftBukkit as dependency for run-time compiler.");
try {
File file = new File(".");
for (File f : file.listFiles(jarFilter)) {
dependencies.add(f.getPath());
dB.log("Loaded " + f.getName());
}
} catch (Exception error) {
dB.log("Could not find CraftBukkit jar or error reading it: " + error.getMessage());
}
if (!externalsFolder.exists() || externalsFolder.list().length == 0) {
return;
}
try {
File file = new File(denizen.getDataFolder() + File.separator + "externals");
File[] files = file.listFiles(javaFilter);
if (files != null && files.length > 0) {
for (File f : files) {
String fileName = f.getName();
dB.log("Processing '" + fileName + "'... ");
JavaSourceCompiler javaSourceCompiler = new JavaSourceCompilerImpl();
JavaSourceCompiler.CompilationUnit compilationUnit = javaSourceCompiler.createCompilationUnit();
if (!dependencies.isEmpty()) {
compilationUnit.addClassPathEntries(dependencies);
}
try {
compilationUnit.addJavaSource(fileName.replace(".java", ""), readFile(f.getAbsolutePath()));
ClassLoader classLoader = javaSourceCompiler.compile(compilationUnit);
Class<dExternal> load = (Class<dExternal>) classLoader.loadClass(fileName.replace(".java", ""));
dExternal loadedClass = load.newInstance();
loadedClass.load();
loadedExternals.add(loadedClass);
} catch (Exception e) {
if (e instanceof IllegalStateException) {
dB.echoError("No JDK found! External .java files will not be loaded.");
dB.echoError(e);
} else {
dB.echoError(ChatColor.RED + "Error compiling " + fileName + "!");
dB.echoError(e);
}
}
}
dB.echoApproval("All externals loaded!");
}
} catch (Exception error) {
dB.echoError(error);
}
}
use of org.abstractmeta.toolbox.compilation.compiler.JavaSourceCompiler in project Denizen-For-Bukkit by DenizenScript.
the class RuntimeCompiler method runString.
public void runString(String input) {
try {
i++;
JavaSourceCompiler javaSourceCompiler = new JavaSourceCompilerImpl();
JavaSourceCompiler.CompilationUnit compilationUnit = javaSourceCompiler.createCompilationUnit();
if (!dependencies.isEmpty()) {
compilationUnit.addClassPathEntries(dependencies);
}
String complete = "import net.aufdemrand.denizen.utilities.debugging.dB;\npublic class CodeTester" + i + " extends net.aufdemrand.denizen.utilities.dExternalRunnable {\n" + "public void unload() {}\npublic void load() {}\n" + "public void run() {\n" + input + "\n}\n}";
compilationUnit.addJavaSource("CodeTester" + i, complete);
ClassLoader classLoader = javaSourceCompiler.compile(compilationUnit);
Class<? extends dExternalRunnable> load = (Class<? extends dExternalRunnable>) classLoader.loadClass("CodeTester" + i);
dExternalRunnable loadedClass = load.newInstance();
loadedClass.load();
loadedClass.run();
loadedClass.unload();
} catch (Exception e) {
dB.echoError(e);
}
}
use of org.abstractmeta.toolbox.compilation.compiler.JavaSourceCompiler in project beakerx by twosigma.
the class JavaCodeRunner method compile.
private boolean compile(Codev codev, String classId, String ret) {
org.abstractmeta.toolbox.compilation.compiler.JavaSourceCompiler javaSourceCompiler = new com.twosigma.beakerx.javash.evaluator.JavaSourceCompiler();
JavaSourceCompiler.CompilationUnit compilationUnit = javaSourceCompiler.createCompilationUnit(new File(javaEvaluator.getOutDir()));
buildClasspath(compilationUnit);
codev.javaSourceCode.append("public class " + JavaEvaluator.WRAPPER_CLASS_NAME + classId + " {\n");
codev.javaSourceCode.append("public static ");
codev.javaSourceCode.append(ret);
codev.javaSourceCode.append(" beakerRun() throws Exception {\n");
addTheRestOfCode(codev);
codev.javaSourceCode.append("}\n");
codev.javaSourceCode.append("}\n");
compilationUnit.addJavaSource(codev.getPname() + "." + JavaEvaluator.WRAPPER_CLASS_NAME + classId, codev.javaSourceCode.toString());
boolean compile = javaSourceCompiler.compile(compilationUnit);
if (compile) {
javaSourceCompiler.persistCompiledClasses(compilationUnit);
return true;
}
return false;
}
use of org.abstractmeta.toolbox.compilation.compiler.JavaSourceCompiler in project beakerx by twosigma.
the class JavaCodeRunner method compileNewDefinitionClass.
private TryResult compileNewDefinitionClass(JobDescriptor j, Matcher m, Codev codev) {
TryResult either;
String cname = m.group(1);
addTheRestOfCode(codev);
org.abstractmeta.toolbox.compilation.compiler.JavaSourceCompiler javaSourceCompiler = new com.twosigma.beakerx.javash.evaluator.JavaSourceCompiler();
JavaSourceCompiler.CompilationUnit compilationUnit = javaSourceCompiler.createCompilationUnit(new File(javaEvaluator.getOutDir()));
buildClasspath(compilationUnit);
compilationUnit.addJavaSource(codev.getPname() + "." + cname, codev.javaSourceCode.toString());
try {
javaSourceCompiler.compile(compilationUnit);
javaSourceCompiler.persistCompiledClasses(compilationUnit);
either = TryResult.createResult(codev.getPname() + "." + cname);
} catch (CompilationException e) {
either = TryResult.createError(buildErrorMessage(e, codev.lineNumbersMapping));
} catch (Exception e) {
either = TryResult.createError("ERROR: " + e.toString());
}
return either;
}
Aggregations