Search in sources :

Example 1 with IllegalFormatConversionException

use of java.util.IllegalFormatConversionException in project pictureapp by EyeSeeTea.

the class DatePickerFragment method onCreateDialog.

@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;
}
Also used : Context(android.content.Context) DatePickerDialog(android.app.DatePickerDialog) Calendar(java.util.Calendar) Resources(android.content.res.Resources) ContextWrapper(android.content.ContextWrapper) IllegalFormatConversionException(java.util.IllegalFormatConversionException)

Example 2 with IllegalFormatConversionException

use of java.util.IllegalFormatConversionException in project xian by happyyangyuan.

the class GelfHandler method makeMessage.

private GelfMessage makeMessage(final LogRecord record) {
    String message = record.getMessage();
    Object[] parameters = record.getParameters();
    if (message == null)
        message = "";
    if (parameters != null && parameters.length > 0) {
        // by default, using {0}, {1}, etc. -> MessageFormat
        message = MessageFormat.format(message, parameters);
        if (message.equals(record.getMessage())) {
            // if the text is the same, assuming this is String.format type log (%s, %d, etc.)
            try {
                message = String.format(message, parameters);
            } catch (IllegalFormatConversionException e) {
                // leaving message as it is to avoid compatibility problems
                message = record.getMessage();
            } catch (NullPointerException e) {
            // ignore
            }
        }
    }
    final String shortMessage;
    if (message.length() > MAX_SHORT_MESSAGE_LENGTH) {
        shortMessage = message.substring(0, MAX_SHORT_MESSAGE_LENGTH - 1);
    } else {
        shortMessage = message;
    }
    if (extractStacktrace) {
        final Throwable thrown = record.getThrown();
        if (null != thrown) {
            final StringWriter sw = new StringWriter();
            thrown.printStackTrace(new PrintWriter(sw));
            message += "\n\r" + sw.toString();
        }
    }
    final GelfMessage gelfMessage = new GelfMessage(shortMessage, message, record.getMillis(), String.valueOf(levelToSyslogLevel(record.getLevel())));
    gelfMessage.addField("SourceClassName", record.getSourceClassName());
    gelfMessage.addField("SourceMethodName", record.getSourceMethodName());
    if (null != getOriginHost()) {
        gelfMessage.setHost(getOriginHost());
    }
    if (null != facility) {
        gelfMessage.setFacility(facility);
    }
    if (null != fields) {
        for (final Map.Entry<String, String> entry : fields.entrySet()) {
            gelfMessage.addField(entry.getKey(), entry.getValue());
        }
    }
    return gelfMessage;
}
Also used : StringWriter(java.io.StringWriter) IllegalFormatConversionException(java.util.IllegalFormatConversionException) Map(java.util.Map) HashMap(java.util.HashMap) PrintWriter(java.io.PrintWriter)

Example 3 with IllegalFormatConversionException

use of java.util.IllegalFormatConversionException in project j2objc by google.

the class FormatterTest method test_formatLjava_lang_String$Ljava_lang_Object_FloatDoubleBigDecimalConversionException.

/**
 * java.util.Formatter#format(String, Object...) for exceptions in
 * Float/Double/BigDecimal conversion type 'e', 'E', 'g', 'G', 'f', 'a', 'A'
 */
public void test_formatLjava_lang_String$Ljava_lang_Object_FloatDoubleBigDecimalConversionException() {
    Formatter f = null;
    final char[] conversions = { 'e', 'E', 'g', 'G', 'f', 'a', 'A' };
    final Object[] illArgs = { false, (byte) 1, (short) 2, 3, (long) 4, new BigInteger("5"), new Character('c'), new Object(), new Date() };
    for (int i = 0; i < illArgs.length; i++) {
        for (int j = 0; j < conversions.length; j++) {
            try {
                f = new Formatter(Locale.UK);
                f.format("%" + conversions[j], illArgs[i]);
                fail("should throw IllegalFormatConversionException");
            } catch (IllegalFormatConversionException e) {
            // expected
            }
        }
    }
    try {
        f = new Formatter(Locale.UK);
        f.format("%a", new BigDecimal(1));
        fail("should throw IllegalFormatConversionException");
    } catch (IllegalFormatConversionException e) {
    // expected
    }
    try {
        f = new Formatter(Locale.UK);
        f.format("%A", new BigDecimal(1));
        fail("should throw IllegalFormatConversionException");
    } catch (IllegalFormatConversionException e) {
    // expected
    }
    final String[] flagsConversionMismatches = { "%,e", "%,E", "%#g", "%#G", "%,a", "%,A", "%(a", "%(A" };
    for (int i = 0; i < flagsConversionMismatches.length; i++) {
        try {
            f = new Formatter(Locale.CHINA);
            f.format(flagsConversionMismatches[i], new BigDecimal(1));
            fail("should throw FormatFlagsConversionMismatchException");
        } catch (FormatFlagsConversionMismatchException e) {
        // expected
        }
        try {
            f = new Formatter(Locale.JAPAN);
            f.format(flagsConversionMismatches[i], (BigDecimal) null);
            fail("should throw FormatFlagsConversionMismatchException");
        } catch (FormatFlagsConversionMismatchException e) {
        // expected
        }
    }
    final String[] missingFormatWidths = { "%-0e", "%0e", "%-e", "%-0E", "%0E", "%-E", "%-0g", "%0g", "%-g", "%-0G", "%0G", "%-G", "%-0f", "%0f", "%-f", "%-0a", "%0a", "%-a", "%-0A", "%0A", "%-A" };
    for (int i = 0; i < missingFormatWidths.length; i++) {
        try {
            f = new Formatter(Locale.KOREA);
            f.format(missingFormatWidths[i], 1f);
            fail("should throw MissingFormatWidthException");
        } catch (MissingFormatWidthException e) {
        // expected
        }
        try {
            f = new Formatter(Locale.KOREA);
            f.format(missingFormatWidths[i], (Float) null);
            fail("should throw MissingFormatWidthException");
        } catch (MissingFormatWidthException e) {
        // expected
        }
    }
    final String[] illFlags = { "%+ e", "%+ E", "%+ g", "%+ G", "%+ f", "%+ a", "%+ A", "%-03e", "%-03E", "%-03g", "%-03G", "%-03f", "%-03a", "%-03A" };
    for (int i = 0; i < illFlags.length; i++) {
        try {
            f = new Formatter(Locale.CANADA);
            f.format(illFlags[i], 1.23d);
            fail("should throw IllegalFormatFlagsException");
        } catch (IllegalFormatFlagsException e) {
        // expected
        }
        try {
            f = new Formatter(Locale.CANADA);
            f.format(illFlags[i], (Double) null);
            fail("should throw IllegalFormatFlagsException");
        } catch (IllegalFormatFlagsException e) {
        // expected
        }
    }
    f = new Formatter(Locale.US);
    try {
        f.format("%F", 1);
        fail("should throw UnknownFormatConversionException");
    } catch (UnknownFormatConversionException e) {
    // expected
    }
}
Also used : FormatFlagsConversionMismatchException(java.util.FormatFlagsConversionMismatchException) Formatter(java.util.Formatter) IllegalFormatConversionException(java.util.IllegalFormatConversionException) Date(java.util.Date) BigDecimal(java.math.BigDecimal) UnknownFormatConversionException(java.util.UnknownFormatConversionException) BigInteger(java.math.BigInteger) IllegalFormatFlagsException(java.util.IllegalFormatFlagsException) MissingFormatWidthException(java.util.MissingFormatWidthException)

Example 4 with IllegalFormatConversionException

use of java.util.IllegalFormatConversionException in project j2objc by google.

the class IllegalFormatConversionExceptionTest method test_getConversion.

/**
 * 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());
}
Also used : IllegalFormatConversionException(java.util.IllegalFormatConversionException)

Example 5 with IllegalFormatConversionException

use of java.util.IllegalFormatConversionException in project j2objc by google.

the class IllegalFormatConversionExceptionTest method test_getArgumentClass.

/**
 * 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());
}
Also used : IllegalFormatConversionException(java.util.IllegalFormatConversionException)

Aggregations

IllegalFormatConversionException (java.util.IllegalFormatConversionException)8 FormatFlagsConversionMismatchException (java.util.FormatFlagsConversionMismatchException)3 Formatter (java.util.Formatter)3 BigInteger (java.math.BigInteger)2 Date (java.util.Date)2 IllegalFormatFlagsException (java.util.IllegalFormatFlagsException)2 IllegalFormatPrecisionException (java.util.IllegalFormatPrecisionException)2 MissingFormatWidthException (java.util.MissingFormatWidthException)2 UnknownFormatConversionException (java.util.UnknownFormatConversionException)2 DatePickerDialog (android.app.DatePickerDialog)1 Context (android.content.Context)1 ContextWrapper (android.content.ContextWrapper)1 Resources (android.content.res.Resources)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 BigDecimal (java.math.BigDecimal)1 Calendar (java.util.Calendar)1 HashMap (java.util.HashMap)1 IllegalFormatCodePointException (java.util.IllegalFormatCodePointException)1 Map (java.util.Map)1