From 67a4d602fed563a2521c24ded69e2b1d8a17ca17 Mon Sep 17 00:00:00 2001 From: uschindler Date: Sun, 14 Sep 2014 21:40:17 +0000 Subject: [PATCH] Fix issue #35: Allow to ignore empty filesets: . Fix issue #36: Allow signaturesFileList and signaturesFile nested elements to enforce existence (does not work with Filesets). --- .../de/thetaphi/forbiddenapis/AntTask.java | 51 ++++++++++++++----- src/test/antunit/TestClassReferences.xml | 2 +- src/test/antunit/TestCommonsIOSignatures.xml | 8 +-- src/test/antunit/TestFileSignatures.xml | 20 ++++++++ src/test/antunit/TestInternalRuntimeCalls.xml | 2 +- src/test/antunit/TestJava8.xml | 2 +- 6 files changed, 63 insertions(+), 22 deletions(-) diff --git a/src/main/java/de/thetaphi/forbiddenapis/AntTask.java b/src/main/java/de/thetaphi/forbiddenapis/AntTask.java index 28b8b769..d26b81e5 100644 --- a/src/main/java/de/thetaphi/forbiddenapis/AntTask.java +++ b/src/main/java/de/thetaphi/forbiddenapis/AntTask.java @@ -23,6 +23,7 @@ import org.apache.tools.ant.Project; import org.apache.tools.ant.Task; import org.apache.tools.ant.types.Path; +import org.apache.tools.ant.types.FileList; import org.apache.tools.ant.types.FileSet; import org.apache.tools.ant.types.Reference; import org.apache.tools.ant.types.Resource; @@ -55,6 +56,7 @@ public final class AntTask extends Task { private boolean internalRuntimeForbidden = false; private boolean restrictClassFilename = true; private boolean failOnMissingClasses = true; + private boolean ignoreEmptyFileset = false; @Override public void execute() throws BuildException { @@ -112,12 +114,9 @@ protected void logInfo(String msg) { } @SuppressWarnings("unchecked") - Iterator iter = (Iterator) apiSignatures.iterator(); + final Iterator iter = (Iterator) apiSignatures.iterator(); while (iter.hasNext()) { final Resource r = iter.next(); - if (!r.isExists()) { - throw new BuildException("Signatures file does not exist: " + r); - } if (r instanceof StringResource) { final String s = ((StringResource) r).getValue(); if (s != null && s.trim().length() > 0) { @@ -142,23 +141,28 @@ protected void logInfo(String msg) { log("Loading classes to check...", Project.MSG_INFO); try { @SuppressWarnings("unchecked") - Iterator iter = (Iterator) classFiles.iterator(); - if (!iter.hasNext()) { - throw new BuildException("There is no given or the fileset does not contain any class files to check."); - } + final Iterator iter = (Iterator) classFiles.iterator(); + boolean foundClass = false; while (iter.hasNext()) { final Resource r = iter.next(); final String name = r.getName(); if (restrictClassFilename && name != null && !name.endsWith(".class")) { continue; } - if (!r.isExists()) { - throw new BuildException("Class file does not exist: " + r); - } checker.addClassToCheck(r.getInputStream()); + foundClass = true; + } + if (!foundClass) { + if (ignoreEmptyFileset) { + log("There is no or other resource collection given, or the collection does not contain any class files to check.", Project.MSG_WARN); + log("Scanned 0 class files.", Project.MSG_INFO); + return; + } else { + throw new BuildException("There is no or other resource collection given, or the collection does not contain any class files to check."); + } } } catch (IOException ioe) { - throw new BuildException("Failed to load one of the given class files:" + ioe); + throw new BuildException("Failed to load one of the given class files: " + ioe); } log("Scanning for API signatures and dependencies...", Project.MSG_INFO); @@ -202,6 +206,22 @@ public FileSet createSignaturesFileSet() { return fs; } + /** List of files with API signatures as nested element */ + public FileList createSignaturesFileList() { + final FileList fl = new FileList(); + fl.setProject(getProject()); + apiSignatures.add(fl); + return fl; + } + + /** Single file with API signatures as nested element */ + public FileResource createSignaturesFile() { + final FileResource fr = new FileResource(); + fr.setProject(getProject()); + apiSignatures.add(fr); + return fr; + } + public BundledSignaturesType createBundledSignatures() { final BundledSignaturesType s = new BundledSignaturesType(); s.setProject(getProject()); @@ -278,4 +298,11 @@ public void setRestrictClassFilename(boolean restrictClassFilename) { this.restrictClassFilename = restrictClassFilename; } + /** Ignore empty fileset/resource collection and print a warning instead. + * Defaults to {@code false}. + */ + public void setIgnoreEmptyFileSet(boolean ignoreEmptyFileset) { + this.ignoreEmptyFileset = ignoreEmptyFileset; + } + } diff --git a/src/test/antunit/TestClassReferences.xml b/src/test/antunit/TestClassReferences.xml index 92d5fd5b..a932b344 100644 --- a/src/test/antunit/TestClassReferences.xml +++ b/src/test/antunit/TestClassReferences.xml @@ -19,7 +19,7 @@ - + java.lang.Integer @ Forbidden class reference! java.util.List @ Forbidden class reference! java.util.AbstractCollection @ Forbidden class reference! diff --git a/src/test/antunit/TestCommonsIOSignatures.xml b/src/test/antunit/TestCommonsIOSignatures.xml index cae198b8..a4ac7378 100644 --- a/src/test/antunit/TestCommonsIOSignatures.xml +++ b/src/test/antunit/TestCommonsIOSignatures.xml @@ -32,13 +32,7 @@ - - - - - - - + \ No newline at end of file diff --git a/src/test/antunit/TestFileSignatures.xml b/src/test/antunit/TestFileSignatures.xml index eb627da6..813960cf 100644 --- a/src/test/antunit/TestFileSignatures.xml +++ b/src/test/antunit/TestFileSignatures.xml @@ -37,4 +37,24 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/antunit/TestInternalRuntimeCalls.xml b/src/test/antunit/TestInternalRuntimeCalls.xml index 719369b1..4e8256fa 100644 --- a/src/test/antunit/TestInternalRuntimeCalls.xml +++ b/src/test/antunit/TestInternalRuntimeCalls.xml @@ -19,7 +19,7 @@ - + diff --git a/src/test/antunit/TestJava8.xml b/src/test/antunit/TestJava8.xml index 8806a7a8..7ba5f7ae 100644 --- a/src/test/antunit/TestJava8.xml +++ b/src/test/antunit/TestJava8.xml @@ -19,7 +19,7 @@ - + java.io.Closeable @ Police alarm! java.lang.StringBuilder @ Police alarm! java.lang.Integer#compareTo(java.lang.Integer) @ Closure alarm!