Browse Source

add support for Java9's javac --release

https://bz.apache.org/bugzilla/show_bug.cgi?id=60172
master
Stefan Bodewig 8 years ago
parent
commit
c1bbf2fc0c
5 changed files with 136 additions and 14 deletions
  1. +4
    -0
      WHATSNEW
  2. +10
    -0
      manual/Tasks/javac.html
  3. +28
    -0
      src/main/org/apache/tools/ant/taskdefs/Javac.java
  4. +32
    -14
      src/main/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapter.java
  5. +62
    -0
      src/tests/junit/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapterTest.java

+ 4
- 0
WHATSNEW View File

@@ -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
===================================



+ 10
- 0
manual/Tasks/javac.html View File

@@ -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>


+ 28
- 0
src/main/org/apache/tools/ant/taskdefs/Javac.java View File

@@ -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;
@@ -787,6 +789,32 @@ public class Javac extends MatchingTask {
: getProject().getProperty(MagicNames.BUILD_JAVAC_TARGET);
}

/**
* 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


+ 32
- 14
src/main/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapter.java View File

@@ -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();


+ 62
- 0
src/tests/junit/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapterTest.java View File

@@ -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"


Loading…
Cancel
Save