@Override public Resources getResources() { if(wrappedResources == null) { Resources r = super.getResources(); wrappedResources = new Resources(r.getAssets(), r.getDisplayMetrics(), r.getConfiguration()) { @NonNull @Override public String getString(int id, Object... formatArgs) throws NotFoundException { try { return super.getString(id, formatArgs); } catch (IllegalFormatConversionException ifce) { Log.e("DatePickerDialogFix", "IllegalFormatConversionException Fixed!", ifce); String template = super.getString(id); template = template.replaceAll("%" + ifce.getConversion(), "%s"); return String.format(getConfiguration().locale, template, formatArgs); } } }; } return wrappedResources; }
public String format(String pattern) { // The value could be an integer value. Try to convert to BigInteger in // order to have access to more conversion formats. try { return String.format(pattern, value.toBigIntegerExact()); } catch (ArithmeticException ae) { // Could not convert to integer value without loss of // information. Fall through to default behavior. } catch (IllegalFormatConversionException ifce) { // The conversion is not valid for the type BigInteger. This // happens, if the format is like "%.1f" but the value is an // integer. Fall through to default behavior. } return String.format(pattern, value); }
@Test public void testMathExpressionCPI() throws Exception { double score = 0.5; int inLinks = 10; int articleCount = 1000; String cpiExpr = "%1$f*Math.log(%3$d/%2$d)"; try { ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByName("JavaScript"); double cpi = (double) engine.eval(String.format(cpiExpr, score, inLinks, articleCount)); assertEquals("Invalid CPI score", 2.302585092994046, cpi, 0); } catch(ScriptException | IllegalFormatConversionException e) { throw new Exception("Cannot evaluate CPI script expression: " + cpiExpr + "; Exception: " + e.getMessage()); } }
@Test(expected = IllegalFormatConversionException.class) public void ensuresThatFormatterFails() throws IOException { new FormattedText( new TextOf("Local time: %d"), Locale.ROOT, Calendar.getInstance() ).asString(); }
/** * set a value in the map * * @param tag the name of the value * @param object the Object to put into the map */ public <T> void set(String tag, T object) { //if the object is null, add a null value to the map if (object == null) { values.put(tag, null); return; } //get the class to convert to Class<T> clazz = (Class<T>) object.getClass(); //get the converter for the specified class Converter<T> converter = converters.getConverter(clazz); //throw an error if there is no converter for the class if (converter == null) { throw new MissingResourceException("No converter given.", converters.getClass().getName(), clazz.getName()); } //convert the value to a string String string = converter.toString(object); //if the result is null, throw an exception if (string == null) throw new IllegalFormatConversionException((char) 0, clazz); //add the key-value pair to the values map values.put(tag, string); }
/** * * @param tag the name of the value * @param clazz the class to convert to * @param separator the string to separate the array elements with (not a * regex) * @return an array of the specified type * @throws IllegalArgumentException if there is no converter for the given * type */ public <T> T[] getArray(String tag, Class<T> clazz, String separator) { //get the converter for the specified class Converter<T> converter = converters.getConverter(clazz); //throw an error if there is no converter for the class if (converter == null) { throw new MissingResourceException("No converter given.", converters.getClass().getName(), clazz.getName()); } if (!values.containsKey(tag)) { throw new IllegalArgumentException(); } //get the value from the map String string = values.get(tag); //if the input is null, return null if (string == null) return null; //separate the string into parts. use the separator as a literal string, not a regex String[] parts = string.split(Pattern.quote(separator)); T[] results = (T[]) Array.newInstance(clazz, parts.length); for (int i=0; i<parts.length; i++) { String part = parts[i]; //convert the string to the object T result = converter.fromString(part); //if the result is null, throw an exception if (result == null) throw new IllegalFormatConversionException((char) 0, clazz); results[i] = result; } return results; }
/** * @param tag the name of the value * @param clazz the class to convert to * @return the value converted to the specified type * @throws MissingResourceException if there is no converter for the given * type * @throws IllegalArgumentException if there is no value with the given tag * @throws IllegalFormatConversionException if the string could not be * converted to the specified object */ public <T> T get(String tag, Class<T> clazz) { if (clazz == null) { throw new IllegalArgumentException("clazz cannot be null."); } //get the converter for the specified class Converter<T> converter = converters.getConverter(clazz); //throw an error if there is no converter for the class if (converter == null) { throw new MissingResourceException("No converter given.", converters.getClass().getName(), clazz.getName()); } if (!values.containsKey(tag)) { throw new IllegalArgumentException(); } //get the value from the map String string = values.get(tag); //if the input is null, return null if (string == null) return null; //convert the string to the object T result = converter.fromString(string); //if the result is null, throw an exception if (result == null) throw new IllegalFormatConversionException((char) 0, clazz); return result; }
public double[] getLatitudeLogitude(IAddress searchCriteria) throws DomainRuntimeException { String[] l_latlong = getLatitudeLogitude(searchCriteria,LatitudeLogitudeFormat.DEGREES); double[] l_dLatLong = new double[2]; if (l_latlong != null) { if (l_latlong.length == 2) { if (l_latlong[0] == null || l_latlong[1] == null) return null; try { //we have two entries to convert if (l_latlong[0] != null && l_latlong[0] != "") l_dLatLong[0] = Double.parseDouble(l_latlong[0]); if (l_latlong[1] != null && l_latlong[1] != "") l_dLatLong[1] = Double.parseDouble(l_latlong[1]); } catch (IllegalFormatConversionException e) { throw new DomainRuntimeException(e.getMessage()); } return l_dLatLong; } } return null; }
@NonNull @Override public String getString(int id, Object... formatArgs) throws NotFoundException { try { return super.getString(id, formatArgs); } catch (IllegalFormatConversionException conversationException) { String template = super.getString(id); char conversion = conversationException.getConversion(); // Trying to replace either all digit patterns (%d) or first one (%1$d). template = template.replaceAll(Pattern.quote("%" + conversion), "%s") .replaceAll(Pattern.quote("%1$" + conversion), "%s"); return String.format(getLocale(), template, formatArgs); } }
/** * @tests java.util.IllegalFormatConversionException#IllegalFormatConversionException(char, * Class) */ public void test_illegalFormatConversionException() { try { new IllegalFormatConversionException(' ', null); fail("should throw NullPointerExcetpion."); } catch (NullPointerException e) { // desired } }
/** * @tests java.util.IllegalFormatConversionException#getArgumentClass() */ public void test_getArgumentClass() { char c = '*'; Class<String> argClass = String.class; IllegalFormatConversionException illegalFormatConversionException = new IllegalFormatConversionException( c, argClass); assertEquals(argClass, illegalFormatConversionException .getArgumentClass()); }
/** * @tests java.util.IllegalFormatConversionException#getConversion() */ public void test_getConversion() { char c = '*'; Class<String> argClass = String.class; IllegalFormatConversionException illegalFormatConversionException = new IllegalFormatConversionException( c, argClass); assertEquals(c, illegalFormatConversionException.getConversion()); }
/** * @tests java.util.IllegalFormatConversionException#getMessage() */ public void test_getMessage() { char c = '*'; Class<String> argClass = String.class; IllegalFormatConversionException illegalFormatConversionException = new IllegalFormatConversionException( c, argClass); assertTrue(null != illegalFormatConversionException.getMessage()); }
public void assertDeserialized(Serializable initial, Serializable deserialized) { SerializationTest.THROWABLE_COMPARATOR.assertDeserialized(initial, deserialized); IllegalFormatConversionException initEx = (IllegalFormatConversionException) initial; IllegalFormatConversionException desrEx = (IllegalFormatConversionException) deserialized; assertEquals("ArgumentClass", initEx.getArgumentClass(), desrEx .getArgumentClass()); assertEquals("Conversion", initEx.getConversion(), desrEx .getConversion()); }
/** * @tests serialization/deserialization compatibility with RI. */ public void testSerializationCompatibility() throws Exception { SerializationTest.verifyGolden(this, new IllegalFormatConversionException('*', String.class), exComparator); }
private void formatChar(char specifier, boolean upperCase) throws IOException { verifyFlags(specifier, MASK_FOR_CHAR_FORMAT); Object arg = args[argumentIndex]; if (precision >= 0) { throw new TIllegalFormatPrecisionException(precision); } int c; if (arg instanceof Character) { c = (Character) arg; } else if (arg instanceof Byte) { c = (char) (byte) arg; } else if (arg instanceof Short) { c = (char) (short) arg; } else if (arg instanceof Integer) { c = (int) arg; if (!Character.isValidCodePoint(c)) { throw new TIllegalFormatCodePointException(c); } } else if (arg == null) { formatGivenString(upperCase, "null"); return; } else { throw new IllegalFormatConversionException(specifier, arg != null ? arg.getClass() : null); } formatGivenString(upperCase, new String(Character.toChars(c))); }
private void formatRadixInt(char specifier, int radixLog2, boolean upperCase) throws IOException { verifyFlags(specifier, MASK_FOR_INT_RADIX_FORMAT); verifyIntFlags(); String str; Object arg = args[argumentIndex]; if (arg instanceof Long) { str = IntegerUtil.toUnsignedLogRadixString((Long) arg, radixLog2); } else if (arg instanceof Integer) { str = IntegerUtil.toUnsignedLogRadixString((Integer) arg, radixLog2); } else if (arg instanceof Short) { str = IntegerUtil.toUnsignedLogRadixString((Short) arg & 0xFFFF, radixLog2); } else if (arg instanceof Byte) { str = IntegerUtil.toUnsignedLogRadixString((Byte) arg & 0xFF, radixLog2); } else { throw new IllegalFormatConversionException(specifier, arg != null ? arg.getClass() : null); } StringBuilder sb = new StringBuilder(); if ((flags & TFormattableFlags.ALTERNATE) != 0) { String prefix = radixLog2 == 4 ? "0x" : "0"; str = prefix + str; } if ((flags & TFormattableFlags.ZERO_PADDED) != 0) { for (int i = str.length(); i < width; ++i) { sb.append(Character.forDigit(0, 10)); } } sb.append(str); formatGivenString(upperCase, sb.toString()); }
@Test(expected = IllegalFormatConversionException.class) public void testGetTagClass4() { OptionsFile o = new OptionsFile(UtilConverters.getInstance()); o.set("A", 0.2); o.get("A", Vector2D.class); }
@Override public Dialog onCreateDialog(Bundle savedInstanceState) { Context context = getActivity(); if (isBrokenSamsungDevice()) { context = new ContextWrapper(getActivity()) { private Resources wrappedResources; @Override public Resources getResources() { Resources r = super.getResources(); if (wrappedResources == null) { wrappedResources = new Resources(r.getAssets(), r.getDisplayMetrics(), r.getConfiguration()) { @NonNull @Override public String getString(int id, Object... formatArgs) throws NotFoundException { try { return super.getString(id, formatArgs); } catch (IllegalFormatConversionException ifce) { Log.e("DatePickerDialogFix", "IllegalFormatConversionException Fixed!", ifce); String template = super.getString(id); template = template.replaceAll("%" + ifce.getConversion(), "%s"); return String.format(getConfiguration().locale, template, formatArgs); } } }; } return wrappedResources; } }; } if (dateTime == null) { // Use the current date as the default date in the picker final Calendar c = Calendar.getInstance(); year = c.get(Calendar.YEAR); month = c.get(Calendar.MONTH); day = c.get(Calendar.DAY_OF_MONTH); } else { year = dateTime.getYear(); month = dateTime.getMonthOfYear() - 1; day = dateTime.getDayOfMonth(); } // Create a new instance of DatePickerDialog and return it dialog = new DatePickerDialog(context, this, year, month, day); return dialog; }