Java源码示例:com.google.errorprone.annotations.FormatString
示例1
@FormatMethod
private void problem(
Severity severity,
int lineNumber,
String filePath,
@FormatString String detailMessage,
Object... args) {
String message = args.length == 0 ? detailMessage : String.format(detailMessage, args);
problemsBySeverity.put(
severity,
String.format(
"%s:%s:%s: %s",
severity.getMessagePrefix(),
filePath.substring(filePath.lastIndexOf('/') + 1),
lineNumber,
message));
}
示例2
/**
* Sets the progress message. The string is lazily evaluated.
*
* @param progressMessage The message to display
* @param subject0 Passed to {@link String#format}
* @param subject1 Passed to {@link String#format}
* @param subject2 Passed to {@link String#format}
* @param subject3 Passed to {@link String#format}
*/
@FormatMethod
public Builder setProgressMessage(
@FormatString String progressMessage,
Object subject0,
Object subject1,
Object subject2,
Object subject3) {
return setProgressMessage(
new LazyString() {
@Override
public String toString() {
return String.format(progressMessage, subject0, subject1, subject2, subject3);
}
});
}
示例3
@FormatMethod
private static String format(final @FormatString String message, final Object... arguments ) {
final String messageToUse = message == null ? StringConstants.EMPTY_STRING : message;
if (arguments == null || arguments.length == 0) {
return messageToUse;
}
return String.format(messageToUse, arguments);
}
示例4
@FormatMethod
public void debug(@FormatString String message, Object... args) {
if (!isDebugEnabled()) {
return;
}
kLogger.debug(format(message, args));
}
示例5
@FormatMethod
public void debug(Throwable throwable, @FormatString String message, Object... args) {
if (!isDebugEnabled()) {
return;
}
kLogger.debug(format(message, args), throwable);
}
示例6
@FormatMethod
public void trace(@FormatString String message, Object... args) {
if (!isTraceEnabled()) {
return;
}
kLogger.trace(format(message, args));
}
示例7
@FormatMethod
public void trace(Throwable throwable, @FormatString String message, Object... args) {
if (!isTraceEnabled()) {
return;
}
kLogger.trace(format(message, args), throwable);
}
示例8
/**
* Check a condition and throw {@link ValidationException} if false
*
* @throws ValidationException if {@code condition} is false
*/
@FormatMethod
public static void checkCondition(boolean condition, @FormatString String format, Object... args)
throws ValidationException {
if (!condition) {
// Don't try to format if there is no args. This allows strings like '%Fooooo'¡
if (args.length == 0) {
throw new ValidationException(format);
}
throw new ValidationException(String.format(format, args));
}
}
示例9
/** Checks a condition or throw {@link EvalException}. */
@FormatMethod
public static void check(boolean condition, @FormatString String format, Object... args)
throws EvalException {
if (!condition) {
throw Starlark.errorf(format, args);
}
}
示例10
@FormatMethod
protected static void logCompose(
final int depth, String methodName, @FormatString String format, Object... args) {
// Compute indentation.
char[] indentChars = new char[depth * 2];
Arrays.fill(indentChars, ' ');
String indent = new String(indentChars);
// Log message.
LogUtils.v(TAG, "%s%s() %s", indent, methodName, String.format(format, args));
}
示例11
@FormatMethod
private void logOrThrow(@FormatString String format, Object... parameters) {
if (isDebug()) {
throwError(format, parameters);
} else {
logError(format, parameters);
}
}
示例12
@FormatMethod
private void logOrThrow(@FormatString String format, Object... parameters) {
if (isDebug()) {
throwError(format, parameters);
} else {
logError(format, parameters);
}
}
示例13
@FormatMethod
private void stepLog(
Level level, @Nullable Throwable cause, @FormatString String fmt, Object... args) {
String msg = String.format(fmt, args);
String toLog = String.format("%s (#%d %s)", msg, id, desc());
logger.at(level).withCause(cause).log(toLog);
}
示例14
/**
* Helper method to print a debugging message, if the given {@link EventHandler} is not {@code
* null}.
*/
@FormatMethod
private static void debugMessage(
@Nullable EventHandler eventHandler, @FormatString String template, Object... args) {
if (eventHandler == null) {
return;
}
eventHandler.handle(Event.info("ToolchainResolution: " + String.format(template, args)));
}
示例15
/**
* Sets the progress message. The string is lazily evaluated.
*
* @param progressMessage The message to display
* @param subject Passed to {@link String#format}
*/
@FormatMethod
public Builder setProgressMessage(@FormatString String progressMessage, Object subject) {
return setProgressMessage(
new LazyString() {
@Override
public String toString() {
return String.format(progressMessage, subject);
}
});
}
示例16
/**
* Sets the progress message. The string is lazily evaluated.
*
* @param progressMessage The message to display
* @param subject0 Passed to {@link String#format}
* @param subject1 Passed to {@link String#format}
*/
@FormatMethod
public Builder setProgressMessage(
@FormatString String progressMessage, Object subject0, Object subject1) {
return setProgressMessage(
new LazyString() {
@Override
public String toString() {
return String.format(progressMessage, subject0, subject1);
}
});
}
示例17
/**
* Sets the progress message. The string is lazily evaluated.
*
* @param progressMessage The message to display
* @param subject0 Passed to {@link String#format}
* @param subject1 Passed to {@link String#format}
* @param subject2 Passed to {@link String#format}
*/
@FormatMethod
public Builder setProgressMessage(
@FormatString String progressMessage, Object subject0, Object subject1, Object subject2) {
return setProgressMessage(
new LazyString() {
@Override
public String toString() {
return String.format(progressMessage, subject0, subject1, subject2);
}
});
}
示例18
/** Calls {@link String#format} at command line expansion time. */
@FormatMethod
public Builder addFormatted(@FormatString String formatStr, Object... args) {
Preconditions.checkNotNull(formatStr);
FormatArg.push(arguments, formatStr, args);
return this;
}
示例19
@FormatMethod
private void validate(boolean condition, @FormatString String message, Object... args) {
if (!condition) {
throw InvalidBundleException.builder().withUserMessage(message, args).build();
}
}
示例20
/** Same as {@link #setDescription(String)} but allowing formatted string. */
@FormatMethod
Builder setDescription(@FormatString String description, Object... args) {
return setDescription(String.format(description, args));
}
示例21
@FormatMethod
public InternalExceptionBuilder<T> withInternalMessage(
@FormatString String message, Object... args) {
this.internalMessage = String.format(checkNotNull(message), args);
return this;
}
示例22
@FormatMethod
public UserExceptionBuilder<T> withUserMessage(@FormatString String message, Object... args) {
this.userMessage = String.format(checkNotNull(message), args);
return this;
}
示例23
@FormatMethod
public DeviceNotFoundException(@FormatString String message, Object... args) {
super(String.format(checkNotNull(message), args));
}
示例24
@FormatMethod
public void info(@FormatString String message, Object... args) {
kLogger.info(format(message, args));
}
示例25
@FormatMethod
public void info(Throwable throwable, @FormatString String message, Object... args) {
kLogger.info(format(message, args), throwable);
}
示例26
@FormatMethod
public void warn(@FormatString String message, Object... args) {
kLogger.warn(format(message, args));
}
示例27
@FormatMethod
public void warn(Throwable throwable, @FormatString String message, Object... args) {
kLogger.warn(format(message, args), throwable);
}
示例28
@FormatMethod
public void error(@FormatString String message, Object... args) {
kLogger.error(format(message, args));
}
示例29
@FormatMethod
public void error(Throwable throwable, @FormatString String message, Object... args) {
kLogger.error(format(message, args), throwable);
}
示例30
@FormatMethod
public void error(
SourcePosition sourcePosition, @FormatString String detailMessage, Object... args) {
problem(Severity.ERROR, sourcePosition, detailMessage, args);
}