Skip to content

Commit

Permalink
Allow to set severity per signature
Browse files Browse the repository at this point in the history
  • Loading branch information
kwin committed Jan 6, 2025
1 parent 535ecf7 commit 9bfc24c
Show file tree
Hide file tree
Showing 13 changed files with 387 additions and 89 deletions.
16 changes: 15 additions & 1 deletion src/main/docs/ant-task.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ <h2>Parameters</h2>
<td>Name of a <a href="bundled-signatures.html">built-in signatures</a> file.</td>
</tr>

<tr>
<td>signaturesWithSeveritySuppress</td>
<td><code>String</code></td>
<td></td>
<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>
</tr>

<tr>
<td>signaturesWithSeverityWarn</td>
<td><code>String</code></td>
<td></td>
<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>
</tr>

<tr>
<td>classpath</td>
<td><code>Path</code></td>
Expand Down Expand Up @@ -184,7 +198,7 @@ <h2>Parameters specified as nested elements</h2>

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

<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>
Expand Down
38 changes: 34 additions & 4 deletions src/main/java/de/thetaphi/forbiddenapis/Checker.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.LinkedHashSet;
Expand All @@ -45,6 +46,8 @@
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.Type;

import de.thetaphi.forbiddenapis.Checker.ViolationSeverity;

/**
* Forbidden APIs checker class.
*/
Expand All @@ -58,6 +61,10 @@ public static enum Option {
DISABLE_CLASSLOADING_CACHE
}

public enum ViolationSeverity {
ERROR, WARNING, INFO, DEBUG, SUPPRESS
}

public final boolean isSupportedJDK;

private final long start;
Expand Down Expand Up @@ -360,6 +367,11 @@ public boolean noSignaturesFilesParsed() {
return forbiddenSignatures.noSignaturesFilesParsed();
}

/** Adjusts the severity of a specific signature. */
public void setSignaturesSeverity(Collection<String> signatures, ViolationSeverity severity) throws ParseException, IOException {
forbiddenSignatures.setSignaturesSeverity(signatures, severity);
}

/** 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. */
public void addClassToCheck(final InputStream in, String name) throws IOException {
final ClassReader reader;
Expand Down Expand Up @@ -417,7 +429,7 @@ public void addSuppressAnnotation(String annoName) {
/** Parses a class and checks for valid method invocations */
private int checkClass(ClassMetadata c, Pattern suppressAnnotationsPattern) throws ForbiddenApiException {
final String className = c.getBinaryClassName();
final ClassScanner scanner = new ClassScanner(c, this, forbiddenSignatures, suppressAnnotationsPattern);
final ClassScanner scanner = new ClassScanner(c, this, forbiddenSignatures, suppressAnnotationsPattern, options.contains(Option.FAIL_ON_VIOLATION));
try {
c.getReader().accept(scanner, ClassReader.SKIP_FRAMES);
} catch (RelatedClassLoadingException rcle) {
Expand Down Expand Up @@ -452,12 +464,31 @@ private int checkClass(ClassMetadata c, Pattern suppressAnnotationsPattern) thro
}
final List<ForbiddenViolation> violations = scanner.getSortedViolations();
final Pattern splitter = Pattern.compile(Pattern.quote(ForbiddenViolation.SEPARATOR));
int numErrors = 0;
for (final ForbiddenViolation v : violations) {
if (v.severity == ViolationSeverity.ERROR) {
numErrors++;
}
for (final String line : splitter.split(v.format(className, scanner.getSourceFile()))) {
logger.error(line);
switch (v.severity) {
case DEBUG:
logger.debug(line);
break;
case INFO:
logger.info(line);
break;
case WARNING:
logger.warn(line);
break;
case ERROR:
logger.error(line);
break;
default:
break;
}
}
}
return violations.size();
return numErrors;
}

public void run() throws ForbiddenApiException {
Expand All @@ -483,5 +514,4 @@ public void run() throws ForbiddenApiException {
logger.info(message);
}
}

}
Loading

0 comments on commit 9bfc24c

Please sign in to comment.