1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
package com.sun.tools.javafx.main; |
27 | |
|
28 | |
import java.io.*; |
29 | |
import java.util.HashSet; |
30 | |
import java.util.LinkedHashMap; |
31 | |
import java.util.Map; |
32 | |
import java.util.MissingResourceException; |
33 | |
import java.util.ResourceBundle; |
34 | |
import java.util.Set; |
35 | |
import java.util.logging.Handler; |
36 | |
import java.util.logging.Level; |
37 | |
import java.util.logging.Logger; |
38 | |
import javax.tools.JavaFileManager; |
39 | |
import javax.tools.JavaFileObject; |
40 | |
import javax.tools.DiagnosticListener; |
41 | |
import com.sun.source.util.TaskEvent; |
42 | |
import com.sun.source.util.TaskListener; |
43 | |
import com.sun.tools.javac.util.*; |
44 | |
import com.sun.tools.javac.code.*; |
45 | |
import com.sun.tools.javac.tree.*; |
46 | |
import com.sun.tools.javafx.tree.*; |
47 | |
import com.sun.tools.javac.comp.*; |
48 | |
import com.sun.tools.javac.jvm.*; |
49 | |
import com.sun.tools.javac.code.Symbol.*; |
50 | |
import com.sun.tools.javac.tree.JCTree.*; |
51 | |
import com.sun.tools.javac.processing.*; |
52 | |
import com.sun.tools.javafx.comp.*; |
53 | |
import com.sun.tools.javafx.code.*; |
54 | |
import com.sun.tools.javafx.util.MsgSym; |
55 | |
import static javax.tools.StandardLocation.CLASS_OUTPUT; |
56 | |
import static com.sun.tools.javac.util.ListBuffer.lb; |
57 | |
import com.sun.tools.javafx.antlr.JavafxSyntacticAnalysis; |
58 | |
import com.sun.tools.javafx.util.PlatformPlugin; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | 411 | public class JavafxCompiler implements ClassReader.SourceCompleter { |
71 | |
private final static String javafxErrorsKey = "com.sun.tools.javafx.resources.javafxcompiler"; |
72 | |
|
73 | 12 | protected static final Context.Key<JavafxCompiler> compilerKey = |
74 | |
new Context.Key<JavafxCompiler>(); |
75 | |
|
76 | |
|
77 | |
public static JavafxCompiler instance(Context context) { |
78 | 750 | JavafxCompiler instance = context.get(compilerKey); |
79 | 750 | if (instance == null) |
80 | 399 | instance = new JavafxCompiler(context); |
81 | 750 | return instance; |
82 | |
} |
83 | |
|
84 | |
|
85 | |
|
86 | |
public static String version() { |
87 | 0 | return version("release"); |
88 | |
} |
89 | |
|
90 | |
|
91 | |
|
92 | |
public static String fullVersion() { |
93 | 0 | return version("full"); |
94 | |
} |
95 | |
|
96 | |
private static final String versionRBName = "com.sun.tools.javafx.resources.version"; |
97 | |
private static ResourceBundle versionRB; |
98 | |
|
99 | |
private static String version(String key) { |
100 | 0 | if (versionRB == null) { |
101 | |
try { |
102 | 0 | versionRB = ResourceBundle.getBundle(versionRBName); |
103 | 0 | } catch (MissingResourceException e) { |
104 | 0 | return Log.getLocalizedString(MsgSym.MESSAGE_VERSION_RESOURCE_MISSING, System.getProperty("java.version")); |
105 | 0 | } |
106 | |
} |
107 | |
try { |
108 | 0 | return versionRB.getString(key); |
109 | |
} |
110 | 0 | catch (MissingResourceException e) { |
111 | 0 | return Log.getLocalizedString(MsgSym.MESSAGE_VERSION_UNKNOWN, System.getProperty("java.version")); |
112 | |
} |
113 | |
} |
114 | |
|
115 | 79 | private static enum CompilePolicy { |
116 | |
|
117 | |
|
118 | |
|
119 | 12 | ATTR_ONLY, |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | 12 | CHECK_ONLY, |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | 12 | SIMPLE, |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | 12 | BY_FILE, |
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | 12 | BY_TODO; |
150 | |
|
151 | |
static CompilePolicy decode(String option) { |
152 | 399 | if (option == null) |
153 | 399 | return DEFAULT_COMPILE_POLICY; |
154 | 0 | else if (option.equals("attr")) |
155 | 0 | return ATTR_ONLY; |
156 | 0 | else if (option.equals("check")) |
157 | 0 | return CHECK_ONLY; |
158 | 0 | else if (option.equals("simple")) |
159 | 0 | return SIMPLE; |
160 | 0 | else if (option.equals("byfile")) |
161 | 0 | return BY_FILE; |
162 | 0 | else if (option.equals("bytodo")) |
163 | 0 | return BY_TODO; |
164 | |
else |
165 | 0 | return DEFAULT_COMPILE_POLICY; |
166 | |
} |
167 | |
} |
168 | |
|
169 | 12 | private static CompilePolicy DEFAULT_COMPILE_POLICY = CompilePolicy.BY_TODO; |
170 | |
|
171 | 48 | private static enum ImplicitSourcePolicy { |
172 | |
|
173 | 12 | NONE, |
174 | |
|
175 | 12 | CLASS, |
176 | |
|
177 | 12 | UNSET; |
178 | |
|
179 | |
static ImplicitSourcePolicy decode(String option) { |
180 | 399 | if (option == null) |
181 | 399 | return UNSET; |
182 | 0 | else if (option.equals("none")) |
183 | 0 | return NONE; |
184 | 0 | else if (option.equals("class")) |
185 | 0 | return CLASS; |
186 | |
else |
187 | 0 | return UNSET; |
188 | |
} |
189 | |
} |
190 | |
|
191 | |
|
192 | |
|
193 | |
protected Options options; |
194 | |
|
195 | |
|
196 | |
|
197 | |
public Log log; |
198 | |
|
199 | |
|
200 | |
|
201 | |
protected JavafxTreeMaker make; |
202 | |
|
203 | |
|
204 | |
|
205 | |
protected ClassWriter writer; |
206 | |
|
207 | |
|
208 | |
|
209 | |
protected JavafxEnter enter; |
210 | |
|
211 | |
|
212 | |
|
213 | |
protected JavafxSymtab syms; |
214 | |
|
215 | |
|
216 | |
|
217 | |
protected Source source; |
218 | |
|
219 | |
|
220 | |
|
221 | |
protected Name.Table names; |
222 | |
|
223 | |
|
224 | |
|
225 | |
protected JavafxAttr attr; |
226 | |
|
227 | |
|
228 | |
|
229 | |
protected JavafxCheck chk; |
230 | |
|
231 | |
|
232 | |
|
233 | |
protected JavafxAnnotate annotate; |
234 | |
|
235 | |
|
236 | |
|
237 | |
protected JavafxPrepForBackEnd prepForBackEnd; |
238 | |
|
239 | |
|
240 | |
|
241 | |
protected JavafxJavaCompiler javafxJavaCompiler; |
242 | |
|
243 | |
|
244 | |
|
245 | |
protected final Name completionFailureName; |
246 | |
|
247 | |
|
248 | |
|
249 | |
protected Types types; |
250 | |
|
251 | |
|
252 | |
|
253 | |
protected JavaFileManager fileManager; |
254 | |
|
255 | |
|
256 | |
|
257 | |
protected TaskListener taskListener; |
258 | |
|
259 | |
protected JavafxSyntacticAnalysis syntacticAnalysis; |
260 | |
protected JavafxVarUsageAnalysis varUsageAnalysis; |
261 | |
protected JavafxToJava jfxToJava; |
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
protected boolean implicitSourceFilesRead; |
267 | |
|
268 | |
protected Context context; |
269 | |
|
270 | |
|
271 | |
|
272 | 399 | public JavafxCompiler(final Context context) { |
273 | 399 | this.context = context; |
274 | 399 | context.put(compilerKey, this); |
275 | 399 | registerServices(context); |
276 | 399 | JavafxClassReader.instance(context).sourceCompleter = this; |
277 | |
|
278 | 399 | javafxJavaCompiler = JavafxJavaCompiler.instance(context); |
279 | 399 | names = Name.Table.instance(context); |
280 | 399 | options = Options.instance(context); |
281 | 399 | log = Log.instance(context); |
282 | 399 | make = (JavafxTreeMaker)JavafxTreeMaker.instance(context); |
283 | 399 | writer = ClassWriter.instance(context); |
284 | 399 | enter = JavafxEnter.instance(context); |
285 | 399 | todo = JavafxTodo.instance(context); |
286 | |
|
287 | 399 | fileManager = context.get(JavaFileManager.class); |
288 | |
|
289 | 399 | syntacticAnalysis = JavafxSyntacticAnalysis.instance(context); |
290 | 399 | varUsageAnalysis = JavafxVarUsageAnalysis.instance(context); |
291 | 399 | jfxToJava = JavafxToJava.instance(context); |
292 | 399 | prepForBackEnd = JavafxPrepForBackEnd.instance(context); |
293 | |
|
294 | |
|
295 | 399 | Messages.instance(context).add(javafxErrorsKey); |
296 | |
try { |
297 | |
|
298 | 399 | syms = (JavafxSymtab)JavafxSymtab.instance(context); |
299 | 0 | } catch (CompletionFailure ex) { |
300 | |
|
301 | 0 | log.error(MsgSym.MESSAGE_CANNOT_ACCESS, ex.sym, ex.errmsg); |
302 | 0 | if (ex instanceof ClassReader.BadClassFile) |
303 | 0 | throw new Abort(); |
304 | 399 | } |
305 | 399 | source = Source.instance(context); |
306 | 399 | attr = JavafxAttr.instance(context); |
307 | 399 | chk = JavafxCheck.instance(context); |
308 | 399 | annotate = JavafxAnnotate.instance(context); |
309 | 399 | types = Types.instance(context); |
310 | 399 | taskListener = context.get(TaskListener.class); |
311 | |
|
312 | 399 | Options options = Options.instance(context); |
313 | |
|
314 | 399 | verbose = options.get("-verbose") != null; |
315 | 399 | sourceOutput = options.get("-printsource") != null; |
316 | 399 | stubOutput = options.get("-stubs") != null; |
317 | 399 | relax = options.get("-relax") != null; |
318 | 399 | printFlat = options.get("-printflat") != null; |
319 | 399 | attrParseOnly = options.get("-attrparseonly") != null; |
320 | 399 | encoding = options.get("-encoding"); |
321 | 399 | lineDebugInfo = options.get("-g:") == null || |
322 | |
options.get("-g:lines") != null; |
323 | 399 | devVerbose = options.get("dev") != null; |
324 | 399 | processPcks = options.get("process.packages") != null; |
325 | |
|
326 | 399 | verboseCompilePolicy = options.get("verboseCompilePolicy") != null; |
327 | |
|
328 | 399 | if (attrParseOnly) |
329 | 0 | compilePolicy = CompilePolicy.ATTR_ONLY; |
330 | |
else |
331 | 399 | compilePolicy = CompilePolicy.decode(options.get("compilePolicy")); |
332 | |
|
333 | 399 | implicitSourcePolicy = ImplicitSourcePolicy.decode(options.get("-implicit")); |
334 | |
|
335 | 399 | completionFailureName = |
336 | |
(options.get("failcomplete") != null) |
337 | |
? names.fromString(options.get("failcomplete")) |
338 | |
: null; |
339 | 399 | } |
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
public boolean verbose; |
347 | |
|
348 | |
|
349 | |
|
350 | |
public boolean sourceOutput; |
351 | |
|
352 | |
|
353 | |
|
354 | |
public boolean stubOutput; |
355 | |
|
356 | |
|
357 | |
|
358 | |
public boolean attrParseOnly; |
359 | |
|
360 | |
|
361 | |
|
362 | |
boolean relax; |
363 | |
|
364 | |
|
365 | |
|
366 | |
public boolean printFlat; |
367 | |
|
368 | |
|
369 | |
|
370 | |
public String encoding; |
371 | |
|
372 | |
|
373 | |
|
374 | |
public boolean lineDebugInfo; |
375 | |
|
376 | |
|
377 | |
|
378 | |
protected boolean devVerbose; |
379 | |
|
380 | |
|
381 | |
|
382 | |
protected boolean processPcks; |
383 | |
|
384 | |
|
385 | |
|
386 | |
|
387 | 399 | protected boolean explicitAnnotationProcessingRequested = false; |
388 | |
|
389 | |
|
390 | |
|
391 | |
|
392 | |
protected CompilePolicy compilePolicy; |
393 | |
|
394 | |
|
395 | |
|
396 | |
|
397 | |
protected ImplicitSourcePolicy implicitSourcePolicy; |
398 | |
|
399 | |
|
400 | |
|
401 | |
|
402 | |
public boolean verboseCompilePolicy; |
403 | |
|
404 | |
|
405 | |
|
406 | |
public JavafxTodo todo; |
407 | |
|
408 | 399 | private Set<JavafxEnv<JavafxAttrContext>> deferredSugar = new HashSet<JavafxEnv<JavafxAttrContext>>(); |
409 | |
|
410 | |
|
411 | |
|
412 | |
|
413 | |
|
414 | 399 | protected Set<JavaFileObject> inputFiles = new HashSet<JavaFileObject>(); |
415 | |
|
416 | |
|
417 | |
|
418 | |
public int errorCount() { |
419 | 2604 | return log.nerrors; |
420 | |
} |
421 | |
|
422 | |
protected final <T> List<T> stopIfError(ListBuffer<T> listBuffer) { |
423 | 379 | if (errorCount() == 0) |
424 | 331 | return listBuffer.toList(); |
425 | |
else |
426 | 48 | return List.nil(); |
427 | |
} |
428 | |
|
429 | |
protected final <T> List<T> stopIfError(List<T> list) { |
430 | 380 | if (errorCount() == 0) |
431 | 370 | return list; |
432 | |
else |
433 | 10 | return List.nil(); |
434 | |
} |
435 | |
|
436 | |
|
437 | |
|
438 | |
public int warningCount() { |
439 | 349 | return log.nwarnings; |
440 | |
} |
441 | |
|
442 | |
|
443 | |
|
444 | |
|
445 | |
|
446 | |
public CharSequence readSource(JavaFileObject filename) { |
447 | |
try { |
448 | 407 | inputFiles.add(filename); |
449 | 407 | return filename.getCharContent(false); |
450 | 0 | } catch (IOException e) { |
451 | 0 | log.error(MsgSym.MESSAGE_ERROR_READING_FILE, filename, e.getLocalizedMessage()); |
452 | 0 | return null; |
453 | |
} |
454 | |
} |
455 | |
|
456 | |
|
457 | |
|
458 | |
|
459 | |
|
460 | |
protected JCCompilationUnit parse(JavaFileObject filename, CharSequence content) { |
461 | 407 | long msec = now(); |
462 | 407 | JCCompilationUnit tree = null; |
463 | 407 | if ((content != null) && (content.toString().trim().length() != 0)) { |
464 | 407 | if (verbose) { |
465 | 0 | printVerbose(MsgSym.MESSAGE_PARSING_STARTED, filename); |
466 | |
} |
467 | 407 | if (taskListener != null) { |
468 | 0 | TaskEvent e = new TaskEvent(TaskEvent.Kind.PARSE, filename); |
469 | 0 | taskListener.started(e); |
470 | |
} |
471 | 407 | int initialErrorCount = log.nerrors; |
472 | |
|
473 | |
|
474 | |
|
475 | 407 | tree = syntacticAnalysis.parse(content); |
476 | 407 | parseErrors |= (log.nerrors > initialErrorCount); |
477 | 407 | if (tree != null && lineDebugInfo) { |
478 | 397 | String hunk = content.toString(); |
479 | 397 | tree.lineMap = Position.makeLineMap(hunk.toCharArray(), hunk.length(), false); |
480 | |
} |
481 | |
} |
482 | 407 | if (verbose) { |
483 | 0 | printVerbose(MsgSym.MESSAGE_PARSING_DONE, Long.toString(elapsed(msec))); |
484 | |
} |
485 | |
|
486 | |
|
487 | 407 | if (tree == null) { |
488 | |
|
489 | 10 | tree = make.TopLevel(List.<JCTree.JCAnnotation>nil(), |
490 | |
null, List.<JCTree>nil()); |
491 | |
} |
492 | |
|
493 | 407 | tree.sourcefile = filename; |
494 | |
|
495 | 407 | printJavafxSource(tree, content); |
496 | |
|
497 | 407 | if (content != null && taskListener != null) { |
498 | 0 | TaskEvent e = new TaskEvent(TaskEvent.Kind.PARSE, tree); |
499 | 0 | taskListener.finished(e); |
500 | |
} |
501 | |
|
502 | 407 | return tree; |
503 | |
} |
504 | |
|
505 | 399 | public boolean keepComments = false; |
506 | |
protected boolean keepComments() { |
507 | 0 | return keepComments || sourceOutput || stubOutput; |
508 | |
} |
509 | |
|
510 | |
|
511 | |
|
512 | |
|
513 | |
public JCTree.JCCompilationUnit parse(JavaFileObject filename) { |
514 | 407 | JavaFileObject prev = log.useSource(filename); |
515 | |
try { |
516 | 407 | JCTree.JCCompilationUnit t = parse(filename, readSource(filename)); |
517 | 407 | if (t.endPositions != null) |
518 | 44 | log.setEndPosTable(filename, t.endPositions); |
519 | 407 | return t; |
520 | |
} finally { |
521 | 407 | log.useSource(prev); |
522 | |
} |
523 | |
} |
524 | |
|
525 | |
|
526 | |
|
527 | |
void printJavaSource(JavafxEnv<JavafxAttrContext> env) { |
528 | 340 | String dump = options.get("dumpjava"); |
529 | 340 | if (dump != null) { |
530 | |
try { |
531 | 0 | String fn = env.toplevel.sourcefile.toString().replace(".fx", ".javadump"); |
532 | 0 | File outFile = new File(dump, (new File(fn)).getName()); |
533 | 0 | FileWriter fw = new FileWriter(outFile); |
534 | 0 | BufferedWriter out = new BufferedWriter(fw); |
535 | |
try { |
536 | 0 | new JavaPretty(out, true, context).printUnit(env.toplevel, null); |
537 | |
} finally { |
538 | 0 | out.close(); |
539 | 0 | } |
540 | 0 | } catch (IOException ex) { |
541 | 0 | System.err.println("Exception thrown in JavaFX pretty printing: " + ex); |
542 | 0 | } |
543 | |
} |
544 | 340 | } |
545 | |
|
546 | |
|
547 | |
|
548 | |
void printJavafxSource(JCCompilationUnit cu, CharSequence content) { |
549 | 407 | String dump = options.get("dumpfx"); |
550 | 407 | BufferedWriter out = null; |
551 | 407 | if (dump != null) { |
552 | |
try { |
553 | |
try { |
554 | 29 | String fn = cu.sourcefile.toString().replace(".fx", ".fxdump"); |
555 | 29 | File outFile = new File(dump, (new File(fn)).getName()); |
556 | 29 | FileWriter fw = new FileWriter(outFile); |
557 | 29 | out = new BufferedWriter(fw); |
558 | 29 | new JavafxPretty(out, true, content).printUnit(cu, null); |
559 | |
} finally { |
560 | 29 | if (out != null) { |
561 | 29 | out.close(); |
562 | |
} |
563 | |
} |
564 | 0 | } catch (IOException ex) { |
565 | 0 | System.err.println("Exception thrown in JavaFX pretty printing: " + ex); |
566 | 29 | } |
567 | |
} |
568 | 407 | } |
569 | |
|
570 | |
|
571 | |
|
572 | |
|
573 | |
|
574 | |
|
575 | |
|
576 | |
public void complete(ClassSymbol c) throws CompletionFailure { |
577 | |
|
578 | 0 | if (completionFailureName == c.fullname) { |
579 | 0 | throw new CompletionFailure(c, "user-selected completion failure by class name"); |
580 | |
} |
581 | |
JCCompilationUnit tree; |
582 | 0 | JavaFileObject filename = c.classfile; |
583 | 0 | JavaFileObject prev = log.useSource(filename); |
584 | |
|
585 | |
try { |
586 | 0 | tree = parse(filename, filename.getCharContent(false)); |
587 | 0 | } catch (IOException e) { |
588 | 0 | log.error(MsgSym.MESSAGE_ERROR_READING_FILE, filename, e); |
589 | 0 | tree = make.TopLevel(List.<JCTree.JCAnnotation>nil(), null, List.<JCTree>nil()); |
590 | |
} finally { |
591 | 0 | log.useSource(prev); |
592 | 0 | } |
593 | |
|
594 | 0 | if (taskListener != null) { |
595 | 0 | TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, tree); |
596 | 0 | taskListener.started(e); |
597 | |
} |
598 | |
|
599 | 0 | enter.complete(List.of(tree), c); |
600 | |
|
601 | 0 | if (taskListener != null) { |
602 | 0 | TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, tree); |
603 | 0 | taskListener.finished(e); |
604 | |
} |
605 | |
|
606 | 0 | if (enter.getEnv(c) == null) { |
607 | 0 | boolean isPkgInfo = |
608 | |
tree.sourcefile.isNameCompatible("package-info", |
609 | |
JavaFileObject.Kind.SOURCE); |
610 | 0 | if (isPkgInfo) { |
611 | 0 | if (enter.getEnv(tree.packge) == null) { |
612 | 0 | String msg |
613 | |
= Log.getLocalizedString(MsgSym.MESSAGE_FILE_DOES_NOT_CONTAIN_PACKAGE, |
614 | |
c.location()); |
615 | 0 | throw new ClassReader.BadClassFile(c, filename, msg); |
616 | |
} |
617 | |
} else { |
618 | 0 | throw new |
619 | |
ClassReader.BadClassFile(c, filename, Log. |
620 | |
getLocalizedString(MsgSym.MESSAGE_FILE_DOES_NOT_CONTAIN_CLASS, |
621 | |
c.fullname)); |
622 | |
} |
623 | |
} |
624 | |
|
625 | 0 | implicitSourceFilesRead = true; |
626 | 0 | } |
627 | |
|
628 | |
|
629 | 399 | private boolean hasBeenUsed = false; |
630 | 399 | private long start_msec = 0; |
631 | 399 | public long elapsed_msec = 0; |
632 | |
|
633 | |
|
634 | 399 | private boolean parseErrors = false; |
635 | |
|
636 | |
public void compile(List<JavaFileObject> sourceFileObject) |
637 | |
throws Throwable { |
638 | 0 | compile(sourceFileObject, List.<String>nil()); |
639 | 0 | } |
640 | |
|
641 | |
|
642 | |
|
643 | |
|
644 | |
|
645 | |
|
646 | |
|
647 | |
|
648 | |
|
649 | |
public void compile(List<JavaFileObject> sourceFileObjects, |
650 | |
List<String> classnames) |
651 | |
throws IOException |
652 | |
{ |
653 | |
|
654 | |
|
655 | 380 | if (hasBeenUsed) |
656 | 0 | throw new AssertionError("attempt to reuse JavaCompiler"); |
657 | 380 | hasBeenUsed = true; |
658 | |
|
659 | 380 | start_msec = now(); |
660 | |
try { |
661 | |
|
662 | 380 | List<JCCompilationUnit> cus = stopIfError(parseFiles(sourceFileObjects)); |
663 | |
|
664 | |
|
665 | |
|
666 | |
|
667 | 380 | enterTrees(cus); |
668 | |
|
669 | 380 | compile2(null); |
670 | 377 | if (attr != null) { |
671 | 377 | attr.clearCaches(); |
672 | |
} |
673 | 377 | close(); |
674 | 0 | } catch (Abort ex) { |
675 | 0 | if (devVerbose) |
676 | 0 | ex.printStackTrace(); |
677 | 377 | } |
678 | 377 | } |
679 | |
|
680 | |
public List<JavafxEnv<JavafxAttrContext>> jfxToJava(List<JavafxEnv<JavafxAttrContext>> envs) { |
681 | 379 | ListBuffer<JavafxEnv<JavafxAttrContext>> results = lb(); |
682 | 758 | for (List<JavafxEnv<JavafxAttrContext>> l = envs; l.nonEmpty(); l = l.tail) { |
683 | 379 | jfxToJava(l.head, results); |
684 | |
} |
685 | 379 | return stopIfError(results); |
686 | |
} |
687 | |
|
688 | |
public List<JavafxEnv<JavafxAttrContext>> jfxToJava(JavafxEnv<JavafxAttrContext> env) { |
689 | 0 | ListBuffer<JavafxEnv<JavafxAttrContext>> results = lb(); |
690 | 0 | jfxToJava(env, results); |
691 | 0 | return stopIfError(results); |
692 | |
} |
693 | |
|
694 | |
protected void jfxToJava(JavafxEnv<JavafxAttrContext> env, ListBuffer<JavafxEnv<JavafxAttrContext>> results) { |
695 | |
try { |
696 | 379 | if (errorCount() > 0) |
697 | |
return; |
698 | |
|
699 | 340 | if (relax || deferredSugar.contains(env)) { |
700 | 0 | results.append(env); |
701 | |
return; |
702 | |
} |
703 | |
|
704 | 340 | if (verboseCompilePolicy) |
705 | 0 | log.printLines(log.noticeWriter, "[flow " + env.enclClass.sym + "]"); |
706 | 340 | JavaFileObject prev = log.useSource( |
707 | |
env.enclClass.sym.sourcefile != null ? |
708 | |
env.enclClass.sym.sourcefile : |
709 | |
env.toplevel.sourcefile); |
710 | |
try { |
711 | 340 | make.at(Position.FIRSTPOS); |
712 | 340 | jfxToJava.toJava(env); |
713 | |
|
714 | 340 | if (errorCount() > 0) |
715 | |
return; |
716 | |
|
717 | 340 | results.append(env); |
718 | |
} |
719 | |
finally { |
720 | 340 | log.useSource(prev); |
721 | 340 | } |
722 | |
} |
723 | |
finally { |
724 | 379 | if (taskListener != null) { |
725 | 0 | TaskEvent e = new TaskEvent(TaskEvent.Kind.ANALYZE, env.toplevel, env.enclClass.sym); |
726 | 0 | taskListener.finished(e); |
727 | 39 | } |
728 | |
} |
729 | 340 | } |
730 | |
|
731 | |
|
732 | |
|
733 | |
|
734 | |
|
735 | |
private void compile2(ListBuffer<JavaFileObject> results) throws IOException { |
736 | |
try { |
737 | 381 | switch (compilePolicy) { |
738 | |
case ATTR_ONLY: |
739 | 0 | attribute(todo); |
740 | 0 | break; |
741 | |
|
742 | |
case CHECK_ONLY: |
743 | 0 | backEnd(prepForBackEnd(jfxToJava(varAnalysis(attribute(todo)))), results); |
744 | 0 | break; |
745 | |
|
746 | |
case SIMPLE: |
747 | 0 | backEnd(prepForBackEnd(jfxToJava(varAnalysis(attribute(todo)))), results); |
748 | 0 | break; |
749 | |
|
750 | |
case BY_FILE: { |
751 | 0 | ListBuffer<JavafxEnv<JavafxAttrContext>> envbuff = ListBuffer.lb(); |
752 | 0 | for (List<JavafxEnv<JavafxAttrContext>> list : groupByFile(jfxToJava(varAnalysis(attribute(todo)))).values()) |
753 | 0 | envbuff.appendList(prepForBackEnd(list)); |
754 | 0 | backEnd(envbuff.toList(), results); |
755 | 0 | break; |
756 | |
} |
757 | |
case BY_TODO: { |
758 | 381 | ListBuffer<JavafxEnv<JavafxAttrContext>> envbuff = ListBuffer.lb(); |
759 | 760 | while (todo.nonEmpty()) { |
760 | 382 | envbuff.append(attribute(todo.next())); |
761 | |
} |
762 | |
|
763 | 378 | backEnd(prepForBackEnd(jfxToJava(varAnalysis(envbuff.toList()))), results); |
764 | 378 | break; |
765 | |
} |
766 | |
default: |
767 | 0 | assert false: "unknown compile policy"; |
768 | |
} |
769 | 0 | } catch (Abort ex) { |
770 | 0 | if (devVerbose) |
771 | 0 | ex.printStackTrace(); |
772 | 378 | } |
773 | |
|
774 | 378 | if (verbose) { |
775 | 0 | elapsed_msec = elapsed(start_msec);; |
776 | 0 | printVerbose(MsgSym.MESSAGE_TOTAL, Long.toString(elapsed_msec)); |
777 | |
} |
778 | |
|
779 | 378 | reportDeferredDiagnostics(); |
780 | |
|
781 | 378 | if (!log.hasDiagnosticListener()) { |
782 | 349 | printCount(MsgSym.MESSAGEPREFIX_ERROR, errorCount()); |
783 | 349 | printCount(MsgSym.MESSAGEPREFIX_WARN, warningCount()); |
784 | |
} |
785 | |
|
786 | 378 | ((JavafxTypes) types).clearCaches(); |
787 | 378 | } |
788 | |
|
789 | |
|
790 | |
|
791 | |
|
792 | |
public void generate(ListBuffer<JavaFileObject> results) throws IOException { |
793 | 1 | compile2(results); |
794 | 1 | } |
795 | |
|
796 | |
private void backEnd(List<JavafxEnv<JavafxAttrContext>> envs, ListBuffer<JavaFileObject> results) throws IOException { |
797 | 379 | ListBuffer<JCCompilationUnit> trees = lb(); |
798 | 379 | for (JavafxEnv<JavafxAttrContext> env : envs) { |
799 | 340 | trees.append(env.toplevel); |
800 | |
} |
801 | |
|
802 | 379 | PlatformPlugin plugin = PlatformPlugin.instance(context); |
803 | 379 | if (plugin != null) { |
804 | 0 | plugin.process(trees); |
805 | 0 | if (Log.instance(context).nerrors > 0) |
806 | 0 | return; |
807 | |
} |
808 | |
|
809 | 379 | javafxJavaCompiler.backEnd(trees.toList(), results); |
810 | 379 | } |
811 | |
|
812 | |
|
813 | |
|
814 | |
|
815 | |
public List<JCCompilationUnit> parseFiles(List<JavaFileObject> fileObjects) throws IOException { |
816 | 396 | if (errorCount() > 0) |
817 | 0 | return List.nil(); |
818 | |
|
819 | |
|
820 | 396 | ListBuffer<JCCompilationUnit> trees = lb(); |
821 | 396 | for (JavaFileObject fileObject : fileObjects) |
822 | 407 | trees.append(parse(fileObject)); |
823 | 396 | return trees.toList(); |
824 | |
} |
825 | |
|
826 | |
|
827 | |
|
828 | |
|
829 | |
|
830 | |
|
831 | |
public List<JCCompilationUnit> enterTrees(List<JCCompilationUnit> roots) { |
832 | |
|
833 | 383 | if (taskListener != null) { |
834 | 0 | for (JCCompilationUnit unit: roots) { |
835 | 0 | TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, unit); |
836 | 0 | taskListener.started(e); |
837 | 0 | } |
838 | |
} |
839 | |
|
840 | 383 | enter.main(roots); |
841 | |
|
842 | 383 | if (taskListener != null) { |
843 | 0 | for (JCCompilationUnit unit: roots) { |
844 | 0 | TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, unit); |
845 | 0 | taskListener.finished(e); |
846 | 0 | } |
847 | |
} |
848 | |
|
849 | |
|
850 | |
|
851 | 383 | if (sourceOutput || stubOutput) { |
852 | 0 | ListBuffer<JCClassDecl> cdefs = lb(); |
853 | 0 | for (JCCompilationUnit unit : roots) { |
854 | 0 | for (List<JCTree> defs = unit.defs; |
855 | 0 | defs.nonEmpty(); |
856 | 0 | defs = defs.tail) { |
857 | 0 | if (defs.head instanceof JCClassDecl) |
858 | 0 | cdefs.append((JCClassDecl)defs.head); |
859 | |
} |
860 | |
} |
861 | |
} |
862 | 383 | return roots; |
863 | |
} |
864 | |
|
865 | |
|
866 | |
|
867 | |
|
868 | |
public void errorCheck() throws IOException { |
869 | 1 | backEnd(prepForBackEnd(jfxToJava(varAnalysis(attribute(todo)))), null); |
870 | 1 | } |
871 | |
|
872 | |
|
873 | |
|
874 | |
|
875 | |
public void attribute() { |
876 | 5 | attribute(todo); |
877 | 5 | } |
878 | |
|
879 | |
|
880 | |
|
881 | |
|
882 | |
|
883 | |
|
884 | |
|
885 | |
|
886 | |
public List<JavafxEnv<JavafxAttrContext>> attribute(ListBuffer<JavafxEnv<JavafxAttrContext>> envs) { |
887 | 6 | ListBuffer<JavafxEnv<JavafxAttrContext>> results = lb(); |
888 | 9 | while (envs.nonEmpty()) |
889 | 3 | results.append(attribute(envs.next())); |
890 | 6 | return results.toList(); |
891 | |
} |
892 | |
|
893 | |
|
894 | |
|
895 | |
|
896 | |
|
897 | |
public JavafxEnv<JavafxAttrContext> attribute(JavafxEnv<JavafxAttrContext> env) { |
898 | 385 | if (verboseCompilePolicy) |
899 | 0 | Log.printLines(log.noticeWriter, "[attribute " + env.enclClass.sym + "]"); |
900 | 385 | if (verbose) |
901 | 0 | printVerbose(MsgSym.MESSAGE_CHECKING_ATTRIBUTION, env.enclClass.sym); |
902 | |
|
903 | 385 | if (taskListener != null) { |
904 | 0 | TaskEvent e = new TaskEvent(TaskEvent.Kind.ANALYZE, env.toplevel, env.enclClass.sym); |
905 | 0 | taskListener.started(e); |
906 | |
} |
907 | |
|
908 | 385 | JavaFileObject prev = log.useSource( |
909 | |
env.enclClass.sym.sourcefile != null ? |
910 | |
env.enclClass.sym.sourcefile : |
911 | |
env.toplevel.sourcefile); |
912 | |
try { |
913 | 385 | attr.attribClass(env.tree.pos(), env.tree instanceof JFXClassDeclaration ? (JFXClassDeclaration)env.tree : null, |
914 | |
env.enclClass.sym); |
915 | |
} |
916 | |
finally { |
917 | 385 | log.useSource(prev); |
918 | 382 | } |
919 | |
|
920 | 382 | return env; |
921 | |
} |
922 | |
|
923 | |
|
924 | |
|
925 | |
|
926 | |
|
927 | |
public List<JavafxEnv<JavafxAttrContext>> varAnalysis(List<JavafxEnv<JavafxAttrContext>> envs) { |
928 | 758 | for (List<JavafxEnv<JavafxAttrContext>> l = envs; l.nonEmpty(); l = l.tail) { |
929 | 379 | varAnalysis(l.head); |
930 | |
} |
931 | 379 | return envs; |
932 | |
} |
933 | |
|
934 | |
|
935 | |
|
936 | |
|
937 | |
|
938 | |
public JavafxEnv<JavafxAttrContext> varAnalysis(JavafxEnv<JavafxAttrContext> env) { |
939 | 379 | if (verboseCompilePolicy) |
940 | 0 | log.printLines(log.noticeWriter, "[type-morph " + env.enclClass.sym + "]"); |
941 | |
|
942 | 379 | JavaFileObject prev = log.useSource( |
943 | |
env.enclClass.sym.sourcefile != null ? |
944 | |
env.enclClass.sym.sourcefile : |
945 | |
env.toplevel.sourcefile); |
946 | |
try { |
947 | 379 | varUsageAnalysis.analyzeVarUse(env); |
948 | |
} |
949 | |
finally { |
950 | 379 | log.useSource(prev); |
951 | 379 | } |
952 | |
|
953 | 379 | return env; |
954 | |
} |
955 | |
|
956 | |
public List<JavafxEnv<JavafxAttrContext>> prepForBackEnd(List<JavafxEnv<JavafxAttrContext>> envs) { |
957 | 719 | for (List<JavafxEnv<JavafxAttrContext>> l = envs; l.nonEmpty(); l = l.tail) { |
958 | 340 | prepForBackEnd(l.head); |
959 | |
} |
960 | 379 | return envs; |
961 | |
} |
962 | |
|
963 | |
public JavafxEnv<JavafxAttrContext> prepForBackEnd(JavafxEnv<JavafxAttrContext> env) { |
964 | 340 | if (verboseCompilePolicy) |
965 | 0 | log.printLines(log.noticeWriter, "[prep-for-back-end " + env.enclClass.sym + "]"); |
966 | 340 | printJavaSource(env); |
967 | |
|
968 | 340 | JavaFileObject prev = log.useSource( |
969 | |
env.enclClass.sym.sourcefile != null ? |
970 | |
env.enclClass.sym.sourcefile : |
971 | |
env.toplevel.sourcefile); |
972 | |
try { |
973 | 340 | prepForBackEnd.prep(env); |
974 | |
} |
975 | |
finally { |
976 | 340 | log.useSource(prev); |
977 | 340 | } |
978 | |
|
979 | 340 | return env; |
980 | |
} |
981 | |
|
982 | |
|
983 | |
Map<JCCompilationUnit, List<JavafxEnv<JavafxAttrContext>>> groupByFile(List<JavafxEnv<JavafxAttrContext>> list) { |
984 | |
|
985 | 0 | Map<JCCompilationUnit, List<JavafxEnv<JavafxAttrContext>>> map = new LinkedHashMap<JCCompilationUnit, List<JavafxEnv<JavafxAttrContext>>>(); |
986 | 0 | Set<JCCompilationUnit> fixupSet = new HashSet<JCTree.JCCompilationUnit>(); |
987 | 0 | for (List<JavafxEnv<JavafxAttrContext>> l = list; l.nonEmpty(); l = l.tail) { |
988 | 0 | JavafxEnv<JavafxAttrContext> env = l.head; |
989 | 0 | List<JavafxEnv<JavafxAttrContext>> sublist = map.get(env.toplevel); |
990 | 0 | if (sublist == null) |
991 | 0 | sublist = List.of(env); |
992 | |
else { |
993 | |
|
994 | |
|
995 | 0 | sublist = sublist.prepend(env); |
996 | 0 | fixupSet.add(env.toplevel); |
997 | |
} |
998 | 0 | map.put(env.toplevel, sublist); |
999 | |
} |
1000 | |
|
1001 | 0 | for (JCTree.JCCompilationUnit tree: fixupSet) |
1002 | 0 | map.put(tree, map.get(tree).reverse()); |
1003 | 0 | return map; |
1004 | |
} |
1005 | |
|
1006 | |
JCClassDecl removeMethodBodies(JCClassDecl cdef) { |
1007 | 0 | final boolean isInterface = (cdef.mods.flags & Flags.INTERFACE) != 0; |
1008 | 0 | class MethodBodyRemover extends TreeTranslator { |
1009 | |
public void visitMethodDef(JCMethodDecl tree) { |
1010 | 0 | tree.mods.flags &= ~Flags.SYNCHRONIZED; |
1011 | 0 | for (JCVariableDecl vd : tree.params) |
1012 | 0 | vd.mods.flags &= ~Flags.FINAL; |
1013 | 0 | tree.body = null; |
1014 | 0 | super.visitMethodDef(tree); |
1015 | 0 | } |
1016 | |
public void visitVarDef(JCVariableDecl tree) { |
1017 | 0 | if (tree.init != null && tree.init.type.constValue() == null) |
1018 | 0 | tree.init = null; |
1019 | 0 | super.visitVarDef(tree); |
1020 | 0 | } |
1021 | |
public void visitClassDef(JCClassDecl tree) { |
1022 | 0 | ListBuffer<JCTree> newdefs = lb(); |
1023 | 0 | for (List<JCTree> it = tree.defs; it.tail != null; it = it.tail) { |
1024 | 0 | JCTree t = it.head; |
1025 | 0 | switch (t.getTag()) { |
1026 | |
case JCTree.CLASSDEF: |
1027 | 0 | if (isInterface || |
1028 | |
(((JCClassDecl) t).mods.flags & (Flags.PROTECTED|Flags.PUBLIC)) != 0 || |
1029 | |
(((JCClassDecl) t).mods.flags & (Flags.PRIVATE)) == 0 && ((JCClassDecl) t).sym.packge().getQualifiedName() == names.java_lang) |
1030 | 0 | newdefs.append(t); |
1031 | |
break; |
1032 | |
case JCTree.METHODDEF: |
1033 | 0 | if (isInterface || |
1034 | |
(((JCMethodDecl) t).mods.flags & (Flags.PROTECTED|Flags.PUBLIC)) != 0 || |
1035 | |
((JCMethodDecl) t).sym.name == names.init || |
1036 | |
(((JCMethodDecl) t).mods.flags & (Flags.PRIVATE)) == 0 && ((JCMethodDecl) t).sym.packge().getQualifiedName() == names.java_lang) |
1037 | 0 | newdefs.append(t); |
1038 | |
break; |
1039 | |
case JCTree.VARDEF: |
1040 | 0 | if (isInterface || (((JCVariableDecl) t).mods.flags & (Flags.PROTECTED|Flags.PUBLIC)) != 0 || |
1041 | |
(((JCVariableDecl) t).mods.flags & (Flags.PRIVATE)) == 0 && ((JCVariableDecl) t).sym.packge().getQualifiedName() == names.java_lang) |
1042 | 0 | newdefs.append(t); |
1043 | |
break; |
1044 | |
default: |
1045 | |
break; |
1046 | |
} |
1047 | |
} |
1048 | 0 | tree.defs = newdefs.toList(); |
1049 | 0 | super.visitClassDef(tree); |
1050 | 0 | } |
1051 | |
} |
1052 | 0 | MethodBodyRemover r = new MethodBodyRemover(); |
1053 | 0 | return r.translate(cdef); |
1054 | |
} |
1055 | |
|
1056 | |
public void reportDeferredDiagnostics() { |
1057 | 378 | chk.reportDeferredDiagnostics(); |
1058 | 378 | } |
1059 | |
|
1060 | |
|
1061 | |
|
1062 | |
public void close() { |
1063 | 757 | close(true); |
1064 | 757 | } |
1065 | |
|
1066 | |
private void close(boolean disposeNames) { |
1067 | 757 | make = null; |
1068 | 757 | writer = null; |
1069 | 757 | enter = null; |
1070 | 757 | if (todo != null) |
1071 | 380 | todo.clear(); |
1072 | 757 | todo = null; |
1073 | 757 | syms = null; |
1074 | 757 | source = null; |
1075 | 757 | attr = null; |
1076 | 757 | chk = null; |
1077 | 757 | annotate = null; |
1078 | 757 | types = null; |
1079 | |
|
1080 | 757 | log.flush(); |
1081 | |
try { |
1082 | 757 | fileManager.flush(); |
1083 | 0 | } catch (IOException e) { |
1084 | 0 | throw new Abort(e); |
1085 | |
} finally { |
1086 | 757 | if (names != null && disposeNames) |
1087 | 380 | names.dispose(); |
1088 | 757 | names = null; |
1089 | 757 | } |
1090 | 757 | } |
1091 | |
|
1092 | |
|
1093 | |
|
1094 | |
|
1095 | |
|
1096 | |
protected void printVerbose(String key, Object arg) { |
1097 | 0 | Log.printLines(log.noticeWriter, Log.getLocalizedString(MsgSym.MESSAGEPREFIX_VERBOSE + key, arg)); |
1098 | 0 | } |
1099 | |
|
1100 | |
|
1101 | |
|
1102 | |
protected void printCount(String kind, int count) { |
1103 | 698 | if (count != 0) { |
1104 | |
String text; |
1105 | 45 | if (count == 1) |
1106 | 35 | text = Log.getLocalizedString(MsgSym.MESSAGEPREFIX_COUNT + kind, String.valueOf(count)); |
1107 | |
else |
1108 | 10 | text = Log.getLocalizedString(MsgSym.MESSAGEPREFIX_COUNT + kind + MsgSym.MESSAGESUFFIX_PLURAL, String.valueOf(count)); |
1109 | 45 | Log.printLines(log.errWriter, text); |
1110 | 45 | log.errWriter.flush(); |
1111 | |
} |
1112 | 698 | } |
1113 | |
|
1114 | |
private static long now() { |
1115 | 787 | return System.currentTimeMillis(); |
1116 | |
} |
1117 | |
|
1118 | |
private static long elapsed(long then) { |
1119 | 0 | return now() - then; |
1120 | |
} |
1121 | |
|
1122 | |
public void initRound(JavafxCompiler prev) { |
1123 | 0 | keepComments = prev.keepComments; |
1124 | 0 | start_msec = prev.start_msec; |
1125 | 0 | hasBeenUsed = true; |
1126 | 0 | } |
1127 | |
|
1128 | |
public static void enableLogging() { |
1129 | 0 | Logger logger = Logger.getLogger(com.sun.tools.javac.Main.class.getPackage().getName()); |
1130 | 0 | logger.setLevel(Level.ALL); |
1131 | 0 | for (Handler h : logger.getParent().getHandlers()) { |
1132 | 0 | h.setLevel(Level.ALL); |
1133 | |
} |
1134 | |
|
1135 | 0 | } |
1136 | |
|
1137 | |
protected void registerServices(final Context context) { |
1138 | |
|
1139 | 399 | if (context.get(JavaFileManager.class) == null) { |
1140 | 0 | com.sun.tools.javafx.util.JavafxFileManager.preRegister(context); |
1141 | |
} |
1142 | 399 | com.sun.tools.javafx.tree.JavafxTreeMaker.preRegister(context); |
1143 | 399 | com.sun.tools.javafx.tree.JavafxTreeInfo.preRegister(context); |
1144 | 399 | com.sun.tools.javafx.code.JavafxSymtab.preRegister(context); |
1145 | 399 | com.sun.tools.javafx.code.JavafxTypes.preRegister(context); |
1146 | 399 | } |
1147 | |
} |