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

changeset 1
9a66ca7c79fa
child 375
2ce3597237f0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/javax/tools/JavaCompiler.java	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,329 @@
     1.4 +/*
     1.5 + * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Sun designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Sun in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.26 + * have any questions.
    1.27 + */
    1.28 +
    1.29 +package javax.tools;
    1.30 +
    1.31 +import java.io.File;
    1.32 +import java.io.InputStream;
    1.33 +import java.io.Writer;
    1.34 +import java.nio.charset.Charset;
    1.35 +import java.util.List;
    1.36 +import java.util.Locale;
    1.37 +import java.util.concurrent.Callable;
    1.38 +import javax.annotation.processing.Processor;
    1.39 +
    1.40 +/**
    1.41 + * Interface to invoke Java™ programming language compilers from
    1.42 + * programs.
    1.43 + *
    1.44 + * <p>The compiler might generate diagnostics during compilation (for
    1.45 + * example, error messages).  If a diagnostic listener is provided,
    1.46 + * the diagnostics will be supplied to the listener.  If no listener
    1.47 + * is provided, the diagnostics will be formatted in an unspecified
    1.48 + * format and written to the default output, which is {@code
    1.49 + * System.err} unless otherwise specified.  Even if a diagnostic
    1.50 + * listener is supplied, some diagnostics might not fit in a {@code
    1.51 + * Diagnostic} and will be written to the default output.
    1.52 + *
    1.53 + * <p>A compiler tool has an associated standard file manager, which
    1.54 + * is the file manager that is native to the tool (or built-in).  The
    1.55 + * standard file manager can be obtained by calling {@linkplain
    1.56 + * #getStandardFileManager getStandardFileManager}.
    1.57 + *
    1.58 + * <p>A compiler tool must function with any file manager as long as
    1.59 + * any additional requirements as detailed in the methods below are
    1.60 + * met.  If no file manager is provided, the compiler tool will use a
    1.61 + * standard file manager such as the one returned by {@linkplain
    1.62 + * #getStandardFileManager getStandardFileManager}.
    1.63 + *
    1.64 + * <p>An instance implementing this interface must conform to the Java
    1.65 + * Language Specification and generate class files conforming to the
    1.66 + * Java Virtual Machine specification.  The versions of these
    1.67 + * specifications are defined in the {@linkplain Tool} interface.
    1.68 + *
    1.69 + * Additionally, an instance of this interface supporting {@link
    1.70 + * javax.lang.model.SourceVersion#RELEASE_6 SourceVersion.RELEASE_6}
    1.71 + * or higher must also support {@linkplain javax.annotation.processing
    1.72 + * annotation processing}.
    1.73 + *
    1.74 + * <p>The compiler relies on two services: {@linkplain
    1.75 + * DiagnosticListener diagnostic listener} and {@linkplain
    1.76 + * JavaFileManager file manager}.  Although most classes and
    1.77 + * interfaces in this package defines an API for compilers (and
    1.78 + * tools in general) the interfaces {@linkplain DiagnosticListener},
    1.79 + * {@linkplain JavaFileManager}, {@linkplain FileObject}, and
    1.80 + * {@linkplain JavaFileObject} are not intended to be used in
    1.81 + * applications.  Instead these interfaces are intended to be
    1.82 + * implemented and used to provide customized services for a
    1.83 + * compiler and thus defines an SPI for compilers.
    1.84 + *
    1.85 + * <p>There are a number of classes and interfaces in this package
    1.86 + * which are designed to ease the implementation of the SPI to
    1.87 + * customize the behavior of a compiler:
    1.88 + *
    1.89 + * <dl>
    1.90 + *   <dt>{@link StandardJavaFileManager}</dt>
    1.91 + *   <dd>
    1.92 + *
    1.93 + *     Every compiler which implements this interface provides a
    1.94 + *     standard file manager for operating on regular {@linkplain
    1.95 + *     java.io.File files}.  The StandardJavaFileManager interface
    1.96 + *     defines additional methods for creating file objects from
    1.97 + *     regular files.
    1.98 + *
    1.99 + *     <p>The standard file manager serves two purposes:
   1.100 + *
   1.101 + *     <ul>
   1.102 + *       <li>basic building block for customizing how a compiler reads
   1.103 + *       and writes files</li>
   1.104 + *       <li>sharing between multiple compilation tasks</li>
   1.105 + *     </ul>
   1.106 + *
   1.107 + *     <p>Reusing a file manager can potentially reduce overhead of
   1.108 + *     scanning the file system and reading jar files.  Although there
   1.109 + *     might be no reduction in overhead, a standard file manager must
   1.110 + *     work with multiple sequential compilations making the following
   1.111 + *     example a recommended coding pattern:
   1.112 + *
   1.113 + *     <pre>
   1.114 + *       Files[] files1 = ... ; // input for first compilation task
   1.115 + *       Files[] files2 = ... ; // input for second compilation task
   1.116 + *
   1.117 + *       JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
   1.118 + *       StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
   1.119 + *
   1.120 + *       {@code Iterable<? extends JavaFileObject>} compilationUnits1 =
   1.121 + *           fileManager.getJavaFileObjectsFromFiles({@linkplain java.util.Arrays#asList Arrays.asList}(files1));
   1.122 + *       compiler.getTask(null, fileManager, null, null, null, compilationUnits1).call();
   1.123 + *
   1.124 + *       {@code Iterable<? extends JavaFileObject>} compilationUnits2 =
   1.125 + *           fileManager.getJavaFileObjects(files2); // use alternative method
   1.126 + *       // reuse the same file manager to allow caching of jar files
   1.127 + *       compiler.getTask(null, fileManager, null, null, null, compilationUnits2).call();
   1.128 + *
   1.129 + *       fileManager.close();</pre>
   1.130 + *
   1.131 + *   </dd>
   1.132 + *
   1.133 + *   <dt>{@link DiagnosticCollector}</dt>
   1.134 + *   <dd>
   1.135 + *     Used to collect diagnostics in a list, for example:
   1.136 + *     <pre>
   1.137 + *       {@code Iterable<? extends JavaFileObject>} compilationUnits = ...;
   1.138 + *       JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
   1.139 + *       {@code DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();}
   1.140 + *       StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
   1.141 + *       compiler.getTask(null, fileManager, diagnostics, null, null, compilationUnits).call();
   1.142 + *
   1.143 + *       for (Diagnostic diagnostic : diagnostics.getDiagnostics())
   1.144 + *           System.out.format("Error on line %d in %d%n",
   1.145 + *                             diagnostic.getLineNumber()
   1.146 + *                             diagnostic.getSource().toUri());
   1.147 + *
   1.148 + *       fileManager.close();</pre>
   1.149 + *   </dd>
   1.150 + *
   1.151 + *   <dt>
   1.152 + *     {@link ForwardingJavaFileManager}, {@link ForwardingFileObject}, and
   1.153 + *     {@link ForwardingJavaFileObject}
   1.154 + *   </dt>
   1.155 + *   <dd>
   1.156 + *
   1.157 + *     Subclassing is not available for overriding the behavior of a
   1.158 + *     standard file manager as it is created by calling a method on a
   1.159 + *     compiler, not by invoking a constructor.  Instead forwarding
   1.160 + *     (or delegation) should be used.  These classes makes it easy to
   1.161 + *     forward most calls to a given file manager or file object while
   1.162 + *     allowing customizing behavior.  For example, consider how to
   1.163 + *     log all calls to {@linkplain JavaFileManager#flush}:
   1.164 + *
   1.165 + *     <pre>
   1.166 + *       final {@linkplain java.util.logging.Logger Logger} logger = ...;
   1.167 + *       {@code Iterable<? extends JavaFileObject>} compilationUnits = ...;
   1.168 + *       JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
   1.169 + *       StandardJavaFileManager stdFileManager = compiler.getStandardFileManager(null, null, null);
   1.170 + *       JavaFileManager fileManager = new ForwardingJavaFileManager(stdFileManager) {
   1.171 + *           public void flush() {
   1.172 + *               logger.entering(StandardJavaFileManager.class.getName(), "flush");
   1.173 + *               super.flush();
   1.174 + *               logger.exiting(StandardJavaFileManager.class.getName(), "flush");
   1.175 + *           }
   1.176 + *       };
   1.177 + *       compiler.getTask(null, fileManager, null, null, null, compilationUnits).call();</pre>
   1.178 + *   </dd>
   1.179 + *
   1.180 + *   <dt>{@link SimpleJavaFileObject}</dt>
   1.181 + *   <dd>
   1.182 + *
   1.183 + *     This class provides a basic file object implementation which
   1.184 + *     can be used as building block for creating file objects.  For
   1.185 + *     example, here is how to define a file object which represent
   1.186 + *     source code stored in a string:
   1.187 + *
   1.188 + *     <pre>
   1.189 + *       /**
   1.190 + *        * A file object used to represent source coming from a string.
   1.191 + *        {@code *}/
   1.192 + *       public class JavaSourceFromString extends SimpleJavaFileObject {
   1.193 + *           /**
   1.194 + *            * The source code of this "file".
   1.195 + *            {@code *}/
   1.196 + *           final String code;
   1.197 + *
   1.198 + *           /**
   1.199 + *            * Constructs a new JavaSourceFromString.
   1.200 + *            * {@code @}param name the name of the compilation unit represented by this file object
   1.201 + *            * {@code @}param code the source code for the compilation unit represented by this file object
   1.202 + *            {@code *}/
   1.203 + *           JavaSourceFromString(String name, String code) {
   1.204 + *               super({@linkplain java.net.URI#create URI.create}("string:///" + name.replace('.','/') + Kind.SOURCE.extension),
   1.205 + *                     Kind.SOURCE);
   1.206 + *               this.code = code;
   1.207 + *           }
   1.208 + *
   1.209 + *           {@code @}Override
   1.210 + *           public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   1.211 + *               return code;
   1.212 + *           }
   1.213 + *       }</pre>
   1.214 + *   </dd>
   1.215 + * </dl>
   1.216 + *
   1.217 + * @author Peter von der Ah&eacute;
   1.218 + * @author Jonathan Gibbons
   1.219 + * @see DiagnosticListener
   1.220 + * @see Diagnostic
   1.221 + * @see JavaFileManager
   1.222 + * @since 1.6
   1.223 + */
   1.224 +public interface JavaCompiler extends Tool, OptionChecker {
   1.225 +
   1.226 +    /**
   1.227 +     * Creates a future for a compilation task with the given
   1.228 +     * components and arguments.  The compilation might not have
   1.229 +     * completed as described in the CompilationTask interface.
   1.230 +     *
   1.231 +     * <p>If a file manager is provided, it must be able to handle all
   1.232 +     * locations defined in {@link StandardLocation}.
   1.233 +     *
   1.234 +     * @param out a Writer for additional output from the compiler;
   1.235 +     * use {@code System.err} if {@code null}
   1.236 +     * @param fileManager a file manager; if {@code null} use the
   1.237 +     * compiler's standard filemanager
   1.238 +     * @param diagnosticListener a diagnostic listener; if {@code
   1.239 +     * null} use the compiler's default method for reporting
   1.240 +     * diagnostics
   1.241 +     * @param options compiler options, {@code null} means no options
   1.242 +     * @param classes class names (for annotation processing), {@code
   1.243 +     * null} means no class names
   1.244 +     * @param compilationUnits the compilation units to compile, {@code
   1.245 +     * null} means no compilation units
   1.246 +     * @return an object representing the compilation
   1.247 +     * @throws RuntimeException if an unrecoverable error
   1.248 +     * occurred in a user supplied component.  The
   1.249 +     * {@linkplain Throwable#getCause() cause} will be the error in
   1.250 +     * user code.
   1.251 +     * @throws IllegalArgumentException if any of the given
   1.252 +     * compilation units are of other kind than
   1.253 +     * {@linkplain JavaFileObject.Kind#SOURCE source}
   1.254 +     */
   1.255 +    CompilationTask getTask(Writer out,
   1.256 +                            JavaFileManager fileManager,
   1.257 +                            DiagnosticListener<? super JavaFileObject> diagnosticListener,
   1.258 +                            Iterable<String> options,
   1.259 +                            Iterable<String> classes,
   1.260 +                            Iterable<? extends JavaFileObject> compilationUnits);
   1.261 +
   1.262 +    /**
   1.263 +     * Gets a new instance of the standard file manager implementation
   1.264 +     * for this tool.  The file manager will use the given diagnostic
   1.265 +     * listener for producing any non-fatal diagnostics.  Fatal errors
   1.266 +     * will be signalled with the appropriate exceptions.
   1.267 +     *
   1.268 +     * <p>The standard file manager will be automatically reopened if
   1.269 +     * it is accessed after calls to {@code flush} or {@code close}.
   1.270 +     * The standard file manager must be usable with other tools.
   1.271 +     *
   1.272 +     * @param diagnosticListener a diagnostic listener for non-fatal
   1.273 +     * diagnostics; if {@code null} use the compiler's default method
   1.274 +     * for reporting diagnostics
   1.275 +     * @param locale the locale to apply when formatting diagnostics;
   1.276 +     * {@code null} means the {@linkplain Locale#getDefault() default locale}.
   1.277 +     * @param charset the character set used for decoding bytes; if
   1.278 +     * {@code null} use the platform default
   1.279 +     * @return the standard file manager
   1.280 +     */
   1.281 +    StandardJavaFileManager getStandardFileManager(
   1.282 +        DiagnosticListener<? super JavaFileObject> diagnosticListener,
   1.283 +        Locale locale,
   1.284 +        Charset charset);
   1.285 +
   1.286 +    /**
   1.287 +     * Interface representing a future for a compilation task.  The
   1.288 +     * compilation task has not yet started.  To start the task, call
   1.289 +     * the {@linkplain #call call} method.
   1.290 +     *
   1.291 +     * <p>Before calling the call method, additional aspects of the
   1.292 +     * task can be configured, for example, by calling the
   1.293 +     * {@linkplain #setProcessors setProcessors} method.
   1.294 +     */
   1.295 +    interface CompilationTask extends Callable<Boolean> {
   1.296 +
   1.297 +        /**
   1.298 +         * Sets processors (for annotation processing).  This will
   1.299 +         * bypass the normal discovery mechanism.
   1.300 +         *
   1.301 +         * @param processors processors (for annotation processing)
   1.302 +         * @throws IllegalStateException if the task has started
   1.303 +         */
   1.304 +        void setProcessors(Iterable<? extends Processor> processors);
   1.305 +
   1.306 +        /**
   1.307 +         * Set the locale to be applied when formatting diagnostics and
   1.308 +         * other localized data.
   1.309 +         *
   1.310 +         * @param locale the locale to apply; {@code null} means apply no
   1.311 +         * locale
   1.312 +         * @throws IllegalStateException if the task has started
   1.313 +         */
   1.314 +        void setLocale(Locale locale);
   1.315 +
   1.316 +        /**
   1.317 +         * Performs this compilation task.  The compilation may only
   1.318 +         * be performed once.  Subsequent calls to this method throw
   1.319 +         * IllegalStateException.
   1.320 +         *
   1.321 +         * @return true if and only all the files compiled without errors;
   1.322 +         * false otherwise
   1.323 +         *
   1.324 +         * @throws RuntimeException if an unrecoverable error occurred
   1.325 +         * in a user-supplied component.  The
   1.326 +         * {@linkplain Throwable#getCause() cause} will be the error
   1.327 +         * in user code.
   1.328 +         * @throws IllegalStateException if called more than once
   1.329 +         */
   1.330 +        Boolean call();
   1.331 +    }
   1.332 +}

mercurial