use of com.intellij.refactoring.changeSignature.JavaChangeSignatureDialog in project intellij-community by JetBrains.
the class CreateParameterFromUsageFix method showDialog.
private static void showDialog(final PsiMethod method, final GrReferenceExpression ref, final Project project) {
final String name = ref.getReferenceName();
final List<PsiType> types = GroovyExpectedTypesProvider.getDefaultExpectedTypes(ref);
PsiType unboxed = types.isEmpty() ? null : TypesUtil.unboxPrimitiveTypeWrapper(types.get(0));
@NotNull final PsiType type = unboxed != null ? unboxed : PsiType.getJavaLangObject(ref.getManager(), ref.getResolveScope());
if (method instanceof GrMethod) {
GrMethodDescriptor descriptor = new GrMethodDescriptor((GrMethod) method);
GrChangeSignatureDialog dialog = new GrChangeSignatureDialog(project, descriptor, true, ref);
List<GrParameterInfo> parameters = dialog.getParameters();
parameters.add(createParameterInfo(name, type));
dialog.setParameterInfos(parameters);
dialog.show();
} else if (method != null) {
JavaChangeSignatureDialog dialog = new JavaChangeSignatureDialog(project, method, false, ref);
final List<ParameterInfoImpl> parameterInfos = new ArrayList<>(Arrays.asList(ParameterInfoImpl.fromMethod(method)));
ParameterInfoImpl parameterInfo = new ParameterInfoImpl(-1, name, type, PsiTypesUtil.getDefaultValueOfType(type), false);
if (!method.isVarArgs()) {
parameterInfos.add(parameterInfo);
} else {
parameterInfos.add(parameterInfos.size() - 1, parameterInfo);
}
dialog.setParameterInfos(parameterInfos);
dialog.show();
}
}
use of com.intellij.refactoring.changeSignature.JavaChangeSignatureDialog 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.JavaChangeSignatureDialog 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;
}
use of com.intellij.refactoring.changeSignature.JavaChangeSignatureDialog 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;
}
}
Aggregations