@@ -90,6 +90,29 @@ public static enum Option {
90
90
// descriptors (not internal names) of all annotations that suppress:
91
91
final Set <String > suppressAnnotations = new LinkedHashSet <String >();
92
92
93
+ private static enum UnresolvableReporting {
94
+ FAIL () {
95
+ @ Override
96
+ public void parseFailed (Logger logger , String message , String signature ) throws ParseException {
97
+ throw new ParseException (String .format (Locale .ENGLISH , "%s while parsing signature: %s" , message , signature ));
98
+ }
99
+ },
100
+ WARNING () {
101
+ @ Override
102
+ public void parseFailed (Logger logger , String message , String signature ) throws ParseException {
103
+ logger .warn (String .format (Locale .ENGLISH , "%s while parsing signature: %s [signature ignored]" , message , signature ));
104
+ }
105
+ },
106
+ SILENT () {
107
+ @ Override
108
+ public void parseFailed (Logger logger , String message , String signature ) throws ParseException {
109
+ // keep silent
110
+ }
111
+ };
112
+
113
+ public abstract void parseFailed (Logger logger , String message , String signature ) throws ParseException ;
114
+ }
115
+
93
116
public Checker (Logger logger , ClassLoader loader , Option ... options ) {
94
117
this (logger , loader , (options .length == 0 ) ? EnumSet .noneOf (Option .class ) : EnumSet .copyOf (Arrays .asList (options )));
95
118
}
@@ -296,16 +319,8 @@ public ClassSignature lookupRelatedClass(String internalName) {
296
319
return c ;
297
320
}
298
321
299
- private void reportParseFailed (boolean failOnUnresolvableSignatures , String message , String signature ) throws ParseException {
300
- if (failOnUnresolvableSignatures ) {
301
- throw new ParseException (String .format (Locale .ENGLISH , "%s while parsing signature: %s" , message , signature ));
302
- } else {
303
- logger .warn (String .format (Locale .ENGLISH , "%s while parsing signature: %s [signature ignored]" , message , signature ));
304
- }
305
- }
306
-
307
322
/** Adds the method signature to the list of disallowed methods. The Signature is checked against the given ClassLoader. */
308
- private void addSignature (final String line , final String defaultMessage , final boolean failOnUnresolvableSignatures ) throws ParseException ,IOException {
323
+ private void addSignature (final String line , final String defaultMessage , final UnresolvableReporting report ) throws ParseException ,IOException {
309
324
final String clazz , field , signature , message ;
310
325
final Method method ;
311
326
int p = line .indexOf ('@' );
@@ -355,7 +370,7 @@ private void addSignature(final String line, final String defaultMessage, final
355
370
try {
356
371
c = getClassFromClassLoader (clazz );
357
372
} catch (ClassNotFoundException cnfe ) {
358
- reportParseFailed ( failOnUnresolvableSignatures , String .format (Locale .ENGLISH , "Class '%s' not found on classpath" , cnfe .getMessage ()), signature );
373
+ report . parseFailed ( logger , String .format (Locale .ENGLISH , "Class '%s' not found on classpath" , cnfe .getMessage ()), signature );
359
374
return ;
360
375
}
361
376
if (method != null ) {
@@ -370,13 +385,13 @@ private void addSignature(final String line, final String defaultMessage, final
370
385
}
371
386
}
372
387
if (!found ) {
373
- reportParseFailed ( failOnUnresolvableSignatures , "Method not found" , signature );
388
+ report . parseFailed ( logger , "Method not found" , signature );
374
389
return ;
375
390
}
376
391
} else if (field != null ) {
377
392
assert method == null ;
378
393
if (!c .fields .contains (field )) {
379
- reportParseFailed ( failOnUnresolvableSignatures , "Field not found" , signature );
394
+ report . parseFailed ( logger , "Field not found" , signature );
380
395
return ;
381
396
}
382
397
forbiddenFields .put (c .className + '\000' + field , printout );
@@ -445,29 +460,29 @@ private void parseSignaturesFile(InputStream in, boolean allowBundled) throws IO
445
460
private static final String DEFAULT_MESSAGE_PREFIX = "@defaultMessage " ;
446
461
private static final String IGNORE_UNRESOLVABLE_LINE = "@ignoreUnresolvable" ;
447
462
448
- private void parseSignaturesFile (Reader reader , boolean allowBundled ) throws IOException ,ParseException {
463
+ private void parseSignaturesFile (Reader reader , boolean isBundled ) throws IOException ,ParseException {
449
464
final BufferedReader r = new BufferedReader (reader );
450
465
try {
451
466
String line , defaultMessage = null ;
452
- boolean failOnUnresolvableSignatures = options .contains (Option .FAIL_ON_UNRESOLVABLE_SIGNATURES );
467
+ UnresolvableReporting reporter = options .contains (Option .FAIL_ON_UNRESOLVABLE_SIGNATURES ) ? UnresolvableReporting . FAIL : UnresolvableReporting . WARNING ;
453
468
while ((line = r .readLine ()) != null ) {
454
469
line = line .trim ();
455
470
if (line .length () == 0 || line .startsWith ("#" ))
456
471
continue ;
457
472
if (line .startsWith ("@" )) {
458
- if (allowBundled && line .startsWith (BUNDLED_PREFIX )) {
473
+ if (isBundled && line .startsWith (BUNDLED_PREFIX )) {
459
474
final String name = line .substring (BUNDLED_PREFIX .length ()).trim ();
460
475
parseBundledSignatures (name , null , false );
461
476
} else if (line .startsWith (DEFAULT_MESSAGE_PREFIX )) {
462
477
defaultMessage = line .substring (DEFAULT_MESSAGE_PREFIX .length ()).trim ();
463
478
if (defaultMessage .length () == 0 ) defaultMessage = null ;
464
479
} else if (line .equals (IGNORE_UNRESOLVABLE_LINE )) {
465
- failOnUnresolvableSignatures = false ;
480
+ reporter = isBundled ? UnresolvableReporting . SILENT : UnresolvableReporting . WARNING ;
466
481
} else {
467
482
throw new ParseException ("Invalid line in signature file: " + line );
468
483
}
469
484
} else {
470
- addSignature (line , defaultMessage , failOnUnresolvableSignatures );
485
+ addSignature (line , defaultMessage , reporter );
471
486
}
472
487
}
473
488
} finally {
0 commit comments