Java 8 JavaCompiler - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 JavaCompiler class.

This code example shows how to use the following methods:getStandardFileManager
public class NodeExtender { 
 
	private static final Logger logger   = Logger.getLogger(NodeExtender.class.getName());
 
	private static final JavaCompiler compiler       = ToolProvider.getSystemJavaCompiler();
	private static final JavaFileManager fileManager = new ClassFileManager(compiler.getStandardFileManager(null, null, null));
	private static final ClassLoader classLoader     = fileManager.getClassLoader(null);
	private static final Map<String, Class> classes  = new TreeMap<>();
 
	private List<JavaFileObject> jfiles  = null;
	private Set<String> fqcns            = null;
 
	public NodeExtender() { 
 
		jfiles      = new ArrayList<>();
		fqcns       = new LinkedHashSet<>();
	} 
 
	public static ClassLoader getClassLoader() {
		return classLoader;
5
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getTask
        testDir.mkdirs();
        fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(testDir));
 
        JavaSource js = new JavaSource();
        System.err.println(js.getCharContent(false));
        CompilationTask t = comp.getTask(null, fm, null, null, null, Arrays.asList(js));
        if (!t.call())
            throw new Error("compilation failed");
 
        File testClass = new File(testDir, "Test.class");
        String out = javap(testClass);
 
        // Extract class sig from first line of Java source 
        String expect = js.source.replaceAll("(?s)^(.* Test[^{]+?) *\\{.*", "$1");
 
        // Extract class sig from line from javap output 
        String found = out.replaceAll("(?s).*\n(.* Test[^{]+?) *\\{.*", "$1");
 
        checkEqual("class signature", expect, found);
 
5
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getStandardFileManager, getTask
5
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getTask
	 */ 
	protected CompilationResult doCompilation( 
			Iterable<JavaFileObject> compilationUnits) {
		List<String> options = Arrays.asList("-encoding", "UTF-8", "-g");
		DiagnosticListener<JavaFileObject> listener = new ErrorListener();
		EclipseCompilerClassFileManager manager = new EclipseCompilerClassFileManager(
				javaCompiler, listener);
		manager.setParentClassLoader(parentClassLoader);
 
		if (libraries != null) {
			try { 
				manager.addLibraries(libraries);
			} catch (IOException e) {
				log.error("Can't set libraries for compiler.", e);
			} 
		} 
 
		JavaCompiler.CompilationTask task = javaCompiler.getTask(null, manager,
				listener, options, null, compilationUnits);
 

4
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getTask
	 */ 
	protected CompilationResult doCompilation( 
			Iterable<JavaFileObject> compilationUnits) {
		List<String> options = Arrays.asList("-encoding", "UTF-8", "-g");
		DiagnosticListener<JavaFileObject> listener = new ErrorListener();
		JDKClassFileManager manager = new JDKClassFileManager(javaCompiler,
				listener);
		manager.setParentClassLoader(parentClassLoader);
 
		if (libraries != null) {
			try { 
				manager.addLibraries(libraries);
			} catch (IOException e) {
				log.error("Can't set libraries for compiler.", e);
			} 
		} 
 
		JavaCompiler.CompilationTask task = javaCompiler.getTask(null, manager,
				listener, options, null, compilationUnits);
 

4
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getClass, run
        options.addAll(getAPTOptions());
        options.addAll(classes);
         
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayOutputStream err = new ByteArrayOutputStream();
        int compilationResult = compiler.run(null, out, err, options.toArray(new String[options.size()]));
 
//        Processor.elementCache.clear(); 
        if (compilationResult != 0) {
            System.err.println(compiler.getClass().getName());
            Assert.fail("Compilation Failed:\n " + new String(err.toByteArray(), "UTF-8"));
        } 
    } 
     
    protected Collection<String> getAPTOptions() {
        return Collections.emptyList();
    } 
 
} 
4
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getTask
    protected boolean internalJava6Compile(JavaCompiler compiler, JavaFileManager fileManager,
                                           DiagnosticListener<JavaFileObject> listener,
                                           Iterable<? extends JavaFileObject> fileList) {
        List<String> args = new ArrayList<String>();
        addArgs(args);
        CompilationTask task = compiler.getTask(null, fileManager, listener, args, null, fileList);
        Boolean ret = task.call();
        try { 
            fileManager.close();
        } catch (IOException e) {
            System.err.print("[ERROR] IOException during compiling.");
            e.printStackTrace();
        } 
        return ret;
    } 
 
    public boolean internalCompile(String[] args, int sourceFileIndex) {
        Process p = null; 
        String cmdArray[] = null;
        File tmpFile = null;
4
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getStandardFileManager
 
    private static final class MemoryFileManager extends ForwardingJavaFileManager<JavaFileManager> {
        private final Map<String, Output> map = new HashMap<String, Output>();
 
        MemoryFileManager(JavaCompiler compiler) {
            super(compiler.getStandardFileManager(null, null, null));
        } 
 
        @Override 
        public Output getJavaFileForOutput(Location location, String name, Kind kind, FileObject source) {
            Output mc = this.map.get(name);
            if (mc == null) {
                mc = new Output(name);
                this.map.put(name, mc);
            } 
            return mc;
        } 
    } 
 
    private static class MemoryFileObject extends SimpleJavaFileObject {
4
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getStandardFileManager
public class JavaCCUtils { 
 
	private static final Random RANDOM = new Random();
	 
	private static JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
	private static JavaFileManager fileManager = new ClassFileManager(compiler.getStandardFileManager(null, null, null));
	 
	/** 
	 * Compiles source code into a class that gets loaded in the current classloaded 
	 * @param fullName 
	 * @param src 
	 * @return 
	 * @throws ClassNotFoundException 
	 */ 
	public static Class<?> compile(String fullName, String src) throws ClassNotFoundException {
		List<JavaFileObject> jfiles = new ArrayList<JavaFileObject>();
		jfiles.add(new CharSequenceJavaFileObject(fullName, src));
 
		// We specify a task to the compiler. Compiler should use our file 
		// manager and our list of "files". 
4
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:run
        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");
        } 
 
    } 
 
} 
4
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
See Code Examples for Java 8 JavaCompiler Methods: