36
36
import java .net .URLConnection ;
37
37
import java .util .Arrays ;
38
38
import java .util .Collections ;
39
+ import java .util .EnumSet ;
39
40
import java .util .HashMap ;
40
41
import java .util .LinkedHashSet ;
41
42
import java .util .List ;
56
57
* (which violates the spec).
57
58
*/
58
59
public abstract class Checker implements RelatedClassLookup {
60
+
61
+ public static enum Option {
62
+ INTERNAL_RUNTIME_FORBIDDEN ,
63
+ FAIL_ON_MISSING_CLASSES ,
64
+ FAIL_ON_VIOLATION ,
65
+ FAIL_ON_UNRESOLVABLE_SIGNATURES
66
+ }
59
67
60
68
public final boolean isSupportedJDK ;
61
69
@@ -64,7 +72,7 @@ public abstract class Checker implements RelatedClassLookup {
64
72
final Set <File > bootClassPathJars ;
65
73
final Set <String > bootClassPathDirs ;
66
74
final ClassLoader loader ;
67
- final boolean internalRuntimeForbidden , failOnMissingClasses , failOnViolation , defaultFailOnUnresolvableSignatures ;
75
+ final EnumSet < Option > options ;
68
76
69
77
// key is the internal name (slashed):
70
78
final Map <String ,ClassSignature > classesToCheck = new HashMap <String ,ClassSignature >();
@@ -86,12 +94,14 @@ public abstract class Checker implements RelatedClassLookup {
86
94
protected abstract void logWarn (String msg );
87
95
protected abstract void logInfo (String msg );
88
96
89
- public Checker (ClassLoader loader , boolean internalRuntimeForbidden , boolean failOnMissingClasses , boolean failOnViolation , boolean defaultFailOnUnresolvableSignatures ) {
97
+ //public Checker(ClassLoader loader, boolean internalRuntimeForbidden, boolean failOnMissingClasses, boolean failOnViolation, boolean defaultFailOnUnresolvableSignatures) {
98
+ public Checker (ClassLoader loader , Option ... options ) {
99
+ this (loader , EnumSet .copyOf (Arrays .asList (options )));
100
+ }
101
+
102
+ public Checker (ClassLoader loader , EnumSet <Option > options ) {
90
103
this .loader = loader ;
91
- this .internalRuntimeForbidden = internalRuntimeForbidden ;
92
- this .failOnMissingClasses = failOnMissingClasses ;
93
- this .failOnViolation = failOnViolation ;
94
- this .defaultFailOnUnresolvableSignatures = defaultFailOnUnresolvableSignatures ;
104
+ this .options = options ;
95
105
this .start = System .currentTimeMillis ();
96
106
97
107
// default (always available)
@@ -216,7 +226,7 @@ public ClassSignature lookupRelatedClass(String internalName) {
216
226
// use binary name, so we need to convert:
217
227
c = getClassFromClassLoader (type .getClassName ());
218
228
} catch (ClassNotFoundException cnfe ) {
219
- if (failOnMissingClasses ) {
229
+ if (options . contains ( Option . FAIL_ON_MISSING_CLASSES ) ) {
220
230
throw new WrapperRuntimeException (cnfe );
221
231
} else {
222
232
logWarn (String .format (Locale .ENGLISH ,
@@ -359,7 +369,7 @@ private void parseSignaturesFile(Reader reader, boolean allowBundled) throws IOE
359
369
final BufferedReader r = new BufferedReader (reader );
360
370
try {
361
371
String line , defaultMessage = null ;
362
- boolean failOnUnresolvableSignatures = this . defaultFailOnUnresolvableSignatures ;
372
+ boolean failOnUnresolvableSignatures = options . contains ( Option . FAIL_ON_UNRESOLVABLE_SIGNATURES ) ;
363
373
while ((line = r .readLine ()) != null ) {
364
374
line = line .trim ();
365
375
if (line .length () == 0 || line .startsWith ("#" ))
@@ -397,7 +407,7 @@ public final void addClassToCheck(final InputStream in) throws IOException {
397
407
}
398
408
399
409
public final boolean hasNoSignatures () {
400
- return forbiddenMethods .isEmpty () && forbiddenFields .isEmpty () && forbiddenClasses .isEmpty () && forbiddenClassPatterns .isEmpty () && (!internalRuntimeForbidden );
410
+ return forbiddenMethods .isEmpty () && forbiddenFields .isEmpty () && forbiddenClasses .isEmpty () && forbiddenClassPatterns .isEmpty () && (!options . contains ( Option . INTERNAL_RUNTIME_FORBIDDEN ) );
401
411
}
402
412
403
413
/** Adds the given annotation class for suppressing errors. */
@@ -413,7 +423,7 @@ public final void addSuppressAnnotation(String annoName) {
413
423
/** Parses a class and checks for valid method invocations */
414
424
private int checkClass (final ClassReader reader , Pattern suppressAnnotationsPattern ) {
415
425
final String className = Type .getObjectType (reader .getClassName ()).getClassName ();
416
- final ClassScanner scanner = new ClassScanner (this , forbiddenClasses , forbiddenClassPatterns , forbiddenMethods , forbiddenFields , suppressAnnotationsPattern , internalRuntimeForbidden );
426
+ final ClassScanner scanner = new ClassScanner (this , forbiddenClasses , forbiddenClassPatterns , forbiddenMethods , forbiddenFields , suppressAnnotationsPattern , options . contains ( Option . INTERNAL_RUNTIME_FORBIDDEN ) );
417
427
reader .accept (scanner , ClassReader .SKIP_FRAMES );
418
428
final List <ForbiddenViolation > violations = scanner .getSortedViolations ();
419
429
final Pattern splitter = Pattern .compile (Pattern .quote ("\n " ));
@@ -440,7 +450,7 @@ public final void run() throws ForbiddenApiException {
440
450
final String message = String .format (Locale .ENGLISH ,
441
451
"Scanned %d (and %d related) class file(s) for forbidden API invocations (in %.2fs), %d error(s)." ,
442
452
classesToCheck .size (), classesToCheck .isEmpty () ? 0 : classpathClassCache .size (), (System .currentTimeMillis () - start ) / 1000.0 , errors );
443
- if (failOnViolation && errors > 0 ) {
453
+ if (options . contains ( Option . FAIL_ON_VIOLATION ) && errors > 0 ) {
444
454
logError (message );
445
455
throw new ForbiddenApiException ("Check for forbidden API calls failed, see log." );
446
456
} else {
0 commit comments