Here you can find the source of compileTreeWithErrors(JavaCompiler compiler, List
public static boolean compileTreeWithErrors(JavaCompiler compiler, List<String> options, File targetFolder, DiagnosticListener<? super JavaFileObject> diagnosticListener, boolean addProcessorsToClasspath)
//package com.java2s; /******************************************************************************* * Copyright (c) 2007, 2015 BEA Systems, Inc. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors:/*from ww w. j a va2s . c o m*/ * wharley@bea.com - initial API and implementation * IBM Corporation - fix for 342936 * het@google.com - Bug 415274 - Annotation processing throws a NPE in getElementsAnnotatedWith() *******************************************************************************/ import java.io.File; import java.io.StringWriter; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; import java.util.Locale; import javax.tools.DiagnosticListener; import javax.tools.JavaCompiler; import javax.tools.JavaCompiler.CompilationTask; import javax.tools.JavaFileObject; import javax.tools.StandardJavaFileManager; public class Main { private static String _processorJarPath; private static String _jls8ProcessorJarPath; private static String _tmpSrcFolderName; private static String _tmpBinFolderName; private static String _tmpGenFolderName; /** * Compile the contents of a directory tree, collecting errors so that they can be * compared with expected errors. * @param compiler the system compiler or Eclipse compiler * @param options will be passed to the compiler * @param targetFolder the folder to compile * @param errors a StringWriter into which compiler output will be written * @return true if the compilation was successful */ public static boolean compileTreeWithErrors(JavaCompiler compiler, List<String> options, File targetFolder, DiagnosticListener<? super JavaFileObject> diagnosticListener) { return compileTreeWithErrors(compiler, options, targetFolder, diagnosticListener, false, true); } public static boolean compileTreeWithErrors(JavaCompiler compiler, List<String> options, File targetFolder, DiagnosticListener<? super JavaFileObject> diagnosticListener, boolean addProcessorsToClasspath) { return compileTreeWithErrors(compiler, options, targetFolder, diagnosticListener, false, addProcessorsToClasspath); } public static boolean compileTreeWithErrors(JavaCompiler compiler, List<String> options, File targetFolder, DiagnosticListener<? super JavaFileObject> diagnosticListener, boolean useJLS8Processors, boolean addProcessorsToClasspath) { StandardJavaFileManager manager = compiler.getStandardFileManager(null, Locale.getDefault(), Charset.defaultCharset()); // create new list containing inputfile List<File> files = new ArrayList<File>(); findFilesUnder(targetFolder, files); Iterable<? extends JavaFileObject> units = manager.getJavaFileObjectsFromFiles(files); options.add("-d"); options.add(_tmpBinFolderName); options.add("-s"); options.add(_tmpGenFolderName); addProcessorPaths(options, useJLS8Processors, addProcessorsToClasspath); // use writer to prevent System.out/err to be polluted with problems StringWriter writer = new StringWriter(); CompilationTask task = compiler.getTask(writer, manager, diagnosticListener, options, null, units); Boolean result = task.call(); return result.booleanValue(); } /** * Recursively collect all the files under some root. Ignore directories named "CVS". * Used when compiling multiple source files. * @param files a List<File> to which all the files found will be added * @return the set of Files under a root folder. */ public static void findFilesUnder(File rootFolder, List<File> files) { for (File child : rootFolder.listFiles()) { if ("CVS".equals(child.getName())) { continue; } if (child.isDirectory()) { findFilesUnder(child, files); } else { files.add(child); } } } private static void addProcessorPaths(List<String> options, boolean useJLS8Processors, boolean addToNormalClasspath) { String path = useJLS8Processors ? _jls8ProcessorJarPath : _processorJarPath; if (addToNormalClasspath) { options.add("-cp"); options.add(_tmpSrcFolderName + File.pathSeparator + _tmpGenFolderName + File.pathSeparator + path); } options.add("-processorpath"); options.add(path); } }