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