Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow arbitrary Ant resources as signatures #78

Merged
merged 2 commits into from
Sep 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/docs/ant-task.html
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ <h2>Parameters specified as nested elements</h2>

<ul>
<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>
<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>
<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>
<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>
<li>Place signatures as plain text (use CDATA sections) inside the <code>forbiddenapis</code> element.</li>
</ul>

Expand Down
24 changes: 15 additions & 9 deletions src/main/java/de/thetaphi/forbiddenapis/ant/AntTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.tools.ant.types.resources.FileResource;
import org.apache.tools.ant.types.resources.Resources;
import org.apache.tools.ant.types.resources.StringResource;
import org.apache.tools.ant.types.resources.Union;

import de.thetaphi.forbiddenapis.Checker;
import de.thetaphi.forbiddenapis.ForbiddenApiException;
Expand All @@ -42,10 +43,10 @@

import java.io.IOException;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.List;
import java.util.LinkedHashSet;
import java.util.Locale;

/**
Expand All @@ -56,10 +57,10 @@
*/
public class AntTask extends Task {

private final Resources classFiles = new Resources();
private final Resources apiSignatures = new Resources();
private final List<BundledSignaturesType> bundledSignatures = new ArrayList<BundledSignaturesType>();
private final List<SuppressAnnotationType> suppressAnnotations = new ArrayList<SuppressAnnotationType>();
private final Union classFiles = new Union();
private final Union apiSignatures = new Union();
private final Collection<BundledSignaturesType> bundledSignatures = new LinkedHashSet<BundledSignaturesType>();
private final Collection<SuppressAnnotationType> suppressAnnotations = new LinkedHashSet<SuppressAnnotationType>();
private Path classpath = null;

private boolean failOnUnsupportedJava = false;
Expand Down Expand Up @@ -155,7 +156,7 @@ public void info(String msg) {
}

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

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

private <T extends ResourceCollection> T addSignaturesResource(T res) {
((ProjectComponent) res).setProject(getProject());
private <T extends ProjectComponent & ResourceCollection> T addSignaturesResource(T res) {
res.setProject(getProject());
apiSignatures.add(res);
return res;
}
Expand All @@ -236,6 +237,11 @@ public void setSignaturesFile(File file) {
createSignaturesFile().setFile(file);
}

/** Creates a collection of arbitrary Ant resources */
public Resources createSignaturesResources() {
return addSignaturesResource(new Resources());
}

/** Support for API signatures list as nested text */
public void addText(String text) {
addSignaturesResource(new StringResource(text));
Expand Down
14 changes: 14 additions & 0 deletions src/test/antunit/TestFileSignatures.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,18 @@
<au:assertLogContains level="error" text="java.lang.String#substring(int,int) [You are crazy that you disallow substrings]"/>
</target>

<target name="testGeneralResources">
<au:expectfailure expectedMessage="Check for forbidden API calls failed, see log">
<forbiddenapis classpathref="path.all">
<fileset refid="main.classes"/>
<signaturesResources>
<file file="signatures1.txt"/>
<string>java.util.Locale#ENGLISH @ We are speaking chinese here!</string>
</signaturesResources>
</forbiddenapis>
</au:expectfailure>
<au:assertLogContains level="error" text="java.lang.String#substring(int,int) [You are crazy that you disallow substrings]"/>
<au:assertLogContains level="error" text="java.util.Locale#ENGLISH [We are speaking chinese here!]"/>
</target>

</project>