Skip to content

Commit 9bfc24c

Browse files
committed
Allow to set severity per signature
This closes #252 and #219
1 parent 535ecf7 commit 9bfc24c

13 files changed

+387
-89
lines changed

src/main/docs/ant-task.html

+15-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,20 @@ <h2>Parameters</h2>
7373
<td>Name of a <a href="bundled-signatures.html">built-in signatures</a> file.</td>
7474
</tr>
7575

76+
<tr>
77+
<td>signaturesWithSeveritySuppress</td>
78+
<td><code>String</code></td>
79+
<td></td>
80+
<td>A forbidden API signature for which violations should not be reported at all (i.e. neither fail the build nor appear in the logs). This takes precedence over<code>failOnViolation</code> and <code>signaturesWithSeverityWarn</code>.</td>
81+
</tr>
82+
83+
<tr>
84+
<td>signaturesWithSeverityWarn</td>
85+
<td><code>String</code></td>
86+
<td></td>
87+
<td>A forbidden API signature for which violations should be reported as warnings (i.e. not fail the build). This takes precedence over<code>failOnViolation</code>.</td>
88+
</tr>
89+
7690
<tr>
7791
<td>classpath</td>
7892
<td><code>Path</code></td>
@@ -184,7 +198,7 @@ <h2>Parameters specified as nested elements</h2>
184198

185199
<p>This task supports all <a href="https://ant.apache.org/manual/Types/resources.html">Ant resource</a> types
186200
(<code>fileset</code>, <code>filelist</code>, <code>file</code>, <code>tarfileset</code>, <code>zipfileset</code>,...)
187-
and uses all class files from them. It automatically adds an implcit filter to file names ending in <code>'.class'</code>,
201+
and uses all class files from them. It automatically adds an implicit filter to file names ending in <code>'.class'</code>,
188202
so you don't need to add this as include attribute to those collections.</p>
189203

190204
<p>You can also pass one or multiple <code>classpath</code> elements to form a classpath. Ideally use the same configuration like the <code>javac</code> task.</p>

src/main/java/de/thetaphi/forbiddenapis/Checker.java

+34-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.net.URL;
3131
import java.net.URLConnection;
3232
import java.util.Arrays;
33+
import java.util.Collection;
3334
import java.util.EnumSet;
3435
import java.util.HashMap;
3536
import java.util.LinkedHashSet;
@@ -45,6 +46,8 @@
4546
import org.objectweb.asm.ClassReader;
4647
import org.objectweb.asm.Type;
4748

49+
import de.thetaphi.forbiddenapis.Checker.ViolationSeverity;
50+
4851
/**
4952
* Forbidden APIs checker class.
5053
*/
@@ -58,6 +61,10 @@ public static enum Option {
5861
DISABLE_CLASSLOADING_CACHE
5962
}
6063

64+
public enum ViolationSeverity {
65+
ERROR, WARNING, INFO, DEBUG, SUPPRESS
66+
}
67+
6168
public final boolean isSupportedJDK;
6269

6370
private final long start;
@@ -360,6 +367,11 @@ public boolean noSignaturesFilesParsed() {
360367
return forbiddenSignatures.noSignaturesFilesParsed();
361368
}
362369

370+
/** Adjusts the severity of a specific signature. */
371+
public void setSignaturesSeverity(Collection<String> signatures, ViolationSeverity severity) throws ParseException, IOException {
372+
forbiddenSignatures.setSignaturesSeverity(signatures, severity);
373+
}
374+
363375
/** Parses and adds a class from the given stream to the list of classes to check. Closes the stream when parsed (on Exception, too)! Does not log anything. */
364376
public void addClassToCheck(final InputStream in, String name) throws IOException {
365377
final ClassReader reader;
@@ -417,7 +429,7 @@ public void addSuppressAnnotation(String annoName) {
417429
/** Parses a class and checks for valid method invocations */
418430
private int checkClass(ClassMetadata c, Pattern suppressAnnotationsPattern) throws ForbiddenApiException {
419431
final String className = c.getBinaryClassName();
420-
final ClassScanner scanner = new ClassScanner(c, this, forbiddenSignatures, suppressAnnotationsPattern);
432+
final ClassScanner scanner = new ClassScanner(c, this, forbiddenSignatures, suppressAnnotationsPattern, options.contains(Option.FAIL_ON_VIOLATION));
421433
try {
422434
c.getReader().accept(scanner, ClassReader.SKIP_FRAMES);
423435
} catch (RelatedClassLoadingException rcle) {
@@ -452,12 +464,31 @@ private int checkClass(ClassMetadata c, Pattern suppressAnnotationsPattern) thro
452464
}
453465
final List<ForbiddenViolation> violations = scanner.getSortedViolations();
454466
final Pattern splitter = Pattern.compile(Pattern.quote(ForbiddenViolation.SEPARATOR));
467+
int numErrors = 0;
455468
for (final ForbiddenViolation v : violations) {
469+
if (v.severity == ViolationSeverity.ERROR) {
470+
numErrors++;
471+
}
456472
for (final String line : splitter.split(v.format(className, scanner.getSourceFile()))) {
457-
logger.error(line);
473+
switch (v.severity) {
474+
case DEBUG:
475+
logger.debug(line);
476+
break;
477+
case INFO:
478+
logger.info(line);
479+
break;
480+
case WARNING:
481+
logger.warn(line);
482+
break;
483+
case ERROR:
484+
logger.error(line);
485+
break;
486+
default:
487+
break;
488+
}
458489
}
459490
}
460-
return violations.size();
491+
return numErrors;
461492
}
462493

463494
public void run() throws ForbiddenApiException {
@@ -483,5 +514,4 @@ public void run() throws ForbiddenApiException {
483514
logger.info(message);
484515
}
485516
}
486-
487517
}

0 commit comments

Comments
 (0)