use of com.sun.source.tree.ExpressionTree in project error-prone by google.
the class ElementsCountedInLoop method matchEnhancedForLoop.
@Override
public Description matchEnhancedForLoop(EnhancedForLoopTree tree, VisitorState state) {
JCEnhancedForLoop enhancedForLoop = (JCEnhancedForLoop) tree;
IdentifierTree identifier = getIncrementedIdentifer(extractSingleStatement(enhancedForLoop.body));
if (identifier != null) {
ExpressionTree expression = tree.getExpression();
Fix fix;
if (isSubtypeOf("java.util.Collection").matches(expression, state)) {
String replacement = identifier + " += " + expression + ".size();";
fix = SuggestedFix.replace(tree, replacement);
} else if (isArrayType().matches(expression, state)) {
String replacement = identifier + " += " + expression + ".length;";
fix = SuggestedFix.replace(tree, replacement);
} else {
String replacement = identifier + " += Iterables.size(" + expression + ");";
fix = SuggestedFix.builder().replace(tree, replacement).addImport("com.google.common.collect.Iterables").build();
}
return describeMatch(tree, fix);
}
return Description.NO_MATCH;
}
use of com.sun.source.tree.ExpressionTree in project error-prone by google.
the class ModifyingCollectionWithItself method describe.
/**
* We expect that the lhs is a field and the rhs is an identifier, specifically a parameter to the
* method. We base our suggested fixes on this expectation.
*
* <p>Case 1: If lhs is a field and rhs is an identifier, find a method parameter of the same type
* and similar name and suggest it as the rhs. (Guess that they have misspelled the identifier.)
*
* <p>Case 2: If lhs is a field and rhs is not an identifier, find a method parameter of the same
* type and similar name and suggest it as the rhs.
*
* <p>Case 3: If lhs is not a field and rhs is an identifier, find a class field of the same type
* and similar name and suggest it as the lhs.
*
* <p>Case 4: Otherwise replace with literal meaning of functionality
*/
private Description describe(MethodInvocationTree methodInvocationTree, VisitorState state) {
ExpressionTree receiver = ASTHelpers.getReceiver(methodInvocationTree);
List<? extends ExpressionTree> arguments = methodInvocationTree.getArguments();
ExpressionTree argument;
// .addAll(int, Collection); for the true case
argument = arguments.size() == 2 ? arguments.get(1) : arguments.get(0);
Description.Builder builder = buildDescription(methodInvocationTree);
for (Fix fix : buildFixes(methodInvocationTree, state, receiver, argument)) {
builder.addFix(fix);
}
return builder.build();
}
use of com.sun.source.tree.ExpressionTree in project error-prone by google.
the class CompileTimeConstantChecker method matchArguments.
/**
* Matches formal parameters with
* {@link com.google.errorprone.annotations.CompileTimeConstant} annotations against
* corresponding actual parameters.
*
* @param state the visitor state
* @param calleeSymbol the method whose formal parameters to consider
* @param actualParams the list of actual parameters
* @return a {@code Description} of the match <i>iff</i> for any of the actual parameters that
* is annotated with {@link com.google.errorprone.annotations.CompileTimeConstant}, the
* corresponding formal parameter is not a compile-time-constant expression in the sense of
* {@link CompileTimeConstantExpressionMatcher}. Otherwise returns {@code Description.NO_MATCH}.
*/
private Description matchArguments(VisitorState state, final Symbol.MethodSymbol calleeSymbol, Iterator<? extends ExpressionTree> actualParams) {
Symbol.VarSymbol lastFormalParam = null;
for (Symbol.VarSymbol formalParam : calleeSymbol.getParameters()) {
lastFormalParam = formalParam;
// be a non-constant parameter for a @CompileTimeConstant formal.
if (!actualParams.hasNext()) {
return Description.NO_MATCH;
}
ExpressionTree actualParam = actualParams.next();
if (hasCompileTimeConstantAnnotation(state, formalParam)) {
if (!compileTimeConstExpressionMatcher.matches(actualParam, state)) {
return handleMatch(actualParam, state);
}
}
}
// we need to check the remaining args as well.
if (lastFormalParam == null || (lastFormalParam.flags() & Flags.VARARGS) == 0) {
return Description.NO_MATCH;
}
if (!hasCompileTimeConstantAnnotation(state, lastFormalParam)) {
return Description.NO_MATCH;
}
while (actualParams.hasNext()) {
ExpressionTree actualParam = actualParams.next();
if (!compileTimeConstExpressionMatcher.matches(actualParam, state)) {
return handleMatch(actualParam, state);
}
}
return Description.NO_MATCH;
}
use of com.sun.source.tree.ExpressionTree in project error-prone by google.
the class DefaultCharset method matchMethodInvocation.
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (state.isAndroidCompatible()) {
return NO_MATCH;
}
if (STRING_GET_BYTES.matches(tree, state)) {
Description.Builder description = buildDescription(tree);
Tree parent = state.getPath().getParentPath().getLeaf();
if (parent instanceof ExpressionTree && BYTESTRING_COPY_FROM.matches((ExpressionTree) parent, state)) {
byteStringFixes(description, tree, (ExpressionTree) parent, state);
} else {
appendCharsets(description, tree, tree.getMethodSelect(), tree.getArguments(), state);
}
return description.build();
}
if (FILE_NEW_WRITER.matches(tree, state)) {
Description.Builder description = buildDescription(tree);
appendCharsets(description, tree, tree.getMethodSelect(), tree.getArguments(), state);
return description.build();
}
return NO_MATCH;
}
use of com.sun.source.tree.ExpressionTree in project error-prone by google.
the class PreconditionsCheckNotNull method matchMethodInvocation.
@Override
public Description matchMethodInvocation(MethodInvocationTree methodInvocationTree, VisitorState state) {
if (!matcher.matches(methodInvocationTree, state)) {
return Description.NO_MATCH;
}
List<? extends ExpressionTree> arguments = methodInvocationTree.getArguments();
ExpressionTree stringLiteralValue = arguments.get(0);
Fix fix;
if (arguments.size() == 2) {
fix = SuggestedFix.swap(arguments.get(0), arguments.get(1));
} else {
fix = SuggestedFix.delete(state.getPath().getParentPath().getLeaf());
}
return describeMatch(stringLiteralValue, fix);
}
Aggregations