WrapWithAdapterMethodCallFix: wrap with List.of() instead of Arrays.asList() on Java 9 and higher (IDEA-269380)

GitOrigin-RevId: f39f5c58348afb6b3c57dbc059860f940c6ec363
This commit is contained in:
Bas Leijdekkers
2021-05-16 10:41:03 +02:00
committed by intellij-monorepo-bot
parent 358da7cc4e
commit 223eeb0b0f
34 changed files with 121 additions and 77 deletions

View File

@@ -1,18 +1,4 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.daemon.QuickFixBundle;
@@ -45,6 +31,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.function.Predicate;
import static com.intellij.pom.java.LanguageLevel.JDK_11;
import static com.intellij.pom.java.LanguageLevel.JDK_1_9;
public final class WrapWithAdapterMethodCallFix extends LocalQuickFixAndIntentionActionOnPsiElement implements HighPriorityAction {
static class Wrapper extends ArgumentFixerActionFactory {
@@ -163,7 +150,12 @@ public final class WrapWithAdapterMethodCallFix extends LocalQuickFixAndIntentio
outType -> outType.equalsToText("java.nio.file.Path") && isAppropriateLanguageLevel(outType, level -> level.isLessThan(JDK_11))),
new Wrapper("java.util.Arrays.asList({0})",
inType -> inType instanceof PsiArrayType && ((PsiArrayType)inType).getComponentType() instanceof PsiClassType,
outType -> InheritanceUtil.isInheritor(outType, CommonClassNames.JAVA_LANG_ITERABLE)),
outType -> InheritanceUtil.isInheritor(outType, CommonClassNames.JAVA_LANG_ITERABLE) &&
isAppropriateLanguageLevel(outType ,l -> l.isLessThan(JDK_1_9))),
new Wrapper("java.util.List.of({0})",
inType -> inType instanceof PsiArrayType && ((PsiArrayType)inType).getComponentType() instanceof PsiClassType,
outType -> InheritanceUtil.isInheritor(outType, CommonClassNames.JAVA_LANG_ITERABLE) &&
isAppropriateLanguageLevel(outType ,l -> l.isAtLeast(JDK_1_9))),
new Wrapper("java.lang.Math.toIntExact({0})",
inType -> PsiType.LONG.equals(inType) || inType.equalsToText(CommonClassNames.JAVA_LANG_LONG),
outType -> PsiType.INT.equals(outType) || outType.equalsToText(CommonClassNames.JAVA_LANG_INTEGER)),
@@ -218,10 +210,7 @@ public final class WrapWithAdapterMethodCallFix extends LocalQuickFixAndIntentio
@NotNull PsiFile file,
@NotNull PsiElement startElement,
@NotNull PsiElement endElement) {
return myType != null &&
myWrapper != null &&
myType.isValid() &&
BaseIntentionAction.canModify(startElement);
return myType != null && myWrapper != null && myType.isValid() && BaseIntentionAction.canModify(startElement);
}
@Override
@@ -240,10 +229,7 @@ public final class WrapWithAdapterMethodCallFix extends LocalQuickFixAndIntentio
private static class MyMethodArgumentFix extends MethodArgumentFix implements HighPriorityAction {
protected MyMethodArgumentFix(@NotNull PsiExpressionList list,
int i,
@NotNull PsiType toType,
@NotNull Wrapper fixerActionFactory) {
protected MyMethodArgumentFix(@NotNull PsiExpressionList list, int i, @NotNull PsiType toType, @NotNull Wrapper fixerActionFactory) {
super(list, i, toType, fixerActionFactory);
}

View File

@@ -299,7 +299,7 @@ java.8.collection.removeif.inspection.fix.name=Replace the loop with 'Collection
java.8.list.sort.inspection.description=Collections.sort could be replaced with List.sort
java.8.list.sort.inspection.fix.name=Replace with List.sort
wrap.with.optional.parameter.text=Wrap {0, choice, 1#1st|2#2nd|3#3rd|4#{0,number}th} parameter using ''java.util.Optional''
wrap.with.optional.parameter.text=Wrap {0, choice, 1#1st|2#2nd|3#3rd|4#{0,number}th} argument using ''java.util.Optional''
wrap.with.optional.single.parameter.text=Wrap using 'java.util.Optional'
move.file.to.source.root.text=Move file to a source root
@@ -327,8 +327,8 @@ insert.sam.method.call.fix.family.name=Insert single abstract method call
wrap.with.adapter.call.family.name=Wrap using adapter call or object
wrap.with.adapter.text=Wrap using ''{0}''
wrap.with.adapter.parameter.single.text=Wrap parameter using ''{0}''
wrap.with.adapter.parameter.multiple.text=Wrap {0, choice, 1#1st|2#2nd|3#3rd|4#{0,number}th} parameter using ''{1}''
wrap.with.adapter.parameter.single.text=Wrap argument using ''{0}''
wrap.with.adapter.parameter.multiple.text=Wrap {0, choice, 1#1st|2#2nd|3#3rd|4#{0,number}th} argument using ''{1}''
java.9.merge.module.statements.fix.family.name=Merge with other ''{0}'' directive
java.9.merge.module.statements.fix.name=Merge with other ''{0} {1}'' directive

View File

@@ -1,4 +1,4 @@
// "Wrap parameter using 'new File()'" "true"
// "Wrap argument using 'new File()'" "true"
import java.io.File;
class Test {

View File

@@ -1,4 +1,4 @@
// "Wrap 2nd parameter using 'new File()'" "true"
// "Wrap 2nd argument using 'new File()'" "true"
import java.io.File;
class Test {

View File

@@ -1,4 +1,4 @@
// "Wrap 1st parameter using 'Collections.singleton()'" "true"
// "Wrap 1st argument using 'Collections.singleton()'" "true"
import java.util.*;
class Test {

View File

@@ -1,4 +1,4 @@
// "Wrap parameter using 'new File()'" "true"
// "Wrap argument using 'new File()'" "true"
import java.io.File;
class Test {

View File

@@ -1,4 +1,4 @@
// "Wrap parameter using 'new File()'" "true"
// "Wrap argument using 'new File()'" "true"
import java.io.File;
class Test {

View File

@@ -1,4 +1,4 @@
// "Wrap 2nd parameter using 'new File()'" "true"
// "Wrap 2nd argument using 'new File()'" "true"
import java.io.File;
class Test {

View File

@@ -1,4 +1,4 @@
// "Wrap parameter using 'new File()'" "false"
// "Wrap argument using 'new File()'" "false"
import java.io.File;
class Test {

View File

@@ -1,4 +1,4 @@
// "Wrap 1st parameter using 'Collections.singleton()'" "true"
// "Wrap 1st argument using 'Collections.singleton()'" "true"
import java.util.*;
class Test {

View File

@@ -1,4 +1,4 @@
// "Wrap parameter using 'new File()'" "true"
// "Wrap argument using 'new File()'" "true"
import java.io.File;
class Test {

View File

@@ -1,4 +1,4 @@
// "Wrap parameter using 'new File()'" "false"
// "Wrap argument using 'new File()'" "false"
class Foo {
private static Y[] parse(Iterable<String> ss) {
return Y.toArray(X.from(ss).transform(s -> pa<caret>rse(s)));

View File

@@ -1,4 +1,4 @@
// "Wrap parameter using 'Path.of()'" "true"
// "Wrap argument using 'Path.of()'" "true"
import java.nio.file.*;
class Test {

View File

@@ -1,10 +0,0 @@
// "Wrap parameter using 'Paths.get()'" "false"
import java.nio.file.*;
class Test {
void m() {
Files.readString("path");
}
}

View File

@@ -1,4 +1,4 @@
// "Wrap parameter using 'Path.of()'" "true"
// "Wrap argument using 'Path.of()'" "true"
import java.nio.file.*;
class Test {

View File

@@ -1,4 +1,4 @@
// "Wrap parameter using 'Paths.get()'" "false"
// "Wrap argument using 'Paths.get()'" "false"
import java.nio.file.*;
class Test {

View File

@@ -1,4 +1,4 @@
// "Wrap 4th parameter using 'Arrays.asList()'" "true"
// "Wrap 4th argument using 'Arrays.asList()'" "true"
import java.util.Arrays;
import java.util.List;

View File

@@ -1,4 +1,4 @@
// "Wrap parameter using 'Arrays.asList()'" "true"
// "Wrap argument using 'Arrays.asList()'" "true"
import java.util.Arrays;
import java.util.List;

View File

@@ -1,4 +1,4 @@
// "Wrap 4th parameter using 'Arrays.asList()'" "true"
// "Wrap 4th argument using 'Arrays.asList()'" "true"
import java.util.List;
public class Test {

View File

@@ -1,4 +1,4 @@
// "Wrap parameter using 'Arrays.asList()'" "false"
// "Wrap argument using 'Arrays.asList()'" "false"
import java.util.LinkedList;
public class Test {

View File

@@ -1,4 +1,4 @@
// "Wrap parameter using 'Arrays.asList()'" "false"
// "Wrap argument using 'Arrays.asList()'" "false"
import java.util.LinkedList;
public class Test {

View File

@@ -1,4 +1,4 @@
// "Wrap parameter using 'Arrays.asList()'" "true"
// "Wrap argument using 'Arrays.asList()'" "true"
import java.util.List;
public class Test {

View File

@@ -0,0 +1,13 @@
// "Wrap 4th argument using 'List.of()'" "true"
import java.util.List;
public class Test {
void list(int i, int j, int k, List<String> l, String s) {
}
void m(String[] a) {
list(1, 2, 3, List.of(a), "asd");
}
}

View File

@@ -0,0 +1,13 @@
// "Wrap argument using 'List.of()'" "true"
import java.util.List;
public class Test {
void list(List<String> l) {
}
void m(String[] a) {
list(List.of(a));
}
}

View File

@@ -0,0 +1,13 @@
// "Wrap 4th argument using 'List.of()'" "true"
import java.util.List;
public class Test {
void list(int i, int j, int k, List<String> l, String s) {
}
void m(String[] a) {
list(1, 2, 3, a<caret>, "asd");
}
}

View File

@@ -0,0 +1,13 @@
// "Wrap argument using 'List.of()'" "true"
import java.util.List;
public class Test {
void list(List<String> l) {
}
void m(String[] a) {
list(a<caret>);
}
}

View File

@@ -1,4 +1,4 @@
// "Wrap 2nd parameter using 'Math.toIntExact()'" "true"
// "Wrap 2nd argument using 'Math.toIntExact()'" "true"
public class Test {
void longMethod(int k, int thisIsInt) {

View File

@@ -1,4 +1,4 @@
// "Wrap parameter using 'Math.toIntExact()'" "true"
// "Wrap argument using 'Math.toIntExact()'" "true"
public class Test {
void m(int i) {

View File

@@ -1,4 +1,4 @@
// "Wrap 2nd parameter using 'Math.toIntExact()'" "true"
// "Wrap 2nd argument using 'Math.toIntExact()'" "true"
public class Test {
void longMethod(int k, int thisIsInt) {

View File

@@ -1,4 +1,4 @@
// "Wrap parameter using 'Math.toIntExact()'" "true"
// "Wrap argument using 'Math.toIntExact()'" "true"
public class Test {
void m(int i) {

View File

@@ -1,4 +1,4 @@
// "Wrap 2nd parameter using 'java.util.Optional'" "true"
// "Wrap 2nd argument using 'java.util.Optional'" "true"
import java.util.Optional;
public class Test {

View File

@@ -1,4 +1,4 @@
// "Wrap 2nd parameter using 'java.util.Optional'" "true"
// "Wrap 2nd argument using 'java.util.Optional'" "true"
import java.util.Optional;
public class Test {

View File

@@ -1,21 +1,11 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.java.codeInsight.daemon.quickFix;
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
import com.intellij.testFramework.LightProjectDescriptor;
import org.jetbrains.annotations.NotNull;
import static com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase.JAVA_8;
/**
* @author Dmitry Batkovich
@@ -25,4 +15,9 @@ public class WrapArrayToArraysAsListFixTest extends LightQuickFixParameterizedTe
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/wrapArrayToArraysAsList";
}
@Override
protected @NotNull LightProjectDescriptor getProjectDescriptor() {
return JAVA_8;
}
}

View File

@@ -0,0 +1,21 @@
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.java.codeInsight.daemon.quickFix;
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
import com.intellij.testFramework.LightProjectDescriptor;
import org.jetbrains.annotations.NotNull;
import static com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase.JAVA_9;
public class WrapArrayToArraysListOfFixTest extends LightQuickFixParameterizedTestCase {
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/wrapArrayToListOf";
}
@NotNull
@Override
protected LightProjectDescriptor getProjectDescriptor() {
return JAVA_9;
}
}