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