Skip to content

Commit

Permalink
Migrate AbstractToString onto java.util.Optional
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=252572232
  • Loading branch information
graememorgan authored and ronshapiro committed Jun 12, 2019
1 parent 0fbefb0 commit 7052474
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import static com.google.errorprone.matchers.Matchers.instanceMethod;
import static com.google.errorprone.matchers.Matchers.staticMethod;

import com.google.common.base.Optional;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker.IdentifierTreeMatcher;
import com.google.errorprone.bugpatterns.BugChecker.MemberSelectTreeMatcher;
Expand All @@ -42,6 +41,7 @@
import com.sun.tools.javac.code.Symbol.VarSymbol;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.Type.MethodType;
import java.util.Optional;

/**
* An abstract matcher for implicit and explicit calls to {@code Object.toString()}, for use on
Expand All @@ -65,7 +65,7 @@ public abstract class AbstractToString extends BugChecker

/** Adds the description message for match on the type without fixes. */
protected Optional<String> descriptionMessageForDefaultMatch(Type type, VisitorState state) {
return Optional.absent();
return Optional.empty();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static com.google.errorprone.BugPattern.SeverityLevel.ERROR;
import static com.google.errorprone.matchers.method.MethodMatchers.instanceMethod;

import com.google.common.base.Optional;
import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
import com.google.errorprone.fixes.Fix;
Expand All @@ -30,6 +29,7 @@
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.Tree;
import java.util.Optional;

/**
* @author adgar@google.com (Mike Edgar)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import static com.google.errorprone.util.ASTHelpers.getReceiver;
import static com.google.errorprone.util.ASTHelpers.getSymbol;

import com.google.common.base.Optional;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams;
import com.google.errorprone.BugPattern;
Expand All @@ -35,6 +34,7 @@
import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.tree.Tree;
import com.sun.tools.javac.code.Type;
import java.util.Optional;

/** Flags calls to {@code toString} on lite protos. */
@BugPattern(
Expand Down Expand Up @@ -89,11 +89,11 @@ protected Optional<String> descriptionMessageForDefaultMatch(Type type, VisitorS

@Override
protected Optional<Fix> implicitToStringFix(ExpressionTree tree, VisitorState state) {
return Optional.absent();
return Optional.empty();
}

@Override
protected Optional<Fix> toStringFix(Tree parent, ExpressionTree tree, VisitorState state) {
return Optional.absent();
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;

import com.google.common.base.Optional;
import com.google.common.collect.Iterables;
import com.google.errorprone.BugPattern;
import com.google.errorprone.BugPattern.ProvidesFix;
Expand All @@ -32,6 +31,7 @@
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.Types;
import com.sun.tools.javac.util.Names;
import java.util.Optional;

/**
* Warns against calling toString() on Objects which don't have toString() method overridden and
Expand Down Expand Up @@ -94,11 +94,11 @@ protected Optional<String> descriptionMessageForDefaultMatch(Type type, VisitorS

@Override
protected Optional<Fix> implicitToStringFix(ExpressionTree tree, VisitorState state) {
return Optional.absent();
return Optional.empty();
}

@Override
protected Optional<Fix> toStringFix(Tree parent, ExpressionTree tree, VisitorState state) {
return Optional.absent();
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@
package com.google.errorprone.bugpatterns;

import static com.google.errorprone.BugPattern.SeverityLevel.ERROR;
import static com.google.errorprone.predicates.TypePredicates.isDescendantOf;

import com.google.common.base.Optional;
import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
import com.google.errorprone.fixes.Fix;
import com.google.errorprone.predicates.TypePredicate;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.Tree;
import com.sun.tools.javac.code.Type;
import java.util.Optional;

/** @author cushon@google.com (Liam Miller-Cushon) */
@BugPattern(
Expand All @@ -35,14 +34,7 @@
severity = ERROR)
public class StreamToString extends AbstractToString {

static final TypePredicate STREAM =
new TypePredicate() {
@Override
public boolean apply(Type type, VisitorState state) {
Type stream = state.getTypeFromString("java.util.stream.Stream");
return ASTHelpers.isSubtype(type, stream, state);
}
};
private static final TypePredicate STREAM = isDescendantOf("java.util.stream.Stream");

@Override
protected TypePredicate typePredicate() {
Expand All @@ -51,11 +43,11 @@ protected TypePredicate typePredicate() {

@Override
protected Optional<Fix> implicitToStringFix(ExpressionTree tree, VisitorState state) {
return Optional.absent();
return Optional.empty();
}

@Override
protected Optional<Fix> toStringFix(Tree parent, ExpressionTree tree, VisitorState state) {
return Optional.absent();
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import static com.google.errorprone.util.ASTHelpers.getReceiver;
import static com.google.errorprone.util.ASTHelpers.isSubtype;

import com.google.common.base.Optional;
import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
import com.google.errorprone.fixes.Fix;
Expand All @@ -37,6 +36,7 @@
import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.tree.Tree;
import com.sun.tools.javac.code.Type;
import java.util.Optional;

/**
* Flags {@code com.sun.source.tree.Tree#toString} usage in {@link BugChecker}s.
Expand Down Expand Up @@ -84,30 +84,26 @@ protected Optional<Fix> implicitToStringFix(ExpressionTree tree, VisitorState st
@Override
protected Optional<Fix> toStringFix(Tree parent, ExpressionTree tree, VisitorState state) {
if (!(parent instanceof MethodInvocationTree)) {
return Optional.absent();
return Optional.empty();
}
ExpressionTree receiver = getReceiver((ExpressionTree) parent);
if (receiver == null) {
return Optional.absent();
return Optional.empty();
}
return fix(receiver, parent, state);
}

private static Optional<Fix> fix(Tree target, Tree replace, VisitorState state) {
return Optional.fromJavaUtil(
FindIdentifiers.findAllIdents(state).stream()
.filter(
s ->
isSubtype(
s.type,
state.getTypeFromString("com.google.errorprone.VisitorState"),
state))
.findFirst()
.map(
s ->
SuggestedFix.replace(
replace,
String.format(
"%s.getSourceForNode(%s)", s, state.getSourceForNode(target)))));
return FindIdentifiers.findAllIdents(state).stream()
.filter(
s ->
isSubtype(
s.type, state.getTypeFromString("com.google.errorprone.VisitorState"), state))
.findFirst()
.map(
s ->
SuggestedFix.replace(
replace,
String.format("%s.getSourceForNode(%s)", s, state.getSourceForNode(target))));
}
}

0 comments on commit 7052474

Please sign in to comment.