summaryrefslogtreecommitdiff
path: root/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/highlighting/EffectiveFinal.java
blob: 001b1f39c4942cc5e37daebf21b3b79023b15c21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
interface I {
  int m(int i);
}
interface J {
  int m();
}

public class XXX {

    static void foo() {
        int l = 0;
        int j = 0;
        j = 2;
        final int L = 0;
        I i = (int h) -> { int k = 0; return h + <error descr="Variable used in lambda expression should be effectively final">j</error> + l + L; };
    }

    void bar() {
        int l = 0;
        int j = 0;
        j = 2;
        final int L = 0;
        I i = (int h) -> { int k = 0; return h + k + <error descr="Variable used in lambda expression should be effectively final">j</error> + l + L; };
    }
    
     void foo(J i) { }
 
     void m1(int x) {
         int y = 1;
         foo(() -> x+y);
     }
 
     void m2(int x) {
         int y;
         y = 1;
         foo(() -> x+y);
     }
 
     void m3(int x, boolean cond) {
         int y;
         if (cond) y = 1;
         foo(() -> x+<error descr="Variable 'y' might not have been initialized">y</error>);
     }
 
     void m4(int x, boolean cond) {
         int y;
         if (cond) y = 1;
         else y = 2;
         foo(() -> x+y);
     }
 
     void m5(int x, boolean cond) {
         int y;
         if (cond) y = 1;
         y = 2;
         foo(() -> x+<error descr="Variable used in lambda expression should be effectively final">y</error>);
     }
 
     void m6(int x) {
         foo(() -> <error descr="Variable used in lambda expression should be effectively final">x</error>+1);
       x++;
     }
 
     void m7(int x) {
         foo(() -> <error descr="Variable used in lambda expression should be effectively final">x</error>=1);
     }
 
     void m8() {
         int y;
         foo(() -> <error descr="Variable used in lambda expression should be effectively final">y</error>=1);
     }
}

class Sample {
        public static void main(String[] args) {
                Runnable runnable = () -> {
                        Integer i;
                        if (true) {
                                i = 111;
                                System.out.println(i);
                        }
                };

                Runnable runnable2 = () -> {
                        Integer i2 = 333;
                        i2 = 444;
                        System.out.println(i2);
                };

                runnable.run();         // prints 111
                runnable2.run();        // prints 444
        }
}

class ParameterIsEffectivelyFinal {
  {
    Comparable<String> c = o->{
      new Runnable() {
        @Override
        public void run() {
          System.out.println(o);
        }
      }.run();
      return 0;
    };
    Comparable<String> c1 = o->{
      o = "";
      new Runnable() {
        @Override
        public void run() {
          System.out.println(<error descr="Variable 'o' is accessed from within inner class, needs to be declared final">o</error>);
        }
      }.run();
      return 0;
    };
  }
}

class IDEA114737 {
  private void on(String propertyName) {
    if (!"taskServices".equals(propertyName)) {
      return;
    }
    java.util.List<String> newList = null;
    Comparable<String> c1 = o -> {
      System.out.println(newList);
      return 0;
    };
  }
}

class IDEA128196 {
  void a() {
    int value;

    try {
      value = 1;
    } catch (Exception e) {
      return;
    }

    new Thread(() -> System.out.println(value));
  }
}

class FinalAssignmentInInitializer {
  private final String x;
  {
    Runnable r = () -> <error descr="Cannot assign a value to final variable 'x'">x</error> = "";
    x = "";
  }
}

class AssignmentToFinalInsideLambda {
  boolean isTrue() {
    return true;
  }

  Runnable r = () -> {
    final int i;
    if (isTrue()) {
      i = 1;
    } else {
      i = 0;
    }
  };

  void a() {
    Runnable r = () -> {
      final int i;
      if (isTrue()) {
        i = 1;
      } else {
        i = 0;
      }
    };
  }
}