JDK-8234683 : allow discoverable javac plugins to be invoked by default
  • Type: CSR
  • Component: tools
  • Sub-Component: javac
  • Priority: P3
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 14
  • Submitted: 2019-11-22
  • Updated: 2019-12-03
  • Resolved: 2019-12-03
Related Reports
CSR :  

Allow a javac [plugin](https://docs.oracle.com/en/java/javase/13/docs/api/jdk.compiler/com/sun/source/util/Plugin.html) to be invoked by default if it is not otherwise invoked with command-line arguments.


Currently, javac plugins must be invoked explicitly from the command line, which may be inconvenient or impractical when javac is being invoked from higher level build tools such as Maven or Ant.  In this context, "command line" includes both invocation from binary executables such as `bin/javac` or `bin/javac.exe` that launch a new JVM, and invocation by APIs that take an array or list of string-valued options that are similar to the arguments passed to a binary executable. This includes API such as:

*   [javax.tools.JavaCompiler.getTask](https://docs.oracle.com/en/java/javase/13/docs/api/java.compiler/javax/tools/JavaCompiler.html#getTask(java.io.Writer,javax.tools.JavaFileManager,javax.tools.DiagnosticListener,java.lang.Iterable,java.lang.Iterable,java.lang.Iterable)),
*   [java.util.spi.ToolProvider.run](https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/spi/ToolProvider.html#run(java.io.PrintWriter,java.io.PrintWriter,java.lang.String...)),
* and the legacy entry points in [com.sun.tools.javac.Main](https://docs.oracle.com/en/java/javase/13/docs/api/jdk.compiler/com/sun/tools/javac/Main.html).


It is already the case that plugins can be linked into an image, using `jlink`, so that there is no need to specify any path options to locate the plugin. Allowing plugins to be started automatically would also eliminate the need to use a `-Xplugin` option on the command line.

The proposed behavior should be "opt-in", so as not to affect any existing plugins.  The behavior can be achieved by adding a new method `boolean autoStart()` to the `Plugin` interface, with a default implementation of `return false;`.  The behavior is defined so that it can be overridden by an explicit invocation on the command line; this could be used to "opt-out" in those situations where the autostart behavior is _not_ desired.

The ability to start plugins by default is similar to the way that javac will automatically run annotation processors found by the default discovery process for annotation processors.


The following method is added to `com.sun.tools.javac.Plugin`:

@@ -62,4 +62,20 @@
      * @param args Arguments, if any, for the plug-in
     void init(JavacTask task, String... args);
+    /**
+     * Returns whether or not this plugin should be automatically started,
+     * even if not explicitly specified in the command-line options.
+     *
+     * <p>This method will be called by javac for all plugins located by the
+     * service loader. If the method returns {@code true}, the plugin will be
+     * {@link #init(JavacTask,String[]) initialized} with an empty array of
+     * string arguments if it is not otherwise initialized due to an explicit
+     * command-line option.
+     *
+     * @return whether or not this plugin should be automatically started
+     */
+    default boolean autoStart() {
+        return false;
+    }


Moving to Approved.

Moving to Provisional rather than Approved.