Skip to content

Commit aa79cc9

Browse files
committed
Merge branch 'main' of https://github.com/policeman-tools/forbidden-apis into scanjar
2 parents 2c20a4f + 14c003f commit aa79cc9

File tree

21 files changed

+1137
-103
lines changed

21 files changed

+1137
-103
lines changed

build.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
<equals arg1="${-cleaned.specification.version}" arg2="21"/>
7272
<equals arg1="${-cleaned.specification.version}" arg2="22"/>
7373
<equals arg1="${-cleaned.specification.version}" arg2="23"/>
74+
<equals arg1="${-cleaned.specification.version}" arg2="24"/>
7475
</or>
7576
</condition>
7677

ivy.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<!ENTITY maven.api.version "3.1.0">
1919
<!ENTITY aether.api.version "0.9.0.M2">
2020
<!ENTITY gradle.version "3.4">
21-
<!ENTITY asm.version "9.7.1">
21+
<!ENTITY asm.version "9.8">
2222
<!ENTITY jarjar.asm.version "5.2">
2323
]>
2424
<ivy-module version="2.0">

src/main/docs/ant-task.html

Lines changed: 15 additions & 1 deletion
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/docs/bundled-signatures.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ <h1>Bundled Signatures Documentation</h1>
2929
<li><strong><tt>jdk-unsafe-*</tt>:</strong> Signatures
3030
of &quot;unsafe&quot; methods that use default charset, default locale, or default timezone. For server applications it is very
3131
stupid to call those methods, as the results will definitely not what the user wants
32-
(for Java <tt>*</tt> = 1.7, 1.8, 9,..., 23; Ant / Maven / Gradle automatically add the compile Java version).</li>
32+
(for Java <tt>*</tt> = 1.7, 1.8, 9,..., 24; Ant / Maven / Gradle automatically add the compile Java version).</li>
3333

3434
<li><strong><tt>jdk-deprecated-*</tt>:</strong> This disallows all deprecated
35-
methods from the JDK (for Java <tt>*</tt> = 1.7, 1.8, 9,..., 23; Ant / Maven / Gradle automatically add the compile Java version).</li>
35+
methods from the JDK (for Java <tt>*</tt> = 1.7, 1.8, 9,..., 24; Ant / Maven / Gradle automatically add the compile Java version).</li>
3636

3737
<li><strong><tt>jdk-internal-*</tt>:</strong> Lists all internal packages of the JDK as of <code>Security.getProperty(&quot;package.access&quot;)</code>.
3838
Calling those methods will always trigger security manager and is completely forbidden from Java 9 on
39-
(for Java <tt>*</tt> = 1.7, 1.8, 9,..., 23; Ant / Maven / Gradle automatically add the compile Java version, <em>since forbiddenapis v2.1</em>).</li>
39+
(for Java <tt>*</tt> = 1.7, 1.8, 9,..., 24; Ant / Maven / Gradle automatically add the compile Java version, <em>since forbiddenapis v2.1</em>).</li>
4040

4141
<li><strong><tt>jdk-non-portable</tt>:</strong> Signatures of all non-portable (like <code>com.sun.management.HotSpotDiagnosticMXBean</code>)
4242
or internal runtime APIs (like <code>sun.misc.Unsafe</code>). This is a superset of <tt>jdk-internal</tt>.<br>
@@ -53,7 +53,7 @@ <h1>Bundled Signatures Documentation</h1>
5353

5454
<li><strong><tt>commons-io-unsafe-*</tt>:</strong> If your application uses the famous <i>Apache Common-IO</i> library,
5555
this adds signatures of all methods that depend on default charset
56-
(for versions <tt>*</tt> = 1.0, 1.1, 1.2, 1.3, 1.4, 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8.0, 2.9.0, 2.10.0, 2.11.0, 2.12.0, 2.13.0, 2.14.0, 2.15.0, 2.15.1, 2.16.0, 2.16.1, 2.17.0).</li>
56+
(for versions <tt>*</tt> = 1.0, 1.1, 1.2, 1.3, 1.4, 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8.0, 2.9.0, 2.10.0, 2.11.0, 2.12.0, 2.13.0, 2.14.0, 2.15.0, 2.15.1, 2.16.0, 2.16.1, 2.17.0, 2.18.0).</li>
5757

5858
</ul>
5959

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

Lines changed: 32 additions & 4 deletions
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;
@@ -58,6 +59,10 @@ public static enum Option {
5859
DISABLE_CLASSLOADING_CACHE
5960
}
6061

62+
public enum ViolationSeverity {
63+
ERROR, WARNING, INFO, DEBUG, SUPPRESS
64+
}
65+
6166
public final boolean isSupportedJDK;
6267

6368
private final long start;
@@ -360,6 +365,11 @@ public boolean noSignaturesFilesParsed() {
360365
return forbiddenSignatures.noSignaturesFilesParsed();
361366
}
362367

368+
/** Adjusts the severity of a specific signature. */
369+
public void setSignaturesSeverity(Collection<String> signatures, ViolationSeverity severity) throws ParseException, IOException {
370+
forbiddenSignatures.setSignaturesSeverity(signatures, severity);
371+
}
372+
363373
/** 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. */
364374
public void addClassToCheck(final InputStream in, String name) throws IOException {
365375
final ClassReader reader;
@@ -417,7 +427,7 @@ public void addSuppressAnnotation(String annoName) {
417427
/** Parses a class and checks for valid method invocations */
418428
private int checkClass(ClassMetadata c, Pattern suppressAnnotationsPattern) throws ForbiddenApiException {
419429
final String className = c.getBinaryClassName();
420-
final ClassScanner scanner = new ClassScanner(c, this, forbiddenSignatures, suppressAnnotationsPattern);
430+
final ClassScanner scanner = new ClassScanner(c, this, forbiddenSignatures, suppressAnnotationsPattern, options.contains(Option.FAIL_ON_VIOLATION));
421431
try {
422432
c.getReader().accept(scanner, ClassReader.SKIP_FRAMES);
423433
} catch (RelatedClassLoadingException rcle) {
@@ -452,12 +462,31 @@ private int checkClass(ClassMetadata c, Pattern suppressAnnotationsPattern) thro
452462
}
453463
final List<ForbiddenViolation> violations = scanner.getSortedViolations();
454464
final Pattern splitter = Pattern.compile(Pattern.quote(ForbiddenViolation.SEPARATOR));
465+
int numErrors = 0;
455466
for (final ForbiddenViolation v : violations) {
467+
if (v.severity == ViolationSeverity.ERROR) {
468+
numErrors++;
469+
}
456470
for (final String line : splitter.split(v.format(className, scanner.getSourceFile()))) {
457-
logger.error(line);
471+
switch (v.severity) {
472+
case DEBUG:
473+
logger.debug(line);
474+
break;
475+
case INFO:
476+
logger.info(line);
477+
break;
478+
case WARNING:
479+
logger.warn(line);
480+
break;
481+
case ERROR:
482+
logger.error(line);
483+
break;
484+
default:
485+
break;
486+
}
458487
}
459488
}
460-
return violations.size();
489+
return numErrors;
461490
}
462491

463492
public void run() throws ForbiddenApiException {
@@ -483,5 +512,4 @@ public void run() throws ForbiddenApiException {
483512
logger.info(message);
484513
}
485514
}
486-
487515
}

0 commit comments

Comments
 (0)