use of com.google.errorprone.matchers.Description in project error-prone by google.
the class AssertionFailureIgnored method matchMethodInvocation.
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (!ASSERTION.matches(tree, state)) {
return NO_MATCH;
}
JCTry tryStatement = enclosingTry(state);
if (tryStatement == null) {
return NO_MATCH;
}
Optional<JCCatch> maybeCatchTree = catchesType(tryStatement, state.getSymtab().assertionErrorType, state);
if (!maybeCatchTree.isPresent()) {
return NO_MATCH;
}
JCCatch catchTree = maybeCatchTree.get();
VarSymbol parameter = ASTHelpers.getSymbol(catchTree.getParameter());
boolean rethrows = firstNonNull(new TreeScanner<Boolean, Void>() {
@Override
public Boolean visitThrow(ThrowTree tree, Void unused) {
if (Objects.equals(parameter, ASTHelpers.getSymbol(tree.getExpression()))) {
return true;
}
if (NEW_THROWABLE.matches(tree.getExpression(), state) && ((NewClassTree) tree.getExpression()).getArguments().stream().anyMatch(arg -> Objects.equals(parameter, ASTHelpers.getSymbol(arg)))) {
return true;
}
return super.visitThrow(tree, null);
}
@Override
public Boolean reduce(Boolean a, Boolean b) {
return firstNonNull(a, false) || firstNonNull(b, false);
}
}.scan(catchTree.getBlock(), null), false);
if (rethrows) {
return NO_MATCH;
}
Description.Builder description = buildDescription(tree);
buildFix(tryStatement, tree, state).ifPresent(description::addFix);
return description.build();
}
use of com.google.errorprone.matchers.Description in project error-prone by google.
the class InconsistentCapitalization method matchClass.
@Override
public Description matchClass(ClassTree tree, VisitorState state) {
ImmutableSet<Symbol> fields = FieldScanner.findFields(tree);
if (fields.isEmpty()) {
return Description.NO_MATCH;
}
ImmutableMap<String, Symbol> fieldNamesMap = fields.stream().collect(toImmutableMap(symbol -> symbol.toString().toLowerCase(), identity()));
ImmutableMap<TreePath, Symbol> matchedParameters = MatchingParametersScanner.findMatchingParameters(fieldNamesMap, state.getPath());
if (matchedParameters.isEmpty()) {
return Description.NO_MATCH;
}
for (Entry<TreePath, Symbol> entry : matchedParameters.entrySet()) {
TreePath parameterPath = entry.getKey();
Symbol field = entry.getValue();
String fieldName = field.getSimpleName().toString();
VariableTree parameterTree = (VariableTree) parameterPath.getLeaf();
SuggestedFix.Builder fix = SuggestedFix.builder().merge(SuggestedFixes.renameVariable(parameterTree, fieldName, state));
if (parameterPath.getParentPath() != null) {
String qualifiedName = getExplicitQualification(parameterPath, tree, state) + field.getSimpleName();
// If the field was accessed in a non-qualified way, by renaming the parameter this may
// cause clashes with it. Thus, it is required to qualify all uses of the field within the
// parameter's scope just in case.
parameterPath.getParentPath().getLeaf().accept(new TreeScanner<Void, Void>() {
@Override
public Void visitIdentifier(IdentifierTree tree, Void unused) {
if (field.equals(ASTHelpers.getSymbol(tree))) {
fix.replace(tree, qualifiedName);
}
return null;
}
}, null);
}
state.reportMatch(buildDescription(parameterPath.getLeaf()).setMessage(String.format("Found the field '%s' with the same name as the parameter '%s' but with " + "different capitalization.", fieldName, ((VariableTree) parameterPath.getLeaf()).getName())).addFix(fix.build()).build());
}
return Description.NO_MATCH;
}
use of com.google.errorprone.matchers.Description in project error-prone by google.
the class IncrementInForLoopAndHeader method matchForLoop.
@Override
public Description matchForLoop(ForLoopTree forLoopTree, VisitorState visitorState) {
List<? extends ExpressionStatementTree> updates = forLoopTree.getUpdate();
// keep track of all the symbols that are updated in the for loop header
final Set<Symbol> incrementedSymbols = updates.stream().filter(expStateTree -> expStateTree.getExpression() instanceof UnaryTree).map(expStateTree -> ASTHelpers.getSymbol(((UnaryTree) expStateTree.getExpression()).getExpression())).collect(Collectors.toCollection(HashSet::new));
// track if they are updated in the body without a conditional surrounding them
StatementTree body = forLoopTree.getStatement();
List<? extends StatementTree> statementTrees = body instanceof BlockTree ? ((BlockTree) body).getStatements() : ImmutableList.of(body);
for (StatementTree s : statementTrees) {
if (!CONDITIONALS.contains(s.getKind())) {
Optional<Symbol> opSymbol = returnUnarySym(s);
if (opSymbol.isPresent() && incrementedSymbols.contains(opSymbol.get())) {
// both ++ and --
return describeMatch(forLoopTree);
}
}
}
return Description.NO_MATCH;
}
use of com.google.errorprone.matchers.Description in project error-prone by google.
the class DoubleBraceInitialization method matchNewClass.
@Override
public Description matchNewClass(NewClassTree tree, VisitorState state) {
ClassTree body = tree.getClassBody();
if (body == null) {
return NO_MATCH;
}
ImmutableList<? extends Tree> members = body.getMembers().stream().filter(m -> !(m instanceof MethodTree && ASTHelpers.isGeneratedConstructor((MethodTree) m))).collect(toImmutableList());
if (members.size() != 1) {
return NO_MATCH;
}
Tree member = Iterables.getOnlyElement(members);
if (!(member instanceof BlockTree)) {
return NO_MATCH;
}
BlockTree block = (BlockTree) member;
Optional<CollectionTypes> collectionType = Stream.of(CollectionTypes.values()).filter(type -> type.constructorMatcher.matches(tree, state)).findFirst();
if (!collectionType.isPresent()) {
return NO_MATCH;
}
Description.Builder description = buildDescription(tree);
collectionType.get().maybeFix(tree, state, block).ifPresent(description::addFix);
return description.build();
}
use of com.google.errorprone.matchers.Description in project error-prone by google.
the class JdkObsolete method matchNewClass.
@Override
public Description matchNewClass(NewClassTree tree, VisitorState state) {
MethodSymbol constructor = ASTHelpers.getSymbol(tree);
if (constructor == null) {
return NO_MATCH;
}
Symbol owner = constructor.owner;
Description description = describeIfObsolete(// don't refactor anonymous implementations of LinkedList
tree.getClassBody() == null ? tree.getIdentifier() : null, owner.name.isEmpty() ? state.getTypes().directSupertypes(owner.asType()) : ImmutableList.of(owner.asType()), state);
if (description == NO_MATCH) {
return NO_MATCH;
}
if (owner.getQualifiedName().contentEquals("java.lang.StringBuffer")) {
boolean[] found = { false };
new TreeScanner<Void, Void>() {
@Override
public Void visitMethodInvocation(MethodInvocationTree tree, Void unused) {
if (MATCHER_STRINGBUFFER.matches(tree, state)) {
found[0] = true;
}
return null;
}
}.scan(state.getPath().getCompilationUnit(), null);
if (found[0]) {
return NO_MATCH;
}
}
return description;
}
Aggregations