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