Search in sources :

Example 16 with ParameterInfoImpl

use of com.intellij.refactoring.changeSignature.ParameterInfoImpl in project intellij-community by JetBrains.

the class ChangeMethodSignatureFromUsageFix method findNewParamsPlace.

protected boolean findNewParamsPlace(PsiExpression[] expressions, PsiMethod targetMethod, PsiSubstitutor substitutor, StringBuilder buf, HashSet<ParameterInfoImpl> newParams, PsiParameter[] parameters, List<ParameterInfoImpl> result) {
    // find which parameters to introduce and where
    Set<String> existingNames = new HashSet<>();
    for (PsiParameter parameter : parameters) {
        existingNames.add(parameter.getName());
    }
    int ei = 0;
    int pi = 0;
    PsiParameter varargParam = targetMethod.isVarArgs() ? parameters[parameters.length - 1] : null;
    while (ei < expressions.length || pi < parameters.length) {
        if (buf.length() > 0)
            buf.append(", ");
        PsiExpression expression = ei < expressions.length ? expressions[ei] : null;
        PsiParameter parameter = pi < parameters.length ? parameters[pi] : null;
        PsiType paramType = parameter == null ? null : substitutor.substitute(parameter.getType());
        boolean parameterAssignable = paramType != null && (expression == null || TypeConversionUtil.areTypesAssignmentCompatible(paramType, expression));
        if (parameterAssignable) {
            final PsiType type = parameter.getType();
            result.add(new ParameterInfoImpl(pi, parameter.getName(), type));
            buf.append(escapePresentableType(type));
            pi++;
            ei++;
        } else if (isArgumentInVarargPosition(expressions, ei, varargParam, substitutor)) {
            if (pi == parameters.length - 1) {
                assert varargParam != null;
                final PsiType type = varargParam.getType();
                result.add(new ParameterInfoImpl(pi, varargParam.getName(), type));
                buf.append(escapePresentableType(type));
            }
            pi++;
            ei++;
        } else if (expression != null) {
            if (varargParam != null && pi >= parameters.length)
                return false;
            if (PsiPolyExpressionUtil.isPolyExpression(expression))
                return false;
            PsiType exprType = RefactoringUtil.getTypeByExpression(expression);
            if (exprType == null)
                return false;
            if (exprType instanceof PsiDisjunctionType) {
                exprType = ((PsiDisjunctionType) exprType).getLeastUpperBound();
            }
            JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(expression.getProject());
            String name = suggestUniqueParameterName(codeStyleManager, expression, exprType, existingNames);
            final ParameterInfoImpl newParameterInfo = new ParameterInfoImpl(-1, name, exprType, expression.getText().replace('\n', ' '));
            result.add(newParameterInfo);
            newParams.add(newParameterInfo);
            buf.append("<b>").append(escapePresentableType(exprType)).append("</b>");
            ei++;
        }
    }
    if (result.size() != expressions.length && varargParam == null)
        return false;
    return true;
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) ParameterInfoImpl(com.intellij.refactoring.changeSignature.ParameterInfoImpl)

Example 17 with ParameterInfoImpl

use of com.intellij.refactoring.changeSignature.ParameterInfoImpl in project intellij-community by JetBrains.

the class ChangeMethodSignatureFromUsageFix method formatTypesList.

@Nullable
private static String formatTypesList(ParameterInfoImpl[] infos, PsiElement context) {
    if (infos == null)
        return null;
    StringBuilder result = new StringBuilder();
    try {
        for (ParameterInfoImpl info : infos) {
            PsiType type = info.createType(context);
            if (type == null)
                return null;
            if (result.length() != 0)
                result.append(", ");
            result.append(type.getPresentableText());
        }
        return result.toString();
    } catch (IncorrectOperationException e) {
        return null;
    }
}
Also used : ParameterInfoImpl(com.intellij.refactoring.changeSignature.ParameterInfoImpl) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Nullable(org.jetbrains.annotations.Nullable)

Example 18 with ParameterInfoImpl

use of com.intellij.refactoring.changeSignature.ParameterInfoImpl in project intellij-community by JetBrains.

the class ChangeMethodSignatureFromUsageFix method getShortText.

@Nullable
private String getShortText() {
    final StringBuilder buf = new StringBuilder();
    final HashSet<ParameterInfoImpl> newParams = new HashSet<>();
    final HashSet<ParameterInfoImpl> removedParams = new HashSet<>();
    final HashSet<ParameterInfoImpl> changedParams = new HashSet<>();
    getNewParametersInfo(myExpressions, myTargetMethod, mySubstitutor, buf, newParams, removedParams, changedParams);
    final String targetMethodName = myTargetMethod.getName();
    if (myTargetMethod.getContainingClass().findMethodsByName(targetMethodName, true).length == 1) {
        if (newParams.size() == 1) {
            final ParameterInfoImpl p = newParams.iterator().next();
            return QuickFixBundle.message("add.parameter.from.usage.text", p.getTypeText(), (ArrayUtil.find(myNewParametersInfo, p) + 1), targetMethodName);
        }
        if (removedParams.size() == 1) {
            final ParameterInfoImpl p = removedParams.iterator().next();
            return QuickFixBundle.message("remove.parameter.from.usage.text", (p.getOldIndex() + 1), targetMethodName);
        }
        if (changedParams.size() == 1) {
            final ParameterInfoImpl p = changedParams.iterator().next();
            return QuickFixBundle.message("change.parameter.from.usage.text", (p.getOldIndex() + 1), targetMethodName, myTargetMethod.getParameterList().getParameters()[p.getOldIndex()].getType().getPresentableText(), p.getTypeText());
        }
    }
    return "<html> Change signature of " + targetMethodName + "(" + buf.toString() + ")</html>";
}
Also used : ParameterInfoImpl(com.intellij.refactoring.changeSignature.ParameterInfoImpl) Nullable(org.jetbrains.annotations.Nullable)

Example 19 with ParameterInfoImpl

use of com.intellij.refactoring.changeSignature.ParameterInfoImpl in project intellij-community by JetBrains.

the class MethodParameterFix method getNewParametersInfo.

@NotNull
private ParameterInfoImpl[] getNewParametersInfo(PsiMethod method) throws IncorrectOperationException {
    List<ParameterInfoImpl> result = new ArrayList<>();
    PsiParameter[] parameters = method.getParameterList().getParameters();
    PsiElementFactory factory = JavaPsiFacade.getInstance(method.getProject()).getElementFactory();
    JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(method.getProject());
    SuggestedNameInfo nameInfo = codeStyleManager.suggestVariableName(VariableKind.PARAMETER, null, null, myParameterType);
    PsiParameter newParameter = factory.createParameter(nameInfo.names[0], myParameterType);
    if (method.getContainingClass().isInterface()) {
        PsiUtil.setModifierProperty(newParameter, PsiModifier.FINAL, false);
    }
    for (int i = 0; i < parameters.length; i++) {
        PsiParameter parameter = parameters[i];
        if (i == myIndex) {
            newParameter.setName(parameter.getName());
            parameter = newParameter;
        }
        result.add(new ParameterInfoImpl(i, parameter.getName(), parameter.getType()));
    }
    if (parameters.length == myIndex) {
        result.add(new ParameterInfoImpl(-1, newParameter.getName(), newParameter.getType()));
    }
    return result.toArray(new ParameterInfoImpl[result.size()]);
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) ArrayList(java.util.ArrayList) ParameterInfoImpl(com.intellij.refactoring.changeSignature.ParameterInfoImpl) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) NotNull(org.jetbrains.annotations.NotNull)

Example 20 with ParameterInfoImpl

use of com.intellij.refactoring.changeSignature.ParameterInfoImpl in project intellij-community by JetBrains.

the class ChangeMethodSignatureFromUsageReverseOrderFix method isAvailable.

@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
    if (myTargetMethod.isValid() && myExpressions.length > myTargetMethod.getParameterList().getParametersCount()) {
        if (super.isAvailable(project, editor, file)) {
            final ArrayList<ParameterInfoImpl> result = new ArrayList<>();
            if (super.findNewParamsPlace(myExpressions, myTargetMethod, mySubstitutor, new StringBuilder(), new HashSet<>(), myTargetMethod.getParameterList().getParameters(), result)) {
                if (myNewParametersInfo.length != result.size())
                    return true;
                for (int i = 0, size = result.size(); i < size; i++) {
                    ParameterInfoImpl info = result.get(i);
                    info.setName(myNewParametersInfo[i].getName());
                    if (!myNewParametersInfo[i].equals(info))
                        return true;
                }
            }
        }
    }
    return false;
}
Also used : ArrayList(java.util.ArrayList) ParameterInfoImpl(com.intellij.refactoring.changeSignature.ParameterInfoImpl)

Aggregations

ParameterInfoImpl (com.intellij.refactoring.changeSignature.ParameterInfoImpl)34 ArrayList (java.util.ArrayList)10 ChangeSignatureProcessor (com.intellij.refactoring.changeSignature.ChangeSignatureProcessor)6 NotNull (org.jetbrains.annotations.NotNull)5 Nullable (org.jetbrains.annotations.Nullable)5 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)4 JavaChangeSignatureDialog (com.intellij.refactoring.changeSignature.JavaChangeSignatureDialog)4 HashSet (java.util.HashSet)3 JavaChangeInfoImpl (com.intellij.refactoring.changeSignature.JavaChangeInfoImpl)2 JavaMethodDescriptor (com.intellij.refactoring.changeSignature.JavaMethodDescriptor)2 ThrownExceptionInfo (com.intellij.refactoring.changeSignature.ThrownExceptionInfo)2 IntroduceParameterObjectProcessor (com.intellij.refactoring.introduceParameterObject.IntroduceParameterObjectProcessor)2 JavaIntroduceParameterObjectClassDescriptor (com.intellij.refactoring.introduceparameterobject.JavaIntroduceParameterObjectClassDescriptor)2 UsageInfo (com.intellij.usageView.UsageInfo)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 LocalQuickFixAndIntentionActionOnPsiElement (com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement)1 FindUsagesHandler (com.intellij.find.findUsages.FindUsagesHandler)1 FindUsagesManager (com.intellij.find.findUsages.FindUsagesManager)1 JavaMethodFindUsagesOptions (com.intellij.find.findUsages.JavaMethodFindUsagesOptions)1 FindManagerImpl (com.intellij.find.impl.FindManagerImpl)1