ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject ant git commit: add support for Java9's javac --release
Date Sat, 08 Oct 2016 14:34:30 GMT
Repository: ant
Updated Branches:
  refs/heads/1.9.x c2666b199 -> c1bbf2fc0


add support for Java9's javac --release

https://bz.apache.org/bugzilla/show_bug.cgi?id=60172


Project: http://git-wip-us.apache.org/repos/asf/ant/repo
Commit: http://git-wip-us.apache.org/repos/asf/ant/commit/c1bbf2fc
Tree: http://git-wip-us.apache.org/repos/asf/ant/tree/c1bbf2fc
Diff: http://git-wip-us.apache.org/repos/asf/ant/diff/c1bbf2fc

Branch: refs/heads/1.9.x
Commit: c1bbf2fc0ca870384cdec8f437fb1fb9a4699db9
Parents: c2666b1
Author: Stefan Bodewig <bodewig@apache.org>
Authored: Sat Oct 8 16:33:52 2016 +0200
Committer: Stefan Bodewig <bodewig@apache.org>
Committed: Sat Oct 8 16:33:52 2016 +0200

----------------------------------------------------------------------
 WHATSNEW                                        |  4 ++
 manual/Tasks/javac.html                         | 10 ++++
 .../org/apache/tools/ant/taskdefs/Javac.java    | 28 +++++++++
 .../compilers/DefaultCompilerAdapter.java       | 46 ++++++++++-----
 .../compilers/DefaultCompilerAdapterTest.java   | 62 ++++++++++++++++++++
 5 files changed, 136 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ant/blob/c1bbf2fc/WHATSNEW
----------------------------------------------------------------------
diff --git a/WHATSNEW b/WHATSNEW
index 6eda90f..ae03903 100644
--- a/WHATSNEW
+++ b/WHATSNEW
@@ -102,6 +102,10 @@ Other changes:
    if when a SecurityManager is active.
    Bugzilla Report 60060
 
+ * support for javac's --release switch introduced with Java9 has been
+   added.
+   Bugzilla Report 60172
+
 Changes from Ant 1.9.6 TO Ant 1.9.7
 ===================================
 

http://git-wip-us.apache.org/repos/asf/ant/blob/c1bbf2fc/manual/Tasks/javac.html
----------------------------------------------------------------------
diff --git a/manual/Tasks/javac.html b/manual/Tasks/javac.html
index 202215f..68c6310 100644
--- a/manual/Tasks/javac.html
+++ b/manual/Tasks/javac.html
@@ -522,6 +522,16 @@ invoking the compiler.</p>
       <em>Since Ant 1.9.8</em>.
     <td align="center" valign="top">No</td>
   </tr>
+  <tr>
+    <td valign="top">release</td>
+    <td valign="top">
+      Specify the value for the <code>--release</code> switch.Ignored
+      when running on JDK &lt; 9.<br>
+      When set and running on JDK &gt;= 9 the source and target
+      attributes as well as the bootclasspath will be ignored.
+      <em>Since Ant 1.9.8</em>.
+    <td align="center" valign="top">No</td>
+  </tr>
 </table>
 
 <h3>Parameters specified as nested elements</h3>

http://git-wip-us.apache.org/repos/asf/ant/blob/c1bbf2fc/src/main/org/apache/tools/ant/taskdefs/Javac.java
----------------------------------------------------------------------
diff --git a/src/main/org/apache/tools/ant/taskdefs/Javac.java b/src/main/org/apache/tools/ant/taskdefs/Javac.java
index 3452592..23c19dd 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Javac.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Javac.java
@@ -67,6 +67,7 @@ import org.apache.tools.ant.util.facade.FacadeTaskHelper;
  * <li>includejavaruntime
  * <li>source
  * <li>compiler
+ * <li>release
  * </ul>
  * Of these arguments, the <b>sourcedir</b> and <b>destdir</b> are
required.
  * <p>
@@ -121,6 +122,7 @@ public class Javac extends MatchingTask {
     private boolean depend = false;
     private boolean verbose = false;
     private String targetAttribute;
+    private String release;
     private Path bootclasspath;
     private Path extdirs;
     private Boolean includeAntRuntime;
@@ -788,6 +790,32 @@ public class Javac extends MatchingTask {
     }
 
     /**
+     * Sets the version to use for the {@code --release} switch that
+     * combines {@code source}, {@code target} and setting the
+     * bootclasspath.
+     *
+     * Values depend on the compiler, for jdk 9 the valid values are
+     * "6", "7", "8", "9".
+     * @param release the value of the release attribute
+     * @since Ant 1.9.8
+     */
+    public void setRelease(final String release) {
+        this.release = release;
+    }
+
+    /**
+     * Gets the version to use for the {@code --release} switch that
+     * combines {@code source}, {@code target} and setting the
+     * bootclasspath.
+     *
+     * @return the value of the release attribute
+     * @since Ant 1.9.8
+     */
+    public String getRelease() {
+        return release;
+    }
+
+    /**
      * If true, includes Ant's own classpath in the classpath.
      * @param include if true, includes Ant's own classpath in the classpath
      */

http://git-wip-us.apache.org/repos/asf/ant/blob/c1bbf2fc/src/main/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapter.java
----------------------------------------------------------------------
diff --git a/src/main/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapter.java
b/src/main/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapter.java
index 827b402..82373a9 100644
--- a/src/main/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapter.java
+++ b/src/main/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapter.java
@@ -70,6 +70,7 @@ public abstract class DefaultCompilerAdapter
     protected boolean depend = false;
     protected boolean verbose = false;
     protected String target;
+    protected String release;
     protected Path bootclasspath;
     protected Path extdirs;
     protected Path compileClasspath;
@@ -111,6 +112,7 @@ public abstract class DefaultCompilerAdapter
         depend = attributes.getDepend();
         verbose = attributes.getVerbose();
         target = attributes.getTarget();
+        release = attributes.getRelease();
         bootclasspath = attributes.getBootclasspath();
         extdirs = attributes.getExtdirs();
         compileList = attributes.getFileList();
@@ -321,15 +323,17 @@ public abstract class DefaultCompilerAdapter
                 cmd.createArgument().setValue("-sourcepath");
                 cmd.createArgument().setPath(sourcepath);
             }
-            if (target != null) {
-                cmd.createArgument().setValue("-target");
-                cmd.createArgument().setValue(target);
-            }
+            if (release == null || !assumeJava19()) {
+                if (target != null) {
+                    cmd.createArgument().setValue("-target");
+                    cmd.createArgument().setValue(target);
+                }
 
-            final Path bp = getBootClassPath();
-            if (bp.size() > 0) {
-                cmd.createArgument().setValue("-bootclasspath");
-                cmd.createArgument().setPath(bp);
+                final Path bp = getBootClassPath();
+                if (bp.size() > 0) {
+                    cmd.createArgument().setValue("-bootclasspath");
+                    cmd.createArgument().setPath(bp);
+                }
             }
 
             if (extdirs != null && extdirs.size() > 0) {
@@ -390,13 +394,27 @@ public abstract class DefaultCompilerAdapter
         setupJavacCommandlineSwitches(cmd, true);
         if (!assumeJava13()) { // -source added with JDK 1.4
             final String t = attributes.getTarget();
-            if (attributes.getSource() != null) {
-                cmd.createArgument().setValue("-source");
-                cmd.createArgument()
-                    .setValue(adjustSourceValue(attributes.getSource()));
+            final String s = attributes.getSource();
+            if (release == null || !assumeJava19()) {
+                if (release != null) {
+                    attributes.log("Support for javac --release has been added"
+                                   + " in Java9 ignoring it");
+                }
+                if (s != null) {
+                    cmd.createArgument().setValue("-source");
+                    cmd.createArgument().setValue(adjustSourceValue(s));
 
-            } else if (t != null && mustSetSourceForTarget(t)) {
-                setImplicitSourceSwitch(cmd, t, adjustSourceValue(t));
+                } else if (t != null && mustSetSourceForTarget(t)) {
+                    setImplicitSourceSwitch(cmd, t, adjustSourceValue(t));
+                }
+            } else { // Java 9+ and release has been set
+                if (t != null || s != null || getBootClassPath().size() > 0) {
+                    attributes.log("Ignoring source, target and bootclasspath"
+                                   + " as release has been set",
+                                   Project.MSG_WARN);
+                }
+                cmd.createArgument().setValue("--release");
+                cmd.createArgument().setValue(release);
             }
         }
         final Path msp = getModulesourcepath();

http://git-wip-us.apache.org/repos/asf/ant/blob/c1bbf2fc/src/tests/junit/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapterTest.java
----------------------------------------------------------------------
diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapterTest.java
b/src/tests/junit/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapterTest.java
index 7ed5b3f..cb274a8 100644
--- a/src/tests/junit/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapterTest.java
+++ b/src/tests/junit/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapterTest.java
@@ -68,6 +68,18 @@ public class DefaultCompilerAdapterTest {
         }
     }
 
+    private static class SourceTargetHelperNoOverride extends DefaultCompilerAdapter {
+
+        public boolean execute() { return false; }
+
+        /**
+         * public to avoid classloader issues.
+         */
+        public Commandline setupModernJavacCommandlineSwitches(Commandline cmd) {
+            return super.setupModernJavacCommandlineSwitches(cmd);
+        }
+    }
+
     @Test
     public void testSourceIsIgnoredForJavac13() {
         testSource(null, "javac1.3", "", null, "1.1");
@@ -384,6 +396,56 @@ public class DefaultCompilerAdapterTest {
         }
     }
 
+    @Test
+    public void releaseIsIgnoredForJava8() {
+        LogCapturingJavac javac = new LogCapturingJavac();
+        Project p = new Project();
+        javac.setProject(p);
+        javac.setCompiler("javac8");
+        javac.setSource("6");
+        javac.setTarget("6");
+        javac.setRelease("6");
+        javac.setSourcepath(new Path(p));
+        SourceTargetHelperNoOverride sth = new SourceTargetHelperNoOverride();
+        sth.setJavac(javac);
+        Commandline cmd = new Commandline();
+        sth.setupModernJavacCommandlineSwitches(cmd);
+        assertContains("Support for javac --release has been added in "
+                       + "Java9 ignoring it", javac.getLog());
+        String[] args = cmd.getCommandline();
+        assertEquals(7, args.length);
+        assertEquals("-classpath", args[0]);
+        assertEquals("-target", args[2]);
+        assertEquals("6", args[3]);
+        assertEquals("-g:none", args[4]);
+        assertEquals("-source", args[5]);
+        assertEquals("6", args[6]);
+    }
+
+    @Test
+    public void releaseIsUsedForJava9() {
+        LogCapturingJavac javac = new LogCapturingJavac();
+        Project p = new Project();
+        javac.setProject(p);
+        javac.setCompiler("javac9");
+        javac.setSource("6");
+        javac.setTarget("6");
+        javac.setRelease("6");
+        javac.setSourcepath(new Path(p));
+        SourceTargetHelperNoOverride sth = new SourceTargetHelperNoOverride();
+        sth.setJavac(javac);
+        Commandline cmd = new Commandline();
+        sth.setupModernJavacCommandlineSwitches(cmd);
+        assertContains("Ignoring source, target and bootclasspath as "
+                       + "release has been set", javac.getLog());
+        String[] args = cmd.getCommandline();
+        assertEquals(5, args.length);
+        assertEquals("-classpath", args[0]);
+        assertEquals("-g:none", args[2]);
+        assertEquals("--release", args[3]);
+        assertEquals("6", args[4]);
+    }
+
     private void commonSourceDowngrades(String javaVersion) {
         testSource("1.3", javaVersion,
                    "If you specify -target 1.1 you now must also specify"


Mime
View raw message