Debian Codegraph @

checker/tests/nullness/generics/MethodTypeVars.java

raw
1 import org.checkerframework.checker.nullness.qual.*;
2
3 /*
4 * This test is based on Issue 93:
5 * https://github.com/typetools/checker-framework/issues/93
6 */
7 public class MethodTypeVars {
8 void m() {
9 // :: error: (type.argument.type.incompatible)
10 Object a = A.badMethod(null);
11 Object b = A.badMethod(new Object());
12
13 // TODO: false negative. See #979
14 //// :: error: (type.argument.type.incompatible)
15 A.goodMethod(null);
16 A.goodMethod(new Object());
17 }
18 }
19
20 class A {
21 public static <T extends @NonNull Object> T badMethod(T t) {
22 // :: warning: [unchecked] unchecked cast
23 return (T) new Object();
24 }
25
26 public static <T extends @NonNull Object> void goodMethod(T t) {}
27 }
28
29 class B {
30 public <T> void indexOf1(T[] a, @Nullable Object elt) {}
31 // This is not valid Java syntax.
32 // public void indexOf2(?[] a, @Nullable Object elt) {}
33
34 void call() {
35 Integer[] arg = new Integer[] {1, 2, 3, 4};
36 indexOf1(arg, Integer.valueOf(5));
37 // indexOf2(arg, new Integer(5));
38 }
39 }