public static void assertCompiles(String name, String source) { URLClassLoader parent = (URLClassLoader) CompileUtils.class.getClassLoader(); SimpleCompiler compiler = new SimpleCompiler(); MemFileManager fileManager = new MemFileManager(parent, compiler.getStandardFileManager(null, null, null)); String classpath = SimpleCompiler.getClassPath(parent); List<String> compilationOptions = Arrays.asList("-classpath", classpath, "-g:none"); // compile SimpleJavaFileObject javaFileObject = new MemSourceFileObject(name, source); Writer out = new StringWriter(); JavaCompiler.CompilationTask task = compiler.getTask(out, fileManager, null, compilationOptions, null, Collections.singletonList(javaFileObject)); if (!task.call()) { Assert.fail("Compilation of " + source + " failed.\n" + out.toString()); } }
String javaClass = "code of new java Class2 that extends existing Class1"; SimpleCompiler sc = new SimpleCompiler(); sc.cook(javaClass); Class<?> executeClass = sc.getClassLoader().loadClass("Class2"); Class1 instance = (Class1) executeClass.getConstructor().newInstance();
import org.codehaus.janino.SimpleCompiler; public class JaninoSimpleTest { public static void main(String[] args) throws Throwable { String filename = args[0]; String className = args[1]; SimpleCompiler compiler = new SimpleCompiler(filename); ClassLoader loader = compiler.getClassLoader(); Class compClass = loader.loadClass(className); Object instance = compClass.newInstance(); } }
protected void compile(Class<? extends AbstractProcessor> processorClass, List<String> classes, String target) throws IOException { JavaCompiler compiler = new SimpleCompiler(); System.out.println(compiler.getClass().getName()); List<String> options = new ArrayList<String>(classes.size() + 3); options.add("-s"); options.add("target/" + target); options.add("-proc:only"); options.add("-processor"); options.add(processorClass.getName()); options.add("-sourcepath"); options.add("src/test/java"); options.addAll(classes); int compilationResult = compiler.run(null, System.out, System.err, options.toArray(new String[options.size()])); if (compilationResult == 0) { System.out.println("Compilation is successful"); } else { Assert.fail("Compilation Failed"); } }
private String getClasspath() { if (classPath == null) { if (classLoader instanceof URLClassLoader) { classPath = getClassPath((URLClassLoader) classLoader); } else { throw new IllegalArgumentException("Unsupported ClassLoader " + classLoader); } } return classPath; }
@Override public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) { for (String a : arguments) { if (a.equals("-classpath")) { return compiler.run(in, out, err, arguments); } } // no classpath given List<String> args = new ArrayList<String>(arguments.length + 2); args.add("-classpath"); args.add(getClasspath()); for (String arg : arguments) { args.add(arg); } return compiler.run(in, out, err, args.toArray(new String[args.size()])); }
private void testJanino() throws Exception { SimpleCompiler sc = new SimpleCompiler(); sc.cook("public class Arne{ public float doWork(){return 42.0f;}}"); Class<?> arneClass = sc.getClassLoader().loadClass("Arne"); Object arne = arneClass.newInstance(); Method doWork = arneClass.getDeclaredMethod("doWork"); Object result = doWork.invoke(arne, new Object[0]); System.out.println(result); }
private void compile(MetaDataExporter exporter) { JavaCompiler compiler = new SimpleCompiler(); Set<String> classes = exporter.getClasses(); int compilationResult = compiler.run(null, null, null, classes.toArray(new String[classes.size()])); if(compilationResult == 0) { System.out.println("Compilation is successful"); } else { Assert.fail("Compilation Failed"); } }
public JDKEvaluatorFactory(URLClassLoader parent, JavaCompiler compiler) { this.fileManager = new MemFileManager(parent, compiler.getStandardFileManager(null, null, null)); this.compiler = compiler; this.classpath = SimpleCompiler.getClassPath(parent); this.loader = fileManager.getClassLoader(StandardLocation.CLASS_OUTPUT); this.compilationOptions = Arrays.asList("-classpath", classpath, "-g:none"); }
public static void assertCompiles(String name, String source) { URLClassLoader parent = (URLClassLoader) CompileUtils.class.getClassLoader(); SimpleCompiler compiler = new SimpleCompiler(); MemFileManager fileManager = new MemFileManager(parent, compiler.getStandardFileManager(null, null, null)); String classpath = SimpleCompiler.getClassPath(parent); List<String> compilationOptions = Arrays.asList("-classpath", classpath, "-g:none"); // compile SimpleJavaFileObject javaFileObject = new MemSourceFileObject(name, source); Writer out = new StringWriter(); JavaCompiler.CompilationTask task = compiler.getTask(out, fileManager, null, compilationOptions, null, Collections.singletonList(javaFileObject)); if (!task.call()) { Assert.fail("Compilation of " + source + " failed.\n" + out.toString()); } }