use of com.intellij.refactoring.changeSignature.ChangeSignatureProcessor in project intellij-community by JetBrains.
the class CreateParameterFromUsageFix method invokeImpl.
@Override
protected void invokeImpl(PsiClass targetClass) {
TransactionGuard.getInstance().submitTransactionLater(targetClass.getProject(), () -> {
if (!myReferenceExpression.isValid())
return;
if (CreateFromUsageUtils.isValidReference(myReferenceExpression, false))
return;
final Project project = myReferenceExpression.getProject();
PsiType[] expectedTypes = CreateFromUsageUtils.guessType(myReferenceExpression, false);
PsiType type = expectedTypes[0];
final String varName = myReferenceExpression.getReferenceName();
PsiMethod method = PsiTreeUtil.getParentOfType(myReferenceExpression, PsiMethod.class);
LOG.assertTrue(method != null);
method = IntroduceParameterHandler.chooseEnclosingMethod(method);
if (method == null)
return;
method = SuperMethodWarningUtil.checkSuperMethod(method, RefactoringBundle.message("to.refactor"));
if (method == null)
return;
final List<ParameterInfoImpl> parameterInfos = new ArrayList<>(Arrays.asList(ParameterInfoImpl.fromMethod(method)));
ParameterInfoImpl parameterInfo = new ParameterInfoImpl(-1, varName, type, varName, false);
if (!method.isVarArgs()) {
parameterInfos.add(parameterInfo);
} else {
parameterInfos.add(parameterInfos.size() - 1, parameterInfo);
}
if (ApplicationManager.getApplication().isUnitTestMode()) {
ParameterInfoImpl[] array = parameterInfos.toArray(new ParameterInfoImpl[parameterInfos.size()]);
String modifier = PsiUtil.getAccessModifier(PsiUtil.getAccessLevel(method.getModifierList()));
ChangeSignatureProcessor processor = new ChangeSignatureProcessor(project, method, false, modifier, method.getName(), method.getReturnType(), array);
processor.run();
} else {
try {
JavaChangeSignatureDialog dialog = JavaChangeSignatureDialog.createAndPreselectNew(project, method, parameterInfos, true, myReferenceExpression);
dialog.setParameterInfos(parameterInfos);
if (dialog.showAndGet()) {
for (ParameterInfoImpl info : parameterInfos) {
if (info.getOldIndex() == -1) {
final String newParamName = info.getName();
if (!Comparing.strEqual(varName, newParamName)) {
final PsiExpression newExpr = JavaPsiFacade.getElementFactory(project).createExpressionFromText(newParamName, method);
new WriteCommandAction(project) {
@Override
protected void run(@NotNull Result result) throws Throwable {
final PsiReferenceExpression[] refs = CreateFromUsageUtils.collectExpressions(myReferenceExpression, PsiMember.class, PsiFile.class);
for (PsiReferenceExpression ref : refs) {
ref.replace(newExpr.copy());
}
}
}.execute();
}
break;
}
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
}
use of com.intellij.refactoring.changeSignature.ChangeSignatureProcessor in project intellij-community by JetBrains.
the class MethodParameterFix method invoke.
@Override
public void invoke(@NotNull final Project project, @NotNull final PsiFile file, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
final PsiMethod myMethod = (PsiMethod) startElement;
if (!FileModificationService.getInstance().prepareFileForWrite(myMethod.getContainingFile()))
return;
try {
PsiMethod method = myMethod;
if (myFixWholeHierarchy) {
method = myMethod.findDeepestSuperMethod();
if (method == null)
method = myMethod;
}
final PsiMethod finalMethod = method;
ChangeSignatureProcessor processor = new ChangeSignatureProcessor(project, finalMethod, false, null, finalMethod.getName(), finalMethod.getReturnType(), getNewParametersInfo(finalMethod));
processor.run();
UndoUtil.markPsiFileForUndo(file);
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
use of com.intellij.refactoring.changeSignature.ChangeSignatureProcessor in project intellij-community by JetBrains.
the class MethodReturnTypeFix method changeReturnType.
@NotNull
private List<PsiMethod> changeReturnType(final PsiMethod method, @NotNull final PsiType returnType) {
final PsiMethod[] methods = getChangeRoots(method, returnType);
if (methods == null) {
// canceled
return Collections.emptyList();
}
final MethodSignatureChangeVisitor methodSignatureChangeVisitor = new MethodSignatureChangeVisitor();
for (PsiMethod targetMethod : methods) {
methodSignatureChangeVisitor.addBase(targetMethod);
ChangeSignatureProcessor processor = new UsagesAwareChangeSignatureProcessor(method.getProject(), targetMethod, false, null, myName, returnType, RemoveUnusedParameterFix.getNewParametersInfo(targetMethod, null), methodSignatureChangeVisitor);
processor.run();
}
return methodSignatureChangeVisitor.getAffectedMethods();
}
use of com.intellij.refactoring.changeSignature.ChangeSignatureProcessor in project intellij-community by JetBrains.
the class SuperMethodReturnFix method invoke.
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) {
if (!FileModificationService.getInstance().prepareFileForWrite(mySuperMethod.getContainingFile()))
return;
ChangeSignatureProcessor processor = new ChangeSignatureProcessor(project, mySuperMethod, false, null, mySuperMethod.getName(), mySuperMethodType, ParameterInfoImpl.fromMethod(mySuperMethod));
processor.run();
}
use of com.intellij.refactoring.changeSignature.ChangeSignatureProcessor in project intellij-community by JetBrains.
the class VariableTypeFix method changeMethodSignatureIfNeeded.
private boolean changeMethodSignatureIfNeeded(PsiVariable myVariable) {
if (myVariable instanceof PsiParameter) {
final PsiElement scope = ((PsiParameter) myVariable).getDeclarationScope();
if (scope instanceof PsiMethod) {
final PsiMethod method = (PsiMethod) scope;
final PsiMethod psiMethod = SuperMethodWarningUtil.checkSuperMethod(method, RefactoringBundle.message("to.refactor"));
if (psiMethod == null)
return true;
final int parameterIndex = method.getParameterList().getParameterIndex((PsiParameter) myVariable);
if (!FileModificationService.getInstance().prepareFileForWrite(psiMethod.getContainingFile()))
return true;
final ArrayList<ParameterInfoImpl> infos = new ArrayList<>();
int i = 0;
for (PsiParameter parameter : psiMethod.getParameterList().getParameters()) {
final boolean changeType = i == parameterIndex;
infos.add(new ParameterInfoImpl(i++, parameter.getName(), changeType ? getReturnType() : parameter.getType()));
}
if (!ApplicationManager.getApplication().isUnitTestMode()) {
final JavaChangeSignatureDialog dialog = new JavaChangeSignatureDialog(psiMethod.getProject(), psiMethod, false, myVariable);
dialog.setParameterInfos(infos);
dialog.show();
} else {
ChangeSignatureProcessor processor = new ChangeSignatureProcessor(psiMethod.getProject(), psiMethod, false, null, psiMethod.getName(), psiMethod.getReturnType(), infos.toArray(new ParameterInfoImpl[infos.size()]));
processor.run();
}
return true;
}
}
return false;
}
Aggregations