mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 13:02:30 +07:00
guava type migration: fix predicate method reference
GitOrigin-RevId: b1a33cbbc9b315b7d91bbbc46752b102ef463751
This commit is contained in:
committed by
intellij-monorepo-bot
parent
6fbf89c541
commit
8741f42d2d
@@ -1,4 +1,4 @@
|
||||
// Copyright 2000-2020 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.
|
||||
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.refactoring.typeMigration.rules.guava;
|
||||
|
||||
import com.intellij.codeInspection.java18StreamApi.PseudoLambdaReplaceTemplate;
|
||||
@@ -17,7 +17,6 @@ import com.intellij.refactoring.typeMigration.TypeConversionDescriptorBase;
|
||||
import com.intellij.refactoring.typeMigration.TypeEvaluator;
|
||||
import com.intellij.refactoring.typeMigration.TypeMigrationLabeler;
|
||||
import com.intellij.refactoring.typeMigration.rules.TypeConversionRule;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.SmartList;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.util.containers.Stack;
|
||||
@@ -34,8 +33,7 @@ import java.util.*;
|
||||
*/
|
||||
public class GuavaFluentIterableConversionRule extends BaseGuavaTypeConversionRule {
|
||||
private static final Logger LOG = Logger.getInstance(GuavaFluentIterableConversionRule.class);
|
||||
private static final Map<@NonNls String, TypeConversionDescriptorFactory> DESCRIPTORS_MAP =
|
||||
new HashMap<>();
|
||||
private static final Map<@NonNls String, TypeConversionDescriptorFactory> DESCRIPTORS_MAP = new HashMap<>();
|
||||
|
||||
public static final Set<@NonNls String> CHAIN_HEAD_METHODS = ContainerUtil.newHashSet("from", "of", "fromNullable");
|
||||
public static final @NonNls String FLUENT_ITERABLE = "com.google.common.collect.FluentIterable";
|
||||
@@ -225,9 +223,9 @@ public class GuavaFluentIterableConversionRule extends BaseGuavaTypeConversionRu
|
||||
return super.replace(expression, evaluator);
|
||||
}
|
||||
|
||||
private String suggestName(String baseName, JavaCodeStyleManager codeStyleManager, PsiElement place) {
|
||||
final SuggestedNameInfo suggestedNameInfo = codeStyleManager
|
||||
.suggestVariableName(VariableKind.LOCAL_VARIABLE, baseName, null, null, false);
|
||||
private static String suggestName(String baseName, JavaCodeStyleManager codeStyleManager, PsiElement place) {
|
||||
final SuggestedNameInfo suggestedNameInfo =
|
||||
codeStyleManager.suggestVariableName(VariableKind.LOCAL_VARIABLE, baseName, null, null, false);
|
||||
return codeStyleManager.suggestUniqueVariableName(suggestedNameInfo, place, false).names[0];
|
||||
}
|
||||
};
|
||||
@@ -376,7 +374,7 @@ public class GuavaFluentIterableConversionRule extends BaseGuavaTypeConversionRu
|
||||
}
|
||||
|
||||
@Override
|
||||
public PsiExpression replace(@NotNull PsiExpression expression, @NotNull TypeEvaluator evaluator) throws IncorrectOperationException {
|
||||
public PsiExpression replace(@NotNull PsiExpression expression, @NotNull TypeEvaluator evaluator) {
|
||||
Stack<PsiMethodCallExpression> methodChainStack = new Stack<>();
|
||||
PsiMethodCallExpression current = (PsiMethodCallExpression) expression;
|
||||
while (current != null) {
|
||||
|
||||
@@ -1,32 +1,16 @@
|
||||
/*
|
||||
* Copyright 2000-2015 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-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.refactoring.typeMigration.rules.guava;
|
||||
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.refactoring.typeMigration.TypeConversionDescriptorBase;
|
||||
import com.intellij.refactoring.typeMigration.TypeEvaluator;
|
||||
import com.intellij.refactoring.typeMigration.TypeMigrationLabeler;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
/**
|
||||
* @author Dmitry Batkovich
|
||||
*/
|
||||
@@ -62,8 +46,10 @@ public class GuavaPredicateConversionRule extends GuavaLambdaConversionRule {
|
||||
}
|
||||
return new TypeConversionDescriptorBase() {
|
||||
@Override
|
||||
public PsiExpression replace(PsiExpression expression, @NotNull TypeEvaluator evaluator) throws IncorrectOperationException {
|
||||
return (PsiExpression)expression.replace(JavaPsiFacade.getElementFactory(expression.getProject()).createExpressionFromText(expression.getText() + "::apply", expression));
|
||||
public PsiExpression replace(PsiExpression expression, @NotNull TypeEvaluator evaluator) {
|
||||
final PsiExpression methodReference =
|
||||
JavaPsiFacade.getElementFactory(expression.getProject()).createExpressionFromText(expression.getText() + "::test", expression);
|
||||
return (PsiExpression)expression.replace(methodReference);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// Copyright 2000-2020 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.
|
||||
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.refactoring.typeMigration.rules.guava;
|
||||
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
@@ -10,7 +10,6 @@ import com.intellij.psi.util.RedundantCastUtil;
|
||||
import com.intellij.refactoring.typeMigration.TypeConversionDescriptor;
|
||||
import com.intellij.refactoring.typeMigration.TypeConversionDescriptorBase;
|
||||
import com.intellij.refactoring.typeMigration.TypeEvaluator;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -21,11 +20,11 @@ import java.util.Set;
|
||||
/**
|
||||
* @author Dmitry Batkovich
|
||||
*/
|
||||
public final class GuavaPredicatesUtil {
|
||||
final class GuavaPredicatesUtil {
|
||||
private static final Logger LOG = Logger.getInstance(GuavaPredicatesUtil.class);
|
||||
|
||||
static final Set<String> PREDICATES_AND_OR = ContainerUtil.newHashSet("or", "and");
|
||||
static final String PREDICATES_NOT = "not";
|
||||
private static final Set<String> PREDICATES_AND_OR = ContainerUtil.newHashSet("or", "and");
|
||||
private static final String PREDICATES_NOT = "not";
|
||||
public static final Set<String> PREDICATES_METHOD_NAMES =
|
||||
ContainerUtil.newHashSet("alwaysTrue", "alwaysFalse", "isNull", "notNull", "equalTo", "not", "or", "and");
|
||||
|
||||
@@ -123,7 +122,7 @@ public final class GuavaPredicatesUtil {
|
||||
}
|
||||
|
||||
@Override
|
||||
public PsiExpression replace(PsiExpression expression, @NotNull TypeEvaluator evaluator) throws IncorrectOperationException {
|
||||
public PsiExpression replace(PsiExpression expression, @NotNull TypeEvaluator evaluator) {
|
||||
@NonNls String newExpressionString =
|
||||
GuavaConversionUtil.adjustLambdaContainingExpression(((PsiMethodCallExpression)expression).getArgumentList().getExpressions()[0], true, myTargetType, evaluator).getText() + ".negate()";
|
||||
|
||||
@@ -156,17 +155,15 @@ public final class GuavaPredicatesUtil {
|
||||
}
|
||||
|
||||
@Override
|
||||
public PsiExpression replace(PsiExpression expression, @NotNull TypeEvaluator evaluator) throws IncorrectOperationException {
|
||||
public PsiExpression replace(PsiExpression expression, @NotNull TypeEvaluator evaluator) {
|
||||
final PsiMethodCallExpression methodCall = (PsiMethodCallExpression)expression;
|
||||
final String methodName = methodCall.getMethodExpression().getReferenceName();
|
||||
|
||||
final PsiExpression[] arguments = methodCall.getArgumentList().getExpressions();
|
||||
final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(expression.getProject());
|
||||
if (arguments.length == 1) {
|
||||
return (PsiExpression)expression.replace(GuavaConversionUtil.adjustLambdaContainingExpression(arguments[0], true, myTargetType, evaluator));
|
||||
}
|
||||
LOG.assertTrue(arguments.length != 0);
|
||||
@NonNls StringBuilder replaceBy = new StringBuilder();
|
||||
final @NonNls StringBuilder replaceBy = new StringBuilder();
|
||||
final String methodName = methodCall.getMethodExpression().getReferenceName();
|
||||
for (int i = 1; i < arguments.length; i++) {
|
||||
PsiExpression argument = arguments[i];
|
||||
replaceBy.append(".").append(methodName).append("(").append(GuavaConversionUtil.adjustLambdaContainingExpression(argument, false, myTargetType, evaluator).getText()).append(")");
|
||||
@@ -179,6 +176,7 @@ public final class GuavaPredicatesUtil {
|
||||
else if (!GuavaConversionUtil.isJavaLambda(parent, evaluator)) {
|
||||
replaceBy.append("::test");
|
||||
}
|
||||
final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(expression.getProject());
|
||||
return (PsiExpression)expression.replace(elementFactory.createExpressionFromText(replaceBy.toString(), expression));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.codeInsight;
|
||||
|
||||
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
|
||||
@@ -9,10 +10,6 @@ import org.jetbrains.annotations.NotNull;
|
||||
* @author anna
|
||||
*/
|
||||
public class ConvertToAtomicIntentionTest extends LightQuickFixParameterizedTestCase {
|
||||
@Override
|
||||
protected boolean shouldBeAvailableAfterExecution() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getBasePath() {
|
||||
|
||||
@@ -257,6 +257,10 @@ public class GuavaInspectionTest extends JavaCodeInsightFixtureTestCase {
|
||||
doTestAllFile();
|
||||
}
|
||||
|
||||
public void testPredicates5() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testFluentIterableElementTypeChanged() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.FluentIterable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Main7 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Optional<? extends String> image = FluentIterable.from(new ArrayList<String>()).firstMatch(getPredicate());
|
||||
if (image.isPresent()) {
|
||||
System.out.println(image.get());
|
||||
}
|
||||
}
|
||||
|
||||
static Predicate<caret><String> getPredicate() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.collect.FluentIterable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class Main7 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Optional<? extends String> image = FluentIterable.from(new ArrayList<String>()).firstMatch(getPredicate()::test);
|
||||
if (image.isPresent()) {
|
||||
System.out.println(image.get());
|
||||
}
|
||||
}
|
||||
|
||||
static Predicate<String> getPredicate() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user