use of com.google.errorprone.annotations.FormatString in project error-prone by google.
the class StrictFormatStringValidation method isFormatStringParameter.
/**
* Returns whether an input {@link Symbol} is a format string in a {@link FormatMethod}. This is
* true if the {@link Symbol} is a {@link String} parameter in a {@link FormatMethod} and is
* either:
*
* <ol>
* <li>Annotated with {@link FormatString}
* <li>The first {@link String} parameter in the method with no other parameters annotated
* {@link FormatString}.
* </ol>
*/
private static boolean isFormatStringParameter(Symbol formatString, VisitorState state) {
Type stringType = state.getSymtab().stringType;
// The input symbol must be a String and a parameter of a @FormatMethod to be a @FormatString.
if (!ASTHelpers.isSameType(formatString.type, stringType, state) || !(formatString.owner instanceof MethodSymbol) || !ASTHelpers.hasAnnotation(formatString.owner, FormatMethod.class, state)) {
return false;
}
// If the format string is annotated @FormatString in a @FormatMethod, it is a format string.
if (ASTHelpers.hasAnnotation(formatString, FormatString.class, state)) {
return true;
}
// Check if format string is the first string with no @FormatString params in the @FormatMethod.
MethodSymbol owner = (MethodSymbol) formatString.owner;
boolean formatStringFound = false;
for (Symbol param : owner.getParameters()) {
if (param == formatString) {
formatStringFound = true;
}
if (ASTHelpers.isSameType(param.type, stringType, state)) {
// format string since it wasn't annotated @FormatString.
if (!formatStringFound) {
return false;
} else if (ASTHelpers.hasAnnotation(param, FormatString.class, state)) {
return false;
}
}
}
return true;
}
use of com.google.errorprone.annotations.FormatString in project closure-compiler by google.
the class ErrorReporter method reportError.
@FormatMethod
public final void reportError(SourcePosition location, @FormatString String format, Object... arguments) {
hadError = true;
String message = SimpleFormat.format(format, arguments);
reportError(location, message);
}
use of com.google.errorprone.annotations.FormatString in project closure-compiler by google.
the class ErrorReporter method reportWarning.
@FormatMethod
public final void reportWarning(SourcePosition location, @FormatString String format, Object... arguments) {
String message = SimpleFormat.format(format, arguments);
reportWarning(location, message);
}
use of com.google.errorprone.annotations.FormatString in project error-prone by google.
the class StrictFormatStringValidation method validate.
@Nullable
public static ValidationResult validate(ExpressionTree formatStringTree, List<? extends ExpressionTree> args, VisitorState state) {
if (MOCKITO_ARGUMENT_MATCHER.matches(formatStringTree, state)) {
// that people can verify @FormatMethod methods.
return null;
}
Stream<String> formatStringValues = FormatStringValidation.constValues(formatStringTree);
// so don't bother with annotations and just check if the parameters match the format string.
if (formatStringValues != null) {
return FormatStringValidation.validate(ImmutableList.<ExpressionTree>builder().add(formatStringTree).addAll(args).build(), state);
}
// The format string is not a compile time constant. Check if it is an @FormatString method
// parameter or is in an @FormatMethod method.
Symbol formatStringSymbol = ASTHelpers.getSymbol(formatStringTree);
if (!(formatStringSymbol instanceof VarSymbol)) {
return ValidationResult.create(null, String.format("Format strings must be either a literal or a variable. Other expressions" + " are not valid.\n" + "Invalid format string: %s", formatStringTree));
}
if ((formatStringSymbol.flags() & (Flags.FINAL | Flags.EFFECTIVELY_FINAL)) == 0) {
return ValidationResult.create(null, "All variables passed as @FormatString must be final or effectively final");
}
if (formatStringSymbol.getKind() == ElementKind.PARAMETER) {
return validateFormatStringParamter(formatStringTree, formatStringSymbol, args, state);
} else {
// works with the format arguments.
return validateFormatStringVariable(formatStringTree, formatStringSymbol, args, state);
}
}
Aggregations