31 import java.net.URL; |
31 import java.net.URL; |
32 import java.security.DigestInputStream; |
32 import java.security.DigestInputStream; |
33 import java.security.MessageDigest; |
33 import java.security.MessageDigest; |
34 import java.util.Arrays; |
34 import java.util.Arrays; |
35 import java.util.Collection; |
35 import java.util.Collection; |
|
36 import java.util.Iterator; |
36 import java.util.LinkedHashSet; |
37 import java.util.LinkedHashSet; |
|
38 import java.util.ServiceLoader; |
37 import java.util.Set; |
39 import java.util.Set; |
|
40 |
|
41 import javax.annotation.processing.Processor; |
38 import javax.tools.JavaFileManager; |
42 import javax.tools.JavaFileManager; |
39 import javax.tools.JavaFileObject; |
43 import javax.tools.JavaFileObject; |
40 import javax.annotation.processing.Processor; |
44 |
41 |
45 import com.sun.source.util.JavacTask; |
|
46 import com.sun.source.util.Plugin; |
42 import com.sun.tools.javac.code.Source; |
47 import com.sun.tools.javac.code.Source; |
43 import com.sun.tools.javac.file.CacheFSInfo; |
48 import com.sun.tools.javac.file.CacheFSInfo; |
44 import com.sun.tools.javac.file.JavacFileManager; |
49 import com.sun.tools.javac.file.JavacFileManager; |
45 import com.sun.tools.javac.jvm.Target; |
50 import com.sun.tools.javac.jvm.Target; |
|
51 import com.sun.tools.javac.processing.AnnotationProcessingError; |
|
52 import com.sun.tools.javac.processing.JavacProcessingEnvironment; |
46 import com.sun.tools.javac.util.*; |
53 import com.sun.tools.javac.util.*; |
|
54 import com.sun.tools.javac.util.Log.PrefixKind; |
47 import com.sun.tools.javac.util.Log.WriterKind; |
55 import com.sun.tools.javac.util.Log.WriterKind; |
48 import com.sun.tools.javac.util.Log.PrefixKind; |
|
49 import com.sun.tools.javac.processing.AnnotationProcessingError; |
|
50 |
|
51 import static com.sun.tools.javac.main.Option.*; |
56 import static com.sun.tools.javac.main.Option.*; |
52 |
57 |
53 /** This class provides a commandline interface to the GJC compiler. |
58 /** This class provides a command line interface to the javac compiler. |
54 * |
59 * |
55 * <p><b>This is NOT part of any supported API. |
60 * <p><b>This is NOT part of any supported API. |
56 * If you write code that depends on this, you do so at your own risk. |
61 * If you write code that depends on this, you do so at your own risk. |
57 * This code and its internal interfaces are subject to change or |
62 * This code and its internal interfaces are subject to change or |
58 * deletion without notice.</b> |
63 * deletion without notice.</b> |
420 // allow System property in following line as a Mustang legacy |
425 // allow System property in following line as a Mustang legacy |
421 boolean batchMode = (options.isUnset("nonBatchMode") |
426 boolean batchMode = (options.isUnset("nonBatchMode") |
422 && System.getProperty("nonBatchMode") == null); |
427 && System.getProperty("nonBatchMode") == null); |
423 if (batchMode) |
428 if (batchMode) |
424 CacheFSInfo.preRegister(context); |
429 CacheFSInfo.preRegister(context); |
|
430 |
|
431 // invoke any available plugins |
|
432 String plugins = options.get(PLUGIN); |
|
433 if (plugins != null) { |
|
434 JavacProcessingEnvironment pEnv = JavacProcessingEnvironment.instance(context); |
|
435 ClassLoader cl = pEnv.getProcessorClassLoader(); |
|
436 ServiceLoader<Plugin> sl = ServiceLoader.load(Plugin.class, cl); |
|
437 Set<List<String>> pluginsToCall = new LinkedHashSet<List<String>>(); |
|
438 for (String plugin: plugins.split("\\x00")) { |
|
439 pluginsToCall.add(List.from(plugin.split("\\s+"))); |
|
440 } |
|
441 JavacTask task = null; |
|
442 Iterator<Plugin> iter = sl.iterator(); |
|
443 while (iter.hasNext()) { |
|
444 Plugin plugin = iter.next(); |
|
445 for (List<String> p: pluginsToCall) { |
|
446 if (plugin.getName().equals(p.head)) { |
|
447 pluginsToCall.remove(p); |
|
448 try { |
|
449 if (task == null) |
|
450 task = JavacTask.instance(pEnv); |
|
451 plugin.call(task, p.tail.toArray(new String[p.tail.size()])); |
|
452 } catch (Throwable ex) { |
|
453 if (apiMode) |
|
454 throw new RuntimeException(ex); |
|
455 pluginMessage(ex); |
|
456 return Result.SYSERR; |
|
457 } |
|
458 |
|
459 } |
|
460 } |
|
461 } |
|
462 for (List<String> p: pluginsToCall) { |
|
463 log.printLines(PrefixKind.JAVAC, "msg.plugin.not.found", p.head); |
|
464 } |
|
465 } |
425 |
466 |
426 fileManager = context.get(JavaFileManager.class); |
467 fileManager = context.get(JavaFileManager.class); |
427 |
468 |
428 comp = JavaCompiler.instance(context); |
469 comp = JavaCompiler.instance(context); |
429 if (comp == null) return Result.SYSERR; |
470 if (comp == null) return Result.SYSERR; |
535 void apMessage(AnnotationProcessingError ex) { |
576 void apMessage(AnnotationProcessingError ex) { |
536 log.printLines(PrefixKind.JAVAC, "msg.proc.annotation.uncaught.exception"); |
577 log.printLines(PrefixKind.JAVAC, "msg.proc.annotation.uncaught.exception"); |
537 ex.getCause().printStackTrace(log.getWriter(WriterKind.NOTICE)); |
578 ex.getCause().printStackTrace(log.getWriter(WriterKind.NOTICE)); |
538 } |
579 } |
539 |
580 |
|
581 /** Print a message reporting an uncaught exception from an |
|
582 * annotation processor. |
|
583 */ |
|
584 void pluginMessage(Throwable ex) { |
|
585 log.printLines(PrefixKind.JAVAC, "msg.plugin.uncaught.exception"); |
|
586 ex.printStackTrace(log.getWriter(WriterKind.NOTICE)); |
|
587 } |
|
588 |
540 /** Display the location and checksum of a class. */ |
589 /** Display the location and checksum of a class. */ |
541 void showClass(String className) { |
590 void showClass(String className) { |
542 PrintWriter pw = log.getWriter(WriterKind.NOTICE); |
591 PrintWriter pw = log.getWriter(WriterKind.NOTICE); |
543 pw.println("javac: show class: " + className); |
592 pw.println("javac: show class: " + className); |
544 URL url = getClass().getResource('/' + className.replace('.', '/') + ".class"); |
593 URL url = getClass().getResource('/' + className.replace('.', '/') + ".class"); |