src/share/classes/javax/tools/JavaCompiler.java

Wed, 13 Apr 2011 11:35:43 -0700

author
jjh
date
Wed, 13 Apr 2011 11:35:43 -0700
changeset 972
694ff82ca68e
parent 824
0a509c765657
child 1073
f85d980faaf8
permissions
-rw-r--r--

7032975: API files in javax.annotation.processing need to be updated for references to JLS
7032972: API files in javax.tools need to updated for references to JVM Spec with editions/hyperlinks
7032978: API files in javax.tools need to be updated for references to JLS with editions/hyperlinks
Summary: Removed URLs and 'edition' references
Reviewed-by: jjg, darcy

duke@1 1 /*
jjg@824 2 * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package javax.tools;
duke@1 27
duke@1 28 import java.io.File;
duke@1 29 import java.io.InputStream;
duke@1 30 import java.io.Writer;
duke@1 31 import java.nio.charset.Charset;
duke@1 32 import java.util.List;
duke@1 33 import java.util.Locale;
duke@1 34 import java.util.concurrent.Callable;
duke@1 35 import javax.annotation.processing.Processor;
duke@1 36
duke@1 37 /**
duke@1 38 * Interface to invoke Java™ programming language compilers from
duke@1 39 * programs.
duke@1 40 *
duke@1 41 * <p>The compiler might generate diagnostics during compilation (for
duke@1 42 * example, error messages). If a diagnostic listener is provided,
duke@1 43 * the diagnostics will be supplied to the listener. If no listener
duke@1 44 * is provided, the diagnostics will be formatted in an unspecified
duke@1 45 * format and written to the default output, which is {@code
duke@1 46 * System.err} unless otherwise specified. Even if a diagnostic
duke@1 47 * listener is supplied, some diagnostics might not fit in a {@code
duke@1 48 * Diagnostic} and will be written to the default output.
duke@1 49 *
duke@1 50 * <p>A compiler tool has an associated standard file manager, which
duke@1 51 * is the file manager that is native to the tool (or built-in). The
duke@1 52 * standard file manager can be obtained by calling {@linkplain
duke@1 53 * #getStandardFileManager getStandardFileManager}.
duke@1 54 *
duke@1 55 * <p>A compiler tool must function with any file manager as long as
duke@1 56 * any additional requirements as detailed in the methods below are
duke@1 57 * met. If no file manager is provided, the compiler tool will use a
duke@1 58 * standard file manager such as the one returned by {@linkplain
duke@1 59 * #getStandardFileManager getStandardFileManager}.
duke@1 60 *
jjh@972 61 * <p>An instance implementing this interface must conform to
jjh@972 62 * <cite>The Java&trade; Language Specification</cite>
jjh@972 63 * and generate class files conforming to
jjh@972 64 * <cite>The Java&trade; Virtual Machine Specification</cite>.
jjh@972 65 * The versions of these
duke@1 66 * specifications are defined in the {@linkplain Tool} interface.
duke@1 67 *
duke@1 68 * Additionally, an instance of this interface supporting {@link
duke@1 69 * javax.lang.model.SourceVersion#RELEASE_6 SourceVersion.RELEASE_6}
duke@1 70 * or higher must also support {@linkplain javax.annotation.processing
duke@1 71 * annotation processing}.
duke@1 72 *
duke@1 73 * <p>The compiler relies on two services: {@linkplain
duke@1 74 * DiagnosticListener diagnostic listener} and {@linkplain
duke@1 75 * JavaFileManager file manager}. Although most classes and
duke@1 76 * interfaces in this package defines an API for compilers (and
duke@1 77 * tools in general) the interfaces {@linkplain DiagnosticListener},
duke@1 78 * {@linkplain JavaFileManager}, {@linkplain FileObject}, and
duke@1 79 * {@linkplain JavaFileObject} are not intended to be used in
duke@1 80 * applications. Instead these interfaces are intended to be
duke@1 81 * implemented and used to provide customized services for a
duke@1 82 * compiler and thus defines an SPI for compilers.
duke@1 83 *
duke@1 84 * <p>There are a number of classes and interfaces in this package
duke@1 85 * which are designed to ease the implementation of the SPI to
duke@1 86 * customize the behavior of a compiler:
duke@1 87 *
duke@1 88 * <dl>
duke@1 89 * <dt>{@link StandardJavaFileManager}</dt>
duke@1 90 * <dd>
duke@1 91 *
duke@1 92 * Every compiler which implements this interface provides a
duke@1 93 * standard file manager for operating on regular {@linkplain
duke@1 94 * java.io.File files}. The StandardJavaFileManager interface
duke@1 95 * defines additional methods for creating file objects from
duke@1 96 * regular files.
duke@1 97 *
duke@1 98 * <p>The standard file manager serves two purposes:
duke@1 99 *
duke@1 100 * <ul>
duke@1 101 * <li>basic building block for customizing how a compiler reads
duke@1 102 * and writes files</li>
duke@1 103 * <li>sharing between multiple compilation tasks</li>
duke@1 104 * </ul>
duke@1 105 *
duke@1 106 * <p>Reusing a file manager can potentially reduce overhead of
duke@1 107 * scanning the file system and reading jar files. Although there
duke@1 108 * might be no reduction in overhead, a standard file manager must
duke@1 109 * work with multiple sequential compilations making the following
duke@1 110 * example a recommended coding pattern:
duke@1 111 *
duke@1 112 * <pre>
duke@1 113 * Files[] files1 = ... ; // input for first compilation task
duke@1 114 * Files[] files2 = ... ; // input for second compilation task
duke@1 115 *
duke@1 116 * JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
duke@1 117 * StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
duke@1 118 *
duke@1 119 * {@code Iterable<? extends JavaFileObject>} compilationUnits1 =
duke@1 120 * fileManager.getJavaFileObjectsFromFiles({@linkplain java.util.Arrays#asList Arrays.asList}(files1));
duke@1 121 * compiler.getTask(null, fileManager, null, null, null, compilationUnits1).call();
duke@1 122 *
duke@1 123 * {@code Iterable<? extends JavaFileObject>} compilationUnits2 =
duke@1 124 * fileManager.getJavaFileObjects(files2); // use alternative method
duke@1 125 * // reuse the same file manager to allow caching of jar files
duke@1 126 * compiler.getTask(null, fileManager, null, null, null, compilationUnits2).call();
duke@1 127 *
duke@1 128 * fileManager.close();</pre>
duke@1 129 *
duke@1 130 * </dd>
duke@1 131 *
duke@1 132 * <dt>{@link DiagnosticCollector}</dt>
duke@1 133 * <dd>
duke@1 134 * Used to collect diagnostics in a list, for example:
duke@1 135 * <pre>
duke@1 136 * {@code Iterable<? extends JavaFileObject>} compilationUnits = ...;
duke@1 137 * JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
duke@1 138 * {@code DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();}
duke@1 139 * StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
duke@1 140 * compiler.getTask(null, fileManager, diagnostics, null, null, compilationUnits).call();
duke@1 141 *
jjg@824 142 * for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics())
jjg@824 143 * System.out.format("Error on line %d in %s%n",
jjg@824 144 * diagnostic.getLineNumber(),
duke@1 145 * diagnostic.getSource().toUri());
duke@1 146 *
duke@1 147 * fileManager.close();</pre>
duke@1 148 * </dd>
duke@1 149 *
duke@1 150 * <dt>
duke@1 151 * {@link ForwardingJavaFileManager}, {@link ForwardingFileObject}, and
duke@1 152 * {@link ForwardingJavaFileObject}
duke@1 153 * </dt>
duke@1 154 * <dd>
duke@1 155 *
duke@1 156 * Subclassing is not available for overriding the behavior of a
duke@1 157 * standard file manager as it is created by calling a method on a
duke@1 158 * compiler, not by invoking a constructor. Instead forwarding
duke@1 159 * (or delegation) should be used. These classes makes it easy to
duke@1 160 * forward most calls to a given file manager or file object while
duke@1 161 * allowing customizing behavior. For example, consider how to
duke@1 162 * log all calls to {@linkplain JavaFileManager#flush}:
duke@1 163 *
duke@1 164 * <pre>
duke@1 165 * final {@linkplain java.util.logging.Logger Logger} logger = ...;
duke@1 166 * {@code Iterable<? extends JavaFileObject>} compilationUnits = ...;
duke@1 167 * JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
duke@1 168 * StandardJavaFileManager stdFileManager = compiler.getStandardFileManager(null, null, null);
duke@1 169 * JavaFileManager fileManager = new ForwardingJavaFileManager(stdFileManager) {
duke@1 170 * public void flush() {
duke@1 171 * logger.entering(StandardJavaFileManager.class.getName(), "flush");
duke@1 172 * super.flush();
duke@1 173 * logger.exiting(StandardJavaFileManager.class.getName(), "flush");
duke@1 174 * }
duke@1 175 * };
duke@1 176 * compiler.getTask(null, fileManager, null, null, null, compilationUnits).call();</pre>
duke@1 177 * </dd>
duke@1 178 *
duke@1 179 * <dt>{@link SimpleJavaFileObject}</dt>
duke@1 180 * <dd>
duke@1 181 *
duke@1 182 * This class provides a basic file object implementation which
duke@1 183 * can be used as building block for creating file objects. For
duke@1 184 * example, here is how to define a file object which represent
duke@1 185 * source code stored in a string:
duke@1 186 *
duke@1 187 * <pre>
duke@1 188 * /**
duke@1 189 * * A file object used to represent source coming from a string.
duke@1 190 * {@code *}/
duke@1 191 * public class JavaSourceFromString extends SimpleJavaFileObject {
duke@1 192 * /**
duke@1 193 * * The source code of this "file".
duke@1 194 * {@code *}/
duke@1 195 * final String code;
duke@1 196 *
duke@1 197 * /**
duke@1 198 * * Constructs a new JavaSourceFromString.
duke@1 199 * * {@code @}param name the name of the compilation unit represented by this file object
duke@1 200 * * {@code @}param code the source code for the compilation unit represented by this file object
duke@1 201 * {@code *}/
duke@1 202 * JavaSourceFromString(String name, String code) {
duke@1 203 * super({@linkplain java.net.URI#create URI.create}("string:///" + name.replace('.','/') + Kind.SOURCE.extension),
duke@1 204 * Kind.SOURCE);
duke@1 205 * this.code = code;
duke@1 206 * }
duke@1 207 *
duke@1 208 * {@code @}Override
duke@1 209 * public CharSequence getCharContent(boolean ignoreEncodingErrors) {
duke@1 210 * return code;
duke@1 211 * }
duke@1 212 * }</pre>
duke@1 213 * </dd>
duke@1 214 * </dl>
duke@1 215 *
duke@1 216 * @author Peter von der Ah&eacute;
duke@1 217 * @author Jonathan Gibbons
duke@1 218 * @see DiagnosticListener
duke@1 219 * @see Diagnostic
duke@1 220 * @see JavaFileManager
duke@1 221 * @since 1.6
duke@1 222 */
duke@1 223 public interface JavaCompiler extends Tool, OptionChecker {
duke@1 224
duke@1 225 /**
duke@1 226 * Creates a future for a compilation task with the given
duke@1 227 * components and arguments. The compilation might not have
duke@1 228 * completed as described in the CompilationTask interface.
duke@1 229 *
duke@1 230 * <p>If a file manager is provided, it must be able to handle all
duke@1 231 * locations defined in {@link StandardLocation}.
duke@1 232 *
darcy@375 233 * <p>Note that annotation processing can process both the
darcy@375 234 * compilation units of source code to be compiled, passed with
darcy@375 235 * the {@code compilationUnits} parameter, as well as class
darcy@375 236 * files, whose names are passed with the {@code classes}
darcy@375 237 * parameter.
darcy@375 238 *
duke@1 239 * @param out a Writer for additional output from the compiler;
duke@1 240 * use {@code System.err} if {@code null}
duke@1 241 * @param fileManager a file manager; if {@code null} use the
duke@1 242 * compiler's standard filemanager
duke@1 243 * @param diagnosticListener a diagnostic listener; if {@code
duke@1 244 * null} use the compiler's default method for reporting
duke@1 245 * diagnostics
duke@1 246 * @param options compiler options, {@code null} means no options
darcy@375 247 * @param classes names of classes to be processed by annotation
darcy@375 248 * processing, {@code null} means no class names
duke@1 249 * @param compilationUnits the compilation units to compile, {@code
duke@1 250 * null} means no compilation units
duke@1 251 * @return an object representing the compilation
duke@1 252 * @throws RuntimeException if an unrecoverable error
duke@1 253 * occurred in a user supplied component. The
duke@1 254 * {@linkplain Throwable#getCause() cause} will be the error in
duke@1 255 * user code.
duke@1 256 * @throws IllegalArgumentException if any of the given
duke@1 257 * compilation units are of other kind than
duke@1 258 * {@linkplain JavaFileObject.Kind#SOURCE source}
duke@1 259 */
duke@1 260 CompilationTask getTask(Writer out,
duke@1 261 JavaFileManager fileManager,
duke@1 262 DiagnosticListener<? super JavaFileObject> diagnosticListener,
duke@1 263 Iterable<String> options,
duke@1 264 Iterable<String> classes,
duke@1 265 Iterable<? extends JavaFileObject> compilationUnits);
duke@1 266
duke@1 267 /**
duke@1 268 * Gets a new instance of the standard file manager implementation
duke@1 269 * for this tool. The file manager will use the given diagnostic
duke@1 270 * listener for producing any non-fatal diagnostics. Fatal errors
duke@1 271 * will be signalled with the appropriate exceptions.
duke@1 272 *
duke@1 273 * <p>The standard file manager will be automatically reopened if
duke@1 274 * it is accessed after calls to {@code flush} or {@code close}.
duke@1 275 * The standard file manager must be usable with other tools.
duke@1 276 *
duke@1 277 * @param diagnosticListener a diagnostic listener for non-fatal
duke@1 278 * diagnostics; if {@code null} use the compiler's default method
duke@1 279 * for reporting diagnostics
duke@1 280 * @param locale the locale to apply when formatting diagnostics;
duke@1 281 * {@code null} means the {@linkplain Locale#getDefault() default locale}.
duke@1 282 * @param charset the character set used for decoding bytes; if
duke@1 283 * {@code null} use the platform default
duke@1 284 * @return the standard file manager
duke@1 285 */
duke@1 286 StandardJavaFileManager getStandardFileManager(
duke@1 287 DiagnosticListener<? super JavaFileObject> diagnosticListener,
duke@1 288 Locale locale,
duke@1 289 Charset charset);
duke@1 290
duke@1 291 /**
duke@1 292 * Interface representing a future for a compilation task. The
duke@1 293 * compilation task has not yet started. To start the task, call
duke@1 294 * the {@linkplain #call call} method.
duke@1 295 *
duke@1 296 * <p>Before calling the call method, additional aspects of the
duke@1 297 * task can be configured, for example, by calling the
duke@1 298 * {@linkplain #setProcessors setProcessors} method.
duke@1 299 */
duke@1 300 interface CompilationTask extends Callable<Boolean> {
duke@1 301
duke@1 302 /**
duke@1 303 * Sets processors (for annotation processing). This will
duke@1 304 * bypass the normal discovery mechanism.
duke@1 305 *
duke@1 306 * @param processors processors (for annotation processing)
duke@1 307 * @throws IllegalStateException if the task has started
duke@1 308 */
duke@1 309 void setProcessors(Iterable<? extends Processor> processors);
duke@1 310
duke@1 311 /**
duke@1 312 * Set the locale to be applied when formatting diagnostics and
duke@1 313 * other localized data.
duke@1 314 *
duke@1 315 * @param locale the locale to apply; {@code null} means apply no
duke@1 316 * locale
duke@1 317 * @throws IllegalStateException if the task has started
duke@1 318 */
duke@1 319 void setLocale(Locale locale);
duke@1 320
duke@1 321 /**
duke@1 322 * Performs this compilation task. The compilation may only
duke@1 323 * be performed once. Subsequent calls to this method throw
duke@1 324 * IllegalStateException.
duke@1 325 *
duke@1 326 * @return true if and only all the files compiled without errors;
duke@1 327 * false otherwise
duke@1 328 *
duke@1 329 * @throws RuntimeException if an unrecoverable error occurred
duke@1 330 * in a user-supplied component. The
duke@1 331 * {@linkplain Throwable#getCause() cause} will be the error
duke@1 332 * in user code.
duke@1 333 * @throws IllegalStateException if called more than once
duke@1 334 */
duke@1 335 Boolean call();
duke@1 336 }
duke@1 337 }

mercurial