Skip to content

Commit

Permalink
Sort entries of RefasterRuleCollectionTestCase#elidedTypesAndStaticIm…
Browse files Browse the repository at this point in the history
…ports
  • Loading branch information
mohamedsamehsalah committed Oct 21, 2023
1 parent 2ff3095 commit 505870a
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 2 deletions.
5 changes: 5 additions & 0 deletions refaster-test-support/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
<artifactId>jspecify</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package tech.picnic.errorprone.refaster.test;

import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableListMultimap.toImmutableListMultimap;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.ImmutableSortedSet.toImmutableSortedSet;
import static com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH;
import static com.google.errorprone.BugPattern.SeverityLevel.ERROR;
import static java.util.Comparator.comparing;
import static java.util.Comparator.naturalOrder;
import static java.util.stream.Collectors.joining;
import static tech.picnic.errorprone.refaster.runner.Refaster.INCLUDED_RULES_PATTERN_FLAG;

import com.google.common.collect.ImmutableList;
Expand All @@ -31,9 +34,14 @@
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.LineMap;
import com.sun.source.tree.MemberSelectTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.ReturnTree;
import com.sun.source.tree.Tree;
import com.sun.source.tree.Tree.Kind;
import com.sun.source.util.TreeScanner;
import com.sun.tools.javac.tree.EndPosTable;
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
Expand Down Expand Up @@ -67,6 +75,8 @@ public final class RefasterRuleCollection extends BugChecker implements Compilat
private static final long serialVersionUID = 1L;
private static final String RULE_COLLECTION_FLAG = "RefasterRuleCollection:RuleCollection";
private static final String TEST_METHOD_NAME_PREFIX = "test";
private static final String ELIDED_TYPES_AND_STATIC_IMPORTS_METHOD_NAME =
"elidedTypesAndStaticImports";

private final String ruleCollectionUnderTest;
private final ImmutableSortedSet<String> rulesUnderTest;
Expand Down Expand Up @@ -131,7 +141,10 @@ public static void validate(Class<?> clazz) {

@Override
public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) {
SuggestedFix.Builder fixBuilder = SuggestedFix.builder();

reportIncorrectClassName(tree, state);
reportUnSortedElidedTypesAndStaticImports(tree, state, fixBuilder);

List<Description> matches = new ArrayList<>();
delegate.matchCompilationUnit(
Expand All @@ -146,7 +159,7 @@ public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState s
reportMissingMatches(tree, indexedMatches, state);
reportUnexpectedMatches(tree, indexedMatches, state);

return Description.NO_MATCH;
return fixBuilder.isEmpty() ? Description.NO_MATCH : describeMatch(tree, fixBuilder.build());
}

private void reportIncorrectClassName(CompilationUnitTree tree, VisitorState state) {
Expand All @@ -173,6 +186,68 @@ private void reportIncorrectClassName(CompilationUnitTree tree, VisitorState sta
}
}

private void reportUnSortedElidedTypesAndStaticImports(
CompilationUnitTree compilationUnit, VisitorState state, SuggestedFix.Builder fixBuilder) {
new TreeScanner<@Nullable Void, @Nullable Void>() {
@Override
public @Nullable Void visitMethod(MethodTree tree, @Nullable Void unused) {
String methodName = tree.getName().toString();
if (ELIDED_TYPES_AND_STATIC_IMPORTS_METHOD_NAME.equals(methodName)) {
Optional<ReturnTree> returnTree =
tree.getBody().getStatements().stream()
.filter(statement -> statement.getKind() == Kind.RETURN)
.findFirst()
.map(ReturnTree.class::cast);

returnTree
.map(ReturnTree::getExpression)
.map(MethodInvocationTree.class::cast)
.ifPresent(
methodInvocationTree -> {
List<? extends ExpressionTree> actualArgumentsOrdering =
methodInvocationTree.getArguments();

ImmutableList<? extends ExpressionTree> desiredArgumentOrdering =
actualArgumentsOrdering.stream()
.sorted(
comparing(
arg -> getArgumentName(arg, state),
String.CASE_INSENSITIVE_ORDER))
.collect(toImmutableList());

if (!actualArgumentsOrdering.equals(desiredArgumentOrdering)) {
String suggestion =
desiredArgumentOrdering.stream()
.map(state::getSourceForNode)
.collect(
joining(
", ",
String.format(
"%s(",
state.getSourceForNode(
methodInvocationTree.getMethodSelect())),
")"));

fixBuilder.merge(SuggestedFix.replace(methodInvocationTree, suggestion));
}
});
}
return super.visitMethod(tree, null);
}
}.scan(compilationUnit, null);
}

private static String getArgumentName(ExpressionTree arg, VisitorState state) {
switch (arg.getKind()) {
case MEMBER_SELECT:
return state.getSourceForNode(((MemberSelectTree) arg).getExpression());
case METHOD_INVOCATION:
return state.getSourceForNode(((MethodInvocationTree) arg).getMethodSelect());
default:
return "";
}
}

private static ImmutableRangeMap<Integer, String> indexRuleMatches(
List<Description> matches, EndPosTable endPositions) {
ImmutableRangeMap.Builder<Integer, String> ruleMatches = ImmutableRangeMap.builder();
Expand Down Expand Up @@ -293,7 +368,7 @@ private Optional<String> getRuleUnderTest(MethodTree tree, VisitorState state) {
* Unless this method is `RefasterRuleCollectionTestCase#elidedTypesAndStaticImports`, it's
* misnamed.
*/
if (!"elidedTypesAndStaticImports".equals(methodName)) {
if (!ELIDED_TYPES_AND_STATIC_IMPORTS_METHOD_NAME.equals(methodName)) {
state.reportMatch(
describeMatch(
tree,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package tech.picnic.errorprone.refaster.test;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

/**
* Refaster rule collection to validate reporting of unsorted arguments of {@link
* RefasterRuleCollectionTestCase#elidedTypesAndStaticImports} in test classes.
*/
final class NonSortedElidedTypesAndStaticImportsTestRules
implements RefasterRuleCollectionTestCase {
private NonSortedElidedTypesAndStaticImportsTestRules() {}

@Override
public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(Lists.class, assertDoesNotThrow(() -> null), Iterables.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ final class RefasterRuleCollectionTest {
MethodWithoutPrefixRules.class,
MisnamedTestClassRules.class,
MissingTestAndWrongTestRules.class,
NonSortedElidedTypesAndStaticImportsTestRules.class,
PartialTestMatchRules.class,
RuleWithoutTestRules.class,
ValidRules.class
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package tech.picnic.errorprone.refaster.test;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

/**
* Refaster rule collection to validate reporting of unsorted arguments of {@link
* RefasterRuleCollectionTestCase#elidedTypesAndStaticImports} in test classes.
*/
final class NonSortedElidedTypesAndStaticImportsTestRulesTest
implements RefasterRuleCollectionTestCase {
@Override
public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(Lists.class, assertDoesNotThrow(() -> null), Iterables.class);
}
}

// This is a comment to appease Checkstyle.
;
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package tech.picnic.errorprone.refaster.test;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

/**
* Refaster rule collection to validate reporting of unsorted arguments of {@link
* RefasterRuleCollectionTestCase#elidedTypesAndStaticImports} in test classes.
*/
final class NonSortedElidedTypesAndStaticImportsTestRulesTest
implements RefasterRuleCollectionTestCase {
@Override
public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(assertDoesNotThrow(() -> null), Iterables.class, Lists.class);
}
}

// This is a comment to appease Checkstyle.
/* ERROR: Unexpected token. */
;

0 comments on commit 505870a

Please sign in to comment.