public static void main(String[] args) { // Use the format specifier below, which should throw a // MissingFormatArgumentException. Then, use getFormatSpecifier() // to make sure the returned value equals the original format string. final String formatSpecifier = "%1$5.3s"; try { String formatResult = String.format(formatSpecifier); fail("MissingFormatArgumentException not thrown."); } catch (MissingFormatArgumentException ex) { final String returnedFormatSpecifier = ex.getFormatSpecifier(); if (!returnedFormatSpecifier.equals(formatSpecifier)) { fail("The specified format specifier: " + formatSpecifier + " does not match the value from getFormatSpecifier(): " + returnedFormatSpecifier); } } }
private static String getFormatMsg(String msg, Object[] args) { String result = ""; if (msg == null) { msg = "<null>"; } else { try { result = String.format(msg, args); } catch (MissingFormatArgumentException e) { } } // 简单判断是否格式化正确 if (TextUtils.isEmpty(result.trim()) || !result .contains(XulSystemUtil.objectToString(args[args.length - 1]))) { StringBuilder builder = new StringBuilder(msg); for (Object arg : args) { builder.append(" ").append(XulSystemUtil.objectToString(arg)); } result = builder.toString(); } return result; }
public static boolean processCommand(String raw) { if (Minecraft.getMinecraft().thePlayer == null || !OptionCore.CLIENT_CHAT_PACKETS.getValue()) return false; if (raw.contains(CommandType.PREFIX) && raw.contains(CommandType.SUFFIX)) { final Command command; try { command = new Command(raw); } catch (MissingFormatArgumentException e) { return false; } if (command.type != null) { if (!command.from.equals(StaticPlayerHelper.getName(Minecraft.getMinecraft()))) command.activate(); return true; } } return false; }
@Override public List<String> referMapping(Mapping mapping) { final List<String> commands = new LinkedList<>(); for (String regex : mapping.value()) { regex = regex.replace(DASH, " ").replace(SPACE, " "); try { final String format = regex.replaceAll("\\(.*?\\)", "%s").replace("\\", ""); commands.add(String.format(format, mapping.args())); } catch (MissingFormatArgumentException e) { commands.add(regex); } } return commands; }
/** * Use polymorphism here, this is actually being called by Operator implementations, where they know their number of operands * @param operands operands in list of strings * @param variableRegistry registry for registering possible variables found in operands * @return operands in List of Expr */ protected List<Expr> parseOperands(List<String> operands, VariableRegistry variableRegistry) { ArrayList<Expr> list = new ArrayList<Expr>(); final int numOperands = this.numberOfOperands(); if (operands.size() != numOperands) { throw new MissingFormatArgumentException( this.getSymbol() + " expect " + numOperands + " operands, actual number of operands is: " + operands.size()); } for (int i = 0; i < numOperands; i++) { list.add(Expression.parse(operands.get(i), variableRegistry)); } return list; }
/** * Replicates a string pattern based on a list of objects, generating a list * as result. * @param pattern The string pattern. * @param values The list of objects to be merged with the pattern. * @return A list containing the string pattern replicated to each object * from the list. * @throws NightingaleException Something wrong happened, to be caught in * the higher levels. */ public static List<Object> replicateList(String pattern, List<Object> values) throws NightingaleException { List<Object> result = new ArrayList<Object>(); for (Object value : values) { try { result.add(String.format(pattern, value)); } catch (MissingFormatArgumentException exception) { throw new NightingaleException( messages.getMessage( Messages.ERROR_REPLICATELIST_MISSING_FORMAT_ARGUMENTS_EXCEPTION ), exception ); } } return result; }
public ArgumentFormatter format(Map<String, ? extends Object> mapper) { if (hasFormatted()) return this; StringBuilder sb = new StringBuilder(formatted); while(true) { int start = sb.indexOf("{"); int end = sb.indexOf("}"); if (start < 0 || end < 0) { break; } String key = sb.substring(start + 1, end); Object value = mapper.get(key); if (value == null) { throw new MissingFormatArgumentException("Format specifier '" + key + "'"); } sb.replace(start, end + 1, value.toString()); } /* update */ this.formatted = sb; this.hasFormatted = true; return this; }
private FormattedString getArgument(FormattedString[] args, int index, FormatSpecifierParser fsp, FormattedString lastArgument, boolean hasLastArgumentSet) { if (index == FormatToken.LAST_ARGUMENT_INDEX && !hasLastArgumentSet) { throw new MissingFormatArgumentException("<"); } if (args == null) { return null; } if (index >= args.length) { throw new MissingFormatArgumentException(fsp.getFormatSpecifierText()); } if (index == FormatToken.LAST_ARGUMENT_INDEX) { return lastArgument; } return args[index]; }
private FormatToken parseArgumentIndexAndFlags(FormatToken token) { // Parse the argument index, if there is one. int ch = peek(); if (Character.isDigit(ch)) { int number = nextInt(); if (peek() == '$') { // The number was an argument index. advance(); // Swallow the '$'. if (number == FormatToken.UNSET) { throw new MissingFormatArgumentException(getFormatSpecifierText()); } // k$ stands for the argument whose index is k-1 except that // 0$ and 1$ both stand for the first element. token.setArgIndex(Math.max(0, number - 1)); } } else if (ch == '<') { token.setArgIndex(FormatToken.LAST_ARGUMENT_INDEX); advance(); } // Parse the flags. while (token.setFlag(peek())) { advance(); } return parseConversionType(token); }
/** * 書式文字列に変数を設定した文字列を返します。 * 書式文字列はプロパティファイルに定義します。 * * @param key プロパティのキー * @param args 書式に設定する変数 * @return 書式設定された文字列 */ public String fmt(String key, Object...args) { try { String value = getProperty(key); if(value == null) { LOG.warn("キー「{}」のプロパティは未定義です。", key); return ""; } return String.format(value, args); } catch (MissingFormatArgumentException e) { LOG.warn("プロパティ「{}」の書式設定が失敗しました。", key); return ""; } }
@Test(timeout = 4000) public void test07() throws Throwable { CoreDbConfiguration coreDbConfiguration0 = new CoreDbConfiguration(); CoreDbConfiguration.JDBC_URL = "CoreDbConfiguration [contexts=%s, coreDb=%s, corePassword=%s, corePort=%s, coreServer=%s, coreUsername=%s]"; // Undeclared exception! try { coreDbConfiguration0.getCoreJdbcURL(); fail("Expecting exception: MissingFormatArgumentException"); } catch (MissingFormatArgumentException e) { // // Format specifier '%s' // assertThrownBy("java.util.Formatter", e); } }
@Test(timeout = 4000) public void test10() throws Throwable { CoreDbConfiguration coreDbConfiguration0 = new CoreDbConfiguration(); CoreDbConfiguration.JDBC_URL = "CoreDbConfiguration [contexts=%s, coreDb=%s, corePassword=%s, corePort=%s, coreServer=%s, coreUsername=%s]"; coreDbConfiguration0.corePassword = "local"; // Undeclared exception! try { coreDbConfiguration0.getCoreConnection(); fail("Expecting exception: MissingFormatArgumentException"); } catch (MissingFormatArgumentException e) { // // Format specifier '%s' // assertThrownBy("java.util.Formatter", e); } }
public static void validDirectionsRoute(DirectionsRoute directionsRoute, boolean defaultMilestonesEnabled) { if (defaultMilestonesEnabled && directionsRoute.routeOptions() != null && !directionsRoute.routeOptions().voiceInstructions()) { throw new MissingFormatArgumentException("Using the default milestone requires the " + "directions route to include the voice instructions object."); } }
public static void i(String msg, Object... args) { try { if (BuildConfig.DEBUG) android.util.Log.i(TAG, String.format(msg, args)); } catch (MissingFormatArgumentException e) { android.util.Log.e(TAG, "vitamio.Log", e); android.util.Log.i(TAG, msg); } }
public static void d(String msg, Object... args) { try { if (BuildConfig.DEBUG) android.util.Log.d(TAG, String.format(msg, args)); } catch (MissingFormatArgumentException e) { android.util.Log.e(TAG, "vitamio.Log", e); android.util.Log.d(TAG, msg); } }
/** * Appends to a {@code SafeSqlBuilder} a formatted sql string using the * specified arguments. * * @param builder {@code SafeSqlBuilder} where is appened the formatted sql * @param sql string query with some <code>{}</code> argument place. The * argument can have a number inside to force a argument index (start at 1). * The escape sequence is <code>{{.*}}</code>. * @param arguments arguments list */ public static void formatTo(SafeSqlBuilder builder, String sql, Object... arguments) { Matcher matcher = PATTERN.matcher(sql); int lastIndex = 0; int argIndex = 0; while (matcher.find()) { String before = sql.substring(lastIndex, matcher.start()); String parameter = matcher.group(1); lastIndex = matcher.end(); builder.append(before); if (parameter.isEmpty()) { if (argIndex >= arguments.length) { throw new MissingFormatArgumentException("Argument " + argIndex); } builder.param(arguments[argIndex++]); } else if (parameter.startsWith("{")) { builder.append(parameter); } else { int customArgIndex = Integer.parseInt(parameter) - 1; if (customArgIndex < 0 || customArgIndex >= arguments.length) { throw new MissingFormatArgumentException("Argument " + customArgIndex); } builder.param(arguments[customArgIndex]); } } String lastPart = sql.substring(lastIndex); if (!lastPart.isEmpty()) { builder.append(lastPart); } }
private Command(String raw) { if (!raw.contains("$")) throw new MissingFormatArgumentException("<username> not found in \"" + raw + '"'); if (!raw.contains(CommandType.PREFIX) || !raw.contains(CommandType.SUFFIX)) throw new MissingFormatArgumentException("invalid command: \"" + raw + '"'); this.from = raw.substring(raw.indexOf('$') + 1, raw.lastIndexOf('$')); this.type = CommandType.getCommand(raw); this.to = StaticPlayerHelper.getName(Minecraft.getMinecraft()); this.args = getContent(raw); }