View Javadoc
1   /*
2   RequireThis
3   checkFields = (default)true
4   checkMethods = false
5   validateOnlyOverlapping = false
6   
7   
8   */
9   
10  package com.puppycrawl.tools.checkstyle.checks.coding.requirethis;
11  
12  import java.io.ByteArrayInputStream;
13  import java.io.IOException;
14  import java.io.InputStream;
15  
16  public class InputRequireThisEnumInnerClassesAndBugs3 {
17      int i;
18      void method1() {
19          i = 3; // violation 'Reference to instance variable 'i' needs "this.".'
20      }
21  
22      void method2(int i) {
23          i++;
24          this.i = i;
25          method1();
26          try {
27              this.method1();
28          }
29          catch (RuntimeException e) {
30              e.toString();
31          }
32          this.i--;
33  
34          Integer.toString(10);
35      }
36  
37      <T> void method3()
38      {
39          i = 3; // violation 'Reference to instance variable 'i' needs "this.".'
40      }
41  
42      void method4() {
43          this.<String>method3();
44          this.<I>method3();
45      }
46      int I = 0;
47      private class I {}
48  }
49  
50  //  enum
51  enum MyEnum3
52  {
53      A,
54      B
55      {
56          void doSomething()
57          {
58              z = 1; // violation 'Reference to instance variable 'z' needs "this.".'
59          }
60      };
61  
62      int z;
63      private MyEnum3()
64      {
65          z = 0; // violation 'Reference to instance variable 'z' needs "this.".'
66      }
67  }
68  
69  class Bug21230033 {
70      @Rock(band = "GnR")
71      private String band;
72  
73      class Inner {
74          @Rock(band = {"GnR"})
75          private String band;
76      }
77  
78      class Inner2 {
79          @Rock(band = {"Tool"})
80          private String band;
81      }
82      /*     \m/(>.<)\m/     */
83      @interface Rock {
84          String[] band() default "Metallica";
85      }
86  }
87  
88  class Bug11559213 {
89      private static int CONST = 1;
90      private static int static_method() {
91          return 1;
92      }
93  
94      private int method1() {
95          return CONST;
96      }
97  
98      private int method2() {
99          return static_method();
100     }
101 }
102 
103 interface Issue1553 {
104     String BASE = "A";
105     String EXT = BASE + "B";
106 }
107 
108 class Issue2573 {
109     public void foo() {
110         try (final InputStream foo = new ByteArrayInputStream(new byte[512])) {
111             foo.read();
112         }
113         catch (final IOException e) {
114             e.getCause();
115         }
116     }
117 }
118 
119 class Issue22403 {
120     int i;
121     void foo() {
122         i++; // violation 'Reference to instance variable 'i' needs "this.".'
123         i++; int i = 1; i++; // violation 'Reference to instance variable 'i' needs "this.".'
124         instanceMethod();
125     }
126     void instanceMethod() {};
127 
128     class Nested {
129         void bar() {
130             instanceMethod();
131             i++; // violation 'Reference to instance variable 'i' needs "Issue22403.this.".'
132         }
133     }
134 }
135 
136 class Issue25393{
137     void foo(int i) {}
138     static void foo(double i) {}
139     void foo() {}
140 
141     void bar() {
142         foo(1);
143         foo();
144     }
145 }
146 
147 class NestedRechange3 {
148     final String s = "";
149 
150     NestedRechange3() {
151         String s = "t";
152         s = s.substring(0); // violation 'Reference to instance variable 's' needs "this.".'
153     }
154 
155     private static class NestedStatic {
156         static final String s = "";
157 
158         public void method() {
159             s.substring(0);
160         }
161     }
162 }
163 
164 class NestedFrames3 {
165     int a = 0;
166     int b = 0;
167 
168     public int oneReturnInMethod2() {
169         for (int i = 0; i < 10; i++) {
170             int a = 1;
171             if (a != 2 && true) {
172                 if (true | false) {
173                     if (a - a != 0) {
174                         a += 1;
175                     }
176                 }
177             }
178         }
179         return a + a * a; // 3 violations
180     }
181 
182     public int oneReturnInMethod3() {
183         for (int b = 0; b < 10; b++) {
184         }
185         return b + b * b; // 3 violations
186     }
187     final NestedFrames NestedFrames = new NestedFrames();
188 }