下面列出了怎么用java.util.MissingFormatArgumentException的API类实例代码及写法,或者点击链接到github查看源代码。
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);
}
}
}
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);
}
}
}
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);
}
}
}
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);
}
}
}
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);
}
}
}
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);
}
}
}
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);
}
}
}
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 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);
}
}
}
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);
}
}
}
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);
}
}
}
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);
}
}
}
/**
* 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;
}
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);
}
}
}
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 String safeFormat(String format, Object... args) {
try {
return String.format(format, args);
} catch (MissingFormatArgumentException e) {
LOGGER.warn("Exception while trying to format the message. Falling back by using the format string.", e);
return format;
}
}
private static void checkInvalidVoiceInstructions(RouteOptions routeOptions) {
Boolean instructions = routeOptions.voiceInstructions();
boolean invalidVoiceInstructions = instructions == null
|| !instructions;
if (invalidVoiceInstructions) {
throw new MissingFormatArgumentException("Using the default milestones requires the "
+ "directions route to be requested with voice instructions enabled.");
}
}
private static void checkInvalidBannerInstructions(RouteOptions routeOptions) {
Boolean instructions = routeOptions.bannerInstructions();
boolean invalidBannerInstructions = instructions == null || !instructions;
if (invalidBannerInstructions) {
throw new MissingFormatArgumentException("Using the default milestones requires the "
+ "directions route to be requested with banner instructions enabled.");
}
}
@Test(expected = MissingFormatArgumentException.class)
public void validDirectionsRoute_isInvalidWithNullRouteOptions() throws Exception {
DirectionsRoute route = buildTestDirectionsRoute(DIRECTIONS_WITHOUT_VOICE_INSTRUCTIONS);
RouteOptions invalidRouteOptions = null;
route = route.toBuilder().routeOptions(invalidRouteOptions).build();
ValidationUtils.validDirectionsRoute(route, true);
}
@Test
public void formatArgumentMismatchTest() {
StringFormat stringFormat = new StringFormat("This %s is %s");
assertEquals(stringFormat.getFormat(), "This %s is %s");
assertEquals(stringFormat.getAttributeNames(), new String[]{});
assertEquals(stringFormat.getDescription(), "Apply transformation context attributes [] to 'This %s is %s'");
TUExecutionResult executionResult = stringFormat.execution(transformedAppFolder, transformationContext);
assertEquals(executionResult.getType(), TUExecutionResult.Type.ERROR);
assertNull(executionResult.getValue());
assertEquals(executionResult.getException().getClass(), TransformationUtilityException.class);
assertEquals(executionResult.getException().getMessage(), "String format and arguments don't match");
assertEquals(executionResult.getException().getCause().getClass(), MissingFormatArgumentException.class);
assertEquals(executionResult.getException().getCause().getMessage(), "Format specifier '%s'");
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}