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