View Javadoc
1   package com.puppycrawl.tools.checkstyle.checks.coding.hiddenfield;
2   
3   import java.lang.Integer;
4   import java.util.Arrays;
5   import java.util.HashMap;
6   import java.util.List;
7   import java.util.Map;
8   import java.util.Optional;
9   
10  
11  public class InputHiddenFieldLambdas {
12      /**
13       * Example 1: lambda parameter 'value' on line 16
14       * hides a field 'value' on line 14.
15       */
16      List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
17      Integer value = new Integer(1);
18      {
19          numbers.forEach((Integer value) -> String.valueOf(value)); // 1 violation
20      }
21  
22      /**
23       * Example 2: lambda parameter 'name' on line 27
24       * does not hide a field 'name' on line 25, because
25       * field 'name' can not be referenced from a static context.
26       */
27      static List<String> firstNames = Arrays.asList("Andrei", "Michal", "Roman", "Vladislav");
28      String name = new String();
29      static {
30          firstNames.forEach((String name) -> String.valueOf(name));
31      }
32  
33      /**
34       * Example 3: lambda parameter 'brand' on line 38 (which type is omitted)
35       * does not hide a field 'brand' on line 36, because
36       * field 'brand' can not be referenced from a static context.
37       */
38      static List<String> carBrands = Arrays.asList("BMW", "Mazda", "Volkswagen");
39      String brand = new String();
40      static {
41          carBrands.forEach(brand -> String.valueOf(brand));
42      }
43  
44      /**
45       * Example 4: lambda parameter 'languageCode' on line 48
46       * hides a field 'languageCode' on line 46.
47       */
48      static List<String> languageCodes = Arrays.asList("de", "ja", "fr", "pt");
49      static String languageCode = new String();
50      {
51          languageCodes.forEach(languageCode -> String.valueOf(languageCode)); // 1 violation
52      }
53  
54      /**
55       * Example 5: lambda parameter 'number' on line 57
56       * hides a field 'number' on line 55.
57       */
58      int number = 1;
59      Optional<Object> foo1(int i) {
60          return Optional.of(5).map(number -> { // violation
61              if (number == 1) return true;
62              else if (number == 2) return true;
63              else return false;
64          });
65      }
66  
67      /**
68       * Example 6: lambda parameter 'id' on line 70
69       * hides a field 'id' on line 68.
70       */
71      static long id = 1;
72      Optional<Object> foo2(int i) {
73          return Optional.of(5).map(id -> { // violation
74              if (id == 1) return true;
75              else if (id == 2) return true;
76              else return false;
77          });
78      }
79  
80      /**
81       * Example 7: lambda parameter 'age' on line 84
82       * does not hide a field 'age' on line 82,
83       * because field 'age' can not be referenced from a static context.
84       */
85      int age = 21;
86      static Optional<Object> foo3(int i) {
87          return Optional.of(5).map(age -> {
88              if (age == 1) return true;
89              else if (age == 2) return true;
90              else return false;
91          });
92      }
93  
94      /**
95       * Example 8: lambda parameter 'note' on line 98
96       * hides a field 'note' on line 95.
97       */
98      static String note = new String();
99      private void foo4() {
100         List<String> acceptableNotes = Arrays.asList("C", "D", "E", "F", "G", "A", "B");
101         acceptableNotes.forEach(note -> String.valueOf(note)); // 1 violation
102     }
103 
104     /**
105      * Example 9: lambda parameter 'letter' on line 109
106      * does not hide a field 'letter' on line 106, because
107      * field 'letter' can not be referenced from a static context.
108      */
109     String letter = new String("a");
110     private static void foo5() {
111         List<String> acceptableAlphabet = Arrays.asList("a", "b", "c");
112         acceptableAlphabet.forEach(letter -> String.valueOf(letter));
113     }
114 
115     @FunctionalInterface
116     interface Function <A, B> {
117         public B apply (A a, B b);
118     }
119 
120     /**
121      * Example 10: typed parameters - two hide fields
122      */
123     String stringValue = "248.3";
124     int intValue = 2;
125     {
126         Function <String, Integer> multiAdder = (String stringValue, Integer intValue) -> { // 2 violations
127             return Integer.parseInt(stringValue) + intValue;
128         };
129         String.valueOf(multiAdder.apply ("22.4", 2));
130     }
131 
132     /**
133      * Example 11: typed parameters - one hide field
134      */
135     Double doubleValue = 8.5;
136     {
137         Function <Integer, Double> adder = (Integer integerValue, Double doubleValue) -> { // 1 violation
138             return  integerValue + doubleValue;
139         };
140         String.valueOf(adder.apply(2, 2.2));
141     }
142 
143     /**
144      * Example 11: untyped parameters - two hide fields
145      */
146     String firstString = "Hello,";
147     String secondString = " World!";
148     {
149         Function <String, String> stringConcat = (firstString, secondString) -> { // 2 violations
150             return firstString + secondString;
151         };
152         String.valueOf(stringConcat.apply("A", "B"));
153     }
154 
155     @FunctionalInterface
156     interface SomeFunction<One, Two> {
157         public Two apply(One one, Two two);
158     }
159 
160     /**
161      * Example 11: untyped parameters - one hide field
162      */
163     Integer first = 1;
164     {
165         Function<Integer, Character> turnToZ = (first, second) -> 'z'; // 1 violation
166     }
167 
168     @FunctionalInterface
169     public interface Foo {
170         public String apply();
171     }
172 
173     /**
174      * Example 12: case when no parameters are given
175      */
176     {
177         Foo foo = () -> "";
178     }
179     @FunctionalInterface
180     interface FunctionWithOneParameter<One> {
181         public One apply(One one);
182     }
183 
184     /**
185      * Example 13: internal lambda hides a field
186      */
187     Double mPi = Math.PI;
188     List<Double> simpleNumbers = Arrays.asList(1.0, 2.0, 3.0, 4.0, 5.0, 6.0);
189     {
190         simpleNumbers.forEach(digit -> {
191             FunctionWithOneParameter<Double> strangeAdder = (mPi -> mPi+= digit); // 1 violation
192         });
193     }
194 
195     @FunctionalInterface
196     interface FunctionWithComplexGenerics<One, Two> {
197         public Two foo(One one, Two two);
198     }
199 
200     /**
201      * Example 14: lambda typed with complex generics
202      */
203     List<Double> justSomeList;
204     Map<String, Object> justSomeMap;
205     {
206         FunctionWithComplexGenerics<List<Double>, Map<String, Object>> someWierdFunc =
207             (List<Double> justSomeList, Map<String, Object> justSomeMap) -> { // 2 violations
208                 String.valueOf(justSomeList);
209                 String.valueOf(justSomeMap);
210                 return new HashMap<>();
211             };
212     }
213 
214     /**
215      * Example 15: lambda stored in field (with typed parameter)
216      * hides other field
217      */
218     Object someObject = new Object();
219     FunctionWithOneParameter objectToString = (Object someObject) -> { // 1 violation
220         return someObject.toString();
221     };
222 
223     /**
224      * Example 16: lambda stored in field (with untyped parameter)
225      * hides other field
226      */
227     FunctionWithOneParameter otherObjectToString = someObject -> { // 1 violation
228         return someObject.toString();
229     };
230 }