Search in sources :

Example 1 with Format

use of org.checkerframework.checker.formatter.qual.Format in project checker-framework by typetools.

the class ConversionBasic method main.

public static void main(String... p) {
    Formatter f = new Formatter();
    // test GENERAL, there is nothing we can do wrong
    @Format({ ConversionCategory.GENERAL }) String s = "%s";
    f.format("Suc-%s-ful", "cess");
    f.format("%b", 4);
    f.format("%B", 7.5);
    f.format("%h", new Date());
    f.format("%H", new Integer(4));
    f.format("%s", new ArrayList<Integer>());
    // test CHAR
    @Format({ ConversionCategory.CHAR }) String c = "%c";
    f.format("%c", 'c');
    f.format("%c", (byte) 67);
    f.format("%c", (int) 67);
    f.format("%c", new Character('c'));
    f.format("%c", new Byte((byte) 67));
    f.format("%c", new Short((short) 67));
    f.format("%C", new Integer(67));
    // :: error: (argument.type.incompatible)
    f.format("%c", 7.5);
    // :: error: (argument.type.incompatible)
    f.format("%C", "Value");
    // test INT
    @Format({ ConversionCategory.INT }) String i = "%d";
    f.format("%d", (byte) 67);
    f.format("%o", (short) 67);
    f.format("%x", (int) 67);
    f.format("%X", (long) 67);
    f.format("%d", new Long(67));
    f.format("%d", BigInteger.ONE);
    // :: error: (argument.type.incompatible)
    f.format("%d", 'c');
    // :: error: (argument.type.incompatible)
    f.format("%d", BigDecimal.ONE);
    // test FLOAT
    @Format({ ConversionCategory.FLOAT }) String d = "%f";
    f.format("%e", (float) 67.1);
    f.format("%E", (double) 67.3);
    f.format("%f", new Float(42.5));
    f.format("%g", new Double(42.5));
    f.format("%G", 67.87);
    f.format("%a", BigDecimal.ONE);
    // :: error: (argument.type.incompatible)
    f.format("%A", 1325);
    // :: error: (argument.type.incompatible)
    f.format("%a", BigInteger.ONE);
    // test TIME
    @Format({ ConversionCategory.TIME }) String t = "%tT";
    f.format("%tD", new Date());
    f.format("%TM", (long) 32165456);
    f.format("%TD", Calendar.getInstance());
    // :: error: (argument.type.incompatible)
    f.format("%tD", 1321543512);
    // :: error: (argument.type.incompatible)
    f.format("%tD", new Object());
    System.out.println(f.toString());
    f.close();
}
Also used : Formatter(java.util.Formatter) Date(java.util.Date) BigInteger(java.math.BigInteger) Format(org.checkerframework.checker.formatter.qual.Format)

Example 2 with Format

use of org.checkerframework.checker.formatter.qual.Format in project checker-framework by typetools.

the class FormatterTreeUtil method categoriesToFormatAnnotation.

/**
 * Takes a list of ConversionCategory elements, and returns a syntax tree element that
 * represents a {@link Format} annotation with the list as value.
 */
public AnnotationMirror categoriesToFormatAnnotation(ConversionCategory[] args) {
    AnnotationBuilder builder = new AnnotationBuilder(processingEnv, Format.class.getCanonicalName());
    builder.setValue("value", args);
    return builder.build();
}
Also used : InvalidFormat(org.checkerframework.checker.formatter.qual.InvalidFormat) Format(org.checkerframework.checker.formatter.qual.Format) ReturnsFormat(org.checkerframework.checker.formatter.qual.ReturnsFormat) AnnotationBuilder(org.checkerframework.javacutil.AnnotationBuilder)

Example 3 with Format

use of org.checkerframework.checker.formatter.qual.Format in project checker-framework by typetools.

the class FlowFormatter method main.

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();
}
Also used : Format(org.checkerframework.checker.formatter.qual.Format) Formatter(java.util.Formatter)

Example 4 with Format

use of org.checkerframework.checker.formatter.qual.Format in project checker-framework by typetools.

the class FormatIndexing method main.

public static void main(String... p) {
    Formatter f = new Formatter();
    // GENERAL and self intersections
    @Format({ ConversionCategory.CHAR }) String ccc = "%1$c %<c %c";
    @Format({ ConversionCategory.CHAR }) String c = "%c %<s";
    @Format({ ConversionCategory.INT }) String i = "%d %<s";
    @Format({ ConversionCategory.FLOAT }) String d = "%f %<s";
    @Format({ ConversionCategory.TIME }) String t = "%TT %<s";
    // test CHAR_AND_INT
    @Format({ ConversionCategory.CHAR_AND_INT }) String ici = "%1$d %1$c";
    f.format("%1$c %1$d", (int) 42);
    f.format("%1$c %1$d %1$d", (int) 42);
    // :: error: (argument.type.incompatible)
    f.format("%1$c %1$d", (long) 42);
    // :: error: (argument.type.incompatible)
    f.format("%1$c %1$d", 'c');
    // test INT_AND_TIME
    @Format({ ConversionCategory.INT_AND_TIME }) String iit = "%1$tT %1$d";
    f.format("%1$tT %1$d", (long) 42);
    f.format("%1$d %1$tT %1$d", (long) 42);
    // :: error: (argument.type.incompatible)
    f.format("%1$tT %1$d", (int) 42);
    // :: error: (argument.type.incompatible)
    f.format("%1$tT %1$d", new Date());
    // test NULL
    @Format({ ConversionCategory.NULL }) String cf = "%c %<f";
    @Format({ ConversionCategory.NULL }) String ct = "%c %<TT";
    @Format({ ConversionCategory.NULL }) String _if = "%d %<f";
    @Format({ ConversionCategory.NULL }) String fc = "%f %<c";
    @Format({ ConversionCategory.NULL }) String fi = "%f %<d";
    @Format({ ConversionCategory.NULL }) String ft = "%f %<TT";
    @Format({ ConversionCategory.NULL }) String tc = "%TT %<c";
    @Format({ ConversionCategory.NULL, ConversionCategory.INT }) String tf = "%TT %<f %2$d";
    @Format({ ConversionCategory.NULL }) String icf = "%d %<c %<f";
    @Format({ ConversionCategory.NULL }) String itc = "%d %<TT %<c";
    // :: error: (format.specifier.null)
    f.format(tf, null, 0);
    // :: warning: (format.indirect.arguments) :: error: (format.specifier.null)
    f.format(tf, (Object[]) null);
    // :: error: (format.specifier.null)
    f.format(tf, 'c', 0);
    // :: error: (format.specifier.null)
    f.format(tf, (Object) null, 0);
    // test UNUSED
    // :: warning: (format.argument.unused)
    f.format("%1$s %3$s", "Hello", "Missing", "World");
    // :: warning: (format.argument.unused) :: warning: (format.indirect.arguments)
    f.format("%1$s %3$s", new Object[5]);
    // :: warning: (format.argument.unused) :: warning: (format.indirect.arguments)
    f.format("%5$s", (Object[]) null);
    // :: warning: (format.argument.unused)
    f.format("%3$s", null, null, null);
    // :: warning: (format.argument.unused)
    f.format("%7$s %2$s %4$s %<s %7$s", 0, 0, 0, 0, 0, 0, 0);
    f.close();
    // test UNUSED and NULL
    // :: warning: (format.argument.unused) :: error: (format.specifier.null)
    f.format("%1$s %3$d %3$f", "Hello", "Missing", "World");
}
Also used : Format(org.checkerframework.checker.formatter.qual.Format) Formatter(java.util.Formatter) Date(java.util.Date)

Aggregations

Format (org.checkerframework.checker.formatter.qual.Format)4 Formatter (java.util.Formatter)3 Date (java.util.Date)2 BigInteger (java.math.BigInteger)1 InvalidFormat (org.checkerframework.checker.formatter.qual.InvalidFormat)1 ReturnsFormat (org.checkerframework.checker.formatter.qual.ReturnsFormat)1 AnnotationBuilder (org.checkerframework.javacutil.AnnotationBuilder)1