File: JdkCheck.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 (73 lines) | stat: -rw-r--r-- 2,157 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
import java.util.Optional;
import java.util.function.Supplier;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.checker.optional.qual.Present;

/** Test JDK annotations. */
@SuppressWarnings("optional.parameter")
public class JdkCheck {

    boolean isPresentTest1(@Present Optional<String> pos) {
        return pos.isPresent();
    }

    boolean isPresentTest2(Optional<String> mos) {
        return mos.isPresent();
    }

    String orElseThrowTest1(
            @Present Optional<String> pos, Supplier<RuntimeException> exceptionSupplier) {
        return pos.orElseThrow(exceptionSupplier);
    }

    String orElseThrowTest2(Optional<String> mos, Supplier<RuntimeException> exceptionSupplier) {
        // :: error: (method.invocation.invalid)
        return mos.orElseThrow(exceptionSupplier);
    }

    String getTest1(@Present Optional<String> pos) {
        return pos.get();
    }

    String getTest2(Optional<String> mos) {
        // :: error: (method.invocation.invalid)
        return mos.get();
    }

    @Present Optional<String> ofTestPNn(String s) {
        return Optional.of(s);
    }

    Optional<String> ofTestMNn(String s) {
        return Optional.of(s);
    }

    @Present Optional<String> ofTestPNble(@Nullable String s) {
        // TODO :: error: (of.nullable.argument) :: error: (return.type.incompatible)
        return Optional.of(s);
    }

    Optional<String> ofTestMNble(@Nullable String s) {
        // TODO :: error: (of.nullable.argument) :: error: (return.type.incompatible)
        return Optional.of(s);
    }

    @Present Optional<String> ofNullableTestPNble(@Nullable String s) {
        // :: error: (return.type.incompatible)
        return Optional.ofNullable(s);
    }

    /* TODO: ofNullable with non-null arg gives @Present (+ a warning?)
    @Present Optional<String> ofNullableTestPNn(String s) {
        return Optional.ofNullable(s);
    }
    */

    Optional<String> ofNullableTestMNble(@Nullable String s) {
        return Optional.ofNullable(s);
    }

    Optional<String> ofNullableTestMNn(String s) {
        return Optional.ofNullable(s);
    }
}