IDEA-182905 Refactoring ParameterCastFix.

This commit is contained in:
alexey.afanasiev
2017-12-06 13:33:49 +03:00
parent 97930db6fb
commit 5231e0418f
2 changed files with 24 additions and 25 deletions
@@ -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<GrExpression> 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";
}
}
@@ -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<LocalQuickFix> fixes = new ArrayList<>();
for (Pair<Integer, PsiType> 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()]);