diff --git a/plugin/src/main/java/com/intellij/java/impl/codeInsight/daemon/impl/quickfix/ChangeMethodSignatureFromUsageFix.java b/plugin/src/main/java/com/intellij/java/impl/codeInsight/daemon/impl/quickfix/ChangeMethodSignatureFromUsageFix.java index 9ad55d0a9..7f0471aba 100644 --- a/plugin/src/main/java/com/intellij/java/impl/codeInsight/daemon/impl/quickfix/ChangeMethodSignatureFromUsageFix.java +++ b/plugin/src/main/java/com/intellij/java/impl/codeInsight/daemon/impl/quickfix/ChangeMethodSignatureFromUsageFix.java @@ -226,7 +226,7 @@ public void invoke(@Nonnull Project project, Editor editor, PsiFile file) { return; } - PsiMethod method = SuperMethodWarningUtil.checkSuperMethod(myTargetMethod, RefactoringLocalize.toRefactor().get()); + PsiMethod method = SuperMethodWarningUtil.checkSuperMethod(myTargetMethod, RefactoringLocalize.toRefactor()); if (method == null) { return; } @@ -457,8 +457,8 @@ else if (expressions.length > parameters.length) { if (exprType == null || PsiType.VOID.equals(exprType)) { return null; } - if (exprType instanceof PsiDisjunctionType) { - exprType = ((PsiDisjunctionType) exprType).getLeastUpperBound(); + if (exprType instanceof PsiDisjunctionType disjunctionType) { + exprType = disjunctionType.getLeastUpperBound(); } if (!PsiTypesUtil.allTypeParametersResolved(myTargetMethod, exprType)) { return null; @@ -548,8 +548,8 @@ else if (expression != null) { if (exprType == null || PsiType.VOID.equals(exprType)) { return false; } - if (exprType instanceof PsiDisjunctionType) { - exprType = ((PsiDisjunctionType) exprType).getLeastUpperBound(); + if (exprType instanceof PsiDisjunctionType disjunctionType) { + exprType = disjunctionType.getLeastUpperBound(); } if (!PsiTypesUtil.allTypeParametersResolved(myTargetMethod, exprType)) { return false; diff --git a/plugin/src/main/java/com/intellij/java/impl/codeInsight/daemon/impl/quickfix/CreateParameterFromUsageFix.java b/plugin/src/main/java/com/intellij/java/impl/codeInsight/daemon/impl/quickfix/CreateParameterFromUsageFix.java index ded3c9614..706a811c7 100644 --- a/plugin/src/main/java/com/intellij/java/impl/codeInsight/daemon/impl/quickfix/CreateParameterFromUsageFix.java +++ b/plugin/src/main/java/com/intellij/java/impl/codeInsight/daemon/impl/quickfix/CreateParameterFromUsageFix.java @@ -23,8 +23,8 @@ import com.intellij.java.language.psi.*; import com.intellij.java.language.psi.util.PsiTypesUtil; import com.intellij.java.language.psi.util.PsiUtil; +import consulo.annotation.access.RequiredWriteAction; import consulo.application.Application; -import consulo.application.ApplicationManager; import consulo.application.Result; import consulo.java.analysis.impl.localize.JavaQuickFixLocalize; import consulo.language.editor.WriteCommandAction; @@ -35,6 +35,7 @@ import consulo.localize.LocalizeValue; import consulo.logging.Logger; import consulo.project.Project; +import consulo.ui.annotation.RequiredUIAccess; import consulo.util.lang.Comparing; import java.util.ArrayList; @@ -45,111 +46,131 @@ * @author Mike */ public class CreateParameterFromUsageFix extends CreateVarFromUsageFix { - private static final Logger LOG = Logger.getInstance(CreateParameterFromUsageFix.class); + private static final Logger LOG = Logger.getInstance(CreateParameterFromUsageFix.class); - public CreateParameterFromUsageFix(PsiReferenceExpression referenceElement) { - super(referenceElement); - setText(JavaQuickFixLocalize.createParameterFromUsageFamily()); - } + public CreateParameterFromUsageFix(PsiReferenceExpression referenceElement) { + super(referenceElement); + setText(JavaQuickFixLocalize.createParameterFromUsageFamily()); + } - @Override - protected boolean isAvailableImpl(int offset) { - if (!super.isAvailableImpl(offset)) return false; - if(myReferenceExpression.isQualified()) return false; - PsiElement scope = myReferenceExpression; - do { - scope = PsiTreeUtil.getParentOfType(scope, PsiMethod.class, PsiClass.class); - if (!(scope instanceof PsiAnonymousClass)) { - return scope instanceof PsiMethod && - ((PsiMethod)scope).getParameterList().isPhysical(); - } + @Override + protected boolean isAvailableImpl(int offset) { + if (!super.isAvailableImpl(offset)) { + return false; + } + if (myReferenceExpression.isQualified()) { + return false; + } + PsiElement scope = myReferenceExpression; + do { + scope = PsiTreeUtil.getParentOfType(scope, PsiMethod.class, PsiClass.class); + if (!(scope instanceof PsiAnonymousClass)) { + return scope instanceof PsiMethod method + && method.getParameterList().isPhysical(); + } + } + while (true); } - while (true); - } - @Override - public LocalizeValue getText(String varName) { - return JavaQuickFixLocalize.createParameterFromUsageText(varName); - } + @Override + public LocalizeValue getText(String varName) { + return JavaQuickFixLocalize.createParameterFromUsageText(varName); + } - @Override - protected void invokeImpl(PsiClass targetClass) { - if (CreateFromUsageUtils.isValidReference(myReferenceExpression, false)) return; + @Override + @RequiredUIAccess + protected void invokeImpl(PsiClass targetClass) { + if (CreateFromUsageUtils.isValidReference(myReferenceExpression, false)) { + return; + } - final Project project = myReferenceExpression.getProject(); + final Project project = myReferenceExpression.getProject(); - PsiType[] expectedTypes = CreateFromUsageUtils.guessType(myReferenceExpression, false); - PsiType type = expectedTypes[0]; + 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; + 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, RefactoringLocalize.toRefactor().get()); - if (method == null) return; + method = SuperMethodWarningUtil.checkSuperMethod(method, RefactoringLocalize.toRefactor()); + if (method == null) { + return; + } - final List parameterInfos = - new ArrayList(Arrays.asList(ParameterInfoImpl.fromMethod(method))); - ParameterInfoImpl parameterInfo = new ParameterInfoImpl(-1, varName, type, PsiTypesUtil.getDefaultValueOfType(type), false); - if (!method.isVarArgs()) { - parameterInfos.add(parameterInfo); - } - else { - parameterInfos.add(parameterInfos.size() - 1, parameterInfo); - } + List parameterInfos = new ArrayList<>(Arrays.asList(ParameterInfoImpl.fromMethod(method))); + ParameterInfoImpl parameterInfo = new ParameterInfoImpl(-1, varName, type, PsiTypesUtil.getDefaultValueOfType(type), false); + if (!method.isVarArgs()) { + parameterInfos.add(parameterInfo); + } + else { + parameterInfos.add(parameterInfos.size() - 1, parameterInfo); + } - Application application = ApplicationManager.getApplication(); - if (application.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 { - final PsiMethod finalMethod = method; - application.invokeLater(new Runnable() { - @Override - public void run() { - if (project.isDisposed()) return; - try { - JavaChangeSignatureDialog dialog = JavaChangeSignatureDialog.createAndPreselectNew(project, finalMethod, parameterInfos, true, myReferenceExpression); - dialog.setParameterInfos(parameterInfos); - dialog.show(); - if (dialog.isOK()) { - for (ParameterInfoImpl info : parameterInfos) { - if (info.getOldIndex() == -1) { - String newParamName = info.getName(); - if (!Comparing.strEqual(varName, newParamName)) { - final PsiExpression newExpr = JavaPsiFacade.getElementFactory(project).createExpressionFromText(newParamName, finalMethod); - new WriteCommandAction(project){ - @Override - protected void run(Result result) throws Throwable { - PsiReferenceExpression[] refs = - CreateFromUsageUtils.collectExpressions(myReferenceExpression, PsiMember.class, PsiFile.class); - for (PsiReferenceExpression ref : refs) { - ref.replace(newExpr.copy()); + Application application = Application.get(); + if (application.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 { + PsiMethod finalMethod = method; + application.invokeLater(() -> { + if (project.isDisposed()) { + return; + } + try { + JavaChangeSignatureDialog dialog = JavaChangeSignatureDialog.createAndPreselectNew( + project, + finalMethod, + parameterInfos, + true, + myReferenceExpression + ); + dialog.setParameterInfos(parameterInfos); + dialog.show(); + if (dialog.isOK()) { + for (ParameterInfoImpl info : parameterInfos) { + if (info.getOldIndex() == -1) { + String newParamName = info.getName(); + if (!Comparing.strEqual(varName, newParamName)) { + final PsiExpression newExpr = + JavaPsiFacade.getElementFactory(project).createExpressionFromText(newParamName, finalMethod); + new WriteCommandAction(project) { + @Override + @RequiredWriteAction + protected void run(Result result) throws Throwable { + PsiReferenceExpression[] refs = CreateFromUsageUtils.collectExpressions( + myReferenceExpression, + PsiMember.class, + PsiFile.class + ); + for (PsiReferenceExpression ref : refs) { + ref.replace(newExpr.copy()); + } + } + }.execute(); + } + break; + } } - } - }.execute(); - } - break; + } } - } - } - } - catch (Exception e) { - throw new RuntimeException(e); - } + catch (Exception e) { + throw new RuntimeException(e); + } + }); } - }); } - } - @Override - protected boolean isAllowOuterTargetClass() { - return false; - } + @Override + protected boolean isAllowOuterTargetClass() { + return false; + } } diff --git a/plugin/src/main/java/com/intellij/java/impl/codeInsight/daemon/impl/quickfix/VariableTypeFix.java b/plugin/src/main/java/com/intellij/java/impl/codeInsight/daemon/impl/quickfix/VariableTypeFix.java index 20f05e375..ea4923eb8 100644 --- a/plugin/src/main/java/com/intellij/java/impl/codeInsight/daemon/impl/quickfix/VariableTypeFix.java +++ b/plugin/src/main/java/com/intellij/java/impl/codeInsight/daemon/impl/quickfix/VariableTypeFix.java @@ -36,11 +36,13 @@ import consulo.localize.LocalizeValue; import consulo.logging.Logger; import consulo.project.Project; +import consulo.ui.annotation.RequiredUIAccess; import consulo.usage.UsageViewUtil; import jakarta.annotation.Nonnull; import jakarta.annotation.Nullable; import java.util.ArrayList; +import java.util.List; public class VariableTypeFix extends LocalQuickFixAndIntentionActionOnPsiElement { static final Logger LOG = Logger.getInstance(VariableTypeFix.class); @@ -121,46 +123,42 @@ protected void run() throws Throwable { }.execute(); } + @RequiredUIAccess private boolean changeMethodSignatureIfNeeded(PsiVariable myVariable) { - if (myVariable instanceof PsiParameter) { - PsiElement scope = ((PsiParameter) myVariable).getDeclarationScope(); - if (scope instanceof PsiMethod) { - PsiMethod method = (PsiMethod) scope; - PsiMethod psiMethod = SuperMethodWarningUtil.checkSuperMethod(method, RefactoringLocalize.toRefactor().get()); - if (psiMethod == null) { - return true; - } - int parameterIndex = method.getParameterList().getParameterIndex((PsiParameter) myVariable); - if (!FileModificationService.getInstance().prepareFileForWrite(psiMethod.getContainingFile())) { - return true; - } - ArrayList infos = new ArrayList(); - int i = 0; - for (PsiParameter parameter : psiMethod.getParameterList().getParameters()) { - boolean changeType = i == parameterIndex; - infos.add(new ParameterInfoImpl(i++, parameter.getName(), changeType ? getReturnType() : parameter.getType())); - } - - if (!ApplicationManager.getApplication().isUnitTestMode()) { - 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(); - } + if (myVariable instanceof PsiParameter param && param.getDeclarationScope() instanceof PsiMethod method) { + PsiMethod psiMethod = SuperMethodWarningUtil.checkSuperMethod(method, RefactoringLocalize.toRefactor()); + if (psiMethod == null) { return true; } + int parameterIndex = method.getParameterList().getParameterIndex(param); + if (!FileModificationService.getInstance().prepareFileForWrite(psiMethod.getContainingFile())) { + return true; + } + List infos = new ArrayList<>(); + int i = 0; + for (PsiParameter parameter : psiMethod.getParameterList().getParameters()) { + boolean changeType = i == parameterIndex; + infos.add(new ParameterInfoImpl(i++, parameter.getName(), changeType ? getReturnType() : parameter.getType())); + } + + if (!param.getApplication().isUnitTestMode()) { + JavaChangeSignatureDialog dialog = new JavaChangeSignatureDialog(psiMethod.getProject(), psiMethod, false, param); + 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; } diff --git a/plugin/src/main/java/com/intellij/java/impl/find/findUsages/JavaFindUsagesHandler.java b/plugin/src/main/java/com/intellij/java/impl/find/findUsages/JavaFindUsagesHandler.java index e247bbb62..1f3934f12 100644 --- a/plugin/src/main/java/com/intellij/java/impl/find/findUsages/JavaFindUsagesHandler.java +++ b/plugin/src/main/java/com/intellij/java/impl/find/findUsages/JavaFindUsagesHandler.java @@ -247,7 +247,7 @@ public PsiElement[] getSecondaryElements() { for (PsiMethod accessor : accessors) { ContainerUtil.addAll( elements, - SuperMethodWarningUtil.checkSuperMethods(accessor, FindLocalize.findSuperMethodWarningActionVerb().get()) + SuperMethodWarningUtil.checkSuperMethods(accessor, FindLocalize.findSuperMethodWarningActionVerb()) ); } return PsiUtilCore.toPsiElementArray(elements); diff --git a/plugin/src/main/java/com/intellij/java/impl/find/findUsages/JavaFindUsagesHandlerFactory.java b/plugin/src/main/java/com/intellij/java/impl/find/findUsages/JavaFindUsagesHandlerFactory.java index 77e756b12..def0b16c8 100644 --- a/plugin/src/main/java/com/intellij/java/impl/find/findUsages/JavaFindUsagesHandlerFactory.java +++ b/plugin/src/main/java/com/intellij/java/impl/find/findUsages/JavaFindUsagesHandlerFactory.java @@ -25,83 +25,86 @@ import consulo.find.FindBundle; import consulo.find.FindUsagesHandler; import consulo.find.FindUsagesHandlerFactory; +import consulo.find.localize.FindLocalize; import consulo.language.psi.PsiDirectory; import consulo.language.psi.PsiElement; import consulo.project.Project; +import consulo.ui.annotation.RequiredUIAccess; import jakarta.inject.Inject; import jakarta.annotation.Nonnull; /** * @author peter -*/ + */ @ExtensionImpl(id = "java", order = "last, before default") public class JavaFindUsagesHandlerFactory extends FindUsagesHandlerFactory { - private final JavaClassFindUsagesOptions myFindClassOptions; - private final JavaMethodFindUsagesOptions myFindMethodOptions; - private final JavaPackageFindUsagesOptions myFindPackageOptions; - private final JavaThrowFindUsagesOptions myFindThrowOptions; - private final JavaVariableFindUsagesOptions myFindVariableOptions; - - public static JavaFindUsagesHandlerFactory getInstance(@Nonnull Project project) { - return project.getExtensionPoint(FindUsagesHandlerFactory.class).findExtensionOrFail(JavaFindUsagesHandlerFactory.class); - } - - @Inject - public JavaFindUsagesHandlerFactory(Project project) { - myFindClassOptions = new JavaClassFindUsagesOptions(project); - myFindMethodOptions = new JavaMethodFindUsagesOptions(project); - myFindPackageOptions = new JavaPackageFindUsagesOptions(project); - myFindThrowOptions = new JavaThrowFindUsagesOptions(project); - myFindVariableOptions = new JavaVariableFindUsagesOptions(project); - } + private final JavaClassFindUsagesOptions myFindClassOptions; + private final JavaMethodFindUsagesOptions myFindMethodOptions; + private final JavaPackageFindUsagesOptions myFindPackageOptions; + private final JavaThrowFindUsagesOptions myFindThrowOptions; + private final JavaVariableFindUsagesOptions myFindVariableOptions; - @Override - public boolean canFindUsages(@Nonnull PsiElement element) { - return new JavaFindUsagesProvider().canFindUsagesFor(element); - } + public static JavaFindUsagesHandlerFactory getInstance(@Nonnull Project project) { + return project.getExtensionPoint(FindUsagesHandlerFactory.class).findExtensionOrFail(JavaFindUsagesHandlerFactory.class); + } - @Override - public FindUsagesHandler createFindUsagesHandler(@Nonnull PsiElement element, boolean forHighlightUsages) { - if (element instanceof PsiDirectory directory) { - PsiJavaPackage psiPackage = JavaDirectoryService.getInstance().getPackage(directory); - return psiPackage == null ? null : new JavaFindUsagesHandler(psiPackage, this); + @Inject + public JavaFindUsagesHandlerFactory(Project project) { + myFindClassOptions = new JavaClassFindUsagesOptions(project); + myFindMethodOptions = new JavaMethodFindUsagesOptions(project); + myFindPackageOptions = new JavaPackageFindUsagesOptions(project); + myFindThrowOptions = new JavaThrowFindUsagesOptions(project); + myFindVariableOptions = new JavaVariableFindUsagesOptions(project); } - if (element instanceof PsiMethod method && !forHighlightUsages) { - PsiMethod[] methods = SuperMethodWarningUtil.checkSuperMethods( - method, - FindBundle.message("find.super.method.warning.action.verb") - ); - if (methods.length > 1) { - return new JavaFindUsagesHandler(element, methods, this); - } - if (methods.length == 1) { - return new JavaFindUsagesHandler(methods[0], this); - } - return FindUsagesHandler.NULL_HANDLER; + @Override + public boolean canFindUsages(@Nonnull PsiElement element) { + return new JavaFindUsagesProvider().canFindUsagesFor(element); } - return new JavaFindUsagesHandler(element, this); - } + @Override + @RequiredUIAccess + public FindUsagesHandler createFindUsagesHandler(@Nonnull PsiElement element, boolean forHighlightUsages) { + if (element instanceof PsiDirectory directory) { + PsiJavaPackage psiPackage = JavaDirectoryService.getInstance().getPackage(directory); + return psiPackage == null ? null : new JavaFindUsagesHandler(psiPackage, this); + } + + if (element instanceof PsiMethod method && !forHighlightUsages) { + PsiMethod[] methods = SuperMethodWarningUtil.checkSuperMethods( + method, + FindLocalize.findSuperMethodWarningActionVerb() + ); + if (methods.length > 1) { + return new JavaFindUsagesHandler(element, methods, this); + } + if (methods.length == 1) { + return new JavaFindUsagesHandler(methods[0], this); + } + return FindUsagesHandler.NULL_HANDLER; + } + + return new JavaFindUsagesHandler(element, this); + } - public JavaClassFindUsagesOptions getFindClassOptions() { - return myFindClassOptions; - } + public JavaClassFindUsagesOptions getFindClassOptions() { + return myFindClassOptions; + } - public JavaMethodFindUsagesOptions getFindMethodOptions() { - return myFindMethodOptions; - } + public JavaMethodFindUsagesOptions getFindMethodOptions() { + return myFindMethodOptions; + } - public JavaPackageFindUsagesOptions getFindPackageOptions() { - return myFindPackageOptions; - } + public JavaPackageFindUsagesOptions getFindPackageOptions() { + return myFindPackageOptions; + } - public JavaThrowFindUsagesOptions getFindThrowOptions() { - return myFindThrowOptions; - } + public JavaThrowFindUsagesOptions getFindThrowOptions() { + return myFindThrowOptions; + } - public JavaVariableFindUsagesOptions getFindVariableOptions() { - return myFindVariableOptions; - } + public JavaVariableFindUsagesOptions getFindVariableOptions() { + return myFindVariableOptions; + } } diff --git a/plugin/src/main/java/com/intellij/java/impl/ide/util/SuperMethodWarningDialog.java b/plugin/src/main/java/com/intellij/java/impl/ide/util/SuperMethodWarningDialog.java index c6ecfc0e9..7b421b609 100644 --- a/plugin/src/main/java/com/intellij/java/impl/ide/util/SuperMethodWarningDialog.java +++ b/plugin/src/main/java/com/intellij/java/impl/ide/util/SuperMethodWarningDialog.java @@ -16,10 +16,14 @@ package com.intellij.java.impl.ide.util; import consulo.ide.localize.IdeLocalize; +import consulo.java.localize.JavaLocalize; +import consulo.language.editor.refactoring.localize.RefactoringLocalize; +import consulo.localize.LocalizeValue; import consulo.platform.base.localize.CommonLocalize; import consulo.project.Project; import consulo.ui.ex.awt.DialogWrapper; import consulo.ui.ex.awt.JBLabel; +import consulo.ui.ex.awt.LocalizeAction; import consulo.ui.ex.awt.UIUtil; import consulo.ui.image.Image; import jakarta.annotation.Nonnull; @@ -30,88 +34,96 @@ //TODO: review title and text!!! class SuperMethodWarningDialog extends DialogWrapper { - public static final int NO_EXIT_CODE = NEXT_USER_EXIT_CODE + 1; - private final String myName; - private final String[] myClassNames; - private final String myActionString; - private final boolean myIsSuperAbstract; - private final boolean myIsParentInterface; - private final boolean myIsContainedInInterface; + public static final int NO_EXIT_CODE = NEXT_USER_EXIT_CODE + 1; + private final String myName; + private final String[] myClassNames; + @Nonnull + private final LocalizeValue myActionString; + private final boolean myIsSuperAbstract; + private final boolean myIsParentInterface; + private final boolean myIsContainedInInterface; - public SuperMethodWarningDialog( - Project project, - String name, - String actionString, - boolean isSuperAbstract, - boolean isParentInterface, - boolean isContainedInInterface, - String... classNames - ) { - super(project, true); - myName = name; - myClassNames = classNames; - myActionString = actionString; - myIsSuperAbstract = isSuperAbstract; - myIsParentInterface = isParentInterface; - myIsContainedInInterface = isContainedInInterface; - setTitle(IdeLocalize.titleWarning()); - setButtonsAlignment(SwingUtilities.CENTER); - setOKButtonText(CommonLocalize.buttonYes().get()); - init(); - } - - @Nonnull - protected Action[] createActions(){ - return new Action[]{getOKAction(),new NoAction(),getCancelAction()}; - } - - public JComponent createNorthPanel() { - JPanel panel = new JPanel(new BorderLayout()); - panel.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10)); - Image icon = UIUtil.getWarningIcon(); - if (icon != null){ - JLabel iconLabel = new JBLabel(UIUtil.getQuestionIcon()); - panel.add(iconLabel, BorderLayout.WEST); + public SuperMethodWarningDialog( + Project project, + String name, + @Nonnull LocalizeValue actionString, + boolean isSuperAbstract, + boolean isParentInterface, + boolean isContainedInInterface, + String... classNames + ) { + super(project, true); + myName = name; + myClassNames = classNames; + myActionString = actionString; + myIsSuperAbstract = isSuperAbstract; + myIsParentInterface = isParentInterface; + myIsContainedInInterface = isContainedInInterface; + setTitle(IdeLocalize.titleWarning()); + setButtonsAlignment(SwingUtilities.CENTER); + setOKButtonText(CommonLocalize.buttonYes()); + init(); } - JPanel labelsPanel = new JPanel(new GridLayout(0, 1, 0, 0)); - labelsPanel.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 10)); - String classType = myIsParentInterface ? IdeLocalize.elementOfInterface().get() : IdeLocalize.elementOfClass().get(); - String methodString = IdeLocalize.elementMethod().get(); - labelsPanel.add(new JLabel(IdeLocalize.labelMethod(myName).get())); - if (myClassNames.length == 1) { - String className = myClassNames[0]; - labelsPanel.add(new JLabel( - myIsContainedInInterface || !myIsSuperAbstract - ? IdeLocalize.labelOverridesMethodOf_class_or_interfaceName(methodString, classType, className).get() - : IdeLocalize.labelImplementsMethodOf_class_or_interfaceName(methodString, classType, className).get() - )); - } else { - labelsPanel.add(new JLabel(IdeLocalize.labelImplementsMethodOf_interfaces().get())); - for (String className : myClassNames) { - labelsPanel.add(new JLabel(" " + className)); - } + + @Nonnull + @Override + protected Action[] createActions() { + return new Action[]{getOKAction(), new NoAction(), getCancelAction()}; } - labelsPanel.add(new JLabel(IdeLocalize.promptDoYouWantToAction_verbTheMethodFrom_class(myActionString, myClassNames.length > 1 ? 2 : 1).get())); - panel.add(labelsPanel, BorderLayout.CENTER); - return panel; - } - public static String capitalize(String text) { - return Character.toUpperCase(text.charAt(0)) + text.substring(1); - } + @Override + public JComponent createNorthPanel() { + JPanel panel = new JPanel(new BorderLayout()); + panel.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10)); + Image icon = UIUtil.getWarningIcon(); + if (icon != null) { + JLabel iconLabel = new JBLabel(UIUtil.getQuestionIcon()); + panel.add(iconLabel, BorderLayout.WEST); + } + JPanel labelsPanel = new JPanel(new GridLayout(0, 1, 0, 0)); + labelsPanel.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 10)); + int classType = myIsParentInterface ? 0 : 1; + labelsPanel.add(new JLabel(JavaLocalize.labelMethod(myName).get())); + if (myClassNames.length == 1) { + String className = myClassNames[0]; + labelsPanel.add(new JLabel( + myIsContainedInInterface || !myIsSuperAbstract + ? JavaLocalize.labelOverridesMethodOfClassOrInterfaceName(classType, className).get() + : JavaLocalize.labelImplementsMethodOfClassOrInterfaceName(classType, className).get() + )); + } + else { + labelsPanel.add(new JLabel(JavaLocalize.labelImplementsMethodOfInterfaces().get())); + for (String className : myClassNames) { + labelsPanel.add(new JLabel(" " + className)); + } + } + labelsPanel.add(new JLabel(JavaLocalize.promptDoYouWantToActionVerbTheMethodFromClass( + myActionString == LocalizeValue.empty() ? RefactoringLocalize.toRefactor() : myActionString, + myClassNames.length > 1 ? 2 : 1 + ).get())); + panel.add(labelsPanel, BorderLayout.CENTER); + return panel; + } - public JComponent createCenterPanel() { - return null; - } + public static String capitalize(String text) { + return Character.toUpperCase(text.charAt(0)) + text.substring(1); + } - private class NoAction extends AbstractAction { - public NoAction() { - super(CommonLocalize.buttonNo().get()); + @Override + public JComponent createCenterPanel() { + return null; } - public void actionPerformed(ActionEvent e) { - close(NO_EXIT_CODE); + private class NoAction extends LocalizeAction { + public NoAction() { + super(CommonLocalize.buttonNo()); + } + + @Override + public void actionPerformed(ActionEvent e) { + close(NO_EXIT_CODE); + } } - } } diff --git a/plugin/src/main/java/com/intellij/java/impl/ide/util/SuperMethodWarningUtil.java b/plugin/src/main/java/com/intellij/java/impl/ide/util/SuperMethodWarningUtil.java index 65381232e..cfb5a834e 100644 --- a/plugin/src/main/java/com/intellij/java/impl/ide/util/SuperMethodWarningUtil.java +++ b/plugin/src/main/java/com/intellij/java/impl/ide/util/SuperMethodWarningUtil.java @@ -48,13 +48,13 @@ private SuperMethodWarningUtil() { @Nonnull @RequiredUIAccess - public static PsiMethod[] checkSuperMethods(PsiMethod method, String actionString) { + public static PsiMethod[] checkSuperMethods(PsiMethod method, @Nonnull LocalizeValue actionString) { return checkSuperMethods(method, actionString, null); } @Nonnull @RequiredUIAccess - public static PsiMethod[] checkSuperMethods(PsiMethod method, String actionString, Collection ignore) { + public static PsiMethod[] checkSuperMethods(PsiMethod method, @Nonnull LocalizeValue actionString, Collection ignore) { PsiClass aClass = method.getContainingClass(); if (aClass == null) { return new PsiMethod[]{method}; @@ -102,7 +102,7 @@ public static PsiMethod[] checkSuperMethods(PsiMethod method, String actionStrin } @RequiredUIAccess - public static PsiMethod checkSuperMethod(PsiMethod method, String actionString) { + public static PsiMethod checkSuperMethod(PsiMethod method, @Nonnull LocalizeValue actionString) { PsiClass aClass = method.getContainingClass(); if (aClass == null) { return method; @@ -141,7 +141,12 @@ public static PsiMethod checkSuperMethod(PsiMethod method, String actionString) } @RequiredReadAction - public static void checkSuperMethod(PsiMethod method, String actionString, PsiElementProcessor processor, Editor editor) { + public static void checkSuperMethod( + PsiMethod method, + @Nonnull LocalizeValue actionString, + PsiElementProcessor processor, + Editor editor + ) { PsiClass aClass = method.getContainingClass(); if (aClass == null) { processor.execute(method); @@ -171,9 +176,8 @@ public static void checkSuperMethod(PsiMethod method, String actionString, PsiEl JBList list = new JBList<>(renameBase, renameCurrent); JBPopup popup = ((AWTPopupFactory) JBPopupFactory.getInstance()).createListPopupBuilder(list) .setItemChoosenCallback(() -> { - Object value = list.getSelectedValue(); - if (value instanceof String) { - processor.execute(methods[value.equals(renameBase) ? 0 : 1]); + if (list.getSelectedValue() instanceof String stringValue) { + processor.execute(methods[stringValue.equals(renameBase) ? 0 : 1]); } }) .setMovable(false) diff --git a/plugin/src/main/java/com/intellij/java/impl/refactoring/changeSignature/JavaChangeSignatureHandler.java b/plugin/src/main/java/com/intellij/java/impl/refactoring/changeSignature/JavaChangeSignatureHandler.java index 5ac3d7f27..94862fb41 100644 --- a/plugin/src/main/java/com/intellij/java/impl/refactoring/changeSignature/JavaChangeSignatureHandler.java +++ b/plugin/src/main/java/com/intellij/java/impl/refactoring/changeSignature/JavaChangeSignatureHandler.java @@ -21,180 +21,182 @@ import com.intellij.java.impl.refactoring.changeClassSignature.ChangeClassSignatureDialog; import com.intellij.java.language.psi.*; import consulo.annotation.access.RequiredReadAction; -import consulo.dataContext.DataContext; import consulo.codeEditor.Editor; import consulo.codeEditor.ScrollType; +import consulo.dataContext.DataContext; +import consulo.java.impl.codeInsight.JavaTargetElementUtilEx; +import consulo.language.editor.refactoring.changeSignature.ChangeSignatureHandler; import consulo.language.editor.refactoring.localize.RefactoringLocalize; +import consulo.language.editor.refactoring.util.CommonRefactoringUtil; +import consulo.language.psi.PsiElement; +import consulo.language.psi.PsiFile; +import consulo.language.psi.util.PsiTreeUtil; import consulo.localize.LocalizeValue; import consulo.project.Project; import consulo.ui.annotation.RequiredUIAccess; import consulo.ui.ex.awt.DialogWrapper; -import consulo.language.psi.PsiElement; -import consulo.language.psi.PsiFile; -import consulo.language.psi.util.PsiTreeUtil; -import consulo.language.editor.refactoring.RefactoringBundle; -import consulo.language.editor.refactoring.changeSignature.ChangeSignatureHandler; -import consulo.language.editor.refactoring.util.CommonRefactoringUtil; -import consulo.java.impl.codeInsight.JavaTargetElementUtilEx; - import jakarta.annotation.Nonnull; import jakarta.annotation.Nullable; public class JavaChangeSignatureHandler implements ChangeSignatureHandler { - @Override - @RequiredUIAccess - public void invoke(@Nonnull Project project, Editor editor, PsiFile file, DataContext dataContext) { - editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE); - PsiElement element = findTargetMember(file, editor); - if (element == null) { - element = dataContext.getData(PsiElement.KEY); - } - invokeOnElement(project, editor, element); - } - - @RequiredUIAccess - private static void invokeOnElement(Project project, Editor editor, PsiElement element) { - if (element instanceof PsiMethod method && method.getNameIdentifier() != null) { - invoke(method, project, editor); - } else if (element instanceof PsiClass psiClass) { - invoke(psiClass, editor); - } else { - LocalizeValue message = - RefactoringLocalize.cannotPerformRefactoringWithReason(RefactoringLocalize.errorWrongCaretPositionMethodOrClassName()); - CommonRefactoringUtil.showErrorHint(project, editor, message.get(), REFACTORING_NAME.get(), HelpID.CHANGE_SIGNATURE); + @Override + @RequiredUIAccess + public void invoke(@Nonnull Project project, Editor editor, PsiFile file, DataContext dataContext) { + editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE); + PsiElement element = findTargetMember(file, editor); + if (element == null) { + element = dataContext.getData(PsiElement.KEY); + } + invokeOnElement(project, editor, element); } - } - @Override - @RequiredUIAccess - public void invoke(@Nonnull Project project, @Nonnull PsiElement[] elements, @Nullable DataContext dataContext) { - if (elements.length != 1) { - return; - } - Editor editor = dataContext != null ? dataContext.getData(Editor.KEY) : null; - invokeOnElement(project, editor, elements[0]); - } - - @Nullable - @Override - public String getTargetNotFoundMessage() { - return RefactoringLocalize.errorWrongCaretPositionMethodOrClassName().get(); - } - - @RequiredUIAccess - private static void invoke(PsiMethod method, Project project, @Nullable Editor editor) { - PsiMethod newMethod = SuperMethodWarningUtil.checkSuperMethod(method, RefactoringLocalize.toRefactor().get()); - if (newMethod == null) { - return; + @RequiredUIAccess + private static void invokeOnElement(Project project, Editor editor, PsiElement element) { + if (element instanceof PsiMethod method && method.getNameIdentifier() != null) { + invoke(method, project, editor); + } + else if (element instanceof PsiClass psiClass) { + invoke(psiClass, editor); + } + else { + LocalizeValue message = + RefactoringLocalize.cannotPerformRefactoringWithReason(RefactoringLocalize.errorWrongCaretPositionMethodOrClassName()); + CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.CHANGE_SIGNATURE); + } } - if (!newMethod.equals(method)) { - ChangeSignatureUtil.invokeChangeSignatureOn(newMethod, project); - return; + @Override + @RequiredUIAccess + public void invoke(@Nonnull Project project, @Nonnull PsiElement[] elements, @Nullable DataContext dataContext) { + if (elements.length != 1) { + return; + } + Editor editor = dataContext != null ? dataContext.getData(Editor.KEY) : null; + invokeOnElement(project, editor, elements[0]); } - if (!CommonRefactoringUtil.checkReadOnlyStatus(project, method)) { - return; + @Nullable + @Override + public String getTargetNotFoundMessage() { + return RefactoringLocalize.errorWrongCaretPositionMethodOrClassName().get(); } - PsiClass containingClass = method.getContainingClass(); - PsiReferenceExpression refExpr = editor != null ? JavaTargetElementUtilEx.findReferenceExpression(editor) : null; - boolean allowDelegation = containingClass != null && !containingClass.isInterface(); - DialogWrapper dialog = new JavaChangeSignatureDialog(project, method, allowDelegation, refExpr); - dialog.show(); - } - - @RequiredUIAccess - private static void invoke(PsiClass aClass, Editor editor) { - PsiTypeParameterList typeParameterList = aClass.getTypeParameterList(); - Project project = aClass.getProject(); - if (typeParameterList == null) { - LocalizeValue message = - RefactoringLocalize.cannotPerformRefactoringWithReason(RefactoringLocalize.changeclasssignatureNoTypeParameters()); - CommonRefactoringUtil.showErrorHint(project, editor, message.get(), REFACTORING_NAME.get(), HelpID.CHANGE_CLASS_SIGNATURE); - return; - } - if (!CommonRefactoringUtil.checkReadOnlyStatus(project, aClass)) { - return; - } + @RequiredUIAccess + private static void invoke(PsiMethod method, Project project, @Nullable Editor editor) { + PsiMethod newMethod = SuperMethodWarningUtil.checkSuperMethod(method, RefactoringLocalize.toRefactor()); + if (newMethod == null) { + return; + } - ChangeClassSignatureDialog dialog = new ChangeClassSignatureDialog(aClass, true); - dialog.show(); - } - - @Override - @Nullable - @RequiredUIAccess - public PsiElement findTargetMember(PsiFile file, Editor editor) { - PsiElement element = file.findElementAt(editor.getCaretModel().getOffset()); - return findTargetMember(element); - } - - @Override - @RequiredReadAction - public PsiElement findTargetMember(PsiElement element) { - PsiElement target = findTargetImpl(element); - return JavaRefactoringSupportProvider.isDisableRefactoringForLightElement(target) ? null : target; - } - - @RequiredReadAction - private PsiElement findTargetImpl(PsiElement element) { - if (PsiTreeUtil.getParentOfType(element, PsiParameterList.class) != null) { - return PsiTreeUtil.getParentOfType(element, PsiMethod.class); - } + if (!newMethod.equals(method)) { + ChangeSignatureUtil.invokeChangeSignatureOn(newMethod, project); + return; + } + + if (!CommonRefactoringUtil.checkReadOnlyStatus(project, method)) { + return; + } - PsiTypeParameterList typeParameterList = PsiTreeUtil.getParentOfType(element, PsiTypeParameterList.class); - if (typeParameterList != null) { - return PsiTreeUtil.getParentOfType(typeParameterList, PsiMember.class); + PsiClass containingClass = method.getContainingClass(); + PsiReferenceExpression refExpr = editor != null ? JavaTargetElementUtilEx.findReferenceExpression(editor) : null; + boolean allowDelegation = containingClass != null && !containingClass.isInterface(); + DialogWrapper dialog = new JavaChangeSignatureDialog(project, method, allowDelegation, refExpr); + dialog.show(); } - PsiElement elementParent = element.getParent(); - if (elementParent instanceof PsiMethod method && method.getNameIdentifier() == element) { - PsiClass containingClass = method.getContainingClass(); - if (containingClass != null && containingClass.isAnnotationType()) { - return null; - } - return elementParent; + @RequiredUIAccess + private static void invoke(PsiClass aClass, Editor editor) { + PsiTypeParameterList typeParameterList = aClass.getTypeParameterList(); + Project project = aClass.getProject(); + if (typeParameterList == null) { + LocalizeValue message = + RefactoringLocalize.cannotPerformRefactoringWithReason(RefactoringLocalize.changeclasssignatureNoTypeParameters()); + CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.CHANGE_CLASS_SIGNATURE); + return; + } + if (!CommonRefactoringUtil.checkReadOnlyStatus(project, aClass)) { + return; + } + + ChangeClassSignatureDialog dialog = new ChangeClassSignatureDialog(aClass, true); + dialog.show(); } - if (elementParent instanceof PsiClass psiClass && psiClass.getNameIdentifier() == element) { - return psiClass.isAnnotationType() ? null : elementParent; + + @Override + @Nullable + @RequiredUIAccess + public PsiElement findTargetMember(PsiFile file, Editor editor) { + PsiElement element = file.findElementAt(editor.getCaretModel().getOffset()); + return findTargetMember(element); } - PsiCallExpression expression = PsiTreeUtil.getParentOfType(element, PsiCallExpression.class); - if (expression != null) { - PsiExpression qualifierExpression; - if (expression instanceof PsiMethodCallExpression methodCall) { - qualifierExpression = methodCall.getMethodExpression().getQualifierExpression(); - } else if (expression instanceof PsiNewExpression newExpression) { - qualifierExpression = newExpression.getQualifier(); - } else { - qualifierExpression = null; - } - if (PsiTreeUtil.isAncestor(qualifierExpression, element, false)) { - PsiExpressionList expressionList = PsiTreeUtil.getParentOfType(qualifierExpression, PsiExpressionList.class); - if (expressionList != null) { - PsiElement parent = expressionList.getParent(); - if (parent instanceof PsiCallExpression call) { - return call.resolveMethod(); - } - } - } else { - return expression.resolveMethod(); - } + @Override + @RequiredReadAction + public PsiElement findTargetMember(PsiElement element) { + PsiElement target = findTargetImpl(element); + return JavaRefactoringSupportProvider.isDisableRefactoringForLightElement(target) ? null : target; } - PsiReferenceParameterList referenceParameterList = PsiTreeUtil.getParentOfType(element, PsiReferenceParameterList.class); - if (referenceParameterList != null) { - PsiJavaCodeReferenceElement referenceElement = PsiTreeUtil.getParentOfType(referenceParameterList, PsiJavaCodeReferenceElement.class); - if (referenceElement != null) { - PsiElement resolved = referenceElement.resolve(); - if (resolved instanceof PsiClass) { - return resolved; - } else if (resolved instanceof PsiMethod) { - return resolved; - } - } + @RequiredReadAction + private PsiElement findTargetImpl(PsiElement element) { + if (PsiTreeUtil.getParentOfType(element, PsiParameterList.class) != null) { + return PsiTreeUtil.getParentOfType(element, PsiMethod.class); + } + + PsiTypeParameterList typeParameterList = PsiTreeUtil.getParentOfType(element, PsiTypeParameterList.class); + if (typeParameterList != null) { + return PsiTreeUtil.getParentOfType(typeParameterList, PsiMember.class); + } + + PsiElement elementParent = element.getParent(); + if (elementParent instanceof PsiMethod method && method.getNameIdentifier() == element) { + PsiClass containingClass = method.getContainingClass(); + if (containingClass != null && containingClass.isAnnotationType()) { + return null; + } + return method; + } + if (elementParent instanceof PsiClass psiClass && psiClass.getNameIdentifier() == element) { + return psiClass.isAnnotationType() ? null : psiClass; + } + + PsiCallExpression expression = PsiTreeUtil.getParentOfType(element, PsiCallExpression.class); + if (expression != null) { + PsiExpression qualifierExpression; + if (expression instanceof PsiMethodCallExpression methodCall) { + qualifierExpression = methodCall.getMethodExpression().getQualifierExpression(); + } + else if (expression instanceof PsiNewExpression newExpression) { + qualifierExpression = newExpression.getQualifier(); + } + else { + qualifierExpression = null; + } + if (PsiTreeUtil.isAncestor(qualifierExpression, element, false)) { + PsiExpressionList expressionList = PsiTreeUtil.getParentOfType(qualifierExpression, PsiExpressionList.class); + if (expressionList != null && expressionList.getParent() instanceof PsiCallExpression call) { + return call.resolveMethod(); + } + } + else { + return expression.resolveMethod(); + } + } + + PsiReferenceParameterList referenceParameterList = PsiTreeUtil.getParentOfType(element, PsiReferenceParameterList.class); + if (referenceParameterList != null) { + PsiJavaCodeReferenceElement referenceElement = + PsiTreeUtil.getParentOfType(referenceParameterList, PsiJavaCodeReferenceElement.class); + if (referenceElement != null) { + PsiElement resolved = referenceElement.resolve(); + if (resolved instanceof PsiClass) { + return resolved; + } + else if (resolved instanceof PsiMethod) { + return resolved; + } + } + } + return null; } - return null; - } } diff --git a/plugin/src/main/java/com/intellij/java/impl/refactoring/introduceparameterobject/IntroduceParameterObjectHandler.java b/plugin/src/main/java/com/intellij/java/impl/refactoring/introduceparameterobject/IntroduceParameterObjectHandler.java index 4d05cfa06..63d6f9d16 100644 --- a/plugin/src/main/java/com/intellij/java/impl/refactoring/introduceparameterobject/IntroduceParameterObjectHandler.java +++ b/plugin/src/main/java/com/intellij/java/impl/refactoring/introduceparameterobject/IntroduceParameterObjectHandler.java @@ -49,8 +49,8 @@ public void invoke(@Nonnull Project project, Editor editor, PsiFile file, DataCo scrollingModel.scrollToCaret(ScrollType.MAKE_VISIBLE); PsiElement element = dataContext.getData(PsiElement.KEY); PsiMethod selectedMethod = null; - if (element instanceof PsiMethod) { - selectedMethod = (PsiMethod)element; + if (element instanceof PsiMethod method) { + selectedMethod = method; } else if (element instanceof PsiParameter parameter && parameter.getDeclarationScope() instanceof PsiMethod methodScope) { selectedMethod = methodScope; @@ -59,15 +59,14 @@ else if (element instanceof PsiParameter parameter && parameter.getDeclarationSc CaretModel caretModel = editor.getCaretModel(); int position = caretModel.getOffset(); PsiElement elementAt = file.findElementAt(position); - PsiMethodCallExpression methodCallExpression = - PsiTreeUtil.getParentOfType(elementAt, PsiMethodCallExpression.class); + PsiMethodCallExpression methodCallExpression = PsiTreeUtil.getParentOfType(elementAt, PsiMethodCallExpression.class); if (methodCallExpression != null) { selectedMethod = methodCallExpression.resolveMethod(); } else { PsiParameterList parameterList = PsiTreeUtil.getParentOfType(elementAt, PsiParameterList.class); - if (parameterList != null && parameterList.getParent() instanceof PsiMethod) { - selectedMethod = (PsiMethod)parameterList.getParent(); + if (parameterList != null && parameterList.getParent() instanceof PsiMethod method) { + selectedMethod = method; } } } @@ -75,7 +74,7 @@ else if (element instanceof PsiParameter parameter && parameter.getDeclarationSc LocalizeValue message = RefactoringLocalize.cannotPerformRefactoringWithReason( JavaRefactoringLocalize.theCaretShouldBePositionedAtTheNameOfTheMethodToBeRefactored() ); - CommonRefactoringUtil.showErrorHint(project, editor, message.get(), REFACTORING_NAME.get(), HelpID.IntroduceParameterObject); + CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.IntroduceParameterObject); return; } invoke(project, selectedMethod, editor); @@ -97,7 +96,7 @@ public void invoke(@Nonnull Project project, @Nonnull PsiElement[] elements, Dat @RequiredUIAccess private static void invoke(Project project, PsiMethod selectedMethod, Editor editor) { - PsiMethod newMethod = SuperMethodWarningUtil.checkSuperMethod(selectedMethod, RefactoringLocalize.toRefactor().get()); + PsiMethod newMethod = SuperMethodWarningUtil.checkSuperMethod(selectedMethod, RefactoringLocalize.toRefactor()); if (newMethod == null) { return; } @@ -109,7 +108,7 @@ private static void invoke(Project project, PsiMethod selectedMethod, Editor edi if (parameters.length == 0) { LocalizeValue message = RefactoringLocalize.cannotPerformRefactoringWithReason(JavaRefactoringLocalize.methodSelectedHasNoParameters()); - CommonRefactoringUtil.showErrorHint(project, editor, message.get(), REFACTORING_NAME.get(), HelpID.IntroduceParameterObject); + CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.IntroduceParameterObject); return; } if (newMethod instanceof PsiCompiledElement) { @@ -118,8 +117,8 @@ private static void invoke(Project project, PsiMethod selectedMethod, Editor edi editor, RefactoringLocalize.cannotPerformRefactoringWithReason( JavaRefactoringLocalize.theSelectedMethodCannotBeWrappedBecauseItIsDefinedInANonProjectClass() - ).get(), - REFACTORING_NAME.get(), + ), + REFACTORING_NAME, HelpID.IntroduceParameterObject ); return; diff --git a/plugin/src/main/java/com/intellij/java/impl/refactoring/invertBoolean/InvertBooleanDialog.java b/plugin/src/main/java/com/intellij/java/impl/refactoring/invertBoolean/InvertBooleanDialog.java index 237f50c90..0f10543cc 100644 --- a/plugin/src/main/java/com/intellij/java/impl/refactoring/invertBoolean/InvertBooleanDialog.java +++ b/plugin/src/main/java/com/intellij/java/impl/refactoring/invertBoolean/InvertBooleanDialog.java @@ -15,15 +15,16 @@ */ package com.intellij.java.impl.refactoring.invertBoolean; -import consulo.language.editor.refactoring.localize.RefactoringLocalize; -import consulo.language.findUsage.DescriptiveNameUtil; -import consulo.project.Project; -import consulo.application.HelpManager; -import consulo.language.psi.PsiNamedElement; import com.intellij.java.impl.refactoring.HelpID; -import consulo.language.editor.refactoring.RefactoringBundle; +import consulo.annotation.access.RequiredReadAction; +import consulo.application.HelpManager; +import consulo.language.editor.refactoring.localize.RefactoringLocalize; import consulo.language.editor.refactoring.ui.RefactoringDialog; import consulo.language.editor.refactoring.util.CommonRefactoringUtil; +import consulo.language.findUsage.DescriptiveNameUtil; +import consulo.language.psi.PsiNamedElement; +import consulo.project.Project; +import consulo.ui.annotation.RequiredUIAccess; import consulo.usage.UsageViewUtil; import javax.swing.*; @@ -32,52 +33,60 @@ * @author ven */ public class InvertBooleanDialog extends RefactoringDialog { - private JTextField myNameField; - private JPanel myPanel; - private JLabel myLabel; - private JLabel myCaptionLabel; + private JTextField myNameField; + private JPanel myPanel; + private JLabel myLabel; + private JLabel myCaptionLabel; - private final PsiNamedElement myElement; + private final PsiNamedElement myElement; - public InvertBooleanDialog(PsiNamedElement element) { - super(element.getProject(), false); - myElement = element; - String name = myElement.getName(); - myNameField.setText(name); - myLabel.setLabelFor(myNameField); - String typeString = UsageViewUtil.getType(myElement); - myLabel.setText(RefactoringLocalize.invertBooleanNameOfInvertedElement(typeString).get()); - myCaptionLabel.setText(RefactoringLocalize.invert01(typeString, DescriptiveNameUtil.getDescriptiveName(myElement)).get()); + @RequiredReadAction + public InvertBooleanDialog(PsiNamedElement element) { + super(element.getProject(), false); + myElement = element; + String name = myElement.getName(); + myNameField.setText(name); + myLabel.setLabelFor(myNameField); + String typeString = UsageViewUtil.getType(myElement); + myLabel.setText(RefactoringLocalize.invertBooleanNameOfInvertedElement(typeString).get()); + myCaptionLabel.setText(RefactoringLocalize.invert01(typeString, DescriptiveNameUtil.getDescriptiveName(myElement)).get()); - setTitle(InvertBooleanHandler.REFACTORING_NAME); - init(); - } - - public JComponent getPreferredFocusedComponent() { - return myNameField; - } + setTitle(InvertBooleanHandler.REFACTORING_NAME); + init(); + } - protected void doAction() { - Project project = myElement.getProject(); - String name = myNameField.getText().trim(); - if (name.length() == 0) { - CommonRefactoringUtil.showErrorMessage( - InvertBooleanHandler.REFACTORING_NAME, - RefactoringLocalize.pleaseEnterAValidNameForInvertedElement(UsageViewUtil.getType(myElement)).get(), - HelpID.INVERT_BOOLEAN, - project - ); - return; + @Override + @RequiredUIAccess + public JComponent getPreferredFocusedComponent() { + return myNameField; } - invokeRefactoring(new InvertBooleanProcessor(myElement, name)); - } + @Override + @RequiredUIAccess + protected void doAction() { + Project project = myElement.getProject(); + String name = myNameField.getText().trim(); + if (name.length() == 0) { + CommonRefactoringUtil.showErrorMessage( + InvertBooleanHandler.REFACTORING_NAME, + RefactoringLocalize.pleaseEnterAValidNameForInvertedElement(UsageViewUtil.getType(myElement)), + HelpID.INVERT_BOOLEAN, + project + ); + return; + } - protected void doHelpAction() { - HelpManager.getInstance().invokeHelp(HelpID.INVERT_BOOLEAN); - } + invokeRefactoring(new InvertBooleanProcessor(myElement, name)); + } + + @Override + @RequiredUIAccess + protected void doHelpAction() { + HelpManager.getInstance().invokeHelp(HelpID.INVERT_BOOLEAN); + } - protected JComponent createCenterPanel() { - return myPanel; - } + @Override + protected JComponent createCenterPanel() { + return myPanel; + } } diff --git a/plugin/src/main/java/com/intellij/java/impl/refactoring/invertBoolean/InvertBooleanHandler.java b/plugin/src/main/java/com/intellij/java/impl/refactoring/invertBoolean/InvertBooleanHandler.java index 1045c1de2..4e2e264f1 100644 --- a/plugin/src/main/java/com/intellij/java/impl/refactoring/invertBoolean/InvertBooleanHandler.java +++ b/plugin/src/main/java/com/intellij/java/impl/refactoring/invertBoolean/InvertBooleanHandler.java @@ -24,12 +24,12 @@ import consulo.codeEditor.Editor; import consulo.codeEditor.ScrollType; import consulo.dataContext.DataContext; -import consulo.language.editor.refactoring.RefactoringBundle; import consulo.language.editor.refactoring.action.RefactoringActionHandler; import consulo.language.editor.refactoring.localize.RefactoringLocalize; import consulo.language.editor.refactoring.util.CommonRefactoringUtil; import consulo.language.psi.PsiElement; import consulo.language.psi.PsiFile; +import consulo.localize.LocalizeValue; import consulo.project.Project; import consulo.ui.annotation.RequiredUIAccess; import jakarta.annotation.Nonnull; @@ -38,80 +38,88 @@ * @author ven */ public class InvertBooleanHandler implements RefactoringActionHandler { - static final String REFACTORING_NAME = RefactoringBundle.message("invert.boolean.title"); + static final LocalizeValue REFACTORING_NAME = RefactoringLocalize.invertBooleanTitle(); - @RequiredUIAccess - public void invoke(@Nonnull Project project, Editor editor, PsiFile file, DataContext dataContext) { - editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE); - PsiElement element = dataContext.getData(PsiElement.KEY); - if (element instanceof PsiMethod method) { - invoke(method, project, editor); + @Override + @RequiredUIAccess + public void invoke(@Nonnull Project project, Editor editor, PsiFile file, DataContext dataContext) { + editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE); + PsiElement element = dataContext.getData(PsiElement.KEY); + if (element instanceof PsiMethod method) { + invoke(method, project, editor); + } + else if (element instanceof PsiVariable variable) { + invoke(variable, project, editor); + } + else { + CommonRefactoringUtil.showErrorHint( + project, + editor, + RefactoringLocalize.cannotPerformRefactoringWithReason(RefactoringLocalize.errorWrongCaretPositionMethodOrVariableName()), + REFACTORING_NAME, + HelpID.INVERT_BOOLEAN + ); + } } - else if (element instanceof PsiVariable variable) { - invoke(variable, project, editor); - } - else { - CommonRefactoringUtil.showErrorHint( - project, - editor, - RefactoringLocalize.cannotPerformRefactoringWithReason(RefactoringLocalize.errorWrongCaretPositionMethodOrVariableName()).get(), - REFACTORING_NAME, - HelpID.INVERT_BOOLEAN - ); - } - } - @RequiredUIAccess - private static void invoke(PsiVariable var, Project project, Editor editor) { - PsiType returnType = var.getType(); - if (!PsiType.BOOLEAN.equals(returnType)) { - CommonRefactoringUtil.showErrorHint( - project, - editor, - RefactoringLocalize.cannotPerformRefactoringWithReason(RefactoringLocalize.invertBooleanWrongType()).get(), - REFACTORING_NAME, - HelpID.INVERT_BOOLEAN - ); - return; - } + @RequiredUIAccess + private static void invoke(PsiVariable var, Project project, Editor editor) { + PsiType returnType = var.getType(); + if (!PsiType.BOOLEAN.equals(returnType)) { + CommonRefactoringUtil.showErrorHint( + project, + editor, + RefactoringLocalize.cannotPerformRefactoringWithReason(RefactoringLocalize.invertBooleanWrongType()), + REFACTORING_NAME, + HelpID.INVERT_BOOLEAN + ); + return; + } - if (!CommonRefactoringUtil.checkReadOnlyStatus(project, var)) return; - if (var instanceof PsiParameter parameter && parameter.getDeclarationScope() instanceof PsiMethod method) { - PsiMethod superMethod = SuperMethodWarningUtil.checkSuperMethod(method, RefactoringLocalize.toRefactor().get()); - if (superMethod != null) { - var = superMethod.getParameterList().getParameters()[method.getParameterList().getParameterIndex((PsiParameter)var)]; - } - } + if (!CommonRefactoringUtil.checkReadOnlyStatus(project, var)) { + return; + } + if (var instanceof PsiParameter parameter && parameter.getDeclarationScope() instanceof PsiMethod method) { + PsiMethod superMethod = SuperMethodWarningUtil.checkSuperMethod(method, RefactoringLocalize.toRefactor()); + if (superMethod != null) { + var = superMethod.getParameterList().getParameters()[method.getParameterList().getParameterIndex(parameter)]; + } + } - new InvertBooleanDialog(var).show(); - } - - @RequiredUIAccess - public void invoke(@Nonnull Project project, @Nonnull PsiElement[] elements, @Nonnull DataContext dataContext) { - if (elements.length == 1 && elements[0] instanceof PsiMethod method) { - invoke(method, project, null); + new InvertBooleanDialog(var).show(); } - } - @RequiredUIAccess - private static void invoke(PsiMethod method, Project project, Editor editor) { - PsiType returnType = method.getReturnType(); - if (!PsiType.BOOLEAN.equals(returnType)) { - CommonRefactoringUtil.showErrorHint( - project, - editor, - RefactoringLocalize.cannotPerformRefactoringWithReason(RefactoringLocalize.invertBooleanWrongType()).get(), - REFACTORING_NAME, - HelpID.INVERT_BOOLEAN - ); - return; + @Override + @RequiredUIAccess + public void invoke(@Nonnull Project project, @Nonnull PsiElement[] elements, @Nonnull DataContext dataContext) { + if (elements.length == 1 && elements[0] instanceof PsiMethod method) { + invoke(method, project, null); + } } - PsiMethod superMethod = SuperMethodWarningUtil.checkSuperMethod(method, RefactoringLocalize.toRefactor().get()); - if (superMethod != null) method = superMethod; + @RequiredUIAccess + private static void invoke(PsiMethod method, Project project, Editor editor) { + PsiType returnType = method.getReturnType(); + if (!PsiType.BOOLEAN.equals(returnType)) { + CommonRefactoringUtil.showErrorHint( + project, + editor, + RefactoringLocalize.cannotPerformRefactoringWithReason(RefactoringLocalize.invertBooleanWrongType()), + REFACTORING_NAME, + HelpID.INVERT_BOOLEAN + ); + return; + } + + PsiMethod superMethod = SuperMethodWarningUtil.checkSuperMethod(method, RefactoringLocalize.toRefactor()); + if (superMethod != null) { + method = superMethod; + } - if (!CommonRefactoringUtil.checkReadOnlyStatus(project, method)) return; + if (!CommonRefactoringUtil.checkReadOnlyStatus(project, method)) { + return; + } - new InvertBooleanDialog(method).show(); - } + new InvertBooleanDialog(method).show(); + } } diff --git a/plugin/src/main/java/com/intellij/java/impl/refactoring/invertBoolean/InvertBooleanProcessor.java b/plugin/src/main/java/com/intellij/java/impl/refactoring/invertBoolean/InvertBooleanProcessor.java index 9e0d87318..715b6a706 100644 --- a/plugin/src/main/java/com/intellij/java/impl/refactoring/invertBoolean/InvertBooleanProcessor.java +++ b/plugin/src/main/java/com/intellij/java/impl/refactoring/invertBoolean/InvertBooleanProcessor.java @@ -20,6 +20,7 @@ import com.intellij.java.indexing.search.searches.OverridingMethodsSearch; import com.intellij.java.language.psi.*; import consulo.annotation.access.RequiredReadAction; +import consulo.annotation.access.RequiredWriteAction; import consulo.application.util.query.Query; import consulo.language.editor.refactoring.BaseRefactoringProcessor; import consulo.language.editor.refactoring.rename.RenameProcessor; @@ -222,7 +223,7 @@ private static UsageInfo[] extractUsagesForElement(PsiElement element, UsageInfo } @Override - @RequiredUIAccess + @RequiredWriteAction protected void performRefactoring(@Nonnull UsageInfo[] usages) { for (PsiElement element : myRenameProcessor.getElements()) { try { @@ -268,6 +269,6 @@ protected void performRefactoring(@Nonnull UsageInfo[] usages) { @Nonnull @Override protected String getCommandName() { - return InvertBooleanHandler.REFACTORING_NAME; + return InvertBooleanHandler.REFACTORING_NAME.get(); } } diff --git a/plugin/src/main/java/com/intellij/java/impl/refactoring/rename/RenameJavaMethodProcessor.java b/plugin/src/main/java/com/intellij/java/impl/refactoring/rename/RenameJavaMethodProcessor.java index e1356472e..1cf4a38bc 100644 --- a/plugin/src/main/java/com/intellij/java/impl/refactoring/rename/RenameJavaMethodProcessor.java +++ b/plugin/src/main/java/com/intellij/java/impl/refactoring/rename/RenameJavaMethodProcessor.java @@ -346,7 +346,7 @@ public PsiElement substituteElementToRename(PsiElement element, Editor editor) { return element; } } - return SuperMethodWarningUtil.checkSuperMethod(method, RefactoringLocalize.toRename().get()); + return SuperMethodWarningUtil.checkSuperMethod(method, RefactoringLocalize.toRename()); } @Override @@ -371,7 +371,7 @@ public void substituteElementToRename( else { SuperMethodWarningUtil.checkSuperMethod( psiMethod, - "Rename", + LocalizeValue.localizeTODO("Rename"), method -> { if (!PsiElementRenameHandler.canRename(method.getProject(), editor, method)) { return false; diff --git a/plugin/src/main/java/com/intellij/java/impl/refactoring/safeDelete/JavaSafeDeleteProcessor.java b/plugin/src/main/java/com/intellij/java/impl/refactoring/safeDelete/JavaSafeDeleteProcessor.java index e74444d47..cd44e79a3 100644 --- a/plugin/src/main/java/com/intellij/java/impl/refactoring/safeDelete/JavaSafeDeleteProcessor.java +++ b/plugin/src/main/java/com/intellij/java/impl/refactoring/safeDelete/JavaSafeDeleteProcessor.java @@ -138,7 +138,7 @@ public Collection getElementsToSearch( else if (element instanceof PsiMethod method) { PsiMethod[] methods = SuperMethodWarningUtil.checkSuperMethods( method, - RefactoringLocalize.toDeleteWithUsageSearch().get(), + RefactoringLocalize.toDeleteWithUsageSearch(), allElementsToDelete ); if (methods.length == 0) { diff --git a/plugin/src/main/java/com/intellij/java/impl/refactoring/wrapreturnvalue/WrapReturnValueHandler.java b/plugin/src/main/java/com/intellij/java/impl/refactoring/wrapreturnvalue/WrapReturnValueHandler.java index c82a441fa..e50831e17 100644 --- a/plugin/src/main/java/com/intellij/java/impl/refactoring/wrapreturnvalue/WrapReturnValueHandler.java +++ b/plugin/src/main/java/com/intellij/java/impl/refactoring/wrapreturnvalue/WrapReturnValueHandler.java @@ -50,9 +50,8 @@ public void invoke( ) { ScrollingModel scrollingModel = editor.getScrollingModel(); scrollingModel.scrollToCaret(ScrollType.MAKE_VISIBLE); - PsiElement element = dataContext.getData(PsiElement.KEY); PsiMethod selectedMethod = null; - if (element instanceof PsiMethod method) { + if (dataContext.getData(PsiElement.KEY) instanceof PsiMethod method) { selectedMethod = method; } else { @@ -73,8 +72,8 @@ public void invoke( editor, RefactoringLocalize.cannotPerformRefactoringWithReason( JavaRefactoringLocalize.theCaretShouldBePositionedAtTheNameOfTheMethodToBeRefactored() - ).get(), - REFACTORING_NAME.get(), + ), + REFACTORING_NAME, this.getHelpID() ); return; @@ -112,8 +111,8 @@ private void invoke(Project project, PsiMethod method, Editor editor) { editor, RefactoringLocalize.cannotPerformRefactoringWithReason( JavaRefactoringLocalize.constructorReturnsCanNotBeWrapped() - ).get(), - REFACTORING_NAME.get(), + ), + REFACTORING_NAME, this.getHelpID() ); return; @@ -125,13 +124,13 @@ private void invoke(Project project, PsiMethod method, Editor editor) { editor, RefactoringLocalize.cannotPerformRefactoringWithReason( JavaRefactoringLocalize.methodSelectedReturnsVoid() - ).get(), - REFACTORING_NAME.get(), + ), + REFACTORING_NAME, this.getHelpID() ); return; } - method = SuperMethodWarningUtil.checkSuperMethod(method, RefactoringLocalize.toRefactor().get()); + method = SuperMethodWarningUtil.checkSuperMethod(method, RefactoringLocalize.toRefactor()); if (method == null) { return; } @@ -142,8 +141,8 @@ private void invoke(Project project, PsiMethod method, Editor editor) { editor, RefactoringLocalize.cannotPerformRefactoringWithReason( JavaRefactoringLocalize.theSelectedMethodCannotBeWrappedBecauseItIsDefinedInANonProjectClass() - ).get(), - REFACTORING_NAME.get(), + ), + REFACTORING_NAME, this.getHelpID() ); return; diff --git a/plugin/src/main/resources/LOCALIZE-LIB/en_US/consulo.java.JavaLocalize.yaml b/plugin/src/main/resources/LOCALIZE-LIB/en_US/consulo.java.JavaLocalize.yaml index 1a6f93a5f..6d9f857c8 100644 --- a/plugin/src/main/resources/LOCALIZE-LIB/en_US/consulo.java.JavaLocalize.yaml +++ b/plugin/src/main/resources/LOCALIZE-LIB/en_US/consulo.java.JavaLocalize.yaml @@ -198,6 +198,14 @@ javac.name: text: Javac jrt.node.short: text: '[JRT]' +label.implements.method.of.class.or.interface.name: + text: implements method of {0, choice, 0#interface|1#class} ''{1}''. +label.implements.method.of.interfaces: + text: 'implements methods of the following classes/interfaces:' +label.method: + text: Method ''{0}'' +label.overrides.method.of.class.or.interface.name: + text: overrides method of {0, choice, 0#interface|1#class} ''{1}''. library.choose.one.to.attach: text: Multiple libraries contain the file.
Choose libraries to attach sources to notification.navigation.to.overriding.classes: @@ -218,6 +226,8 @@ options.java.attribute.descriptor.lambda.parameter: text: Parameters//Lambda parameter project.roots.native.library.node.text: text: Native Library Locations +prompt.do.you.want.to.action.verb.the.method.from.class: + text: Do you want {0} the base {1,choice,1#method|2#methods}? quickfix.family.remove.redundant.parameter: text: Remove redundant parameter settings.inlay.java.annotations: @@ -279,4 +289,4 @@ title.interface: title.lambda: text: Lambda title.record: - text: Record + text: Record \ No newline at end of file