View Javadoc
1   /*
2   ParameterAssignment
3   
4   
5   */
6   
7   package com.puppycrawl.tools.checkstyle.checks.coding.parameterassignment;
8   
9   import javax.annotation.Nullable;
10  
11  public class InputParameterAssignmentWithUnchecked {
12      int field;
13      void foo1(int field) {
14          int i = field;
15          this.field = field;
16          i++;
17          field = 0; // violation 'Assignment of parameter 'field' is not allowed.'
18          field += 1; // violation 'Assignment of parameter 'field' is not allowed.'
19          this.field++;
20          field--; // violation 'Assignment of parameter 'field' is not allowed.'
21      }
22      // without parameters
23      void foo2() {
24          field = 0;
25      }
26      @SuppressWarnings(value = "unchecked")
27      void foo3(String field, int field1) {
28          // violation below 'Assignment of parameter 'field1' is not allowed.'
29          this.field = (field1 += field.length());
30      }
31  
32      void foo4() {
33          String hidden = "";
34          new NestedClass() {
35              public void test(String hidden) {
36              }
37          };
38          hidden += "test";
39      }
40  
41      // parameter name must be the same token name
42      void foo5(int EXPR) {
43          int i = EXPR;
44      }
45  
46      SomeInterface obj = q -> q++; // violation 'Assignment of parameter 'q' is not allowed.'
47  
48      // violation below 'Assignment of parameter 'q' is not allowed.'
49      SomeInterface obj2 = (int q) -> q += 12;
50  
51      SomeInterface obj3 = (w) -> w--; // violation 'Assignment of parameter 'w' is not allowed.'
52  
53      AnotherInterface obj4 = (int q, int w) -> obj.equals(obj2);
54  
55      // violation below 'Assignment of parameter 'w' is not allowed.'
56      AnotherInterface obj5 = (q, w) -> w = 14;
57  
58      // violation below 'Assignment of parameter 'a' is not allowed.'
59      SomeInterface obj6 = (@Nullable int a) -> a += 12;
60  
61      AnotherInterface obj7 = (@Nullable int c, @Nullable int d) -> {
62          c += d; // violation 'Assignment of parameter 'c' is not allowed.'
63          d += c; // violation 'Assignment of parameter 'd' is not allowed.'
64      };
65  
66      void method() {
67          int q = 12;
68          SomeInterface obj = (d) -> {
69              SomeInterface b = (c) -> obj2.equals(obj4);
70              int c = 12;
71              c++;
72              SomeInterface r = (field) -> this.field++;
73              d -= 10; // violation 'Assignment of parameter 'd' is not allowed.'
74          };
75      }
76  
77      public static abstract class NestedClass {
78          public abstract void test(String hidden);
79      }
80  
81      public interface SomeInterface {
82          void method(int a);
83      }
84  
85      public interface AnotherInterface {
86          void method(int a, int b);
87      }
88  }