-
Notifications
You must be signed in to change notification settings - Fork 201
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
[PiranhaJava] Delete statements using EasyMock and JUnit API #156
Merged
mkr-plse
merged 8 commits into
uber:master
from
ketkarameya:feature/specific-use-cases-104
Jan 3, 2022
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4ac47a7
Delete statement when it matches a specific Mockito API pattern
2efd810
Adding support for junit and easy mock issue #47
a79301a
Merging all the upstream changes
fc3d50f
Introduced a feature that allows unncessary test methods to be config…
4844f6b
Configuration for handling static and instance methods
1bd3994
Addressing the comments made in the PR
96b7b09
Attempting to fix macos-latest PiranhaSwift issue
a70ca2b
Removing inconsistency in the XCode versions
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
/** | ||
/* | ||
* Copyright (c) 2019 Uber Technologies, Inc. | ||
* | ||
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file | ||
|
@@ -14,13 +14,8 @@ | |
package com.uber.piranha; | ||
|
||
import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION; | ||
import static com.google.errorprone.matchers.ChildMultiMatcher.MatchType.ALL; | ||
import static com.google.errorprone.matchers.Matchers.allOf; | ||
import static com.google.errorprone.matchers.Matchers.instanceMethod; | ||
import static com.google.errorprone.matchers.Matchers.methodInvocation; | ||
import static com.google.errorprone.matchers.Matchers.receiverOfInvocation; | ||
import static com.google.errorprone.matchers.Matchers.staticMethod; | ||
import static com.google.errorprone.matchers.Matchers.toType; | ||
|
||
import com.facebook.infer.annotation.Initializer; | ||
import com.google.auto.service.AutoService; | ||
|
@@ -34,7 +29,6 @@ | |
import com.google.errorprone.bugpatterns.BugChecker; | ||
import com.google.errorprone.fixes.SuggestedFix; | ||
import com.google.errorprone.matchers.Description; | ||
import com.google.errorprone.matchers.Matcher; | ||
import com.google.errorprone.util.ASTHelpers; | ||
import com.google.errorprone.util.FindIdentifiers; | ||
import com.sun.source.tree.AnnotationTree; | ||
|
@@ -78,8 +72,8 @@ | |
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.OptionalInt; | ||
import java.util.Set; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import javax.annotation.Nullable; | ||
|
@@ -369,9 +363,9 @@ private API getXPAPI( | |
for (PiranhaMethodRecord methodRecord : methodRecordsForName) { | ||
// when argumentIndex is specified, if mit's argument at argIndex doesn't match xpFlagName, | ||
// skip to next method property map | ||
Optional<Integer> optionalArgumentIdx = methodRecord.getArgumentIdx(); | ||
OptionalInt optionalArgumentIdx = methodRecord.getArgumentIdx(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My type change tool suggested this :) |
||
if (optionalArgumentIdx.isPresent()) { | ||
int argumentIndex = optionalArgumentIdx.get().intValue(); | ||
int argumentIndex = optionalArgumentIdx.getAsInt(); | ||
if (argumentIndex < mit.getArguments().size()) { | ||
ExpressionTree argTree = mit.getArguments().get(argumentIndex); | ||
Symbol argSym = ASTHelpers.getSymbol(argTree); | ||
|
@@ -705,30 +699,40 @@ private Description updateCode( | |
return Description.NO_MATCH; | ||
} | ||
|
||
private static final String MOCKITO_QN = "org.mockito.Mockito"; | ||
private static final String MOCKITO_WHEN = "when"; | ||
private static final Pattern MOCKITO_THEN = Pattern.compile("then\\W*\\w*"); | ||
|
||
private final Matcher<Tree> MOCKITO_UNNECESSARY_MOCKING_PATTERN = | ||
toType( | ||
MethodInvocationTree.class, | ||
allOf( | ||
instanceMethod().anyClass().withNameMatching(MOCKITO_THEN), | ||
receiverOfInvocation( | ||
methodInvocation( | ||
staticMethod().onClass(MOCKITO_QN).named(MOCKITO_WHEN), | ||
ALL, | ||
(argument, vs) -> { | ||
Value value = evalExpr(argument, vs); | ||
return value == Value.TRUE || value == Value.FALSE; | ||
})))); | ||
|
||
/** | ||
* This method picks up the unnecessary test method as configured in properties.json and converts | ||
* them into a Error-prone AST Matcher and then deletes the containing AST statement. | ||
* | ||
* @param state The visitor state of the statement to be handled | ||
* @return Suggestion Fix for deleting the statement containing a unnecessary test method | ||
* invocation | ||
*/ | ||
private SuggestedFix.Builder handleSpecificAPIPatterns(VisitorState state) { | ||
ExpressionStatementTree stmt = | ||
MethodInvocationTree enclosingMit = | ||
ASTHelpers.findEnclosingNode(state.getPath(), MethodInvocationTree.class); | ||
|
||
ExpressionStatementTree enclosingEst = | ||
ASTHelpers.findEnclosingNode(state.getPath(), ExpressionStatementTree.class); | ||
if (stmt != null && MOCKITO_UNNECESSARY_MOCKING_PATTERN.matches(stmt.getExpression(), state)) { | ||
endPos = state.getEndPosition(stmt); | ||
return SuggestedFix.builder().delete(stmt); | ||
|
||
if (enclosingMit != null | ||
&& enclosingEst != null | ||
&& config | ||
.getUnnecessaryTestMethodRecords() | ||
.stream() | ||
.map( | ||
method -> | ||
method.isStatic() | ||
? method | ||
.getReceiverType() | ||
.map(r -> staticMethod().onClass(r)) | ||
.orElseGet(() -> staticMethod().anyClass()) | ||
: method | ||
.getReceiverType() | ||
.map(r -> instanceMethod().onExactClass(r)) | ||
.orElseGet(() -> instanceMethod().anyClass())) | ||
.anyMatch(matcher -> matcher.matches(enclosingMit, state))) { | ||
endPos = state.getEndPosition(enclosingMit); | ||
return SuggestedFix.builder().delete(enclosingEst); | ||
} | ||
return SuggestedFix.builder(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let us get rid of the term
often
:). How about "The refactored code may contain unnecessary method invocations".