View Javadoc
1   /*
2   RequireThis
3   checkFields = (default)true
4   checkMethods = (default)true
5   validateOnlyOverlapping = false
6   
7   
8   */
9   
10  package com.puppycrawl.tools.checkstyle.checks.coding.requirethis;
11  
12  import java.awt.Toolkit;
13  import java.io.ByteArrayInputStream;
14  import java.io.IOException;
15  import java.io.InputStream;
16  
17  public class InputRequireThisEnumInnerClassesAndBugs {
18      int i;
19      void method1() {
20          i = 3; // violation 'Reference to instance variable 'i' needs "this.".'
21      }
22  
23      void method2(int i) {
24          i++;
25          this.i = i;
26          method1(); // violation 'Method call to 'method1' needs "this.".'
27          try {
28              this.method1();
29          }
30          catch (RuntimeException e) {
31              e.toString();
32          }
33          this.i--;
34  
35          Integer.toString(10);
36      }
37  
38      <T> void method3()
39      {
40          i = 3; // violation 'Reference to instance variable 'i' needs "this.".'
41      }
42  
43      void method4() {
44          this.<String>method3();
45          this.<I>method3();
46      }
47      int I = 0;
48      private class I {}
49  }
50  //  enum
51  enum MyEnum
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 MyEnum()
64      {
65          z = 0; // violation 'Reference to instance variable 'z' needs "this.".'
66      }
67  }
68  
69  class Bug2123003 {
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 Bug1155921 {
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 Issue155 {
104     String BASE = "A";
105     String EXT = BASE + "B";
106 }
107 
108 class Issue257 {
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 Issue2240 {
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(); // violation 'Method call to 'instanceMethod' needs "this.".'
125     }
126     void instanceMethod() {};
127 
128     class Nested {
129         void bar() {
130             instanceMethod(); // violation 'Method .* 'instanceMethod' needs "Issue2240.this.".'
131             i++; // violation 'Reference to instance variable 'i' needs "Issue2240.this.".'
132         }
133     }
134 }
135 
136 class Issue2539{
137     void foo(int i) {}
138     static void foo(double i) {}
139     void foo() {}
140 
141     void bar() {
142         foo(1);
143         foo(); // violation 'Method call to 'foo' needs "this.".'
144     }
145 }
146 class NestedRechange {
147     final String s = "";
148 
149     NestedRechange() {
150         String s = "t";
151         s = s.substring(0); // violation 'Reference to instance variable 's' needs "this.".'
152     }
153 
154     private static class NestedStatic {
155         static final String s = "";
156 
157         public void method() {
158             s.substring(0);
159         }
160     }
161 }
162 class NestedFrames {
163     int a = 0;
164     int b = 0;
165 
166     public int oneReturnInMethod2() {
167         for (int i = 0; i < 10; i++) {
168             int a = 1;
169             if (a != 2 && true) {
170                 if (true | false) {
171                     if (a - a != 0) {
172                         a += 1;
173                     }
174                 }
175             }
176         }
177         return a + a * a; // 3 violations
178     }
179 
180     public int oneReturnInMethod3() {
181         for (int b = 0; b < 10; b++) {
182         }
183         return b + b * b; // 3 violations
184     }
185     final NestedFrames NestedFrames = new NestedFrames();
186 }
187 class Another {
188     void method1() {
189        for (int i = 0; i < 1; i++) {
190            i = i + 1;
191        }
192        for (int i = 0; i < 1; i++) {
193            for (int j = 0; j < 1; i++) {
194                --i;
195            }
196        }
197    }
198    private int i;
199 }
200 class TestClass {
201     private final TestClass field = new TestClass();
202 
203     private String child;
204 
205     public void method() {
206         if (false) {
207             return;
208         } else if (true) {
209             String child = (String) this.child;
210             if (!(this.child instanceof String)) {
211                 child = field.get(child); // violation '.* variable 'field' needs "this.".'
212             }
213         }
214     }
215 
216     public String get(String s) {
217         return s;
218     }
219 }
220 class TestClass3 {
221     private static class Flags {
222         public void method() {
223             final char ch = ' ';
224             parse(ch);
225         }
226 
227         private static void parse(char c) {
228         }
229     }
230 
231     private void parse(String s) {
232     }
233 }