File: FlowFormatter.java

package info (click to toggle)
checker-framework-java 3.2.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 22,840 kB
  • sloc: java: 145,910; xml: 839; sh: 518; makefile: 401; perl: 26
file content (83 lines) | stat: -rw-r--r-- 2,821 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
import java.util.Date;
import java.util.Formatter;
import org.checkerframework.checker.formatter.FormatUtil;
import org.checkerframework.checker.formatter.qual.ConversionCategory;
import org.checkerframework.checker.formatter.qual.Format;
import org.junit.Assert;

public class FlowFormatter {
    public static String callUnqual(String u) {
        return u;
    }

    public static void main(String... p) {
        Formatter f = new Formatter();

        String unqual = System.lineSeparator();
        String qual = "%s %d %f";
        String wrong = "%$s";
        callUnqual("%s");
        callUnqual(qual);
        callUnqual(wrong);
        callUnqual(null);
        // :: error: (format.string.invalid)
        f.format(null);
        @Format({ConversionCategory.GENERAL}) String nullAssign = null;
        // :: error: (format.string.invalid)
        f.format(nullAssign, "string");
        if (false) {
            nullAssign = "%s";
        }
        f.format(nullAssign, "string");
        // :: error: (assignment.type.incompatible)
        @Format({ConversionCategory.GENERAL}) String err0 = unqual;
        // :: error: (assignment.type.incompatible)
        @Format({ConversionCategory.GENERAL}) String err2 = "%$s";
        @Format({ConversionCategory.GENERAL}) String ok = "%s";

        String u = "%s" + " %" + "d";
        String v = FormatUtil.asFormat(u, ConversionCategory.GENERAL, ConversionCategory.INT);
        f.format(u, "String", 1337);
        // :: error: (argument.type.incompatible)
        f.format(u, "String", 7.4);

        try {
            String l = FormatUtil.asFormat(u, ConversionCategory.FLOAT, ConversionCategory.INT);
            Assert.fail("Expected Exception");
        } catch (Error e) {
        }

        String a = "Success: %s %d %f";
        f.format(a, "String", 1337, 7.5);

        String b = "Fail: %d";
        // :: error: (argument.type.incompatible)
        f.format(b, "Wrong");

        @Format({
            ConversionCategory.GENERAL,
            ConversionCategory.INT,
            ConversionCategory.FLOAT,
            ConversionCategory.CHAR
        })
        String s = "Success: %s %d %f %c";
        f.format(s, "OK", 42, 3.14, 'c');

        @Format({ConversionCategory.GENERAL, ConversionCategory.INT, ConversionCategory.FLOAT}) String t = "Fail: %s %d %f";
        // :: error: (argument.type.incompatible)
        f.format(t, "OK", "Wrong", 3.14);

        call(f, "Success: %tM");
        // :: error: (argument.type.incompatible)
        call(f, "Fail: %d");

        System.out.println(f.toString());
        f.close();
    }

    public static void call(Formatter f, @Format({ConversionCategory.TIME}) String s) {
        f.format(s, new Date());
        // :: error: (argument.type.incompatible)
        f.format(s, "Wrong");
    }
}