Debian Codegraph @

checker/tests/nullness/java8/methodref/ReceiversMethodref.java

raw
1 import org.checkerframework.checker.nullness.qual.*;
2
3 // Nullable receivers don't make a lot of sense
4 // But this class tests the sub supertype recevier relationships.
5 // It could just use tainted.
6
7 interface Unbound1 {
8 void apply(@NonNull MyClass my);
9 }
10
11 interface Unbound2 {
12 void apply(@Nullable MyClass my);
13 }
14
15 interface Supplier1<R extends @Nullable Object> {
16 R supply();
17 }
18
19 interface Bound {
20 void apply();
21 }
22
23 class MyClass {
24 void take(@NonNull MyClass this) {}
25
26 void context1(@Nullable MyClass this, @NonNull MyClass my1, @Nullable MyClass my2) {
27
28 Unbound1 u1 = MyClass::take;
29 // :: error: (methodref.receiver.invalid)
30 Unbound2 u2 = MyClass::take;
31
32 Bound b1 = my1::take;
33 // :: error: (methodref.receiver.bound.invalid)
34 Bound b2 = my2::take;
35
36 // :: error: (methodref.receiver.bound.invalid)
37 Bound b11 = this::take;
38 }
39
40 void context2(@NonNull MyClass this) {
41 Bound b21 = this::take;
42 }
43
44 class MySubClass extends MyClass {
45
46 void context1(@Nullable MySubClass this) {
47 // :: error: (methodref.receiver.bound.invalid)
48 Bound b = super::take;
49 }
50
51 void context2(@NonNull MySubClass this) {
52 Bound b = super::take;
53 }
54
55 class Nested {
56 void context1(@Nullable Nested this) {
57 // :: error: (methodref.receiver.bound.invalid)
58 Bound b = MySubClass.super::take;
59 }
60
61 void context2(@NonNull Nested this) {
62 Bound b = MySubClass.super::take;
63 }
64 }
65 }
66 }
67
68 class Outer {
69 class Inner1 {
70 Inner1(@Nullable Outer Outer.this) {}
71 }
72
73 class Inner2 {
74 Inner2(@NonNull Outer Outer.this) {}
75 }
76
77 void context(@Nullable Outer this) {
78 // This one is unbound and needs an Outer as a param
79 Supplier1<Inner1> f1 = Inner1::new;
80 // :: error: (methodref.receiver.bound.invalid)
81 Supplier1<Inner2> f2 = Inner2::new;
82
83 // Supplier1</*3*/Inner> f = /*4*/Inner::new;
84 // 4 <: 3? Constructor annotations?
85 }
86 }