From 5231e0418f8a07d1edbabf7e2be338d5f479030f Mon Sep 17 00:00:00 2001 From: "alexey.afanasiev" Date: Wed, 6 Dec 2017 13:27:32 +0300 Subject: [PATCH] IDEA-182905 Refactoring ParameterCastFix. --- .../assignment/ParameterCastFix.java | 32 ++++++++++++------- .../type/GroovyTypeCheckVisitorHelper.java | 17 ++-------- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/assignment/ParameterCastFix.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/assignment/ParameterCastFix.java index 0426b8ef3a37..a9c025d9f88b 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/assignment/ParameterCastFix.java +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/assignment/ParameterCastFix.java @@ -1,4 +1,6 @@ -// Copyright 2000-2017 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-2017 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 org.jetbrains.plugins.groovy.codeInspection.assignment; import com.intellij.codeInspection.ProblemDescriptor; @@ -9,28 +11,32 @@ import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NotNull; import org.jetbrains.plugins.groovy.codeInspection.GroovyFix; +import org.jetbrains.plugins.groovy.codeInspection.type.GroovyTypeCheckVisitorHelper; import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList; import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression; -import org.jetbrains.plugins.groovy.lang.psi.impl.PsiImplUtil; import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil; +import java.util.List; + /** * @author Max Medvedev */ public class ParameterCastFix extends GroovyFix { - private final GrExpression myArgument; + @NotNull private final PsiType myType; + @NotNull private final String myName; + private final int myPosition; - public ParameterCastFix(int param, @NotNull PsiType type, @NotNull GrExpression argument) { - myArgument = argument; - myType = PsiImplUtil.normalizeWildcardTypeByPosition(type, argument); + public ParameterCastFix(int position, @NotNull PsiType type) { + myType = type; + myPosition = position; StringBuilder builder = new StringBuilder(); builder.append("Cast "); - builder.append(param + 1); - switch (param + 1) { + builder.append(position + 1); + switch (position + 1) { case 1: builder.append("st"); break; @@ -53,10 +59,14 @@ public class ParameterCastFix extends GroovyFix { @Override protected void doFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) throws IncorrectOperationException { final PsiElement element = descriptor.getPsiElement(); - final GrArgumentList list = element instanceof GrArgumentList ? (GrArgumentList)element :PsiUtil.getArgumentsList(element); + final GrArgumentList list = element instanceof GrArgumentList ? (GrArgumentList)element : PsiUtil.getArgumentsList(element); if (list == null) return; - GrCastFix.doSafeCast(project, myType, myArgument); + List callArguments = GroovyTypeCheckVisitorHelper.getExpressionArgumentsOfCall(list); + if (callArguments == null || myPosition >= callArguments.size()) return; + GrExpression expression = callArguments.get(myPosition); + + GrCastFix.doSafeCast(project, myType, expression); } @NotNull @@ -69,6 +79,6 @@ public class ParameterCastFix extends GroovyFix { @NotNull @Override public String getFamilyName() { - return "Add cast"; + return "Add parameter cast"; } } diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/type/GroovyTypeCheckVisitorHelper.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/type/GroovyTypeCheckVisitorHelper.java index 62b152232d2d..7973b5e106c9 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/type/GroovyTypeCheckVisitorHelper.java +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/type/GroovyTypeCheckVisitorHelper.java @@ -1,17 +1,5 @@ /* - * 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-2017 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 org.jetbrains.plugins.groovy.codeInspection.type; @@ -181,7 +169,8 @@ public class GroovyTypeCheckVisitorHelper { final ArrayList fixes = new ArrayList<>(); for (Pair error : allErrors) { if (args.size() > error.first && error.second != null) { - fixes.add(new ParameterCastFix(error.first, error.second, args.get(error.first))); + PsiType type = PsiImplUtil.normalizeWildcardTypeByPosition(error.second, args.get(error.first)); + if (type != null) fixes.add(new ParameterCastFix(error.first, type)); } } return fixes.toArray(new LocalQuickFix[fixes.size()]);