From 705247495bf36cce5f5f45832b618331146a25ac Mon Sep 17 00:00:00 2001 From: ghm Date: Tue, 11 Jun 2019 01:34:42 -0700 Subject: [PATCH] Migrate AbstractToString onto java.util.Optional ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=252572232 --- .../bugpatterns/AbstractToString.java | 4 +-- .../errorprone/bugpatterns/ArrayToString.java | 2 +- .../bugpatterns/LiteProtoToString.java | 6 ++-- .../bugpatterns/ObjectToString.java | 6 ++-- .../bugpatterns/StreamToString.java | 18 +++-------- .../errorprone/bugpatterns/TreeToString.java | 32 ++++++++----------- 6 files changed, 28 insertions(+), 40 deletions(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/AbstractToString.java b/core/src/main/java/com/google/errorprone/bugpatterns/AbstractToString.java index 508cf91a36d..c5b6225787a 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/AbstractToString.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/AbstractToString.java @@ -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; @@ -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 @@ -65,7 +65,7 @@ public abstract class AbstractToString extends BugChecker /** Adds the description message for match on the type without fixes. */ protected Optional descriptionMessageForDefaultMatch(Type type, VisitorState state) { - return Optional.absent(); + return Optional.empty(); } /** diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/ArrayToString.java b/core/src/main/java/com/google/errorprone/bugpatterns/ArrayToString.java index 06443de0dcf..8dc638a06f0 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/ArrayToString.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/ArrayToString.java @@ -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; @@ -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) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/LiteProtoToString.java b/core/src/main/java/com/google/errorprone/bugpatterns/LiteProtoToString.java index 4e3eb393a6c..b9f3198cc93 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/LiteProtoToString.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/LiteProtoToString.java @@ -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; @@ -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( @@ -89,11 +89,11 @@ protected Optional descriptionMessageForDefaultMatch(Type type, VisitorS @Override protected Optional implicitToStringFix(ExpressionTree tree, VisitorState state) { - return Optional.absent(); + return Optional.empty(); } @Override protected Optional toStringFix(Tree parent, ExpressionTree tree, VisitorState state) { - return Optional.absent(); + return Optional.empty(); } } diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/ObjectToString.java b/core/src/main/java/com/google/errorprone/bugpatterns/ObjectToString.java index 0469cd4e6c9..b7df3c59c98 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/ObjectToString.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/ObjectToString.java @@ -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; @@ -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 @@ -94,11 +94,11 @@ protected Optional descriptionMessageForDefaultMatch(Type type, VisitorS @Override protected Optional implicitToStringFix(ExpressionTree tree, VisitorState state) { - return Optional.absent(); + return Optional.empty(); } @Override protected Optional toStringFix(Tree parent, ExpressionTree tree, VisitorState state) { - return Optional.absent(); + return Optional.empty(); } } diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/StreamToString.java b/core/src/main/java/com/google/errorprone/bugpatterns/StreamToString.java index b8335c7ae8b..4ebe4fd59af 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/StreamToString.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/StreamToString.java @@ -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( @@ -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() { @@ -51,11 +43,11 @@ protected TypePredicate typePredicate() { @Override protected Optional implicitToStringFix(ExpressionTree tree, VisitorState state) { - return Optional.absent(); + return Optional.empty(); } @Override protected Optional toStringFix(Tree parent, ExpressionTree tree, VisitorState state) { - return Optional.absent(); + return Optional.empty(); } } diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/TreeToString.java b/core/src/main/java/com/google/errorprone/bugpatterns/TreeToString.java index 91c7f7993b7..07da8447b84 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/TreeToString.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/TreeToString.java @@ -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; @@ -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. @@ -84,30 +84,26 @@ protected Optional implicitToStringFix(ExpressionTree tree, VisitorState st @Override protected Optional 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(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)))); } }