View Javadoc
1   /*
2   VariableDeclarationUsageDistance
3   allowedDistance = 1
4   ignoreVariablePattern = (default)
5   validateBetweenScopes = true
6   ignoreFinal = false
7   
8   
9   */
10  
11  package com.puppycrawl.tools.checkstyle.checks.coding.variabledeclarationusagedistance;
12  
13  import java.util.ArrayList;
14  import java.util.List;
15  
16  public class InputVariableDeclarationUsageDistanceGeneral3 {
17  
18      void nothing() {
19      }
20  
21      class Parent {
22          void mm() {
23          }
24          <T> void xx(List<T> m){}
25      }
26  
27      public void method2() {
28          Integer a = 5;
29          class AClass extends Parent {
30              int i;
31  
32              public AClass(int i) {
33                  this.i = i;
34              }
35  
36              @Override
37              void mm() {
38                  if (a >= 0) {
39                      System.out.println("test");
40                  }
41              }
42          }
43      }
44  
45      public void method3() {
46          Integer a = 5; // violation 'Distance .* is 2.'
47          nothing();
48          InputVariableDeclarationUsageDistanceGeneral3 m =
49                  new InputVariableDeclarationUsageDistanceGeneral3() {
50                      @Override
51                      public void method2() {
52                          if (a >= 0) {
53                              System.out.println("new test");
54                          }
55                      }
56                  };
57      }
58  
59      public interface Ani {
60          public void method4(Object o);
61      }
62  
63      public void method5() {
64          Object a = new Object();
65          class AClass implements Ani {
66              @Override
67              public void method4(Object o) {
68                  if (a.toString().isEmpty()) {
69                      System.out.println(o.toString());
70                  }
71              }
72          }
73      }
74  
75      public void method6() {
76          String b = "";
77          List<Integer> numbers = new ArrayList<>();
78          numbers.add(5);
79          numbers.add(9);
80          numbers.forEach((n) -> {
81              try (AutoCloseable j = new java.io.StringReader("");
82                   final AutoCloseable k = new java.io.StringReader(b);) {
83                  String c = b.toString();
84              } catch (Exception e) {
85                  throw new RuntimeException(e);
86              }
87          });
88      }
89  
90      public void method7() {
91          // until https://github.com/checkstyle/checkstyle/issues/13011, the below distance is 3
92          Integer t = 5;
93          nothing();
94          System.out.println();
95          class BClass extends Parent {
96              @Override
97              void mm() {
98                  System.out.println(t);
99              }
100         }
101     }
102 
103     public void method8() {
104         Integer m = 10;
105         class BClass extends Parent{
106             public BClass getNewInstance(){
107                 return new BClass(){
108                     private final int t = m;
109 
110                     @Override
111                     void mm(){
112                         System.out.println();
113                     }
114                 };
115             }
116         }
117     }
118 }