use of com.sun.source.tree.ExpressionTree in project error-prone by google.
the class StrictFormatStringValidation method validate.
@Nullable
public static ValidationResult validate(ExpressionTree formatStringTree, List<? extends ExpressionTree> args, VisitorState state) {
String formatStringValue = ASTHelpers.constValue(formatStringTree, String.class);
// so don't bother with annotations and just check if the parameters match the format string.
if (formatStringValue != 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);
}
}
use of com.sun.source.tree.ExpressionTree in project error-prone by google.
the class SizeGreaterThanOrEqualsZero method matchBinary.
@Override
public Description matchBinary(BinaryTree tree, VisitorState state) {
// Easy stuff: needs to be a binary expression of the form foo >= 0 or 0 <= foo
ExpressionType expressionType = isGreaterThanEqualToZero(tree);
if (expressionType == ExpressionType.MISMATCH) {
return Description.NO_MATCH;
}
ExpressionTree operand = expressionType == ExpressionType.GREATER_THAN_EQUAL ? tree.getLeftOperand() : tree.getRightOperand();
if (operand instanceof MethodInvocationTree) {
MethodInvocationTree callToSize = (MethodInvocationTree) operand;
if (INSTANCE_METHOD_MATCHER.matches(callToSize, state)) {
return provideReplacementForMethodInvocation(tree, callToSize, state, expressionType);
} else if (STATIC_METHOD_MATCHER.matches(callToSize, state)) {
return provideReplacementForStaticMethodInvocation(tree, callToSize, state, expressionType);
}
} else if (operand instanceof MemberSelectTree) {
if (ARRAY_LENGTH_MATCHER.matches((MemberSelectTree) operand, state)) {
return removeEqualsFromComparison(tree, state, expressionType);
}
}
return Description.NO_MATCH;
}
use of com.sun.source.tree.ExpressionTree in project error-prone by google.
the class SizeGreaterThanOrEqualsZero method provideReplacementForStaticMethodInvocation.
private Description provideReplacementForStaticMethodInvocation(BinaryTree tree, MethodInvocationTree callToSize, final VisitorState state, ExpressionType expressionType) {
ExpressionTree classToken = ASTHelpers.getReceiver(callToSize);
if (HAS_EMPTY_METHOD.matches(classToken, state)) {
List<CharSequence> argumentSourceValues = Lists.transform(callToSize.getArguments(), new Function<ExpressionTree, CharSequence>() {
@Override
public CharSequence apply(ExpressionTree expressionTree) {
return state.getSourceForNode((JCTree) expressionTree);
}
});
String argumentString = Joiner.on(',').join(argumentSourceValues);
return describeMatch(tree, SuggestedFix.replace(tree, "!" + state.getSourceForNode((JCTree) classToken) + ".isEmpty(" + argumentString + ")"));
} else {
return removeEqualsFromComparison(tree, state, expressionType);
}
}
use of com.sun.source.tree.ExpressionTree in project error-prone by google.
the class TruthConstantAsserts method matchMethodInvocation.
@Override
public Description matchMethodInvocation(MethodInvocationTree methodInvocationTree, VisitorState state) {
if (methodInvocationTree.getArguments().isEmpty()) {
return Description.NO_MATCH;
}
if (!TRUTH_SUBJECT_CALL.matches(methodInvocationTree, state)) {
return Description.NO_MATCH;
}
ExpressionTree rec = ASTHelpers.getReceiver(methodInvocationTree);
if (rec == null) {
return Description.NO_MATCH;
}
if (!ASSERT_THAT.matches(rec, state)) {
return Description.NO_MATCH;
}
ExpressionTree expr = getOnlyElement(((MethodInvocationTree) rec).getArguments());
if (expr == null) {
return Description.NO_MATCH;
}
// check that argument of assertThat is a constant
if (ASTHelpers.constValue(expr) == null) {
return Description.NO_MATCH;
}
// check that expectation isn't a constant
ExpressionTree expectation = getOnlyElement(methodInvocationTree.getArguments());
if (ASTHelpers.constValue(expectation) != null) {
return Description.NO_MATCH;
}
SuggestedFix fix = SuggestedFix.swap(expr, expectation);
return buildDescription(methodInvocationTree).addFix(fix).build();
}
use of com.sun.source.tree.ExpressionTree in project error-prone by google.
the class TruthSelfEquals method matchMethodInvocation.
@Override
public Description matchMethodInvocation(MethodInvocationTree methodInvocationTree, VisitorState state) {
if (methodInvocationTree.getArguments().isEmpty()) {
return Description.NO_MATCH;
}
Description.Builder description = buildDescription(methodInvocationTree);
ExpressionTree toReplace = methodInvocationTree.getArguments().get(0);
if (EQUALS_MATCHER.matches(methodInvocationTree, state)) {
description.setMessage(generateSummary(ASTHelpers.getSymbol(methodInvocationTree).getSimpleName().toString(), "passes")).addFix(suggestEqualsTesterFix(methodInvocationTree, toReplace));
} else if (NOT_EQUALS_MATCHER.matches(methodInvocationTree, state)) {
description.setMessage(generateSummary(ASTHelpers.getSymbol(methodInvocationTree).getSimpleName().toString(), "fails"));
} else {
return Description.NO_MATCH;
}
Fix fix = SelfEquals.fieldFix(toReplace, state);
if (fix != null) {
description.addFix(fix);
}
return description.build();
}
Aggregations