author | jjg |
Mon, 16 Jun 2014 11:30:31 -0700 | |
changeset 25006 | 5d5fa4abab27 |
parent 24897 | 655b72d7b96e |
child 25445 | 603f0c93d5c9 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
23792
eabe3e8a29bf
7118295: javac does not explicitly close -Xstdout file
kizune
parents:
22163
diff
changeset
|
2 |
* Copyright (c) 1999, 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.main; |
|
27 |
||
28 |
import java.io.File; |
|
29 |
import java.io.IOException; |
|
30 |
import java.io.PrintWriter; |
|
5319
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
31 |
import java.net.URL; |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
32 |
import java.security.DigestInputStream; |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
33 |
import java.security.MessageDigest; |
11708
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
34 |
import java.util.Arrays; |
10193
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
35 |
import java.util.Collection; |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
23792
diff
changeset
|
36 |
import java.util.LinkedHashMap; |
10193
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
37 |
import java.util.LinkedHashSet; |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
23792
diff
changeset
|
38 |
import java.util.Map; |
10193
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
39 |
import java.util.Set; |
14548
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
40 |
|
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
41 |
import javax.annotation.processing.Processor; |
6721 | 42 |
import javax.tools.JavaFileManager; |
43 |
import javax.tools.JavaFileObject; |
|
10 | 44 |
|
14548
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
45 |
import com.sun.source.util.JavacTask; |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
46 |
import com.sun.source.util.Plugin; |
14960 | 47 |
import com.sun.tools.doclint.DocLint; |
48 |
import com.sun.tools.javac.api.BasicJavacTask; |
|
10 | 49 |
import com.sun.tools.javac.code.Source; |
1208 | 50 |
import com.sun.tools.javac.file.CacheFSInfo; |
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
10
diff
changeset
|
51 |
import com.sun.tools.javac.file.JavacFileManager; |
15724 | 52 |
import com.sun.tools.javac.jvm.Profile; |
10 | 53 |
import com.sun.tools.javac.jvm.Target; |
14548
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
54 |
import com.sun.tools.javac.processing.AnnotationProcessingError; |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
55 |
import com.sun.tools.javac.processing.JavacProcessingEnvironment; |
10 | 56 |
import com.sun.tools.javac.util.*; |
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
57 |
import com.sun.tools.javac.util.Log.PrefixKind; |
14548
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
58 |
import com.sun.tools.javac.util.Log.WriterKind; |
17551
03f330c02d97
8004082: test/tools/javac/plugin/showtype/Test.java fails on windows: jtreg can't delete plugin.jar
jjg
parents:
16799
diff
changeset
|
59 |
import com.sun.tools.javac.util.ServiceLoader; |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
23792
diff
changeset
|
60 |
|
11314 | 61 |
import static com.sun.tools.javac.main.Option.*; |
10 | 62 |
|
14548
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
63 |
/** This class provides a command line interface to the javac compiler. |
10 | 64 |
* |
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
65 |
* <p><b>This is NOT part of any supported API. |
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
66 |
* If you write code that depends on this, you do so at your own risk. |
10 | 67 |
* This code and its internal interfaces are subject to change or |
68 |
* deletion without notice.</b> |
|
69 |
*/ |
|
70 |
public class Main { |
|
71 |
||
72 |
/** The name of the compiler, for use in diagnostics. |
|
73 |
*/ |
|
74 |
String ownName; |
|
75 |
||
76 |
/** The writer to use for diagnostic output. |
|
77 |
*/ |
|
78 |
PrintWriter out; |
|
79 |
||
11052
65b9fa7eaf55
7108668: allow Log to be initialized and used earlier
jjg
parents:
10638
diff
changeset
|
80 |
/** The log to use for diagnostic output. |
65b9fa7eaf55
7108668: allow Log to be initialized and used earlier
jjg
parents:
10638
diff
changeset
|
81 |
*/ |
15724 | 82 |
public Log log; |
11052
65b9fa7eaf55
7108668: allow Log to be initialized and used earlier
jjg
parents:
10638
diff
changeset
|
83 |
|
10 | 84 |
/** |
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
85 |
* If true, certain errors will cause an exception, such as command line |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
86 |
* arg errors, or exceptions in user provided code. |
10 | 87 |
*/ |
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
88 |
boolean apiMode; |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
89 |
|
10 | 90 |
|
91 |
/** Result codes. |
|
92 |
*/ |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
93 |
public enum Result { |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
94 |
OK(0), // Compilation completed with no errors. |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
95 |
ERROR(1), // Completed but reported errors. |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
96 |
CMDERR(2), // Bad command-line arguments |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
97 |
SYSERR(3), // System error or resource exhaustion. |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
98 |
ABNORMAL(4); // Compiler terminated abnormally |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
99 |
|
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
100 |
Result(int exitCode) { |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
101 |
this.exitCode = exitCode; |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
102 |
} |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
103 |
|
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
104 |
public boolean isOK() { |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
105 |
return (exitCode == 0); |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
106 |
} |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
107 |
|
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
108 |
public final int exitCode; |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
109 |
} |
10 | 110 |
|
11314 | 111 |
private Option[] recognizedOptions = |
112 |
Option.getJavaCompilerOptions().toArray(new Option[0]); |
|
10 | 113 |
|
11314 | 114 |
private OptionHelper optionHelper = new OptionHelper() { |
115 |
@Override |
|
116 |
public String get(Option option) { |
|
117 |
return options.get(option); |
|
118 |
} |
|
119 |
||
120 |
@Override |
|
121 |
public void put(String name, String value) { |
|
122 |
options.put(name, value); |
|
10 | 123 |
} |
124 |
||
11314 | 125 |
@Override |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
23792
diff
changeset
|
126 |
public boolean handleFileManagerOption(Option option, String value) { |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
23792
diff
changeset
|
127 |
options.put(option.getText(), value); |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
23792
diff
changeset
|
128 |
deferredFileManagerOptions.put(option, value); |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
23792
diff
changeset
|
129 |
return true; |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
23792
diff
changeset
|
130 |
} |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
23792
diff
changeset
|
131 |
|
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
23792
diff
changeset
|
132 |
@Override |
11314 | 133 |
public void remove(String name) { |
134 |
options.remove(name); |
|
135 |
} |
|
136 |
||
137 |
@Override |
|
138 |
public Log getLog() { |
|
139 |
return log; |
|
140 |
} |
|
141 |
||
142 |
@Override |
|
143 |
public String getOwnName() { |
|
144 |
return ownName; |
|
145 |
} |
|
146 |
||
147 |
@Override |
|
10 | 148 |
public void error(String key, Object... args) { |
149 |
Main.this.error(key, args); |
|
150 |
} |
|
151 |
||
11314 | 152 |
@Override |
10 | 153 |
public void addFile(File f) { |
10193
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
154 |
filenames.add(f); |
10 | 155 |
} |
156 |
||
11314 | 157 |
@Override |
10 | 158 |
public void addClassName(String s) { |
159 |
classnames.append(s); |
|
160 |
} |
|
161 |
||
11314 | 162 |
}; |
10 | 163 |
|
164 |
/** |
|
165 |
* Construct a compiler instance. |
|
166 |
*/ |
|
167 |
public Main(String name) { |
|
168 |
this(name, new PrintWriter(System.err, true)); |
|
169 |
} |
|
170 |
||
171 |
/** |
|
172 |
* Construct a compiler instance. |
|
173 |
*/ |
|
174 |
public Main(String name, PrintWriter out) { |
|
175 |
this.ownName = name; |
|
176 |
this.out = out; |
|
177 |
} |
|
15724 | 178 |
|
10 | 179 |
/** A table of all options that's passed to the JavaCompiler constructor. */ |
180 |
private Options options = null; |
|
181 |
||
182 |
/** The list of source files to process |
|
183 |
*/ |
|
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
23792
diff
changeset
|
184 |
public Set<File> filenames = null; // XXX should be protected or private |
10 | 185 |
|
186 |
/** List of class files names passed on the command line |
|
187 |
*/ |
|
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
23792
diff
changeset
|
188 |
protected ListBuffer<String> classnames = null; |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
23792
diff
changeset
|
189 |
|
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
23792
diff
changeset
|
190 |
public Map<Option, String> deferredFileManagerOptions; // XXX should be protected or private |
10 | 191 |
|
192 |
/** Report a usage error. |
|
193 |
*/ |
|
194 |
void error(String key, Object... args) { |
|
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
195 |
if (apiMode) { |
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
196 |
String msg = log.localize(PrefixKind.JAVAC, key, args); |
10 | 197 |
throw new PropagatedException(new IllegalStateException(msg)); |
198 |
} |
|
199 |
warning(key, args); |
|
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
200 |
log.printLines(PrefixKind.JAVAC, "msg.usage", ownName); |
10 | 201 |
} |
202 |
||
203 |
/** Report a warning. |
|
204 |
*/ |
|
205 |
void warning(String key, Object... args) { |
|
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
206 |
log.printRawLines(ownName + ": " + log.localize(PrefixKind.JAVAC, key, args)); |
10 | 207 |
} |
208 |
||
209 |
public Option getOption(String flag) { |
|
210 |
for (Option option : recognizedOptions) { |
|
211 |
if (option.matches(flag)) |
|
212 |
return option; |
|
213 |
} |
|
214 |
return null; |
|
215 |
} |
|
216 |
||
217 |
public void setOptions(Options options) { |
|
218 |
if (options == null) |
|
219 |
throw new NullPointerException(); |
|
220 |
this.options = options; |
|
221 |
} |
|
222 |
||
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
223 |
public void setAPIMode(boolean apiMode) { |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
224 |
this.apiMode = apiMode; |
10 | 225 |
} |
226 |
||
227 |
/** Process command line arguments: store all command line options |
|
228 |
* in `options' table and return all source filenames. |
|
229 |
* @param flags The array of command line arguments. |
|
230 |
*/ |
|
10193
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
231 |
public Collection<File> processArgs(String[] flags) { // XXX sb protected |
11708
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
232 |
return processArgs(flags, null); |
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
233 |
} |
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
234 |
|
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
235 |
public Collection<File> processArgs(String[] flags, String[] classNames) { // XXX sb protected |
10 | 236 |
int ac = 0; |
237 |
while (ac < flags.length) { |
|
238 |
String flag = flags[ac]; |
|
239 |
ac++; |
|
240 |
||
241 |
Option option = null; |
|
242 |
||
243 |
if (flag.length() > 0) { |
|
244 |
// quick hack to speed up file processing: |
|
245 |
// if the option does not begin with '-', there is no need to check |
|
246 |
// most of the compiler options. |
|
247 |
int firstOptionToCheck = flag.charAt(0) == '-' ? 0 : recognizedOptions.length-1; |
|
248 |
for (int j=firstOptionToCheck; j<recognizedOptions.length; j++) { |
|
249 |
if (recognizedOptions[j].matches(flag)) { |
|
250 |
option = recognizedOptions[j]; |
|
251 |
break; |
|
252 |
} |
|
253 |
} |
|
254 |
} |
|
255 |
||
256 |
if (option == null) { |
|
257 |
error("err.invalid.flag", flag); |
|
258 |
return null; |
|
259 |
} |
|
260 |
||
261 |
if (option.hasArg()) { |
|
262 |
if (ac == flags.length) { |
|
263 |
error("err.req.arg", flag); |
|
264 |
return null; |
|
265 |
} |
|
266 |
String operand = flags[ac]; |
|
267 |
ac++; |
|
11314 | 268 |
if (option.process(optionHelper, flag, operand)) |
10 | 269 |
return null; |
270 |
} else { |
|
11314 | 271 |
if (option.process(optionHelper, flag)) |
10 | 272 |
return null; |
273 |
} |
|
274 |
} |
|
275 |
||
19489
387a4dd51adf
8009640: -profile <compact> does not work when -bootclasspath specified
vromero
parents:
18653
diff
changeset
|
276 |
if (options.get(PROFILE) != null && options.get(BOOTCLASSPATH) != null) { |
387a4dd51adf
8009640: -profile <compact> does not work when -bootclasspath specified
vromero
parents:
18653
diff
changeset
|
277 |
error("err.profile.bootclasspath.conflict"); |
387a4dd51adf
8009640: -profile <compact> does not work when -bootclasspath specified
vromero
parents:
18653
diff
changeset
|
278 |
return null; |
387a4dd51adf
8009640: -profile <compact> does not work when -bootclasspath specified
vromero
parents:
18653
diff
changeset
|
279 |
} |
387a4dd51adf
8009640: -profile <compact> does not work when -bootclasspath specified
vromero
parents:
18653
diff
changeset
|
280 |
|
11708
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
281 |
if (this.classnames != null && classNames != null) { |
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
282 |
this.classnames.addAll(Arrays.asList(classNames)); |
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
283 |
} |
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
284 |
|
6721 | 285 |
if (!checkDirectory(D)) |
10 | 286 |
return null; |
6721 | 287 |
if (!checkDirectory(S)) |
10 | 288 |
return null; |
289 |
||
6721 | 290 |
String sourceString = options.get(SOURCE); |
10 | 291 |
Source source = (sourceString != null) |
292 |
? Source.lookup(sourceString) |
|
293 |
: Source.DEFAULT; |
|
6721 | 294 |
String targetString = options.get(TARGET); |
10 | 295 |
Target target = (targetString != null) |
296 |
? Target.lookup(targetString) |
|
297 |
: Target.DEFAULT; |
|
298 |
// We don't check source/target consistency for CLDC, as J2ME |
|
299 |
// profiles are not aligned with J2SE targets; moreover, a |
|
300 |
// single CLDC target may have many profiles. In addition, |
|
301 |
// this is needed for the continued functioning of the JSR14 |
|
302 |
// prototype. |
|
303 |
if (Character.isDigit(target.name.charAt(0))) { |
|
304 |
if (target.compareTo(source.requiredTarget()) < 0) { |
|
305 |
if (targetString != null) { |
|
306 |
if (sourceString == null) { |
|
307 |
warning("warn.target.default.source.conflict", |
|
308 |
targetString, |
|
309 |
source.requiredTarget().name); |
|
310 |
} else { |
|
311 |
warning("warn.source.target.conflict", |
|
312 |
sourceString, |
|
313 |
source.requiredTarget().name); |
|
314 |
} |
|
315 |
return null; |
|
316 |
} else { |
|
2723
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
317 |
target = source.requiredTarget(); |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
318 |
options.put("-target", target.name); |
10 | 319 |
} |
320 |
} else { |
|
321 |
if (targetString == null && !source.allowGenerics()) { |
|
2723
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
322 |
target = Target.JDK1_4; |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
323 |
options.put("-target", target.name); |
10 | 324 |
} |
325 |
} |
|
326 |
} |
|
5319
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
327 |
|
15724 | 328 |
String profileString = options.get(PROFILE); |
329 |
if (profileString != null) { |
|
330 |
Profile profile = Profile.lookup(profileString); |
|
331 |
if (!profile.isValid(target)) { |
|
332 |
warning("warn.profile.target.conflict", profileString, target.name); |
|
333 |
return null; |
|
334 |
} |
|
335 |
} |
|
336 |
||
5319
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
337 |
// handle this here so it works even if no other options given |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
338 |
String showClass = options.get("showClass"); |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
339 |
if (showClass != null) { |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
340 |
if (showClass.equals("showClass")) // no value given for option |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
341 |
showClass = "com.sun.tools.javac.Main"; |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
342 |
showClass(showClass); |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
343 |
} |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
344 |
|
11052
65b9fa7eaf55
7108668: allow Log to be initialized and used earlier
jjg
parents:
10638
diff
changeset
|
345 |
options.notifyListeners(); |
65b9fa7eaf55
7108668: allow Log to be initialized and used earlier
jjg
parents:
10638
diff
changeset
|
346 |
|
10193
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
347 |
return filenames; |
10 | 348 |
} |
349 |
// where |
|
11314 | 350 |
private boolean checkDirectory(Option option) { |
351 |
String value = options.get(option); |
|
10 | 352 |
if (value == null) |
353 |
return true; |
|
354 |
File file = new File(value); |
|
355 |
if (!file.exists()) { |
|
356 |
error("err.dir.not.found", value); |
|
357 |
return false; |
|
358 |
} |
|
359 |
if (!file.isDirectory()) { |
|
360 |
error("err.file.not.directory", value); |
|
361 |
return false; |
|
362 |
} |
|
363 |
return true; |
|
364 |
} |
|
365 |
||
366 |
/** Programmatic interface for main function. |
|
367 |
* @param args The command line parameters. |
|
368 |
*/ |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
369 |
public Result compile(String[] args) { |
10 | 370 |
Context context = new Context(); |
371 |
JavacFileManager.preRegister(context); // can't create it until Log has been set up |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
372 |
Result result = compile(args, context); |
10 | 373 |
if (fileManager instanceof JavacFileManager) { |
374 |
// A fresh context was created above, so jfm must be a JavacFileManager |
|
375 |
((JavacFileManager)fileManager).close(); |
|
376 |
} |
|
377 |
return result; |
|
378 |
} |
|
379 |
||
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
380 |
public Result compile(String[] args, Context context) { |
10 | 381 |
return compile(args, context, List.<JavaFileObject>nil(), null); |
382 |
} |
|
383 |
||
384 |
/** Programmatic interface for main function. |
|
385 |
* @param args The command line parameters. |
|
386 |
*/ |
|
25006
5d5fa4abab27
8046369: sjavac should not use javac internal API for starting javac
jjg
parents:
24897
diff
changeset
|
387 |
protected Result compile(String[] args, |
10 | 388 |
Context context, |
389 |
List<JavaFileObject> fileObjects, |
|
390 |
Iterable<? extends Processor> processors) |
|
391 |
{ |
|
11708
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
392 |
return compile(args, null, context, fileObjects, processors); |
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
393 |
} |
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
394 |
|
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
395 |
public Result compile(String[] args, |
18653
1407564fb956
8014230: Compilation incorrectly succeeds with inner class constructor with 254 parameters
emc
parents:
17551
diff
changeset
|
396 |
String[] classNames, |
1407564fb956
8014230: Compilation incorrectly succeeds with inner class constructor with 254 parameters
emc
parents:
17551
diff
changeset
|
397 |
Context context, |
1407564fb956
8014230: Compilation incorrectly succeeds with inner class constructor with 254 parameters
emc
parents:
17551
diff
changeset
|
398 |
List<JavaFileObject> fileObjects, |
1407564fb956
8014230: Compilation incorrectly succeeds with inner class constructor with 254 parameters
emc
parents:
17551
diff
changeset
|
399 |
Iterable<? extends Processor> processors) |
11708
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
400 |
{ |
11052
65b9fa7eaf55
7108668: allow Log to be initialized and used earlier
jjg
parents:
10638
diff
changeset
|
401 |
context.put(Log.outKey, out); |
65b9fa7eaf55
7108668: allow Log to be initialized and used earlier
jjg
parents:
10638
diff
changeset
|
402 |
log = Log.instance(context); |
65b9fa7eaf55
7108668: allow Log to be initialized and used earlier
jjg
parents:
10638
diff
changeset
|
403 |
|
10 | 404 |
if (options == null) |
405 |
options = Options.instance(context); // creates a new one |
|
406 |
||
22163 | 407 |
filenames = new LinkedHashSet<>(); |
408 |
classnames = new ListBuffer<>(); |
|
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
23792
diff
changeset
|
409 |
deferredFileManagerOptions = new LinkedHashMap<>(); |
10 | 410 |
JavaCompiler comp = null; |
411 |
/* |
|
412 |
* TODO: Logic below about what is an acceptable command line |
|
413 |
* should be updated to take annotation processing semantics |
|
414 |
* into account. |
|
415 |
*/ |
|
416 |
try { |
|
11708
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
417 |
if (args.length == 0 |
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
418 |
&& (classNames == null || classNames.length == 0) |
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
419 |
&& fileObjects.isEmpty()) { |
11314 | 420 |
Option.HELP.process(optionHelper, "-help"); |
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
421 |
return Result.CMDERR; |
10 | 422 |
} |
423 |
||
10193
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
424 |
Collection<File> files; |
10 | 425 |
try { |
11708
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
426 |
files = processArgs(CommandLine.parse(args), classNames); |
1866 | 427 |
if (files == null) { |
10 | 428 |
// null signals an error in options, abort |
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
429 |
return Result.CMDERR; |
1866 | 430 |
} else if (files.isEmpty() && fileObjects.isEmpty() && classnames.isEmpty()) { |
10 | 431 |
// it is allowed to compile nothing if just asking for help or version info |
6721 | 432 |
if (options.isSet(HELP) |
433 |
|| options.isSet(X) |
|
434 |
|| options.isSet(VERSION) |
|
435 |
|| options.isSet(FULLVERSION)) |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
436 |
return Result.OK; |
8623
cc57bd7697a2
6986895: compiler gives misleading message for no input files
jjg
parents:
8036
diff
changeset
|
437 |
if (JavaCompiler.explicitAnnotationProcessingRequested(options)) { |
cc57bd7697a2
6986895: compiler gives misleading message for no input files
jjg
parents:
8036
diff
changeset
|
438 |
error("err.no.source.files.classes"); |
cc57bd7697a2
6986895: compiler gives misleading message for no input files
jjg
parents:
8036
diff
changeset
|
439 |
} else { |
cc57bd7697a2
6986895: compiler gives misleading message for no input files
jjg
parents:
8036
diff
changeset
|
440 |
error("err.no.source.files"); |
cc57bd7697a2
6986895: compiler gives misleading message for no input files
jjg
parents:
8036
diff
changeset
|
441 |
} |
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
442 |
return Result.CMDERR; |
10 | 443 |
} |
444 |
} catch (java.io.FileNotFoundException e) { |
|
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
445 |
warning("err.file.not.found", e.getMessage()); |
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
446 |
return Result.SYSERR; |
10 | 447 |
} |
448 |
||
6721 | 449 |
boolean forceStdOut = options.isSet("stdout"); |
10 | 450 |
if (forceStdOut) { |
11052
65b9fa7eaf55
7108668: allow Log to be initialized and used earlier
jjg
parents:
10638
diff
changeset
|
451 |
log.flush(); |
11314 | 452 |
log.setWriters(new PrintWriter(System.out, true)); |
10 | 453 |
} |
454 |
||
1208 | 455 |
// allow System property in following line as a Mustang legacy |
6721 | 456 |
boolean batchMode = (options.isUnset("nonBatchMode") |
1208 | 457 |
&& System.getProperty("nonBatchMode") == null); |
458 |
if (batchMode) |
|
459 |
CacheFSInfo.preRegister(context); |
|
460 |
||
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
23792
diff
changeset
|
461 |
fileManager = context.get(JavaFileManager.class); |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
23792
diff
changeset
|
462 |
if (fileManager instanceof BaseFileManager) { |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
23792
diff
changeset
|
463 |
((BaseFileManager) fileManager).handleOptions(deferredFileManagerOptions); |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
23792
diff
changeset
|
464 |
} |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
23792
diff
changeset
|
465 |
|
14960 | 466 |
// FIXME: this code will not be invoked if using JavacTask.parse/analyze/generate |
14548
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
467 |
// invoke any available plugins |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
468 |
String plugins = options.get(PLUGIN); |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
469 |
if (plugins != null) { |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
470 |
JavacProcessingEnvironment pEnv = JavacProcessingEnvironment.instance(context); |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
471 |
ClassLoader cl = pEnv.getProcessorClassLoader(); |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
472 |
ServiceLoader<Plugin> sl = ServiceLoader.load(Plugin.class, cl); |
22163 | 473 |
Set<List<String>> pluginsToCall = new LinkedHashSet<>(); |
14548
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
474 |
for (String plugin: plugins.split("\\x00")) { |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
475 |
pluginsToCall.add(List.from(plugin.split("\\s+"))); |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
476 |
} |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
477 |
JavacTask task = null; |
22159
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
19489
diff
changeset
|
478 |
for (Plugin plugin : sl) { |
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
19489
diff
changeset
|
479 |
for (List<String> p : pluginsToCall) { |
14548
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
480 |
if (plugin.getName().equals(p.head)) { |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
481 |
pluginsToCall.remove(p); |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
482 |
try { |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
483 |
if (task == null) |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
484 |
task = JavacTask.instance(pEnv); |
14954 | 485 |
plugin.init(task, p.tail.toArray(new String[p.tail.size()])); |
14548
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
486 |
} catch (Throwable ex) { |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
487 |
if (apiMode) |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
488 |
throw new RuntimeException(ex); |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
489 |
pluginMessage(ex); |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
490 |
return Result.SYSERR; |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
491 |
} |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
492 |
} |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
493 |
} |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
494 |
} |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
495 |
for (List<String> p: pluginsToCall) { |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
496 |
log.printLines(PrefixKind.JAVAC, "msg.plugin.not.found", p.head); |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
497 |
} |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
498 |
} |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
499 |
|
14960 | 500 |
comp = JavaCompiler.instance(context); |
10 | 501 |
|
14960 | 502 |
// FIXME: this code will not be invoked if using JavacTask.parse/analyze/generate |
503 |
String xdoclint = options.get(XDOCLINT); |
|
504 |
String xdoclintCustom = options.get(XDOCLINT_CUSTOM); |
|
505 |
if (xdoclint != null || xdoclintCustom != null) { |
|
22163 | 506 |
Set<String> doclintOpts = new LinkedHashSet<>(); |
14960 | 507 |
if (xdoclint != null) |
508 |
doclintOpts.add(DocLint.XMSGS_OPTION); |
|
509 |
if (xdoclintCustom != null) { |
|
510 |
for (String s: xdoclintCustom.split("\\s+")) { |
|
511 |
if (s.isEmpty()) |
|
512 |
continue; |
|
513 |
doclintOpts.add(s.replace(XDOCLINT_CUSTOM.text, DocLint.XMSGS_CUSTOM_PREFIX)); |
|
514 |
} |
|
515 |
} |
|
516 |
if (!(doclintOpts.size() == 1 |
|
517 |
&& doclintOpts.iterator().next().equals(DocLint.XMSGS_CUSTOM_PREFIX + "none"))) { |
|
518 |
JavacTask t = BasicJavacTask.instance(context); |
|
16799
fbe4d27b8791
8006346: doclint should make allowance for headers generated by standard doclet
jjg
parents:
15724
diff
changeset
|
519 |
// standard doclet normally generates H1, H2 |
fbe4d27b8791
8006346: doclint should make allowance for headers generated by standard doclet
jjg
parents:
15724
diff
changeset
|
520 |
doclintOpts.add(DocLint.XIMPLICIT_HEADERS + "2"); |
14960 | 521 |
new DocLint().init(t, doclintOpts.toArray(new String[doclintOpts.size()])); |
522 |
comp.keepComments = true; |
|
523 |
} |
|
524 |
} |
|
525 |
||
23792
eabe3e8a29bf
7118295: javac does not explicitly close -Xstdout file
kizune
parents:
22163
diff
changeset
|
526 |
if (options.get(XSTDOUT) != null) { |
eabe3e8a29bf
7118295: javac does not explicitly close -Xstdout file
kizune
parents:
22163
diff
changeset
|
527 |
// Stdout reassigned - ask compiler to close it when it is done |
eabe3e8a29bf
7118295: javac does not explicitly close -Xstdout file
kizune
parents:
22163
diff
changeset
|
528 |
comp.closeables = comp.closeables.prepend(log.getWriter(WriterKind.NOTICE)); |
eabe3e8a29bf
7118295: javac does not explicitly close -Xstdout file
kizune
parents:
22163
diff
changeset
|
529 |
} |
eabe3e8a29bf
7118295: javac does not explicitly close -Xstdout file
kizune
parents:
22163
diff
changeset
|
530 |
|
1866 | 531 |
if (!files.isEmpty()) { |
10 | 532 |
// add filenames to fileObjects |
533 |
comp = JavaCompiler.instance(context); |
|
534 |
List<JavaFileObject> otherFiles = List.nil(); |
|
535 |
JavacFileManager dfm = (JavacFileManager)fileManager; |
|
1866 | 536 |
for (JavaFileObject fo : dfm.getJavaFileObjectsFromFiles(files)) |
10 | 537 |
otherFiles = otherFiles.prepend(fo); |
538 |
for (JavaFileObject fo : otherFiles) |
|
539 |
fileObjects = fileObjects.prepend(fo); |
|
540 |
} |
|
541 |
comp.compile(fileObjects, |
|
542 |
classnames.toList(), |
|
543 |
processors); |
|
544 |
||
1866 | 545 |
if (log.expectDiagKeys != null) { |
7335
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
6721
diff
changeset
|
546 |
if (log.expectDiagKeys.isEmpty()) { |
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
547 |
log.printRawLines("all expected diagnostics found"); |
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
548 |
return Result.OK; |
1866 | 549 |
} else { |
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
550 |
log.printRawLines("expected diagnostic keys not found: " + log.expectDiagKeys); |
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
551 |
return Result.ERROR; |
1866 | 552 |
} |
553 |
} |
|
554 |
||
1996 | 555 |
if (comp.errorCount() != 0) |
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
556 |
return Result.ERROR; |
10 | 557 |
} catch (IOException ex) { |
558 |
ioMessage(ex); |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
559 |
return Result.SYSERR; |
10 | 560 |
} catch (OutOfMemoryError ex) { |
561 |
resourceMessage(ex); |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
562 |
return Result.SYSERR; |
10 | 563 |
} catch (StackOverflowError ex) { |
564 |
resourceMessage(ex); |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
565 |
return Result.SYSERR; |
10 | 566 |
} catch (FatalError ex) { |
567 |
feMessage(ex); |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
568 |
return Result.SYSERR; |
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
569 |
} catch (AnnotationProcessingError ex) { |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
570 |
if (apiMode) |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
571 |
throw new RuntimeException(ex.getCause()); |
10 | 572 |
apMessage(ex); |
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
573 |
return Result.SYSERR; |
10 | 574 |
} catch (ClientCodeException ex) { |
575 |
// as specified by javax.tools.JavaCompiler#getTask |
|
576 |
// and javax.tools.JavaCompiler.CompilationTask#call |
|
577 |
throw new RuntimeException(ex.getCause()); |
|
578 |
} catch (PropagatedException ex) { |
|
579 |
throw ex.getCause(); |
|
580 |
} catch (Throwable ex) { |
|
581 |
// Nasty. If we've already reported an error, compensate |
|
582 |
// for buggy compiler error recovery by swallowing thrown |
|
583 |
// exceptions. |
|
584 |
if (comp == null || comp.errorCount() == 0 || |
|
6721 | 585 |
options == null || options.isSet("dev")) |
10 | 586 |
bugMessage(ex); |
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
587 |
return Result.ABNORMAL; |
10 | 588 |
} finally { |
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
589 |
if (comp != null) { |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
590 |
try { |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
591 |
comp.close(); |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
592 |
} catch (ClientCodeException ex) { |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
593 |
throw new RuntimeException(ex.getCause()); |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
594 |
} |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
595 |
} |
10 | 596 |
filenames = null; |
597 |
options = null; |
|
598 |
} |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
599 |
return Result.OK; |
10 | 600 |
} |
601 |
||
602 |
/** Print a message reporting an internal error. |
|
603 |
*/ |
|
604 |
void bugMessage(Throwable ex) { |
|
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
605 |
log.printLines(PrefixKind.JAVAC, "msg.bug", JavaCompiler.version()); |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
606 |
ex.printStackTrace(log.getWriter(WriterKind.NOTICE)); |
10 | 607 |
} |
608 |
||
6581 | 609 |
/** Print a message reporting a fatal error. |
10 | 610 |
*/ |
611 |
void feMessage(Throwable ex) { |
|
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
612 |
log.printRawLines(ex.getMessage()); |
6721 | 613 |
if (ex.getCause() != null && options.isSet("dev")) { |
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
614 |
ex.getCause().printStackTrace(log.getWriter(WriterKind.NOTICE)); |
6581 | 615 |
} |
10 | 616 |
} |
617 |
||
618 |
/** Print a message reporting an input/output error. |
|
619 |
*/ |
|
620 |
void ioMessage(Throwable ex) { |
|
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
621 |
log.printLines(PrefixKind.JAVAC, "msg.io"); |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
622 |
ex.printStackTrace(log.getWriter(WriterKind.NOTICE)); |
10 | 623 |
} |
624 |
||
625 |
/** Print a message reporting an out-of-resources error. |
|
626 |
*/ |
|
627 |
void resourceMessage(Throwable ex) { |
|
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
628 |
log.printLines(PrefixKind.JAVAC, "msg.resource"); |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
629 |
ex.printStackTrace(log.getWriter(WriterKind.NOTICE)); |
10 | 630 |
} |
631 |
||
632 |
/** Print a message reporting an uncaught exception from an |
|
633 |
* annotation processor. |
|
634 |
*/ |
|
635 |
void apMessage(AnnotationProcessingError ex) { |
|
14448 | 636 |
log.printLines(PrefixKind.JAVAC, "msg.proc.annotation.uncaught.exception"); |
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
637 |
ex.getCause().printStackTrace(log.getWriter(WriterKind.NOTICE)); |
10 | 638 |
} |
639 |
||
14548
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
640 |
/** Print a message reporting an uncaught exception from an |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
641 |
* annotation processor. |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
642 |
*/ |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
643 |
void pluginMessage(Throwable ex) { |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
644 |
log.printLines(PrefixKind.JAVAC, "msg.plugin.uncaught.exception"); |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
645 |
ex.printStackTrace(log.getWriter(WriterKind.NOTICE)); |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
646 |
} |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
647 |
|
5319
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
648 |
/** Display the location and checksum of a class. */ |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
649 |
void showClass(String className) { |
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
650 |
PrintWriter pw = log.getWriter(WriterKind.NOTICE); |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
651 |
pw.println("javac: show class: " + className); |
5319
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
652 |
URL url = getClass().getResource('/' + className.replace('.', '/') + ".class"); |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
653 |
if (url == null) |
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
654 |
pw.println(" class not found"); |
5319
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
655 |
else { |
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
656 |
pw.println(" " + url); |
5319
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
657 |
try { |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
658 |
final String algorithm = "MD5"; |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
659 |
byte[] digest; |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
660 |
MessageDigest md = MessageDigest.getInstance(algorithm); |
22159
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
19489
diff
changeset
|
661 |
try (DigestInputStream in = new DigestInputStream(url.openStream(), md)) { |
5319
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
662 |
byte[] buf = new byte[8192]; |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
663 |
int n; |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
664 |
do { n = in.read(buf); } while (n > 0); |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
665 |
digest = md.digest(); |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
666 |
} |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
667 |
StringBuilder sb = new StringBuilder(); |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
668 |
for (byte b: digest) |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
669 |
sb.append(String.format("%02x", b)); |
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
670 |
pw.println(" " + algorithm + " checksum: " + sb); |
5319
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
671 |
} catch (Exception e) { |
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
672 |
pw.println(" cannot compute digest: " + e); |
5319
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
673 |
} |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
674 |
} |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
675 |
} |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
676 |
|
10 | 677 |
private JavaFileManager fileManager; |
678 |
||
679 |
/* ************************************************************************ |
|
680 |
* Internationalization |
|
681 |
*************************************************************************/ |
|
682 |
||
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
683 |
// /** Find a localized string in the resource bundle. |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
684 |
// * @param key The key for the localized string. |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
685 |
// */ |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
686 |
// public static String getLocalizedString(String key, Object... args) { // FIXME sb private |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
687 |
// try { |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
688 |
// if (messages == null) |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
689 |
// messages = new JavacMessages(javacBundleName); |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
690 |
// return messages.getLocalizedString("javac." + key, args); |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
691 |
// } |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
692 |
// catch (MissingResourceException e) { |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
693 |
// throw new Error("Fatal Error: Resource for javac is missing", e); |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
694 |
// } |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
695 |
// } |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
696 |
// |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
697 |
// public static void useRawMessages(boolean enable) { |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
698 |
// if (enable) { |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
699 |
// messages = new JavacMessages(javacBundleName) { |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
700 |
// @Override |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
701 |
// public String getLocalizedString(String key, Object... args) { |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
702 |
// return key; |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
703 |
// } |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
704 |
// }; |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
705 |
// } else { |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
706 |
// messages = new JavacMessages(javacBundleName); |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
707 |
// } |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
708 |
// } |
10 | 709 |
|
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
710 |
public static final String javacBundleName = |
10 | 711 |
"com.sun.tools.javac.resources.javac"; |
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
712 |
// |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
713 |
// private static JavacMessages messages; |
10 | 714 |
} |