@@ -108,22 +108,44 @@ public Lint suppress(LintCategory... lc) {
108
108
}
109
109
110
110
private final Context context ;
111
+ private final Options options ;
111
112
112
113
// These are initialized lazily to avoid dependency loops
113
114
private Symtab syms ;
114
115
private Names names ;
115
116
116
117
// Invariant: it's never the case that a category is in both "values" and "suppressedValues"
117
- private final EnumSet <LintCategory > values ;
118
- private final EnumSet <LintCategory > suppressedValues ;
118
+ private EnumSet <LintCategory > values ;
119
+ private EnumSet <LintCategory > suppressedValues ;
119
120
120
121
private static final Map <String , LintCategory > map = new ConcurrentHashMap <>(20 );
121
122
122
123
@ SuppressWarnings ("this-escape" )
123
124
protected Lint (Context context ) {
124
- // initialize values according to the lint options
125
- Options options = Options .instance (context );
125
+ this .context = context ;
126
+ context .put (lintKey , this );
127
+ options = Options .instance (context );
128
+ }
129
+
130
+ // Instantiate a non-root ("symbol scoped") instance
131
+ protected Lint (Lint other ) {
132
+ other .initializeRootIfNeeded ();
133
+ this .context = other .context ;
134
+ this .options = other .options ;
135
+ this .syms = other .syms ;
136
+ this .names = other .names ;
137
+ this .values = other .values .clone ();
138
+ this .suppressedValues = other .suppressedValues .clone ();
139
+ }
140
+
141
+ // Process command line options on demand to allow use of root Lint early during startup
142
+ private void initializeRootIfNeeded () {
126
143
144
+ // Already initialized?
145
+ if (values != null )
146
+ return ;
147
+
148
+ // Initialize enabled categories based on "-Xlint" flags
127
149
if (options .isSet (Option .XLINT ) || options .isSet (Option .XLINT_CUSTOM , "all" )) {
128
150
// If -Xlint or -Xlint:all is given, enable all categories by default
129
151
values = EnumSet .allOf (LintCategory .class );
@@ -162,21 +184,11 @@ protected Lint(Context context) {
162
184
}
163
185
164
186
suppressedValues = LintCategory .newEmptySet ();
165
-
166
- this .context = context ;
167
- context .put (lintKey , this );
168
- }
169
-
170
- protected Lint (Lint other ) {
171
- this .context = other .context ;
172
- this .syms = other .syms ;
173
- this .names = other .names ;
174
- this .values = other .values .clone ();
175
- this .suppressedValues = other .suppressedValues .clone ();
176
187
}
177
188
178
189
@ Override
179
190
public String toString () {
191
+ initializeRootIfNeeded ();
180
192
return "Lint:[enable" + values + ",suppress" + suppressedValues + "]" ;
181
193
}
182
194
@@ -404,6 +416,7 @@ public static EnumSet<LintCategory> newEmptySet() {
404
416
* the SuppressWarnings annotation.
405
417
*/
406
418
public boolean isEnabled (LintCategory lc ) {
419
+ initializeRootIfNeeded ();
407
420
return values .contains (lc );
408
421
}
409
422
@@ -414,11 +427,26 @@ public boolean isEnabled(LintCategory lc) {
414
427
* current entity being itself deprecated.
415
428
*/
416
429
public boolean isSuppressed (LintCategory lc ) {
430
+ initializeRootIfNeeded ();
417
431
return suppressedValues .contains (lc );
418
432
}
419
433
420
434
/**
421
435
* Helper method. Log a lint warning if its lint category is enabled.
436
+ *
437
+ * @param log warning destination
438
+ * @param warning key for the localized warning message
439
+ */
440
+ public void logIfEnabled (Log log , LintWarning warning ) {
441
+ logIfEnabled (log , null , warning );
442
+ }
443
+
444
+ /**
445
+ * Helper method. Log a lint warning if its lint category is enabled.
446
+ *
447
+ * @param log warning destination
448
+ * @param pos source position at which to report the warning
449
+ * @param warning key for the localized warning message
422
450
*/
423
451
public void logIfEnabled (Log log , DiagnosticPosition pos , LintWarning warning ) {
424
452
if (isEnabled (warning .getLintCategory ())) {
@@ -450,7 +478,7 @@ public EnumSet<LintCategory> suppressionsFrom(Symbol symbol) {
450
478
* @return set of lint categories, possibly empty but never null
451
479
*/
452
480
private EnumSet <LintCategory > suppressionsFrom (JCAnnotation annotation ) {
453
- initializeIfNeeded ();
481
+ initializeSymbolsIfNeeded ();
454
482
if (annotation == null )
455
483
return LintCategory .newEmptySet ();
456
484
Assert .check (annotation .attribute .type .tsym == syms .suppressWarningsType .tsym );
@@ -459,7 +487,7 @@ private EnumSet<LintCategory> suppressionsFrom(JCAnnotation annotation) {
459
487
460
488
// Find the @SuppressWarnings annotation in the given stream and extract the recognized suppressions
461
489
private EnumSet <LintCategory > suppressionsFrom (Stream <Attribute .Compound > attributes ) {
462
- initializeIfNeeded ();
490
+ initializeSymbolsIfNeeded ();
463
491
EnumSet <LintCategory > result = LintCategory .newEmptySet ();
464
492
attributes
465
493
.filter (attribute -> attribute .type .tsym == syms .suppressWarningsType .tsym )
@@ -480,7 +508,7 @@ private EnumSet<LintCategory> suppressionsFrom(Attribute.Compound suppressWarnin
480
508
return result ;
481
509
}
482
510
483
- private void initializeIfNeeded () {
511
+ private void initializeSymbolsIfNeeded () {
484
512
if (syms == null ) {
485
513
syms = Symtab .instance (context );
486
514
names = Names .instance (context );
0 commit comments