summaryrefslogtreecommitdiffstats
path: root/tools/infer/test/autotest/src/main/java/Eradicate.java
blob: 41e02d0562ad1fadbc49871f14b8d4445bfc6e6c (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import javax.annotation.Nullable;

// Examples taken from the infer website.
public class Eradicate {

    public String f; // Because it is not annoted with nullable -> can never be null!

    public void field(@Nullable Eradicate x) {
        x.f = "3"; // Error: Eradicate null field access
    }

    public void method(@Nullable Object x) {
        String s = x.toString(); // Error: Eradicate null method call
    }

    public void filedNotNull(@Nullable String s) {
        f = s; // Error: Eradicate field not nullable
    }

    public Eradicate() {} // Error: Eradicate field not initialized

    public void str(Eradicate x) {
        String s = x.toString();
    }

    public void callStr(@Nullable Eradicate x) {
        str(x); // Error:  Eradicate parameter not nullable
    }

    public String shouldNotReturnNullBecauseNotAnnotated() {
        return null; // Error: Eradicate return not nullable
    }

    public void redundant() {
        String s = new String("abc");
        if (s != null) { // Error: Eradicate condition redundant
            int n = s.length();
        }
    }

    @Nullable
    public static String someMethod() {
        return ""; // Error: Eradicate return overannotated
    }
}

class B extends Eradicate {
    @Nullable public String shouldNotReturnNullBecauseNotAnnotated() {
        return null; // Error: Eradicate inconsistent subclass return annotation
    }

    public void field(Eradicate x) {} // Error: Inconsistent subclass parameter annotation
}