View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.coding;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.coding.UnusedPrivateFieldCheck.MSG_PRIVATE_FIELD;
24  
25  import java.util.AbstractMap;
26  import java.util.ArrayDeque;
27  import java.util.ArrayList;
28  import java.util.Deque;
29  import java.util.HashMap;
30  import java.util.HashSet;
31  import java.util.List;
32  import java.util.Map;
33  import java.util.Set;
34  
35  import org.junit.jupiter.api.Test;
36  
37  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
38  import com.puppycrawl.tools.checkstyle.api.DetailAST;
39  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
40  import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
41  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
42  
43  class UnusedPrivateFieldCheckTest extends AbstractModuleTestSupport {
44  
45      @Override
46      public String getPackageLocation() {
47  
48          return "com/puppycrawl/tools/checkstyle/checks/coding/unusedprivatefield";
49      }
50  
51      @Test
52      public void testGetRequiredTokens() {
53          final UnusedPrivateFieldCheck checkObj =
54                  new UnusedPrivateFieldCheck();
55          final int[] actual = checkObj.getRequiredTokens();
56  
57          final int[] expected = {
58              TokenTypes.IMPORT,
59              TokenTypes.OBJBLOCK,
60              TokenTypes.VARIABLE_DEF,
61              TokenTypes.PARAMETER_DEF,
62              TokenTypes.PARAMETERS,
63              TokenTypes.SLIST,
64              TokenTypes.IDENT,
65              TokenTypes.METHOD_DEF,
66              TokenTypes.CTOR_DEF,
67              TokenTypes.LAMBDA,
68              TokenTypes.LITERAL_FOR,
69              TokenTypes.LITERAL_CATCH,
70          };
71          assertWithMessage("Required tokens are invalid")
72                  .that(actual)
73                  .isEqualTo(expected);
74      }
75  
76      @Test
77      public void testGetAcceptableTokens() {
78          final UnusedPrivateFieldCheck typeParameterNameCheckObj =
79                  new UnusedPrivateFieldCheck();
80          final int[] actual = typeParameterNameCheckObj.getAcceptableTokens();
81  
82          final int[] expected = {
83              TokenTypes.IMPORT,
84              TokenTypes.OBJBLOCK,
85              TokenTypes.VARIABLE_DEF,
86              TokenTypes.PARAMETER_DEF,
87              TokenTypes.PARAMETERS,
88              TokenTypes.SLIST,
89              TokenTypes.IDENT,
90              TokenTypes.METHOD_DEF,
91              TokenTypes.CTOR_DEF,
92              TokenTypes.LAMBDA,
93              TokenTypes.LITERAL_FOR,
94              TokenTypes.LITERAL_CATCH,
95          };
96          assertWithMessage("Acceptable tokens are invalid")
97                  .that(actual)
98                  .isEqualTo(expected);
99      }
100 
101     @Test
102     public void testUnusedPrivateField() throws Exception {
103         final String[] expected = {
104             "12:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "unused"),
105             "20:30: " + getCheckMessage(MSG_PRIVATE_FIELD, "CONSTANT"),
106             "26:21: " + getCheckMessage(MSG_PRIVATE_FIELD, "innerUnused"),
107             "31:24: " + getCheckMessage(MSG_PRIVATE_FIELD, "field"),
108             "33:23: " + getCheckMessage(MSG_PRIVATE_FIELD, "field2"),
109             "49:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "x"),
110         };
111         verifyWithInlineConfigParser(
112                 getPath("InputUnusedPrivateField.java"), expected);
113     }
114 
115     @Test
116     public void testUnusedPrivateFieldNames() throws Exception {
117         final String[] expected = {
118             "12:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "value"),
119             "29:21: " + getCheckMessage(MSG_PRIVATE_FIELD, "data"),
120             "55:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "i"),
121 
122         };
123         verifyWithInlineConfigParser(
124                 getPath("InputUnusedPrivateFieldNameConflict.java"), expected);
125     }
126 
127     @Test
128     public void testUnusedPrivateFieldScope() throws Exception {
129         final String[] expected = {
130             "20:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "count"),
131             "28:21: " + getCheckMessage(MSG_PRIVATE_FIELD, "size"),
132             "33:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "flag"),
133             "44:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "limit"),
134             "46:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "index"),
135         };
136         verifyWithInlineConfigParser(
137                 getPath("InputUnusedPrivateFieldScopeEdgeCases.java"), expected);
138     }
139 
140     @Test
141     public void testUnusedPrivateField2() throws Exception {
142         final String[] expected = {
143             "12:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "unused"),
144             "14:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "anotherUnused"),
145             "16:20: " + getCheckMessage(MSG_PRIVATE_FIELD, "yetAnother"),
146             "25:21: " + getCheckMessage(MSG_PRIVATE_FIELD, "field"),
147         };
148         verifyWithInlineConfigParser(
149                 getPath("InputUnusedPrivateField2.java"), expected);
150     }
151 
152     @Test
153     public void testUnusedPrivateFieldThis() throws Exception {
154         final String[] expected = {
155             "24:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "value"),
156             "30:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "count"),
157             "43:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "flag"),
158             "51:21: " + getCheckMessage(MSG_PRIVATE_FIELD, "data"),
159             "60:21: " + getCheckMessage(MSG_PRIVATE_FIELD, "size"),
160         };
161         verifyWithInlineConfigParser(
162                 getPath("InputUnusedPrivateFieldThisAccess.java"), expected);
163     }
164 
165     @Test
166     public void testUnusedPrivateFieldAnnotationLombok() throws Exception {
167         final String[] expected = {
168             "17:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "unused"),
169 
170         };
171         verifyWithInlineConfigParser(
172                 getPath("InputUnusedPrivateFieldAnnotationLombok.java"), expected);
173     }
174 
175     @Test
176     public void testUnusedPrivateFieldAnnotationIgnored() throws Exception {
177         final String[] expected = {
178             "17:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "unused"),
179 
180         };
181         verifyWithInlineConfigParser(
182                 getPath("InputUnusedPrivateFieldAnnotationIgnored.java"),
183                 expected);
184     }
185 
186     @Test
187     public void testUnusedPrivateFieldQualifiedThis() throws Exception {
188         final String[] expected = {
189             "15:29: " + getCheckMessage(MSG_PRIVATE_FIELD, "unused"),
190         };
191         verifyWithInlineConfigParser(
192                 getPath("InputUnusedPrivateFieldQualifiedThis.java"),
193                 expected);
194     }
195 
196     @Test
197     public void testUnusedPrivateFieldAnnotationNotIgnored() throws Exception {
198         final String[] expected = {
199             "15:20: " + getCheckMessage(MSG_PRIVATE_FIELD, "service"),
200             "17:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "unused"),
201         };
202         verifyWithInlineConfigParser(
203                 getPath("InputUnusedPrivateFieldAnnotationNotIgnored.java"),
204                 expected);
205     }
206 
207     @Test
208     public void testUnusedPrivateFieldUnresolvable() throws Exception {
209         final String[] expected = {
210             "15:21: " + getCheckMessage(MSG_PRIVATE_FIELD, "field"),
211         };
212         verifyWithInlineConfigParser(
213                 getNonCompilablePath("InputUnusedPrivateFieldQualifiedThisUnresolvable.java"),
214                 expected);
215     }
216 
217     @Test
218     public void testUnusedPrivateFieldAnnotationShortName() throws Exception {
219         final String[] expected = {
220             "17:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "unused"),
221         };
222         verifyWithInlineConfigParser(
223                 getPath("InputUnusedPrivateFieldAnnotationShortName.java"),
224                 expected);
225     }
226 
227     @Test
228     public void testUnusedPrivateFieldAnnotationMultiple() throws Exception {
229         final String[] expected = {
230             "25:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "unused"),
231         };
232         verifyWithInlineConfigParser(
233                 getPath("InputUnusedPrivateFieldAnnotationMultiple.java"),
234                 expected);
235     }
236 
237     @Test
238     public void testUnusedPrivateFieldAnnotationCanonicalNameUsage() throws Exception {
239         final String[] expected = {
240             "17:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "unused"),
241         };
242         verifyWithInlineConfigParser(
243                 getPath("InputUnusedPrivateFieldAnnotationCanonicalNameUsage.java"),
244                 expected);
245     }
246 
247     @Test
248     public void testUnusedPrivateFieldAnnotationSecondOfMultiple() throws Exception {
249         final String[] expected = {
250             "19:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "unused"),
251         };
252         verifyWithInlineConfigParser(
253                 getPath("InputUnusedPrivateFieldAnnotationSecondOfMultiple.java"),
254                 expected);
255     }
256 
257     @Test
258     public void testUnusedPrivateFieldIgnoredFieldNamesCustom() throws Exception {
259         final String[] expected = {
260             "16:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "unused"),
261         };
262         verifyWithInlineConfigParser(
263                 getPath("InputUnusedPrivateFieldIgnoredFieldNamesCustom.java"), expected);
264     }
265 
266     @Test
267     public void testUnusedPrivateFieldAnnotationClassLevel() throws Exception {
268         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
269         verifyWithInlineConfigParser(
270                 getPath("InputUnusedPrivateFieldAnnotationClassLevel.java"),
271                 expected);
272     }
273 
274     @Test
275     public void testUnusedPrivateFieldAnnotation() throws Exception {
276         final String[] expected = {
277             "14:21: " + getCheckMessage(MSG_PRIVATE_FIELD, "unused"),
278         };
279         verifyWithInlineConfigParser(
280                 getPath("InputUnusedPrivateFieldAnonymousClass.java"),
281                 expected);
282     }
283 
284     @Test
285     public void testUnusedPrivateFieldString() throws Exception {
286         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
287         verifyWithInlineConfigParser(
288                 getNonCompilablePath("compact/InputUnusedPrivateFieldEmptyString.java"),
289                 expected);
290     }
291 
292     @Test
293     public void testUnusedPrivateFieldIgnored() throws Exception {
294         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
295         verifyWithInlineConfigParser(
296                 getNonCompilablePath("compact/InputUnusedPrivateFieldIgnored.java"),
297                 expected);
298     }
299 
300     @Test
301     public void testUnusedPrivateFieldEmpty() throws Exception {
302         final String[] expected = {
303             "14:26: " + getCheckMessage(MSG_PRIVATE_FIELD, "obj"),
304             "20:21: " + getCheckMessage(MSG_PRIVATE_FIELD, "value"),
305             "22:32: " + getCheckMessage(MSG_PRIVATE_FIELD, "r"),
306         };
307         verifyWithInlineConfigParser(
308                 getPath("InputUnusedPrivateFieldScopeStackEmpty.java"),
309                 expected);
310     }
311 
312     @Test
313     public void testUnusedPrivateFieldTwo() throws Exception {
314         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
315         verifyWithInlineConfigParser(
316                 getNonCompilablePath("compact/InputUnusedPrivateFieldAnnotation.java"),
317                 expected);
318     }
319 
320     @Test
321     public void testUnusedPrivateFieldNested() throws Exception {
322         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
323         verifyWithInlineConfigParser(
324                 getPath("InputUnusedPrivateFieldNestedAccess.java"), expected);
325     }
326 
327     @Test
328     public void testUnusedPrivateFieldNestedScopes() throws Exception {
329         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
330         verifyWithInlineConfigParser(
331                 getPath("InputUnusedPrivateFieldNestedScopes.java"), expected);
332     }
333 
334     @Test
335     public void testUnusedPrivateFieldAnnotationSerialShortName() throws Exception {
336         final String[] expected = {
337             "17:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "unused"),
338         };
339         verifyWithInlineConfigParser(
340                 getPath("InputUnusedPrivateFieldAnnotationSerialShortName.java"),
341                 expected);
342     }
343 
344     @Test
345     public void testUnusedprivateFieldMethodDef() throws Exception {
346         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
347         verifyWithInlineConfigParser(
348                 getPath("InputUnusedPrivateFieldMethodDef.java"), expected);
349     }
350 
351     @Test
352     public void testUnusedPrivateField3() throws Exception {
353         final String[] expected = {
354             "19:30: " + getCheckMessage(MSG_PRIVATE_FIELD, "CONSTANT"),
355         };
356         verifyWithInlineConfigParser(
357                 getPath("InputUnusedPrivateField3.java"), expected);
358     }
359 
360     @Test
361     public void testUsedFieldBranch() throws Exception {
362         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
363         verifyWithInlineConfigParser(
364                 getPath("InputUnusedPrivateField4.java"), expected);
365     }
366 
367     @Test
368     public void testStateClearedWithinSingleFile() throws Exception {
369         final String[] expected = {
370             "11:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "a"),
371         };
372         verifyWithInlineConfigParser(
373                 getPath("InputUnusedPrivateFieldState.java"), expected);
374     }
375 
376     @Test
377     public void testStateClearedBetweenFiles() throws Exception {
378         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
379         verifyWithInlineConfigParser(
380                 getPath("InputUnusedPrivateFieldFirst.java"),
381                 getPath("InputUnusedPrivateFieldTwo.java"), expected
382         );
383     }
384 
385     @Test
386     public void testUnusedPrivateFieldLambdaScope() throws Exception {
387         final String[] expected = {
388             "21:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "unused"),
389             "38:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "size"),
390         };
391         verifyWithInlineConfigParser(
392                 getPath("InputUnusedPrivateFieldLambdaScope.java"), expected);
393     }
394 
395     @Test
396     public void testUnusedPrivateFieldDeclarationNameCollision() throws Exception {
397         final String[] expected = {
398             "12:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "myClass"),
399             "15:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "myInterface"),
400             "18:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "myEnum"),
401             "21:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "myRecord"),
402             "24:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "myAnnotation"),
403         };
404         verifyWithInlineConfigParser(
405                 getPath("InputUnusedPrivateFieldDeclarationNameCollision.java"), expected);
406     }
407 
408     @Test
409     public void testUnusedPrivateFieldForCatchScope() throws Exception {
410         final String[] expected = {
411             "38:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "unused"),
412         };
413 
414         verifyWithInlineConfigParser(
415                 getPath("InputUnusedPrivateFieldForCatchScope.java"), expected);
416     }
417 
418     @Test
419     public void testUnusedPrivateFieldOuterUsed() throws Exception {
420         final String[] expected = {
421             "16:21: " + getCheckMessage(MSG_PRIVATE_FIELD, "value"),
422             "26:25: " + getCheckMessage(MSG_PRIVATE_FIELD, "x"),
423             "36:29: " + getCheckMessage(MSG_PRIVATE_FIELD, "unusedField"),
424         };
425 
426         verifyWithInlineConfigParser(
427                 getPath("InputUnusedPrivateFieldOuterUsed.java"), expected);
428     }
429 
430     @Test
431     public void testUnusedPrivateFieldOuterUnused() throws Exception {
432         final String[] expected = {
433             "12:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "value"),
434         };
435 
436         verifyWithInlineConfigParser(
437                 getPath("InputUnusedPrivateFieldOuterUnused.java"), expected);
438     }
439 
440     @Test
441     public void testUnusedPrivateFieldDotAccessForms() throws Exception {
442         final String[] expected = {
443             "24:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "objField"),
444             "26:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "shared"),
445             "60:21: " + getCheckMessage(MSG_PRIVATE_FIELD, "middleField"),
446             "79:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "shadowTarget"),
447         };
448 
449         verifyWithInlineConfigParser(
450                 getPath("InputUnusedPrivateFieldDotAccessForms.java"), expected);
451     }
452 
453     @Test
454     public void testUnusedPrivateFieldLocalVarType() throws Exception {
455         final String[] expected = {
456             "11:17: " + getCheckMessage(MSG_PRIVATE_FIELD, "value"),
457         };
458         verifyWithInlineConfigParser(
459                 getPath("InputUnusedPrivateFieldLocalVarType.java"), expected);
460     }
461 
462     @Test
463     public void testClearStateVariables() {
464         final UnusedPrivateFieldCheck check = new UnusedPrivateFieldCheck();
465         final Deque<Set<String>> usedFieldsStack = new ArrayDeque<>();
466         usedFieldsStack.push(new HashSet<>());
467         TestUtil.setInternalState(check, "enclosingTypeNames", usedFieldsStack);
468         check.beginTree(null);
469         final Deque<?> clearedStack = (Deque<?>) TestUtil.getInternalState(check,
470                 "enclosingTypeNames", Deque.class);
471         assertWithMessage("usedFieldsStack state is not cleared on beginTree")
472                 .that(clearedStack)
473                 .isEmpty();
474     }
475 
476     @Test
477     public void testClearStatePrivateFieldsStack() {
478         final UnusedPrivateFieldCheck check = new UnusedPrivateFieldCheck();
479 
480         final Deque<Map<String, DetailAST>> stack = new ArrayDeque<>();
481         stack.push(new HashMap<>());
482         TestUtil.setInternalState(check, "privateFields", stack);
483         check.beginTree(null);
484 
485         final Deque<?> privateFieldsStack = (Deque<?>) TestUtil.getInternalState(check,
486             "privateFields", Deque.class);
487         assertWithMessage("privateFields state is not cleared on beginTree")
488                 .that(privateFieldsStack)
489                 .isEmpty();
490     }
491 
492     @Test
493     public void testClearStateGlobalFields() {
494         final UnusedPrivateFieldCheck check = new UnusedPrivateFieldCheck();
495         final Set<String> globalUsedFields = new HashSet<>();
496         globalUsedFields.add("someField");
497         TestUtil.setInternalState(check, "globalUsedFields", globalUsedFields);
498         check.beginTree(null);
499         final Set<?> cleared = (Set<?>) TestUtil.getInternalState(check,
500                 "globalUsedFields", Set.class);
501         assertWithMessage("globalUsedFields state is not cleared on beginTree")
502                 .that(cleared)
503                 .isEmpty();
504     }
505 
506     @Test
507     public void testClearStateScopeStack() {
508         final UnusedPrivateFieldCheck check = new UnusedPrivateFieldCheck();
509 
510         final Deque<Set<String>> stack = new ArrayDeque<>();
511         stack.push(new HashSet<>());
512         TestUtil.setInternalState(check, "scopeStack", stack);
513         check.beginTree(null);
514 
515         final Deque<?> scopeStack = (Deque<?>) TestUtil.getInternalState(check,
516             "scopeStack", Deque.class);
517 
518         assertWithMessage("scopeStack state is not cleared on beginTree")
519                 .that(scopeStack)
520                 .isEmpty();
521     }
522 
523     @Test
524     public void testClearStateScopeStackSnapshots() {
525         final UnusedPrivateFieldCheck check = new UnusedPrivateFieldCheck();
526 
527         final Deque<Set<String>> stack = new ArrayDeque<>();
528         stack.push(new HashSet<>());
529         TestUtil.setInternalState(check, "scopeStackSnapshots", stack);
530         check.beginTree(null);
531 
532         final Deque<?> scopeStack = (Deque<?>) TestUtil.getInternalState(check,
533             "scopeStackSnapshots", Deque.class);
534 
535         assertWithMessage("scopeStackSnapshots state is not cleared on beginTree")
536                 .that(scopeStack)
537                 .isEmpty();
538     }
539 
540     @Test
541     public void testClearStatePendingFields() {
542         final UnusedPrivateFieldCheck check = new UnusedPrivateFieldCheck();
543         final List<Map.Entry<String, DetailAST>> list = new ArrayList<>();
544         list.add(new AbstractMap.SimpleEntry<>("someField", null));
545 
546         TestUtil.setInternalState(check, "pendingFields", list);
547         check.beginTree(null);
548 
549         final List<?> pendingFields =
550                 (List<?>) TestUtil.getInternalState(check, "pendingFields", List.class);
551 
552         assertWithMessage("pendingFields state is not cleared on beginTree")
553                 .that(pendingFields)
554                 .isEmpty();
555     }
556 
557     @Test
558     public void testIgnoreAnnotationShortNamesInitializedInConstructor() {
559         final UnusedPrivateFieldCheck check = new UnusedPrivateFieldCheck();
560         final Set<?> shortNames =
561                 (Set<?>) TestUtil.getInternalState(
562                         check, "ignoreAnnotationShortNames", Set.class);
563 
564         assertWithMessage("ignoreAnnotationShortNames should be initialized, not null")
565                 .that(shortNames)
566                 .isNotNull();
567         assertWithMessage("ignoreAnnotationShortNames should start empty")
568                 .that(shortNames)
569                 .isEmpty();
570     }
571 
572     @Test
573     public void testClearStateFieldUsages() {
574         final UnusedPrivateFieldCheck check = new UnusedPrivateFieldCheck();
575         final List<Object> usages = new ArrayList<>();
576         usages.add(new Object());
577         TestUtil.setInternalState(check, "fieldUsages", usages);
578         check.beginTree(null);
579         final List<?> cleared = (List<?>)
580                 TestUtil.getInternalState(check, "fieldUsages", List.class);
581         assertWithMessage("fieldUsages state is not cleared on beginTree")
582                 .that(cleared).isEmpty();
583     }
584 
585     @Test
586     public void testClearStateFieldTypedUsages() {
587         final UnusedPrivateFieldCheck check = new UnusedPrivateFieldCheck();
588         final List<Object> usages = new ArrayList<>();
589         usages.add(new Object());
590         TestUtil.setInternalState(check, "typedUsages", usages);
591         check.beginTree(null);
592         final List<?> cleared = (List<?>)
593                 TestUtil.getInternalState(check, "typedUsages", List.class);
594         assertWithMessage("fieldUsages state is not cleared on beginTree")
595                 .that(cleared).isEmpty();
596     }
597 
598     @Test
599     public void testClearStatePrivateFieldsByType() {
600         final UnusedPrivateFieldCheck check = new UnusedPrivateFieldCheck();
601         final Map<Object, Object> map = new HashMap<>();
602         map.put("clear", new Object());
603         TestUtil.setInternalState(check, "privateFieldsByType", map);
604         check.beginTree(null);
605         final Map<?, ?> cleared = (Map<?, ?>)
606                 TestUtil.getInternalState(check, "privateFieldsByType", Map.class);
607         assertWithMessage("privateFieldsByType state is not cleared on beginTree")
608                 .that(cleared).isEmpty();
609     }
610 
611 }