|
| 1 | +/* |
| 2 | + * Copyright 2026 The Error Prone Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.errorprone.bugpatterns; |
| 18 | + |
| 19 | +import static com.google.common.collect.ImmutableList.toImmutableList; |
| 20 | +import static com.google.common.collect.ImmutableSet.toImmutableSet; |
| 21 | +import static com.google.common.collect.Iterables.getOnlyElement; |
| 22 | +import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; |
| 23 | +import static com.google.errorprone.matchers.Description.NO_MATCH; |
| 24 | +import static com.google.errorprone.matchers.method.MethodMatchers.staticMethod; |
| 25 | +import static com.google.errorprone.util.ASTHelpers.getReceiver; |
| 26 | +import static com.google.errorprone.util.ASTHelpers.getStartPosition; |
| 27 | +import static com.google.errorprone.util.ASTHelpers.getSymbol; |
| 28 | +import static com.google.errorprone.util.ASTHelpers.getType; |
| 29 | + |
| 30 | +import com.google.common.base.CaseFormat; |
| 31 | +import com.google.common.collect.ImmutableList; |
| 32 | +import com.google.common.collect.Streams; |
| 33 | +import com.google.errorprone.BugPattern; |
| 34 | +import com.google.errorprone.ErrorProneFlags; |
| 35 | +import com.google.errorprone.VisitorState; |
| 36 | +import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher; |
| 37 | +import com.google.errorprone.bugpatterns.threadsafety.ConstantExpressions; |
| 38 | +import com.google.errorprone.fixes.SuggestedFix; |
| 39 | +import com.google.errorprone.fixes.SuggestedFixes; |
| 40 | +import com.google.errorprone.matchers.Description; |
| 41 | +import com.google.errorprone.matchers.Matcher; |
| 42 | +import com.google.errorprone.util.FindIdentifiers; |
| 43 | +import com.sun.source.tree.BlockTree; |
| 44 | +import com.sun.source.tree.ExpressionStatementTree; |
| 45 | +import com.sun.source.tree.ExpressionTree; |
| 46 | +import com.sun.source.tree.IdentifierTree; |
| 47 | +import com.sun.source.tree.LambdaExpressionTree; |
| 48 | +import com.sun.source.tree.MemberSelectTree; |
| 49 | +import com.sun.source.tree.MethodInvocationTree; |
| 50 | +import com.sun.source.tree.StatementTree; |
| 51 | +import com.sun.tools.javac.code.Symbol.VarSymbol; |
| 52 | +import java.util.stream.IntStream; |
| 53 | +import java.util.stream.Stream; |
| 54 | +import javax.inject.Inject; |
| 55 | +import javax.lang.model.element.ElementKind; |
| 56 | + |
| 57 | +/** A {@link BugChecker}; see the associated {@link BugPattern} annotation for details. */ |
| 58 | +@BugPattern(summary = "Minimize the amount of logic in assertThrows", severity = WARNING) |
| 59 | +public class AssertThrowsMinimizer extends BugChecker implements MethodInvocationTreeMatcher { |
| 60 | + |
| 61 | + private static final Matcher<ExpressionTree> MATCHER = |
| 62 | + staticMethod().onClass("org.junit.Assert").named("assertThrows"); |
| 63 | + |
| 64 | + private final ConstantExpressions constantExpressions; |
| 65 | + private final boolean useVarType; |
| 66 | + |
| 67 | + @Inject |
| 68 | + AssertThrowsMinimizer(ConstantExpressions constantExpressions, ErrorProneFlags flags) { |
| 69 | + this.constantExpressions = constantExpressions; |
| 70 | + this.useVarType = flags.getBoolean("AssertThrowsMinimizer:UseVarType").orElse(false); |
| 71 | + } |
| 72 | + |
| 73 | + record Hoist(ExpressionTree site, String name) {} |
| 74 | + |
| 75 | + @Override |
| 76 | + public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { |
| 77 | + if (!MATCHER.matches(tree, state)) { |
| 78 | + return NO_MATCH; |
| 79 | + } |
| 80 | + if (!(state.getPath().getParentPath().getLeaf() instanceof StatementTree parent)) { |
| 81 | + // We need a scope to declare variables in, assertThrows is usually an expression statement or |
| 82 | + // a variable initializer |
| 83 | + return NO_MATCH; |
| 84 | + } |
| 85 | + if (!(tree.getArguments().getLast() instanceof LambdaExpressionTree lambdaExpressionTree)) { |
| 86 | + return NO_MATCH; |
| 87 | + } |
| 88 | + MethodInvocationTree runnable; |
| 89 | + switch (lambdaExpressionTree.getBody()) { |
| 90 | + case BlockTree blockTree -> { |
| 91 | + if (blockTree.getStatements().size() != 1) { |
| 92 | + return NO_MATCH; |
| 93 | + } |
| 94 | + if (!(getOnlyElement(blockTree.getStatements()) |
| 95 | + instanceof ExpressionStatementTree expressionStatementTree |
| 96 | + && expressionStatementTree.getExpression() |
| 97 | + instanceof MethodInvocationTree methodInvocationTree)) { |
| 98 | + return NO_MATCH; |
| 99 | + } |
| 100 | + runnable = methodInvocationTree; |
| 101 | + } |
| 102 | + case MethodInvocationTree methodInvocationTree -> runnable = methodInvocationTree; |
| 103 | + default -> { |
| 104 | + return NO_MATCH; |
| 105 | + } |
| 106 | + } |
| 107 | + ImmutableList<Hoist> toHoist = |
| 108 | + Streams.concat( |
| 109 | + Stream.ofNullable(getReceiver(runnable)) |
| 110 | + .map(r -> new Hoist(r, receiverVariableName(r, state))), |
| 111 | + Streams.zip( |
| 112 | + runnable.getArguments().stream(), |
| 113 | + getSymbol(runnable).getParameters().stream(), |
| 114 | + (ExpressionTree a, VarSymbol p) -> new Hoist(a, p.getSimpleName().toString()))) |
| 115 | + .filter(h -> needsHoisting(h.site(), state)) |
| 116 | + .collect(toImmutableList()); |
| 117 | + if (toHoist.isEmpty()) { |
| 118 | + return NO_MATCH; |
| 119 | + } |
| 120 | + SuggestedFix.Builder fix = SuggestedFix.builder(); |
| 121 | + StringBuilder hoistedVariables = new StringBuilder(); |
| 122 | + for (Hoist hoist : toHoist) { |
| 123 | + String identifier = avoidShadowing(hoist.name(), state); |
| 124 | + hoistedVariables.append( |
| 125 | + String.format( |
| 126 | + "%s %s = %s;\n", |
| 127 | + useVarType ? "var" : SuggestedFixes.qualifyType(state, fix, getType(hoist.site())), |
| 128 | + identifier, |
| 129 | + state.getSourceForNode(hoist.site()))); |
| 130 | + fix.replace(hoist.site(), identifier); |
| 131 | + } |
| 132 | + fix.prefixWith(parent, hoistedVariables.toString()); |
| 133 | + if (lambdaExpressionTree.getBody() instanceof BlockTree blockTree) { |
| 134 | + fix.replace(getStartPosition(blockTree), getStartPosition(runnable), ""); |
| 135 | + fix.replace(state.getEndPosition(runnable), state.getEndPosition(blockTree), ""); |
| 136 | + } |
| 137 | + return describeMatch(tree, fix.build()); |
| 138 | + } |
| 139 | + |
| 140 | + private static String receiverVariableName(ExpressionTree tree, VisitorState state) { |
| 141 | + return CaseFormat.UPPER_CAMEL.to( |
| 142 | + CaseFormat.LOWER_CAMEL, getType(tree).asElement().getSimpleName().toString()); |
| 143 | + } |
| 144 | + |
| 145 | + private boolean needsHoisting(ExpressionTree tree, VisitorState state) { |
| 146 | + boolean unqualifiedIdentifier = |
| 147 | + switch (tree) { |
| 148 | + case IdentifierTree identifierTree -> true; |
| 149 | + case MemberSelectTree memberSelectTree -> |
| 150 | + memberSelectTree.getExpression() instanceof IdentifierTree identifierTree |
| 151 | + && identifierTree.getName().contentEquals("this"); |
| 152 | + default -> false; |
| 153 | + }; |
| 154 | + if (unqualifiedIdentifier && getSymbol(tree).getKind() == ElementKind.FIELD) { |
| 155 | + return false; |
| 156 | + } |
| 157 | + // This is an imperfect heuristic. These expressions aren't guaranteed not to throw, but may be |
| 158 | + // less valuable to hoist. |
| 159 | + return constantExpressions.constantExpression(tree, state).isEmpty(); |
| 160 | + } |
| 161 | + |
| 162 | + // Stolen from PatternMatchingInstanceof |
| 163 | + // TODO: cushon - add to SuggestedFixes? |
| 164 | + private static String avoidShadowing(String name, VisitorState state) { |
| 165 | + var idents = |
| 166 | + FindIdentifiers.findAllIdents(state).stream() |
| 167 | + .map(s -> s.getSimpleName().toString()) |
| 168 | + .collect(toImmutableSet()); |
| 169 | + return IntStream.iterate(1, i -> i + 1) |
| 170 | + .mapToObj(i -> i == 1 ? name : (name + i)) |
| 171 | + .filter(n -> !idents.contains(n)) |
| 172 | + .findFirst() |
| 173 | + .get(); |
| 174 | + } |
| 175 | +} |
0 commit comments