Java Code Examples for com.google.errorprone.annotations.FormatMethod

The following examples show how to use com.google.errorprone.annotations.FormatMethod. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: j2cl   Source File: Problems.java    License: Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example 2
Source Project: bazel   Source File: SpawnAction.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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);
        }
      });
}
 
Example 3
Source Project: besu   Source File: Preconditions.java    License: Apache License 2.0 5 votes vote down vote up
@FormatMethod
public static void checkGuard(
    final boolean condition,
    final Function<String, ? extends RuntimeException> exceptionGenerator,
    final String template,
    final Object... variables) {
  if (!condition) {
    throw exceptionGenerator.apply(String.format(template, variables));
  }
}
 
Example 4
Source Project: bazel   Source File: SpawnAction.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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);
        }
      });
}
 
Example 5
Source Project: bundletool   Source File: DdmlibDevice.java    License: Apache License 2.0 5 votes vote down vote up
@FormatMethod
private void executeAndPrint(String commandFormat, String... args)
    throws TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException,
        IOException {
  String command = formatCommandWithArgs(commandFormat, args);
  lastOutputLine = null;
  out.println("ADB << " + command);
  // ExecuteShellCommand would only tell us about ADB errors, and NOT the actual shell commands
  // We need another way to check exit values of the commands we run.
  // By adding " && echo OK" we can make sure "OK" is printed if the cmd executed successfully.
  device.executeShellCommand(command + " && echo OK", receiver, timeout, TimeUnit.MILLISECONDS);
  if (!"OK".equals(lastOutputLine)) {
    throw new IOException("ADB command failed.");
  }
}
 
Example 6
Source Project: syndesis   Source File: KLog.java    License: Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 7
Source Project: bazel   Source File: CustomCommandLine.java    License: Apache License 2.0 5 votes vote down vote up
/** 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;
}
 
Example 8
Source Project: syndesis   Source File: KLog.java    License: Apache License 2.0 5 votes vote down vote up
@FormatMethod
public void debug(@FormatString String message, Object... args) {
    if (!isDebugEnabled()) {
        return;
    }
    kLogger.debug(format(message, args));
}
 
Example 9
Source Project: syndesis   Source File: KLog.java    License: Apache License 2.0 5 votes vote down vote up
@FormatMethod
public void trace(@FormatString String message, Object... args) {
    if (!isTraceEnabled()) {
        return;
    }
    kLogger.trace(format(message, args));
}
 
Example 10
Source Project: syndesis   Source File: KLog.java    License: Apache License 2.0 5 votes vote down vote up
@FormatMethod
public void trace(Throwable throwable, @FormatString String message, Object... args) {
    if (!isTraceEnabled()) {
        return;
    }
    kLogger.trace(format(message, args), throwable);
}
 
Example 11
Source Project: j2cl   Source File: Problems.java    License: Apache License 2.0 5 votes vote down vote up
@FormatMethod
private void problem(
    Severity severity, SourcePosition sourcePosition, String detailMessage, Object... args) {
  problem(
      severity,
      // SourcePosition lines are 0 based.
      sourcePosition.getStartFilePosition().getLine() + 1,
      sourcePosition.getFilePath(),
      detailMessage,
      args);
}
 
Example 12
Source Project: turbine   Source File: ClassReader.java    License: Apache License 2.0 5 votes vote down vote up
@FormatMethod
@CheckReturnValue
Error error(String format, Object... args) {
  StringBuilder sb = new StringBuilder();
  if (path != null) {
    sb.append(path).append(": ");
  }
  sb.append(String.format(format, args));
  return new AssertionError(sb.toString());
}
 
Example 13
Source Project: copybara   Source File: ValidationException.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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));
  }
}
 
Example 14
Source Project: copybara   Source File: SkylarkUtil.java    License: Apache License 2.0 5 votes vote down vote up
/** 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);
  }
}
 
Example 15
Source Project: talkback   Source File: ScreenFeedbackManager.java    License: Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example 16
Source Project: talkback   Source File: AccessibilityNode.java    License: Apache License 2.0 5 votes vote down vote up
@FormatMethod
private void logOrThrow(@FormatString String format, Object... parameters) {
  if (isDebug()) {
    throwError(format, parameters);
  } else {
    logError(format, parameters);
  }
}
 
Example 17
Source Project: talkback   Source File: AccessibilityWindow.java    License: Apache License 2.0 5 votes vote down vote up
@FormatMethod
private void logOrThrow(@FormatString String format, Object... parameters) {
  if (isDebug()) {
    throwError(format, parameters);
  } else {
    logError(format, parameters);
  }
}
 
Example 18
Source Project: caffeine   Source File: Caffeine.java    License: Apache License 2.0 5 votes vote down vote up
/** Ensures that the argument expression is true. */
@FormatMethod
static void requireArgument(boolean expression, String template, @Nullable Object... args) {
  if (!expression) {
    throw new IllegalArgumentException(String.format(template, args));
  }
}
 
Example 19
Source Project: caffeine   Source File: Caffeine.java    License: Apache License 2.0 5 votes vote down vote up
/** Ensures that the state expression is true. */
@FormatMethod
static void requireState(boolean expression, String template, @Nullable Object... args) {
  if (!expression) {
    throw new IllegalStateException(String.format(template, args));
  }
}
 
Example 20
Source Project: bazel   Source File: SpawnAction.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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);
        }
      });
}
 
Example 21
Source Project: bazel   Source File: Parser.java    License: Apache License 2.0 5 votes vote down vote up
@FormatMethod
private void reportError(int offset, String format, Object... args) {
  errorsCount++;
  // Limit the number of reported errors to avoid spamming output.
  if (errorsCount <= 5) {
    Location location = locs.getLocation(offset);
    errors.add(new SyntaxError(location, String.format(format, args)));
  }
}
 
Example 22
Source Project: bazel   Source File: LocalSpawnRunner.java    License: Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 23
Source Project: bazel   Source File: SingleToolchainResolutionFunction.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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)));
}
 
Example 24
Source Project: besu   Source File: Framer.java    License: Apache License 2.0 4 votes vote down vote up
@FormatMethod
private static FramingException error(final String s, final Object... params) {
  return new FramingException(String.format(s, params));
}
 
Example 25
Source Project: bundletool   Source File: AssetModuleFilesValidator.java    License: Apache License 2.0 4 votes vote down vote up
@FormatMethod
private void validate(boolean condition, @FormatString String message, Object... args) {
  if (!condition) {
    throw InvalidBundleException.builder().withUserMessage(message, args).build();
  }
}
 
Example 26
Source Project: bundletool   Source File: CommandHelp.java    License: Apache License 2.0 4 votes vote down vote up
/** Same as {@link #setShortDescription(String)} but allowing formatted string. */
@FormatMethod
Builder setShortDescription(String shortDescriptionFormat, Object... args) {
  return setShortDescription(String.format(shortDescriptionFormat, args));
}
 
Example 27
Source Project: bundletool   Source File: CommandHelp.java    License: Apache License 2.0 4 votes vote down vote up
/** Same as {@link #setDescription(String)} but allowing formatted string. */
@FormatMethod
Builder setDescription(@FormatString String description, Object... args) {
  return setDescription(String.format(description, args));
}
 
Example 28
Source Project: bundletool   Source File: DdmlibDevice.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Formats the command string, replacing %s argument placeholders with escaped and single quoted
 * args. This was made to work with Strings only, so format your args beforehand.
 */
@FormatMethod
static String formatCommandWithArgs(String command, String... args) {
  return String.format(
      command, Arrays.stream(args).map(RemoteCommandExecutor::escapeAndSingleQuote).toArray());
}
 
Example 29
Source Project: bundletool   Source File: InternalExceptionBuilder.java    License: Apache License 2.0 4 votes vote down vote up
@FormatMethod
public InternalExceptionBuilder<T> withInternalMessage(
    @FormatString String message, Object... args) {
  this.internalMessage = String.format(checkNotNull(message), args);
  return this;
}
 
Example 30
Source Project: bundletool   Source File: UserExceptionBuilder.java    License: Apache License 2.0 4 votes vote down vote up
@FormatMethod
public UserExceptionBuilder<T> withUserMessage(@FormatString String message, Object... args) {
  this.userMessage = String.format(checkNotNull(message), args);
  return this;
}