Browse Source

Bug 44241: 'activate' Steves <javaconstant> resource

master
Jan Matrne 10 years ago
parent
commit
9953e3cbc0
5 changed files with 106 additions and 1 deletions
  1. +2
    -0
      WHATSNEW
  2. +43
    -1
      manual/Types/resources.html
  3. +1
    -0
      src/main/org/apache/tools/ant/types/defaults.properties
  4. +3
    -0
      src/main/org/apache/tools/ant/types/resources/JavaConstantResource.java
  5. +57
    -0
      src/tests/antunit/types/resources/test.xml

+ 2
- 0
WHATSNEW View File

@@ -13,6 +13,8 @@ Fixed bugs:

Other changes:
--------------
* Hidden <javaconstant> resource is published now. It reads the
value of a specified java constant.


Changes from Ant 1.9.4 TO Ant 1.9.5


+ 43
- 1
manual/Types/resources.html View File

@@ -43,6 +43,8 @@ explicit use beginning in <b>Ant 1.7</b>.
<li><a href="#gzipresource">gzipresource</a> - a GZip compressed resource.</li>
<li><a href="#javaresource">javaresource</a> - a resource loadable
via a Java classloader.</li>
<li><a href="#javaconstant">javaconstant</a> - a constant in a class loadable
via a Java classloader.</li>
<li><a href="#propertyresource">propertyresource</a> - an Ant property.</li>
<li><a href="#string">string</a> - a text string.</li>
<li><a href="#tarentry">tarentry</a> - an entry in a tar file.</li>
@@ -164,9 +166,49 @@ implementations are also usable as single-element
where <b>&lt;classpath&gt;</b> is a <a
href="../using.html#path">path-like structure</a>.</p>


<h4><a name="javaconstant">javaconstant</a></h4>
<p>Loads the value of a java constant. As a specialisation of
<a href="#javaresource">javaresource</a> all of its attributes and nested elements are
supported. A constant must be specified as <tt>public static</tt> otherwise it could not be loaded.</p>

<table border="1" cellpadding="2" cellspacing="0">
<tr>
<td valign="top"><b>Attribute</b></td>
<td valign="top"><b>Description</b></td>
<td align="center" valign="top"><b>Required</b></td>
</tr>
<tr>
<td valign="top">name</td>
<td valign="top">The name of the resource. Must be specified as full qualified
field name.
</td>
<td align="center" valign="top">Yes</td>
</tr>
</table>
<h5>Examples</h5>
This loads the value of the constant <tt>VERSION</tt> of the <tt>org.acme.Main</tt> class
into the <tt>version</tt>-property. The classpath for finding that class is provided via
nested classpath element.
<pre><code>&lt;loadresource property=&quot;version&quot;&gt;
&lt;javaconstant name=&quot;org.acme.Main.VERSION&quot;&gt;
&lt;classpath&gt;
&lt;pathelement location=&quot;${acme.lib.dir}&quot;/&gt;
&lt;/classpath&gt;
&lt;/javaconstant&gt;
&lt;/loadresource&gt;
</code></pre>

Create a new file <tt>c:/temp/org.apache.tools.ant.Main.DEFAULT_BUILD_FILENAME</tt> with the content
of that constant (<tt>build.xml</tt>).
<pre><code>&lt;copy todir=&quot;c:/temp&quot;&gt;
&lt;javaconstant name=&quot;org.apache.tools.ant.Main.DEFAULT_BUILD_FILENAME&quot;/&gt;
&lt;/copy&gt;</code></pre>


<h4><a name="zipentry">zipentry</a></h4>

<p>Represents an entry in a ZIP archive. The archive can be specified
<p>Represents an entry in a ZIP archive. The archive can be specified
using the archive attribute or a nested single-element resource
collection. <code>zipentry</code> only supports file system resources
as nested elements.</p>


+ 1
- 0
src/main/org/apache/tools/ant/types/defaults.properties View File

@@ -93,6 +93,7 @@ gzipresource=org.apache.tools.ant.types.resources.GZipResource
bzip2resource=org.apache.tools.ant.types.resources.BZip2Resource
javaresource=org.apache.tools.ant.types.resources.JavaResource
multirootfileset=org.apache.tools.ant.types.resources.MultiRootFileSet
javaconstant=org.apache.tools.ant.types.resources.JavaConstantResource

#tokenizer implementations
linetokenizer=org.apache.tools.ant.util.LineTokenizer


+ 3
- 0
src/main/org/apache/tools/ant/types/resources/JavaConstantResource.java View File

@@ -39,6 +39,9 @@ public class JavaConstantResource extends AbstractClasspathResource {
protected InputStream openInputStream(ClassLoader cl) throws IOException {
Class<?> clazz;
String constant = getName();
if (constant == null) {
throw new IOException("Attribute 'name' must be set.");
}
int index1 = constant.lastIndexOf('.');
if (index1 < 0) {
throw new IOException("No class name in " + constant);


+ 57
- 0
src/tests/antunit/types/resources/test.xml View File

@@ -400,4 +400,61 @@
</au:assertTrue>
</target>

<target name="testJavaConstant">
<property name="test.tmp.dir" value="${antunit.tmpdir}/testJavaConstant"/>
<mkdir dir="${test.tmp.dir}"/>
<echo file="${test.tmp.dir}/SomeClass.java">
public class SomeClass {
public static final String CONSTANT = "constant";
public final String NOT_STATIC = "not-static";
private static final String PRIVATE = "private";
}
</echo>
<javac srcdir="${test.tmp.dir}" destdir="${test.tmp.dir}"/>
<path id="tmp.cp">
<pathelement location="${test.tmp.dir}"/>
</path>

<loadresource property="actual">
<javaconstant name="org.apache.tools.ant.Main.DEFAULT_BUILD_FILENAME"/>
</loadresource>
<au:assertEquals message="could not read java constant" expected="build.xml" actual="${actual}" />

<!--
We can't test for special error messages using built-in tasks
because they catch these messages
-->
<au:expectfailure>
<loadresource property="p">
<javaconstant/>
</loadresource>
</au:expectfailure>
<au:expectfailure>
<loadresource property="p">
<javaconstant name="org.apache.tools.ant.MissingClass"/>
</loadresource>
</au:expectfailure>
<au:expectfailure>
<loadresource property="p">
<javaconstant name="SomeClass.CONSTANT2" classpathref="tmp.cp"/>
</loadresource>
</au:expectfailure>
<au:expectfailure>
<loadresource property="p">
<javaconstant name="SomeClass.PRIVATE">
<classpath>
<pathelement location="${test.tmp.dir}"/>
</classpath>
</javaconstant>
</loadresource>
</au:expectfailure>
<au:expectfailure>
<loadresource property="p">
<javaconstant name="SomeClass.NOT_STATIC"/>
</loadresource>
</au:expectfailure>
<delete dir="${test.tmp.dir}"/>
</target>

</project>

Loading…
Cancel
Save