View Javadoc
1   /*
2   UnusedLocalVariable
3   allowUnnamedVariables = false
4   
5   */
6   
7   package com.puppycrawl.tools.checkstyle.checks.coding.unusedlocalvariable;
8   
9   import java.lang.annotation.Annotation;
10  
11  public class InputUnusedLocalVariableNestedClasses {
12  
13      public int V = 1;
14      private int G = 1;
15      protected int S = 1;
16      int Q = 1;
17  
18      class AnotherClass {
19  
20          void method() {
21              int V = 1; // violation, 'Unused local variable'
22              int G = 1;
23              int S = 1; // violation, 'Unused local variable'
24              int Q = 1; // violation, 'Unused local variable'
25              InputUnusedLocalVariableNestedClasses obj
26                      = new InputUnusedLocalVariableNestedClasses() {
27                  void method() {
28                      Integer.valueOf(V + G + S + Q);
29                  }
30              };
31              obj.getClass();
32          }
33      }
34  
35      void method() {
36          int variable = 1; // violation, 'Unused local variable'
37          A.B obj = new A.B() {
38              void method() {
39                  variable += 1;
40              }
41          };
42          obj.getClass();
43  
44          int anotherVariable = 2; // violation, 'Unused local variable'
45          A.B.innerClass obj2 = new A.B().new innerClass() {
46              void method() {
47                  anotherVariable += 1;
48              }
49          };
50          obj2.getClass();
51  
52          InputUnusedLocalVariable obj3
53                  = new InputUnusedLocalVariable() { // cross file checking not supported
54          };
55          obj3.getClass();
56      }
57  
58      static class A {
59          static class B {
60              int variable = 1;
61  
62              class innerClass {
63                  protected int anotherVariable = 1;
64                  public int k = 1;
65  
66                  void testInterfaces() {
67                      int s = 12; // violation, 'Unused local variable'
68                      int n = 13; // violation, 'Unused local variable'
69                      foo obj = new foo() {
70                          @Override
71                          public Class<? extends Annotation> annotationType() {
72                              Integer.valueOf(s);
73  
74                              hoo obj = () -> {
75                                  hoo anotherObj = new hoo() {
76                                      @Override
77                                      public void anotherMethod() {
78                                          Integer.valueOf(n);
79                                      }
80                                  };
81                                  anotherObj.getClass();
82                                  String outsideAnonInner = "n is accessible by " + hoo.n;
83                                  outsideAnonInner.isEmpty();
84                              };
85                              obj.getClass();
86                              return null;
87                          }
88                      };
89                      obj.getClass();
90                  }
91              }
92          }
93      }
94  }
95  
96  @interface foo {
97      int s = 2;
98  }
99  
100 interface hoo {
101     int n = 12;
102     void anotherMethod();
103 }