Skip to content

Commit

Permalink
Use JDK 17 features
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 committed Mar 23, 2024
1 parent 4bf62f0 commit fd98b84
Showing 1 changed file with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
@AutoService(BugChecker.class)
@BugPattern(
summary =
"Prefer `Optional#orElseGet` over `Optional#orElse` if the fallback requires additional computation",
"""
Prefer `Optional#orElseGet` over `Optional#orElse` if the fallback requires additional \
computation""",
linkType = NONE,
severity = WARNING,
tags = PERFORMANCE)
Expand Down Expand Up @@ -96,40 +98,38 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
private static boolean requiresComputation(ExpressionTree tree) {
return !(tree instanceof IdentifierTree
|| tree instanceof LiteralTree
|| (tree instanceof MemberSelectTree
&& !requiresComputation(((MemberSelectTree) tree).getExpression()))
|| (tree instanceof MemberSelectTree memberSelect
&& !requiresComputation(memberSelect.getExpression()))
|| ASTHelpers.constValue(tree) != null);
}

/** Returns the nullary method reference matching the given expression, if any. */
private static Optional<String> tryMethodReferenceConversion(
ExpressionTree tree, VisitorState state) {
if (!(tree instanceof MethodInvocationTree)) {
if (!(tree instanceof MethodInvocationTree methodInvocation)) {
return Optional.empty();
}

MethodInvocationTree invocation = (MethodInvocationTree) tree;
if (!invocation.getArguments().isEmpty()) {
if (!methodInvocation.getArguments().isEmpty()) {
return Optional.empty();
}

if (!(invocation.getMethodSelect() instanceof MemberSelectTree)) {
if (!(methodInvocation.getMethodSelect() instanceof MemberSelectTree memberSelect)) {
return Optional.empty();
}

MemberSelectTree method = (MemberSelectTree) invocation.getMethodSelect();
if (requiresComputation(method.getExpression())) {
if (requiresComputation(memberSelect.getExpression())) {
return Optional.empty();
}

return Optional.of(
SourceCode.treeToString(method.getExpression(), state)
SourceCode.treeToString(memberSelect.getExpression(), state)
+ "::"
+ (invocation.getTypeArguments().isEmpty()
+ (methodInvocation.getTypeArguments().isEmpty()
? ""
: invocation.getTypeArguments().stream()
: methodInvocation.getTypeArguments().stream()
.map(arg -> SourceCode.treeToString(arg, state))
.collect(joining(",", "<", ">")))
+ method.getIdentifier());
+ memberSelect.getIdentifier());
}
}

0 comments on commit fd98b84

Please sign in to comment.