File: RawTypesBounded.java

package info (click to toggle)
checker-framework-java 3.2.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,104 kB
  • sloc: java: 145,916; xml: 839; sh: 518; makefile: 404; perl: 26
file content (233 lines) | stat: -rw-r--r-- 6,131 bytes parent folder | download | duplicates (3)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Note that this file is a near duplicate in /nullness and /nullness-uninit

import org.checkerframework.checker.initialization.qual.UnknownInitialization;
import org.checkerframework.checker.nullness.qual.*;

@org.checkerframework.framework.qual.DefaultQualifier(Nullable.class)
class RawTypesBounded {

    class Bad {
        @NonNull String field;

        public Bad() {
            // :: error: (method.invocation.invalid)
            this.init(); // error
            // :: error: (method.invocation.invalid)
            init(); // error

            this.field = "field"; // valid
            // :: error: (assignment.type.incompatible)
            this.field = null; // error
            field = "field"; // valid
            // :: error: (assignment.type.incompatible)
            field = null; // error
        }

        void init() {
            output(this.field.length()); // valid
        }
    }

    class A {
        @NonNull String field;

        public A() {
            this.field = "field"; // valid
            field = "field"; // valid
            this.init(); // valid
            init(); // valid
        }

        public void init(@UnknownInitialization A this) {
            // :: error: (dereference.of.nullable)
            output(this.field.length());
        }

        public void initExpl2(@UnknownInitialization A this) {
            // :: error: (argument.type.incompatible)
            output(this.field);
        }

        public void initImpl1(@UnknownInitialization A this) {
            // :: error: (dereference.of.nullable)
            output(field.length());
        }

        public void initImpl2(@UnknownInitialization A this) {
            // :: error: (argument.type.incompatible)
            output(field);
        }
    }

    class B extends A {
        @NonNull String otherField;

        public B() {
            super();
            // :: error: (assignment.type.incompatible)
            this.otherField = null; // error
            this.otherField = "otherField"; // valid
        }

        @Override
        public void init(@UnknownInitialization B this) {
            // :: error: (dereference.of.nullable)
            output(this.field.length()); // error (TODO: substitution)
            super.init(); // valid
        }

        public void initImpl1(@UnknownInitialization B this) {
            // :: error: (dereference.of.nullable)
            output(field.length()); // error (TODO: substitution)
        }

        public void initExpl2(@UnknownInitialization B this) {
            // :: error: (dereference.of.nullable)
            output(this.otherField.length()); // error
        }

        public void initImpl2(@UnknownInitialization B this) {
            // :: error: (dereference.of.nullable)
            output(otherField.length()); // error
        }

        void other() {
            init(); // valid
            this.init(); // valid
        }

        void otherRaw(@UnknownInitialization B this) {
            init(); // valid
            this.init(); // valid
        }
    }

    class C extends B {

        @NonNull String[] strings;

        @Override
        public void init(@UnknownInitialization C this) {
            // :: error: (dereference.of.nullable)
            output(this.strings.length); // error
            System.out.println(); // valid
        }
    }

    // To test whether the argument is @NonNull and @Initialized
    static void output(@NonNull Object o) {}

    class D extends C {
        @Override
        public void init(@UnknownInitialization D this) {
            this.field = "s";
            output(this.field.length());
        }
    }

    class MyTest {
        int i;

        MyTest(int i) {
            this.i = i;
        }

        void myTest(@UnknownInitialization MyTest this) {
            i++;
        }
    }

    class AllFieldsInitialized {
        long elapsedMillis = 0;
        long startTime = 0;

        // If all fields have an initializer, then the type of "this"
        // should still not be non-raw (there might be uninitilized subclasses)
        public AllFieldsInitialized() {
            // :: error: (method.invocation.invalid)
            nonRawMethod();
        }

        public void nonRawMethod() {}
    }

    class AFSIICell {
        AllFieldsSetInInitializer afsii;
    }

    class AllFieldsSetInInitializer {
        long elapsedMillis;
        long startTime;

        public AllFieldsSetInInitializer() {
            elapsedMillis = 0;
            // :: error: (method.invocation.invalid)
            nonRawMethod();
            startTime = 0;
            // :: error: (method.invocation.invalid)
            nonRawMethod(); // still error (subclasses...)
        }

        public AllFieldsSetInInitializer(boolean b) {
            // :: error: (method.invocation.invalid)
            nonRawMethod();
        }

        public void nonRawMethod() {}
    }

    class ConstructorInvocations {
        int v;

        public ConstructorInvocations(int v) {
            this.v = v;
        }

        public ConstructorInvocations() {
            this(0);
            // :: error: (method.invocation.invalid)
            nonRawMethod();
        }

        public void nonRawMethod() {}
    }

    class MethodAccess {
        public MethodAccess() {
            @NonNull String s = string();
        }

        public @NonNull String string(@UnknownInitialization MethodAccess this) {
            return "nonnull";
        }
    }

    void cast(@UnknownInitialization Object... args) {

        @SuppressWarnings("rawtypes")
        // :: error: (assignment.type.incompatible)
        Object[] argsNonRaw1 = args;

        @SuppressWarnings("cast")
        Object[] argsNonRaw2 = (Object[]) args;
    }

    // default qualifier is @Nullable, so this is OK.
    class RawAfterConstructorBad {
        Object o;

        RawAfterConstructorBad() {}
    }

    class RawAfterConstructorOK1 {
        @Nullable Object o;

        RawAfterConstructorOK1() {}
    }

    class RawAfterConstructorOK2 {
        int a;

        RawAfterConstructorOK2() {}
    }
}