LambdaAndExplicitMethodPair: Arrays.setAll -> Arrays.fill pair added

Allows to make IDEA-185394 in two steps
This commit is contained in:
Tagir Valeev
2018-01-24 13:34:51 +07:00
parent 3e3017f4f4
commit d828baf6e3
5 changed files with 43 additions and 3 deletions

View File

@@ -27,12 +27,13 @@ class LambdaAndExplicitMethodPair {
new LambdaAndExplicitMethodPair("java.util.Objects", "requireNonNullElseGet", "requireNonNullElse", 1, "T", true),
new LambdaAndExplicitMethodPair("org.junit.jupiter.api.Assertions", "assert(?!Timeout).*|fail", "*", -1, JAVA_LANG_STRING, true),
new LambdaAndExplicitMethodPair("org.junit.jupiter.api.Assertions", "assert(True|False)", "*", 0, JAVA_LANG_STRING, true),
new LambdaAndExplicitMethodPair(CommonClassNames.JAVA_UTIL_ARRAYS, "setAll", "fill", 1, null, true, "i")
};
private final @NotNull String myClass;
private final @NotNull Pattern myLambdaMethod;
private final @NotNull String myExplicitMethod;
private final int myParameterIndex;
private final @NotNull String myExplicitParameterType;
private final @Nullable String myExplicitParameterType;
private final boolean myCanUseReturnValue;
private final @NotNull String[] myDefaultLambdaParameters;
@@ -42,14 +43,14 @@ class LambdaAndExplicitMethodPair {
* @param explicitMethod name of the equivalent method ("*" if name is the same as lambdaMethod)
* accepting constant instead of lambda argument (all other args must be the same)
* @param index index of lambda argument, zero-based, or -1 to denote the last argument
* @param explicitParameterType type of explicit parameter
* @param explicitParameterType type of explicit parameter (null if explicit -> lambda conversion is not applicable)
* @param canUseReturnValue true if method return value does not depend on whether lambda or constant version is used
*/
LambdaAndExplicitMethodPair(@NotNull String aClass,
@NotNull @RegExp String lambdaMethod,
@NotNull String explicitMethod,
int index,
@NotNull String explicitParameterType,
@Nullable String explicitParameterType,
boolean canUseReturnValue,
@NotNull String... defaultLambdaParameters) {
myClass = aClass;
@@ -79,6 +80,7 @@ class LambdaAndExplicitMethodPair {
}
PsiExpression getLambdaCandidateFromExplicitCall(PsiMethodCallExpression explicitCall) {
if (myExplicitParameterType == null) return null;
String name = explicitCall.getMethodExpression().getReferenceName();
if (name == null) return null;
if (myExplicitMethod.equals("*")) {

View File

@@ -0,0 +1,8 @@
// "Use 'fill' method without lambda" "true"
import java.util.Arrays;
class Test {
public void test(int[] arr) {
Arrays.fill(arr, 0);
}
}

View File

@@ -0,0 +1,8 @@
// "Use 'fill' method without lambda" "true"
import java.util.Arrays;
class Test {
public void test(int[] arr) {
Arrays.setAll(arr, x <caret>-> 0);
}
}

View File

@@ -0,0 +1,8 @@
// "Use 'fill' method without lambda" "false"
import java.util.Arrays;
class Test {
public void test(int[] arr) {
Arrays.setAll(arr, x <caret>-> x);
}
}

View File

@@ -0,0 +1,14 @@
// "Use 'setAll' method with functional argument" "false"
import java.util.Arrays;
class Test {
int computeValue() {
return 0;
}
public void test(int[] arr) {
// Should not suggest setAll replacement as lambda might be called many times
Arrays.fill(arr, co<caret>mputeValue());
}
}