use of com.intellij.refactoring.changeSignature.ParameterInfoImpl in project intellij-community by JetBrains.
the class ChangeMethodSignatureFromUsageReverseOrderFix method findNewParamsPlace.
@Override
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 = expressions.length - 1;
int pi = parameters.length - 1;
final PsiParameter varargParam = targetMethod.isVarArgs() ? parameters[parameters.length - 1] : null;
final List<String> params = new ArrayList<>();
while (ei >= 0 || pi >= 0) {
PsiExpression expression = ei >= 0 ? expressions[ei] : null;
PsiParameter parameter = pi >= 0 ? 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(0, new ParameterInfoImpl(pi, parameter.getName(), type));
params.add(0, 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(0, new ParameterInfoImpl(pi, varargParam.getName(), type));
params.add(0, escapePresentableType(type));
}
pi--;
ei--;
} else if (expression != null) {
if (varargParam != null && pi >= parameters.length)
return false;
PsiType exprType = RefactoringUtil.getTypeByExpression(expression);
if (exprType == null)
return false;
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(0, newParameterInfo);
newParams.add(newParameterInfo);
params.add(0, "<b>" + escapePresentableType(exprType) + "</b>");
ei--;
}
}
if (result.size() != expressions.length && varargParam == null)
return false;
buf.append(StringUtil.join(params, ", "));
return true;
}
use of com.intellij.refactoring.changeSignature.ParameterInfoImpl in project intellij-community by JetBrains.
the class ChangeMethodSignatureFromUsageFix method getNewParametersInfo.
@Nullable
private ParameterInfoImpl[] getNewParametersInfo(PsiExpression[] expressions, PsiMethod targetMethod, PsiSubstitutor substitutor, final StringBuilder buf, final HashSet<ParameterInfoImpl> newParams, final HashSet<ParameterInfoImpl> removedParams, final HashSet<ParameterInfoImpl> changedParams) {
PsiParameter[] parameters = targetMethod.getParameterList().getParameters();
List<ParameterInfoImpl> result = new ArrayList<>();
if (expressions.length < parameters.length) {
// find which parameters to remove
int ei = 0;
int pi = 0;
while (ei < expressions.length && pi < parameters.length) {
PsiExpression expression = expressions[ei];
PsiParameter parameter = parameters[pi];
PsiType paramType = substitutor.substitute(parameter.getType());
if (buf.length() > 0)
buf.append(", ");
final PsiType parameterType = PsiUtil.convertAnonymousToBaseType(paramType);
final String presentableText = escapePresentableType(parameterType);
final ParameterInfoImpl parameterInfo = new ParameterInfoImpl(pi, parameter.getName(), parameter.getType());
if (TypeConversionUtil.areTypesAssignmentCompatible(paramType, expression)) {
buf.append(presentableText);
result.add(parameterInfo);
pi++;
ei++;
} else {
buf.append("<s>").append(presentableText).append("</s>");
removedParams.add(parameterInfo);
pi++;
}
}
if (result.size() != expressions.length)
return null;
for (int i = pi; i < parameters.length; i++) {
if (buf.length() > 0)
buf.append(", ");
buf.append("<s>").append(escapePresentableType(parameters[i].getType())).append("</s>");
final ParameterInfoImpl parameterInfo = new ParameterInfoImpl(pi, parameters[i].getName(), parameters[i].getType());
removedParams.add(parameterInfo);
}
} else if (expressions.length > parameters.length) {
if (!findNewParamsPlace(expressions, targetMethod, substitutor, buf, newParams, parameters, result))
return null;
} else {
//parameter type changed
for (int i = 0; i < parameters.length; i++) {
if (buf.length() > 0)
buf.append(", ");
PsiParameter parameter = parameters[i];
PsiExpression expression = expressions[i];
PsiType paramType = substitutor.substitute(parameter.getType());
final String presentableText = escapePresentableType(paramType);
if (TypeConversionUtil.areTypesAssignmentCompatible(paramType, expression)) {
result.add(new ParameterInfoImpl(i, parameter.getName(), paramType));
buf.append(presentableText);
} else {
if (PsiPolyExpressionUtil.isPolyExpression(expression))
return null;
PsiType exprType = RefactoringUtil.getTypeByExpression(expression);
if (exprType == null)
return null;
if (exprType instanceof PsiDisjunctionType) {
exprType = ((PsiDisjunctionType) exprType).getLeastUpperBound();
}
final ParameterInfoImpl changedParameterInfo = new ParameterInfoImpl(i, parameter.getName(), exprType);
result.add(changedParameterInfo);
changedParams.add(changedParameterInfo);
buf.append("<s>").append(presentableText).append("</s> <b>").append(escapePresentableType(exprType)).append("</b>");
}
}
// do not perform silly refactorings
boolean isSilly = true;
for (int i = 0; i < result.size(); i++) {
PsiParameter parameter = parameters[i];
PsiType paramType = substitutor.substitute(parameter.getType());
ParameterInfoImpl parameterInfo = result.get(i);
String typeText = parameterInfo.getTypeText();
if (!paramType.equalsToText(typeText) && !paramType.getPresentableText().equals(typeText)) {
isSilly = false;
break;
}
}
if (isSilly)
return null;
}
return result.toArray(new ParameterInfoImpl[result.size()]);
}
use of com.intellij.refactoring.changeSignature.ParameterInfoImpl in project intellij-community by JetBrains.
the class ChangeMethodSignatureFromUsageFix method performChange.
public static List<ParameterInfoImpl> performChange(final Project project, final Editor editor, final PsiFile file, final PsiMethod method, final int minUsagesNumber, final ParameterInfoImpl[] newParametersInfo, final boolean changeAllUsages, final boolean allowDelegation) {
if (!FileModificationService.getInstance().prepareFileForWrite(method.getContainingFile()))
return null;
final FindUsagesManager findUsagesManager = ((FindManagerImpl) FindManager.getInstance(project)).getFindUsagesManager();
final FindUsagesHandler handler = findUsagesManager.getFindUsagesHandler(method, false);
//on failure or cancel (e.g. cancel of super methods dialog)
if (handler == null)
return null;
final JavaMethodFindUsagesOptions options = new JavaMethodFindUsagesOptions(project);
options.isImplementingMethods = true;
options.isOverridingMethods = true;
options.isUsages = true;
options.isSearchForTextOccurrences = false;
final int[] usagesFound = new int[1];
Runnable runnable = () -> {
Processor<UsageInfo> processor = t -> ++usagesFound[0] < minUsagesNumber;
handler.processElementUsages(method, processor, options);
};
String progressTitle = QuickFixBundle.message("searching.for.usages.progress.title");
if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(runnable, progressTitle, true, project))
return null;
if (ApplicationManager.getApplication().isUnitTestMode() || usagesFound[0] < minUsagesNumber) {
ChangeSignatureProcessor processor = new ChangeSignatureProcessor(project, method, false, null, method.getName(), method.getReturnType(), newParametersInfo) {
@Override
@NotNull
protected UsageInfo[] findUsages() {
return changeAllUsages ? super.findUsages() : UsageInfo.EMPTY_ARRAY;
}
@Override
protected void performRefactoring(@NotNull UsageInfo[] usages) {
CommandProcessor.getInstance().setCurrentCommandName(getCommandName());
super.performRefactoring(usages);
}
};
processor.run();
ApplicationManager.getApplication().runWriteAction(() -> UndoUtil.markPsiFileForUndo(file));
return Arrays.asList(newParametersInfo);
} else {
final List<ParameterInfoImpl> parameterInfos = newParametersInfo != null ? new ArrayList<>(Arrays.asList(newParametersInfo)) : new ArrayList<>();
final PsiReferenceExpression refExpr = JavaTargetElementEvaluator.findReferenceExpression(editor);
JavaChangeSignatureDialog dialog = JavaChangeSignatureDialog.createAndPreselectNew(project, method, parameterInfos, allowDelegation, refExpr);
dialog.setParameterInfos(parameterInfos);
dialog.show();
return dialog.isOK() ? dialog.getParameters() : null;
}
}
use of com.intellij.refactoring.changeSignature.ParameterInfoImpl in project intellij-community by JetBrains.
the class MakeMethodStaticProcessor method changeSelf.
protected void changeSelf(PsiElementFactory factory, UsageInfo[] usages) throws IncorrectOperationException {
final MethodJavaDocHelper javaDocHelper = new MethodJavaDocHelper(myMember);
PsiParameterList paramList = myMember.getParameterList();
PsiElement addParameterAfter = null;
PsiDocTag anchor = null;
List<PsiType> addedTypes = new ArrayList<>();
final PsiClass containingClass = myMember.getContainingClass();
LOG.assertTrue(containingClass != null);
if (mySettings.isDelegate()) {
List<ParameterInfoImpl> params = new ArrayList<>();
PsiParameter[] parameters = myMember.getParameterList().getParameters();
if (mySettings.isMakeClassParameter()) {
params.add(new ParameterInfoImpl(-1, mySettings.getClassParameterName(), factory.createType(containingClass, PsiSubstitutor.EMPTY), "this"));
}
if (mySettings.isMakeFieldParameters()) {
for (Settings.FieldParameter parameter : mySettings.getParameterOrderList()) {
params.add(new ParameterInfoImpl(-1, mySettings.getClassParameterName(), parameter.type, parameter.field.getName()));
}
}
for (int i = 0; i < parameters.length; i++) {
params.add(new ParameterInfoImpl(i));
}
final PsiType returnType = myMember.getReturnType();
LOG.assertTrue(returnType != null);
JavaChangeSignatureUsageProcessor.generateDelegate(new JavaChangeInfoImpl(VisibilityUtil.getVisibilityModifier(myMember.getModifierList()), myMember, myMember.getName(), CanonicalTypes.createTypeWrapper(returnType), params.toArray(new ParameterInfoImpl[params.size()]), new ThrownExceptionInfo[0], false, Collections.<PsiMethod>emptySet(), Collections.<PsiMethod>emptySet()));
}
if (mySettings.isMakeClassParameter()) {
// Add parameter for object
PsiType parameterType = factory.createType(containingClass, PsiSubstitutor.EMPTY);
addedTypes.add(parameterType);
final String classParameterName = mySettings.getClassParameterName();
PsiParameter parameter = factory.createParameter(classParameterName, parameterType);
if (makeClassParameterFinal(usages)) {
PsiUtil.setModifierProperty(parameter, PsiModifier.FINAL, true);
}
addParameterAfter = paramList.addAfter(parameter, null);
anchor = javaDocHelper.addParameterAfter(classParameterName, anchor);
}
if (mySettings.isMakeFieldParameters()) {
List<Settings.FieldParameter> parameters = mySettings.getParameterOrderList();
for (Settings.FieldParameter fieldParameter : parameters) {
final PsiType fieldParameterType = fieldParameter.field.getType();
final PsiParameter parameter = factory.createParameter(fieldParameter.name, fieldParameterType);
addedTypes.add(fieldParameterType);
if (makeFieldParameterFinal(fieldParameter.field, usages)) {
PsiUtil.setModifierProperty(parameter, PsiModifier.FINAL, true);
}
addParameterAfter = paramList.addAfter(parameter, addParameterAfter);
anchor = javaDocHelper.addParameterAfter(fieldParameter.name, anchor);
}
}
makeStatic(myMember);
if (myAdditionalMethods != null) {
for (PsiMethod method : myAdditionalMethods) {
makeStatic(method);
}
}
}
use of com.intellij.refactoring.changeSignature.ParameterInfoImpl in project intellij-community by JetBrains.
the class ChangeSignatureTest method testWarnAboutContract.
public void testWarnAboutContract() {
try {
doTest(null, new ParameterInfoImpl[] { new ParameterInfoImpl(1), new ParameterInfoImpl(0) }, false);
fail("Conflict expected");
} catch (BaseRefactoringProcessor.ConflictsInTestsException ignored) {
}
}
Aggregations