From 06d02befe03b8abf9155814877e4a334f28d8c93 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Mon, 20 Mar 2017 12:34:14 +0700 Subject: [PATCH] Optional support for ChainCallExtractor (fixes IDEA-169782) --- .../chainCall/OptionalChainCallExtractor.java | 40 +++++++++++++++++++ resources/src/META-INF/IdeaPlugin.xml | 1 + 2 files changed, 41 insertions(+) create mode 100644 java/java-impl/src/com/intellij/refactoring/chainCall/OptionalChainCallExtractor.java diff --git a/java/java-impl/src/com/intellij/refactoring/chainCall/OptionalChainCallExtractor.java b/java/java-impl/src/com/intellij/refactoring/chainCall/OptionalChainCallExtractor.java new file mode 100644 index 000000000000..ce115b4e0c59 --- /dev/null +++ b/java/java-impl/src/com/intellij/refactoring/chainCall/OptionalChainCallExtractor.java @@ -0,0 +1,40 @@ +/* + * 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. + */ +package com.intellij.refactoring.chainCall; + +import com.intellij.psi.*; +import com.siyeh.ig.callMatcher.CallMatcher; +import org.jetbrains.annotations.NotNull; + +/** + * @author Tagir Valeev + */ +public class OptionalChainCallExtractor implements ChainCallExtractor { + private static final CallMatcher NEXT_CALL = + CallMatcher.instanceCall(CommonClassNames.JAVA_UTIL_OPTIONAL, "ifPresent", "map", "flatMap").parameterCount(1); + + @Override + public boolean canExtractChainCall(@NotNull PsiMethodCallExpression call, PsiExpression expression, PsiType expressionType) { + if (expressionType instanceof PsiPrimitiveType) return false; + PsiExpression qualifier = call.getMethodExpression().getQualifierExpression(); + return qualifier != null && NEXT_CALL.test(call); + } + + @Override + public String getMethodName(PsiVariable variable, PsiExpression expression, PsiType expressionType) { + return "map"; + } +} diff --git a/resources/src/META-INF/IdeaPlugin.xml b/resources/src/META-INF/IdeaPlugin.xml index 01f3e6861a72..7b73a3b04e77 100644 --- a/resources/src/META-INF/IdeaPlugin.xml +++ b/resources/src/META-INF/IdeaPlugin.xml @@ -1900,6 +1900,7 @@ +