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.util.function.Consumer;
13  public class InputRequireThisAllowLambdaParameters {
14      private String s1 = "foo1";
15      int x=-1;
16      int y=-2;
17  
18      void foo1() {
19          final java.util.List<String> strings = new java.util.ArrayList<>();
20          strings.add("foo1");
21          strings.stream().filter(s1 -> s1.contains("f"))  // NO violation; s1 is a lambda parameter
22                  .collect(java.util.stream.Collectors.toList());
23  
24          s1 = "foo1"; // violation 'Reference to instance variable 's1' needs "this.".'
25      }
26  
27      void foo2() {
28          final java.util.List<String> strings = new java.util.ArrayList<>();
29          strings.add("foo1");
30          strings.stream().filter(s1 -> s1.contains(s1 = s1 + "2"))// NO violation;s1 is lambda param
31                  .collect(java.util.stream.Collectors.toList());
32      }
33  
34      class FirstLevel {
35  
36          int x;
37          int y;
38          int z;
39          void methodInFirstLevel(int x) {
40              Consumer<Integer> myConsumer = (y) ->   // NO violation; y is a lambda parameter
41              {
42                  new String("x = " + x);
43                  new String("y = " + y);  // NO violation; y is a lambda parameter
44                  new String("InputRequireThisAllowLambdaParameters.this.x = " +
45                          InputRequireThisAllowLambdaParameters.this.x);
46                  y=x+z++;  // violation 'Reference to instance variable 'z' needs "this.".'
47              };
48              myConsumer.accept(x);
49          }
50      }
51  }
52  
53  class Calculator {
54  
55      int a;
56      int b;
57      interface IntegerMath {
58          int operation(int a, int b);
59      }
60  
61      public int operateBinary(int a, int b, IntegerMath op) {
62          return op.operation(a, b);
63      }
64  
65      public void addSub(String... args) {
66  
67          Calculator myApp = new Calculator();
68          IntegerMath addition = (a, b) -> a = a + b;  // NO violations
69          IntegerMath subtraction = (a, b) -> a = a - b; // NO violations
70          myApp.operateBinary(20, 10, subtraction);
71          myApp.operateBinary(a++, b, addition);  // 2 violations
72      }
73  }
74  
75  class Test {
76      private Thread thread;
77  
78      public void testThreadHasWrongClassLoader() {
79          Thread t = new Thread(() -> {
80              try {
81                  thread.wait(); // violation 'Reference to instance variable 'thread' needs "this.".'
82              }
83              catch (Exception e) {
84              }
85          });
86      }
87  }