Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop various vacuous null checks #191

Merged
merged 1 commit into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.Tree;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import java.util.Optional;

/** A {@link BugChecker} which flags empty methods that seemingly can simply be deleted. */
Expand All @@ -47,8 +46,7 @@ public Description matchMethod(MethodTree tree, VisitorState state) {
return Description.NO_MATCH;
}

MethodSymbol sym = ASTHelpers.getSymbol(tree);
if (sym == null || ASTHelpers.methodCanBeOverridden(sym)) {
if (ASTHelpers.methodCanBeOverridden(ASTHelpers.getSymbol(tree))) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious: why not static import the methods of ASTHelpers? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I've been on the fence about that for a while. I guess I've held off so far that ASTHelpers provides a rather heterogeneous set of operations, that can not always be distinguished from local helper methods. 🤔

return Description.NO_MATCH;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ private static CharSequence getStaticImportSimpleName(Tree tree, VisitorState st
}

private static Optional<String> tryCanonicalizeMethodName(MethodTree tree) {
return Optional.ofNullable(ASTHelpers.getSymbol(tree))
.map(sym -> sym.getQualifiedName().toString())
return Optional.of(ASTHelpers.getSymbol(tree).getQualifiedName().toString())
.filter(name -> name.startsWith(TEST_PREFIX))
.map(name -> name.substring(TEST_PREFIX.length()))
.filter(not(String::isEmpty))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import static com.google.errorprone.matchers.Matchers.anyOf;
import static com.google.errorprone.matchers.method.MethodMatchers.instanceMethod;
import static com.google.errorprone.matchers.method.MethodMatchers.staticMethod;
import static java.util.function.Predicate.not;
import static java.util.stream.Collectors.joining;

import com.google.auto.service.AutoService;
Expand Down Expand Up @@ -84,16 +83,15 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState

private static Optional<Fix> attemptMethodInvocationReplacement(
MethodInvocationTree tree, Type cmpType, boolean isStatic, VisitorState state) {
return Optional.ofNullable(ASTHelpers.getSymbol(tree))
.map(methodSymbol -> methodSymbol.getSimpleName().toString())
.flatMap(
actualMethodName ->
Optional.of(getPreferredMethod(cmpType, isStatic, state))
.filter(not(actualMethodName::equals)))
.map(
preferredMethodName ->
prefixTypeArgumentsIfRelevant(preferredMethodName, tree, cmpType, state))
.map(preferredMethodName -> suggestFix(tree, preferredMethodName, state));
String actualMethodName = ASTHelpers.getSymbol(tree).getSimpleName().toString();
String preferredMethodName = getPreferredMethod(cmpType, isStatic, state);
if (actualMethodName.equals(preferredMethodName)) {
return Optional.empty();
}

return Optional.of(
suggestFix(
tree, prefixTypeArgumentsIfRelevant(preferredMethodName, tree, cmpType, state), state));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.io.UncheckedIOException;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Nullable;
import javax.tools.FileObject;
import javax.tools.JavaFileManager;
Expand Down Expand Up @@ -103,16 +102,10 @@ public Void visitClass(ClassTree node, Void v) {
}

private FileObject getOutputFile(TaskEvent taskEvent, ClassTree tree) throws IOException {
String packageName =
Optional.ofNullable(ASTHelpers.getSymbol(tree))
.map(ASTHelpers::enclosingPackage)
.map(PackageSymbol::toString)
.orElse("");
CharSequence className =
Optional.ofNullable(ASTHelpers.getSymbol(tree))
.map(RefasterRuleCompilerTaskListener::toSimpleFlatName)
.orElseGet(tree::getSimpleName);
String relativeName = className + ".refaster";
ClassSymbol symbol = ASTHelpers.getSymbol(tree);
PackageSymbol enclosingPackage = ASTHelpers.enclosingPackage(symbol);
String packageName = enclosingPackage == null ? "" : enclosingPackage.toString();
String relativeName = toSimpleFlatName(symbol) + ".refaster";

JavaFileManager fileManager = context.get(JavaFileManager.class);
return fileManager.getFileForOutput(
Expand Down