author | jjg |
Thu, 02 Apr 2015 15:56:07 -0700 | |
changeset 29780 | 8f8e54a1fa20 |
parent 29291 | 076c277565f7 |
child 36526 | 3b41f1c69604 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
25006
5d5fa4abab27
8046369: sjavac should not use javac internal API for starting javac
jjg
parents:
20604
diff
changeset
|
2 |
* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 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 |
* |
|
5520 | 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. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javac.api; |
|
27 |
||
28 |
import java.io.InputStream; |
|
29 |
import java.io.OutputStream; |
|
9069
bcab4a29758f
6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
8844
diff
changeset
|
30 |
import java.io.OutputStreamWriter; |
10 | 31 |
import java.io.PrintWriter; |
32 |
import java.io.Writer; |
|
9069
bcab4a29758f
6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
8844
diff
changeset
|
33 |
import java.nio.charset.Charset; |
10 | 34 |
import java.util.Collections; |
35 |
import java.util.EnumSet; |
|
36 |
import java.util.Locale; |
|
29291
076c277565f7
8073550: java* tools: replace obj.getClass hacks with Assert.checkNonNull or Objects.requireNonNull
mcimadamore
parents:
27379
diff
changeset
|
37 |
import java.util.Objects; |
10 | 38 |
import java.util.Set; |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
39 |
|
10 | 40 |
import javax.lang.model.SourceVersion; |
41 |
import javax.tools.*; |
|
42 |
||
43 |
import com.sun.source.util.JavacTask; |
|
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
10
diff
changeset
|
44 |
import com.sun.tools.javac.file.JavacFileManager; |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
45 |
import com.sun.tools.javac.main.Arguments; |
11314 | 46 |
import com.sun.tools.javac.main.Option; |
29780
8f8e54a1fa20
8076420: Consolidate javac file handling in javac.file package
jjg
parents:
29291
diff
changeset
|
47 |
import com.sun.tools.javac.file.BaseFileManager; |
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
48 |
import com.sun.tools.javac.util.ClientCodeException; |
10 | 49 |
import com.sun.tools.javac.util.Context; |
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
26264
diff
changeset
|
50 |
import com.sun.tools.javac.util.DefinedBy; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
26264
diff
changeset
|
51 |
import com.sun.tools.javac.util.DefinedBy.Api; |
10 | 52 |
import com.sun.tools.javac.util.Log; |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
53 |
import com.sun.tools.javac.util.PropagatedException; |
10 | 54 |
|
55 |
/** |
|
56 |
* TODO: describe com.sun.tools.javac.api.Tool |
|
57 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
58 |
* <p><b>This is NOT part of any supported API. |
10 | 59 |
* If you write code that depends on this, you do so at your own |
60 |
* risk. This code and its internal interfaces are subject to change |
|
61 |
* or deletion without notice.</b></p> |
|
62 |
* |
|
63 |
* @author Peter von der Ah\u00e9 |
|
64 |
*/ |
|
65 |
public final class JavacTool implements JavaCompiler { |
|
66 |
/** |
|
11314 | 67 |
* Constructor used by service provider mechanism. The recommended way to |
68 |
* obtain an instance of this class is by using {@link #create} or the |
|
69 |
* service provider mechanism. |
|
14259 | 70 |
* @see javax.tools.JavaCompiler |
10 | 71 |
* @see javax.tools.ToolProvider |
72 |
* @see #create |
|
73 |
*/ |
|
74 |
@Deprecated |
|
75 |
public JavacTool() {} |
|
76 |
||
77 |
/** |
|
78 |
* Static factory method for creating new instances of this tool. |
|
79 |
* @return new instance of this tool |
|
80 |
*/ |
|
81 |
public static JavacTool create() { |
|
82 |
return new JavacTool(); |
|
83 |
} |
|
84 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
26264
diff
changeset
|
85 |
@Override @DefinedBy(Api.COMPILER) |
10 | 86 |
public JavacFileManager getStandardFileManager( |
87 |
DiagnosticListener<? super JavaFileObject> diagnosticListener, |
|
88 |
Locale locale, |
|
89 |
Charset charset) { |
|
90 |
Context context = new Context(); |
|
9069
bcab4a29758f
6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
8844
diff
changeset
|
91 |
context.put(Locale.class, locale); |
10 | 92 |
if (diagnosticListener != null) |
93 |
context.put(DiagnosticListener.class, diagnosticListener); |
|
9069
bcab4a29758f
6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
8844
diff
changeset
|
94 |
PrintWriter pw = (charset == null) |
bcab4a29758f
6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
8844
diff
changeset
|
95 |
? new PrintWriter(System.err, true) |
bcab4a29758f
6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
8844
diff
changeset
|
96 |
: new PrintWriter(new OutputStreamWriter(System.err, charset), true); |
bcab4a29758f
6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
8844
diff
changeset
|
97 |
context.put(Log.outKey, pw); |
10 | 98 |
return new JavacFileManager(context, true, charset); |
99 |
} |
|
100 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
26264
diff
changeset
|
101 |
@Override @DefinedBy(Api.COMPILER) |
10 | 102 |
public JavacTask getTask(Writer out, |
103 |
JavaFileManager fileManager, |
|
104 |
DiagnosticListener<? super JavaFileObject> diagnosticListener, |
|
105 |
Iterable<String> options, |
|
106 |
Iterable<String> classes, |
|
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
107 |
Iterable<? extends JavaFileObject> compilationUnits) { |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
108 |
Context context = new Context(); |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
109 |
return getTask(out, fileManager, diagnosticListener, |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
110 |
options, classes, compilationUnits, |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
111 |
context); |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
112 |
} |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
113 |
|
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
114 |
/* Internal version of getTask, allowing context to be provided. */ |
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
115 |
public JavacTask getTask(Writer out, |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
116 |
JavaFileManager fileManager, |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
117 |
DiagnosticListener<? super JavaFileObject> diagnosticListener, |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
118 |
Iterable<String> options, |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
119 |
Iterable<String> classes, |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
120 |
Iterable<? extends JavaFileObject> compilationUnits, |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
121 |
Context context) |
10 | 122 |
{ |
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
123 |
try { |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
124 |
ClientCodeWrapper ccw = ClientCodeWrapper.instance(context); |
10 | 125 |
|
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
126 |
if (options != null) { |
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
127 |
for (String option : options) |
29291
076c277565f7
8073550: java* tools: replace obj.getClass hacks with Assert.checkNonNull or Objects.requireNonNull
mcimadamore
parents:
27379
diff
changeset
|
128 |
Objects.requireNonNull(option); |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
129 |
} |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
130 |
|
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
131 |
if (classes != null) { |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
132 |
for (String cls : classes) |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
133 |
if (!SourceVersion.isName(cls)) // implicit null check |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
134 |
throw new IllegalArgumentException("Not a valid class name: " + cls); |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
135 |
} |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
136 |
|
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
137 |
if (compilationUnits != null) { |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
138 |
compilationUnits = ccw.wrapJavaFileObjects(compilationUnits); // implicit null check |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
139 |
for (JavaFileObject cu : compilationUnits) { |
20604
13bdd49ddaf0
8015073: c.s.t.javac.api.JavacTool.getTask() - more informative exception
ksrini
parents:
14957
diff
changeset
|
140 |
if (cu.getKind() != JavaFileObject.Kind.SOURCE) { |
13bdd49ddaf0
8015073: c.s.t.javac.api.JavacTool.getTask() - more informative exception
ksrini
parents:
14957
diff
changeset
|
141 |
String kindMsg = "Compilation unit is not of SOURCE kind: " |
13bdd49ddaf0
8015073: c.s.t.javac.api.JavacTool.getTask() - more informative exception
ksrini
parents:
14957
diff
changeset
|
142 |
+ "\"" + cu.getName() + "\""; |
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
143 |
throw new IllegalArgumentException(kindMsg); |
20604
13bdd49ddaf0
8015073: c.s.t.javac.api.JavacTool.getTask() - more informative exception
ksrini
parents:
14957
diff
changeset
|
144 |
} |
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
145 |
} |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
146 |
} |
10 | 147 |
|
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
148 |
if (diagnosticListener != null) |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
149 |
context.put(DiagnosticListener.class, ccw.wrap(diagnosticListener)); |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
150 |
|
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
151 |
if (out == null) |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
152 |
context.put(Log.outKey, new PrintWriter(System.err, true)); |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
153 |
else |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
154 |
context.put(Log.outKey, new PrintWriter(out, true)); |
10 | 155 |
|
27379
5ae894733e09
8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents:
26266
diff
changeset
|
156 |
if (fileManager == null) { |
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
157 |
fileManager = getStandardFileManager(diagnosticListener, null, null); |
27379
5ae894733e09
8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents:
26266
diff
changeset
|
158 |
if (fileManager instanceof BaseFileManager) { |
5ae894733e09
8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents:
26266
diff
changeset
|
159 |
((BaseFileManager) fileManager).autoClose = true; |
5ae894733e09
8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents:
26266
diff
changeset
|
160 |
} |
5ae894733e09
8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents:
26266
diff
changeset
|
161 |
} |
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
162 |
fileManager = ccw.wrap(fileManager); |
11314 | 163 |
|
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
164 |
context.put(JavaFileManager.class, fileManager); |
11314 | 165 |
|
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
166 |
Arguments args = Arguments.instance(context); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
167 |
args.init("javac", options, classes, compilationUnits); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
168 |
return new JavacTaskImpl(context); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
169 |
} catch (PropagatedException ex) { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
170 |
throw ex.getCause(); |
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
171 |
} catch (ClientCodeException ex) { |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
172 |
throw new RuntimeException(ex.getCause()); |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
173 |
} |
10 | 174 |
} |
175 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
26264
diff
changeset
|
176 |
@Override @DefinedBy(Api.COMPILER) |
10 | 177 |
public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) { |
178 |
if (err == null) |
|
179 |
err = System.err; |
|
180 |
for (String argument : arguments) |
|
29291
076c277565f7
8073550: java* tools: replace obj.getClass hacks with Assert.checkNonNull or Objects.requireNonNull
mcimadamore
parents:
27379
diff
changeset
|
181 |
Objects.requireNonNull(argument); |
10 | 182 |
return com.sun.tools.javac.Main.compile(arguments, new PrintWriter(err, true)); |
183 |
} |
|
184 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
26264
diff
changeset
|
185 |
@Override @DefinedBy(Api.COMPILER) |
10 | 186 |
public Set<SourceVersion> getSourceVersions() { |
187 |
return Collections.unmodifiableSet(EnumSet.range(SourceVersion.RELEASE_3, |
|
188 |
SourceVersion.latest())); |
|
189 |
} |
|
190 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
26264
diff
changeset
|
191 |
@Override @DefinedBy(Api.COMPILER) |
10 | 192 |
public int isSupportedOption(String option) { |
11314 | 193 |
Set<Option> recognizedOptions = Option.getJavacToolOptions(); |
194 |
for (Option o : recognizedOptions) { |
|
10 | 195 |
if (o.matches(option)) |
196 |
return o.hasArg() ? 1 : 0; |
|
197 |
} |
|
198 |
return -1; |
|
199 |
} |
|
200 |
||
201 |
} |