use of com.sun.source.tree.ExpressionTree in project error-prone by google.
the class FormatStringValidation method validate.
@Nullable
public static ValidationResult validate(Collection<? extends ExpressionTree> arguments, final VisitorState state) {
Deque<ExpressionTree> args = new ArrayDeque<ExpressionTree>(arguments);
String formatString = ASTHelpers.constValue(args.removeFirst(), String.class);
if (formatString == null) {
return null;
}
// Bail out, since we don't know what the actual argument types are.
if (args.size() == 1) {
Type type = ASTHelpers.getType(Iterables.getOnlyElement(args));
if (type instanceof Type.ArrayType && ASTHelpers.isSameType(((Type.ArrayType) type).elemtype, state.getSymtab().objectType, state)) {
return null;
}
}
Iterable<Object> instances = Iterables.transform(args, new Function<ExpressionTree, Object>() {
@Override
public Object apply(ExpressionTree input) {
try {
return getInstance(input, state);
} catch (Throwable t) {
// ignore symbol completion failures
return null;
}
}
});
return validate(formatString, instances);
}
use of com.sun.source.tree.ExpressionTree in project error-prone by google.
the class StrictFormatStringValidation method validateFormatStringParamter.
/** Helps {@code validate()} validate a format string that is declared as a method parameter. */
private static ValidationResult validateFormatStringParamter(ExpressionTree formatStringTree, Symbol formatStringSymbol, List<? extends ExpressionTree> args, VisitorState state) {
if (!isFormatStringParameter(formatStringSymbol, state)) {
return ValidationResult.create(null, String.format("Format strings must be compile time constant or parameters annotated " + "@FormatString: %s", formatStringTree));
}
List<VarSymbol> ownerParams = ((MethodSymbol) formatStringSymbol.owner).getParameters();
int ownerFormatStringIndex = ownerParams.indexOf(formatStringSymbol);
ImmutableList.Builder<Type> ownerFormatArgTypesBuilder = ImmutableList.builder();
for (VarSymbol paramSymbol : ownerParams.subList(ownerFormatStringIndex + 1, ownerParams.size())) {
ownerFormatArgTypesBuilder.add(paramSymbol.type);
}
ImmutableList<Type> ownerFormatArgTypes = ownerFormatArgTypesBuilder.build();
Types types = state.getTypes();
ImmutableList.Builder<Type> calleeFormatArgTypesBuilder = ImmutableList.builder();
for (ExpressionTree formatArgExpression : args) {
calleeFormatArgTypesBuilder.add(types.erasure(((JCExpression) formatArgExpression).type));
}
ImmutableList<Type> calleeFormatArgTypes = calleeFormatArgTypesBuilder.build();
if (ownerFormatArgTypes.size() != calleeFormatArgTypes.size()) {
return ValidationResult.create(null, String.format("The number of format arguments passed " + "with an @FormatString must match the number of format arguments in the " + "@FormatMethod header where the format string was declared.\n\t" + "Format args passed: %d\n\tFormat args expected: %d", calleeFormatArgTypes.size(), ownerFormatArgTypes.size()));
} else {
for (int i = 0; i < calleeFormatArgTypes.size(); i++) {
if (!ASTHelpers.isSameType(ownerFormatArgTypes.get(i), calleeFormatArgTypes.get(i), state)) {
return ValidationResult.create(null, String.format("The format argument types passed " + "with an @FormatString must match the types of the format arguments in " + "the @FormatMethod header where the format string was declared.\n\t" + "Format arg types passed: %s\n\tFormat arg types expected: %s", calleeFormatArgTypes.toArray(), ownerFormatArgTypes.toArray()));
}
}
}
// Format string usage was valid.
return null;
}
use of com.sun.source.tree.ExpressionTree in project error-prone by google.
the class SizeGreaterThanOrEqualsZero method isGreaterThanEqualToZero.
private ExpressionType isGreaterThanEqualToZero(BinaryTree tree) {
ExpressionTree literalOperand;
ExpressionType returnType;
switch(tree.getKind()) {
case GREATER_THAN_EQUAL:
literalOperand = tree.getRightOperand();
returnType = ExpressionType.GREATER_THAN_EQUAL;
break;
case LESS_THAN_EQUAL:
literalOperand = tree.getLeftOperand();
returnType = ExpressionType.LESS_THAN_EQUAL;
break;
default:
return ExpressionType.MISMATCH;
}
if (literalOperand.getKind() != Kind.INT_LITERAL) {
return ExpressionType.MISMATCH;
}
if (!((LiteralTree) literalOperand).getValue().equals(0)) {
return ExpressionType.MISMATCH;
}
return returnType;
}
use of com.sun.source.tree.ExpressionTree in project error-prone by google.
the class StringBuilderInitWithChar method matchNewClass.
@Override
public Description matchNewClass(NewClassTree tree, VisitorState state) {
if (ASTHelpers.isSameType(state.getSymtab().stringBuilderType, ASTHelpers.getType(tree.getIdentifier()), state) && tree.getArguments().size() == 1) {
ExpressionTree argument = tree.getArguments().get(0);
Type type = ((JCTree) argument).type;
if (type.getKind() == TypeKind.CHAR) {
if (argument.getKind() == Kind.CHAR_LITERAL) {
char ch = (Character) ((LiteralTree) argument).getValue();
return describeMatch(tree, SuggestedFix.replace(argument, "\"" + Convert.quote(Character.toString(ch)) + "\""));
} else {
return describeMatch(tree, SuggestedFix.replace(tree, "new StringBuilder().append(" + state.getSourceForNode((JCTree) argument) + ")"));
}
}
}
return Description.NO_MATCH;
}
use of com.sun.source.tree.ExpressionTree in project error-prone by google.
the class TryFailThrowable method getMessageSnippet.
private static String getMessageSnippet(StatementTree failStatement, VisitorState state, HasOtherParameters hasOtherParameters) {
ExpressionTree expression = ((ExpressionStatementTree) failStatement).getExpression();
MethodSymbol sym = (MethodSymbol) getSymbol(expression);
String tail = hasOtherParameters == HasOtherParameters.TRUE ? ", " : "";
// The above casts were checked earlier by failOrAssert.
return hasInitialStringParameter(sym, state) ? state.getSourceForNode(((MethodInvocationTree) expression).getArguments().get(0)) + tail : "";
}
Aggregations