Skip to content

Commit aa8158f

Browse files
committed
Merge pull request #78 from policeman-tools/features/AntResources
Allow arbitrary Ant resources as signatures
2 parents 874fd43 + bb7e350 commit aa8158f

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

src/main/docs/ant-task.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ <h2>Parameters specified as nested elements</h2>
165165

166166
<ul>
167167
<li>Use <code>bundledSignatures</code> element to pass a <a href="bundled-signatures.html">built-in signatures</a> file, e.g. <code>&lt;bundledsignatures name=&quot;jdk-unsafe-1.7&quot;/&gt;</code></li>
168-
<li>Use <code>signaturesFileSet</code>, <code>signaturesFileList</code>, <code>signaturesFile</code> elements to pass in collections of signatures files. Those elements behave like the corresponding standard Ant types.</li>
168+
<li>Use <code>signaturesResources</code> element to wrap any valid <a href="https://ant.apache.org/manual/Types/resources.html">Ant resource</a> type (filesets,..).</li>
169+
<li>Alternatively, use <code>signaturesFileSet</code>, <code>signaturesFileList</code>, <code>signaturesFile</code> elements to pass in collections of signatures files. Those elements behave like the corresponding standard Ant types.</li>
169170
<li>Place signatures as plain text (use CDATA sections) inside the <code>forbiddenapis</code> element.</li>
170171
</ul>
171172

src/main/java/de/thetaphi/forbiddenapis/ant/AntTask.java

+15-9
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.apache.tools.ant.types.resources.FileResource;
3535
import org.apache.tools.ant.types.resources.Resources;
3636
import org.apache.tools.ant.types.resources.StringResource;
37+
import org.apache.tools.ant.types.resources.Union;
3738

3839
import de.thetaphi.forbiddenapis.Checker;
3940
import de.thetaphi.forbiddenapis.ForbiddenApiException;
@@ -42,10 +43,10 @@
4243

4344
import java.io.IOException;
4445
import java.io.File;
45-
import java.util.ArrayList;
46+
import java.util.Collection;
4647
import java.util.EnumSet;
4748
import java.util.Iterator;
48-
import java.util.List;
49+
import java.util.LinkedHashSet;
4950
import java.util.Locale;
5051

5152
/**
@@ -56,10 +57,10 @@
5657
*/
5758
public class AntTask extends Task {
5859

59-
private final Resources classFiles = new Resources();
60-
private final Resources apiSignatures = new Resources();
61-
private final List<BundledSignaturesType> bundledSignatures = new ArrayList<BundledSignaturesType>();
62-
private final List<SuppressAnnotationType> suppressAnnotations = new ArrayList<SuppressAnnotationType>();
60+
private final Union classFiles = new Union();
61+
private final Union apiSignatures = new Union();
62+
private final Collection<BundledSignaturesType> bundledSignatures = new LinkedHashSet<BundledSignaturesType>();
63+
private final Collection<SuppressAnnotationType> suppressAnnotations = new LinkedHashSet<SuppressAnnotationType>();
6364
private Path classpath = null;
6465

6566
private boolean failOnUnsupportedJava = false;
@@ -155,7 +156,7 @@ public void info(String msg) {
155156
}
156157

157158
if (checker.hasNoSignatures()) {
158-
throw new BuildException("No API signatures found; use signaturesFile=, <signaturesFileSet/>, <bundledSignatures/> or inner text to define those!");
159+
throw new BuildException("No API signatures found; use signaturesFile=, <signatures*/>, <bundledSignatures/> or inner text to define those!");
159160
}
160161

161162
log.info("Loading classes to check...");
@@ -210,8 +211,8 @@ public void setDir(File dir) {
210211
classFiles.add(fs);
211212
}
212213

213-
private <T extends ResourceCollection> T addSignaturesResource(T res) {
214-
((ProjectComponent) res).setProject(getProject());
214+
private <T extends ProjectComponent & ResourceCollection> T addSignaturesResource(T res) {
215+
res.setProject(getProject());
215216
apiSignatures.add(res);
216217
return res;
217218
}
@@ -236,6 +237,11 @@ public void setSignaturesFile(File file) {
236237
createSignaturesFile().setFile(file);
237238
}
238239

240+
/** Creates a collection of arbitrary Ant resources */
241+
public Resources createSignaturesResources() {
242+
return addSignaturesResource(new Resources());
243+
}
244+
239245
/** Support for API signatures list as nested text */
240246
public void addText(String text) {
241247
addSignaturesResource(new StringResource(text));

src/test/antunit/TestFileSignatures.xml

+14
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,18 @@
5757
<au:assertLogContains level="error" text="java.lang.String#substring(int,int) [You are crazy that you disallow substrings]"/>
5858
</target>
5959

60+
<target name="testGeneralResources">
61+
<au:expectfailure expectedMessage="Check for forbidden API calls failed, see log">
62+
<forbiddenapis classpathref="path.all">
63+
<fileset refid="main.classes"/>
64+
<signaturesResources>
65+
<file file="signatures1.txt"/>
66+
<string>java.util.Locale#ENGLISH @ We are speaking chinese here!</string>
67+
</signaturesResources>
68+
</forbiddenapis>
69+
</au:expectfailure>
70+
<au:assertLogContains level="error" text="java.lang.String#substring(int,int) [You are crazy that you disallow substrings]"/>
71+
<au:assertLogContains level="error" text="java.util.Locale#ENGLISH [We are speaking chinese here!]"/>
72+
</target>
73+
6074
</project>

0 commit comments

Comments
 (0)