private void verifyPrintf(MethodInvocationTree tree, FormatParameters parameters) throws FormatFlagsConversionMismatchException, IllegalFormatException, FormatterException { List<? extends ExpressionTree> args = tree.getArguments(); JCLiteral format = (JCLiteral) args.get(parameters.getFormatIndex()); String formatString = (String) format.getValue(); List<String> argTypes = new ArrayList<String>(); for (int i = parameters.getFormatIndex() + 1; i < args.size(); ++i) { Type type = ((JCExpression) args.get(i)).type; argTypes.add(getFormatterType(type)); } try { Formatter.check(formatString, argTypes.toArray(new String[0])); } catch (ExtraFormatArgumentsException e) { return; // We can handle this. } }
@Test public void formatsBoolean() { assertEquals("true", new Formatter().format("%b", true).toString()); assertEquals("false", new Formatter().format("%b", false).toString()); assertEquals("true", new Formatter().format("%b", new Object()).toString()); assertEquals("false", new Formatter().format("%b", null).toString()); assertEquals(" true", new Formatter().format("%6b", true).toString()); assertEquals("true ", new Formatter().format("%-6b", true).toString()); assertEquals("true", new Formatter().format("%2b", true).toString()); assertEquals("tr", new Formatter().format("%2.2b", true).toString()); assertEquals(" tr", new Formatter().format("%4.2b", true).toString()); assertEquals("TRUE", new Formatter().format("%B", true).toString()); try { new Formatter().format("%+b", true); fail("Should have thrown exception"); } catch (FormatFlagsConversionMismatchException e) { assertEquals("+", e.getFlags()); assertEquals('b', e.getConversion()); } }
@Override public void formatTo(Formatter formatter, int flags, int width, int precision) { if ((flags & ALTERNATE) == ALTERNATE) { throw new FormatFlagsConversionMismatchException("#", 's'); } if (precision != -1) { throw new IllegalFormatPrecisionException(precision); } this.value.formatTo(formatter, flags | ALTERNATE, width, precision); }
public void formatTo(Formatter formatter, int flags, int width, int precision) { if ((flags & ALTERNATE) == ALTERNATE) { throw new FormatFlagsConversionMismatchException("#", 's'); } if (precision != -1) { throw new IllegalFormatPrecisionException(precision); } this.value.formatTo(formatter, flags | ALTERNATE, width, precision); }
/** * @tests java.util.FormatFlagsConversionMismatchException#FormatFlagsConversionMismatchException(String, * char) */ public void test_formatFlagsConversionMismatchException() { try { new FormatFlagsConversionMismatchException(null, ' '); fail("should throw NullPointerException."); } catch (NullPointerException e) { // expected } }
/** * @tests java.util.FormatFlagsConversionMismatchException#getFlags() */ public void test_getFlags() { String flags = "MYTESTFLAGS"; char conversion = 'T'; FormatFlagsConversionMismatchException formatFlagsConversionMismatchException = new FormatFlagsConversionMismatchException( flags, conversion); assertEquals(flags, formatFlagsConversionMismatchException.getFlags()); }
/** * @tests java.util.FormatFlagsConversionMismatchException#getConversion() */ public void test_getConversion() { String flags = "MYTESTFLAGS"; char conversion = 'T'; FormatFlagsConversionMismatchException formatFlagsConversionMismatchException = new FormatFlagsConversionMismatchException( flags, conversion); assertEquals(conversion, formatFlagsConversionMismatchException .getConversion()); }
/** * @tests java.util.FormatFlagsConversionMismatchException#getMessage() */ public void test_getMessage() { String flags = "MYTESTFLAGS"; char conversion = 'T'; FormatFlagsConversionMismatchException formatFlagsConversionMismatchException = new FormatFlagsConversionMismatchException( flags, conversion); assertTrue(null != formatFlagsConversionMismatchException.getMessage()); }
public void assertDeserialized(Serializable initial, Serializable deserialized) { SerializationTest.THROWABLE_COMPARATOR.assertDeserialized(initial, deserialized); FormatFlagsConversionMismatchException initEx = (FormatFlagsConversionMismatchException) initial; FormatFlagsConversionMismatchException desrEx = (FormatFlagsConversionMismatchException) deserialized; assertEquals("Flags", initEx.getFlags(), desrEx.getFlags()); assertEquals("Conversion", initEx.getConversion(), desrEx .getConversion()); }
/** * @tests serialization/deserialization. */ public void testSerializationSelf() throws Exception { SerializationTest.verifySelf( new FormatFlagsConversionMismatchException("MYTESTFLAGS", 'T'), exComparator); }
/** * @tests serialization/deserialization compatibility with RI. */ public void testSerializationCompatibility() throws Exception { SerializationTest.verifyGolden(this, new FormatFlagsConversionMismatchException("MYTESTFLAGS", 'T'), exComparator); }
private void assertFormatFlagsConversionMismatchException(Formatter f, String str) { try { f.format(str); fail("should throw FormatFlagsConversionMismatchException: " + str); /* * error on RI, throw IllegalFormatFlagsException specification * says FormatFlagsConversionMismatchException should be thrown */ } catch (FormatFlagsConversionMismatchException e) { // expected } }
FormatSpecifier(String source, String[] sa) throws FormatFlagsConversionMismatchException, FormatterNumberFormatException { int idx = 0; this.source = source; index(sa[idx++]); flags(sa[idx++]); width(sa[idx++]); precision(sa[idx++]); if (sa[idx] != null) { dt = true; if (sa[idx].equals("T")) f.add(Flags.UPPERCASE); } conversion(sa[++idx]); if (dt) checkDateTime(); else if (Conversion.isGeneral(c)) checkGeneral(); else if (Conversion.isCharacter(c)) checkCharacter(); else if (Conversion.isInteger(c)) checkInteger(); else if (Conversion.isFloat(c)) checkFloat(); else if (Conversion.isText(c)) checkText(); else throw new UnknownFormatConversionException(String.valueOf(c)); }
private void checkGeneral() throws FormatFlagsConversionMismatchException { if ((c == Conversion.BOOLEAN || c == Conversion.HASHCODE) && f.contains(Flags.ALTERNATE)) failMismatch(Flags.ALTERNATE, c); // '-' requires a width if (width == -1 && f.contains(Flags.LEFT_JUSTIFY)) throw new MissingFormatWidthException(toString()); checkBadFlags(Flags.PLUS, Flags.LEADING_SPACE, Flags.ZERO_PAD, Flags.GROUP, Flags.PARENTHESES); }
private void checkDateTime() throws FormatFlagsConversionMismatchException { if (precision != -1) throw new IllegalFormatPrecisionException(precision); if (!DateTime.isValid(c)) throw new UnknownFormatConversionException("t" + c); checkBadFlags(Flags.ALTERNATE, Flags.PLUS, Flags.LEADING_SPACE, Flags.ZERO_PAD, Flags.GROUP, Flags.PARENTHESES); // '-' requires a width if (width == -1 && f.contains(Flags.LEFT_JUSTIFY)) throw new MissingFormatWidthException(toString()); }
private void checkCharacter() throws FormatFlagsConversionMismatchException { if (precision != -1) throw new IllegalFormatPrecisionException(precision); checkBadFlags(Flags.ALTERNATE, Flags.PLUS, Flags.LEADING_SPACE, Flags.ZERO_PAD, Flags.GROUP, Flags.PARENTHESES); // '-' requires a width if (width == -1 && f.contains(Flags.LEFT_JUSTIFY)) throw new MissingFormatWidthException(toString()); }
private void checkInteger() throws FormatFlagsConversionMismatchException { checkNumeric(); if (precision != -1) throw new IllegalFormatPrecisionException(precision); if (c == Conversion.DECIMAL_INTEGER) checkBadFlags(Flags.ALTERNATE); else checkBadFlags(Flags.GROUP); }
private void checkFloat() throws FormatFlagsConversionMismatchException { checkNumeric(); if (c == Conversion.DECIMAL_FLOAT) { } else if (c == Conversion.HEXADECIMAL_FLOAT) { checkBadFlags(Flags.PARENTHESES, Flags.GROUP); } else if (c == Conversion.SCIENTIFIC) { checkBadFlags(Flags.GROUP); } else if (c == Conversion.GENERAL) { checkBadFlags(Flags.ALTERNATE); } }
private void printInteger(String arg) throws IllegalFormatConversionException, FormatFlagsConversionMismatchException { if (mightBeUnknown(arg)) return; if (matchSig(arg, Byte.class, Short.class, Integer.class, Long.class)) printLong(); else if (matchSig(arg, BigInteger.class)) { } else failConversion(arg); }
private void printLong() throws FormatFlagsConversionMismatchException { if (c == Conversion.OCTAL_INTEGER) { checkBadFlags(Flags.PARENTHESES, Flags.LEADING_SPACE, Flags.PLUS); } else if (c == Conversion.HEXADECIMAL_INTEGER) { checkBadFlags(Flags.PARENTHESES, Flags.LEADING_SPACE, Flags.PLUS); } }
private static List<FormatSpecifier> parse(String s) throws FormatFlagsConversionMismatchException, FormatterNumberFormatException { ArrayList<FormatSpecifier> al = new ArrayList<FormatSpecifier>(); Matcher m = fsPattern.matcher(s); int i = 0; while (i < s.length()) { if (m.find(i)) { // Anything between the start of the string and the beginning // of the format specifier is either fixed text or contains // an invalid format string. if (m.start() != i) { // Make sure we didn't miss any invalid format specifiers checkText(s.substring(i, m.start())); } // Expect 6 groups in regular expression String[] sa = new String[6]; for (int j = 0; j < m.groupCount(); j++) { sa[j] = m.group(j + 1); } al.add(new FormatSpecifier(m.group(0), sa)); i = m.end(); } else { // No more valid format specifiers. Check for possible invalid // format specifiers. checkText(s.substring(i)); // The rest of the string is fixed text break; } } return al; }