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
|
import java.util.ArrayList;
import java.util.Formatter;
public class VarargsFormatter {
public static void main(String... p) {
Formatter f = new Formatter();
// vararg as parameter
// :: warning: non-varargs call of varargs method with inexact argument type for last
// parameter; :: warning: (format.indirect.arguments)
f.format("Nothing", null); // equivalent to (Object[])null
// :: warning: (format.indirect.arguments)
f.format("Nothing", (Object[]) null);
// :: warning: (format.indirect.arguments)
f.format("%s", (Object[]) null);
// :: warning: (format.indirect.arguments)
f.format("%s %d %x", (Object[]) null);
// :: warning: non-varargs call of varargs method with inexact argument type for last
// parameter; :: warning: (format.indirect.arguments)
f.format("%s %d %x", null); // equivalent to (Object[])null
// :: warning: (format.indirect.arguments)
f.format("%d", new Object[1]);
// :: warning: (format.indirect.arguments)
f.format("%s", new Object[2]);
// :: warning: (format.indirect.arguments)
f.format("%s %s", new Object[0]);
// :: warning: (format.indirect.arguments)
f.format("Empty", new Object[0]);
// :: warning: (format.indirect.arguments)
f.format("Empty", new Object[5]);
f.format("%s", new ArrayList<Object>());
f.format("%d %s", 132, new Object[2]);
f.format("%s %d", new Object[2], 123);
// :: error: (format.missing.arguments)
f.format("%d %s %s", 132, new Object[2]);
// :: error: (argument.type.incompatible)
f.format("%d %d", new Object[2], 123);
// :: error: (format.specifier.null) :: warning: (format.indirect.arguments)
f.format("%d %<f", new Object[1]);
// too many arguments
// :: warning: (format.excess.arguments)
f.format("", 213);
// :: warning: (format.excess.arguments)
f.format("%d", 232, 132);
// :: warning: (format.excess.arguments)
f.format("%s", "a", "b");
// :: warning: (format.excess.arguments)
f.format("%d %s", 123, "a", 123);
// too few arguments
// :: error: (format.missing.arguments)
f.format("%s");
// :: error: (format.missing.arguments)
f.format("%d %s", 545);
// :: error: (format.missing.arguments)
f.format("%s %c %c", 'c', 'c');
f.close();
}
}
|