mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-245337 'Change field type' for record component field does nothing
GitOrigin-RevId: be89f44adee20d7ca36c0705c35b6264b7189762
This commit is contained in:
committed by
intellij-monorepo-bot
parent
dcf82455a6
commit
95f61a978b
+52
-31
@@ -14,13 +14,16 @@ import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.psi.util.JavaPsiRecordUtil;
|
||||
import com.intellij.psi.util.TypeConversionUtil;
|
||||
import com.intellij.refactoring.RefactoringBundle;
|
||||
import com.intellij.refactoring.changeSignature.ChangeSignatureProcessor;
|
||||
import com.intellij.refactoring.changeSignature.JavaChangeSignatureDialog;
|
||||
import com.intellij.refactoring.changeSignature.ParameterInfoImpl;
|
||||
import com.intellij.usageView.UsageViewUtil;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.ObjectUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -102,41 +105,59 @@ public class VariableTypeFix extends LocalQuickFixAndIntentionActionOnPsiElement
|
||||
}
|
||||
|
||||
private boolean changeMethodSignatureIfNeeded(PsiVariable myVariable) {
|
||||
PsiParameter myParameter = null;
|
||||
PsiMethod method = null;
|
||||
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(ParameterInfoImpl.create(i++)
|
||||
.withName(parameter.getName())
|
||||
.withType(changeType ? getReturnType() : parameter.getType()));
|
||||
myParameter = (PsiParameter)myVariable;
|
||||
method = ObjectUtils.tryCast(((PsiParameter)myVariable).getDeclarationScope(), PsiMethod.class);
|
||||
}
|
||||
else if (myVariable instanceof PsiField) {
|
||||
PsiRecordComponent component = JavaPsiRecordUtil.getComponentForField((PsiField)myVariable);
|
||||
PsiClass aClass = ((PsiField)myVariable).getContainingClass();
|
||||
if (component != null && aClass != null) {
|
||||
method = JavaPsiRecordUtil.findCanonicalConstructor(aClass);
|
||||
if (method != null) {
|
||||
int index = ArrayUtil.indexOf(aClass.getRecordComponents(), component);
|
||||
PsiParameter parameter = method.getParameterList().getParameter(index);
|
||||
if (parameter != null && parameter.getName().equals(myVariable.getName())) {
|
||||
myParameter = parameter;
|
||||
}
|
||||
}
|
||||
|
||||
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[0]));
|
||||
processor.run();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
if (method == null || myParameter == null) return false;
|
||||
changeSignature(myVariable, myParameter, method);
|
||||
return true;
|
||||
}
|
||||
|
||||
private void changeSignature(PsiVariable myVariable, PsiParameter myParameter, PsiMethod method) {
|
||||
final PsiMethod psiMethod = SuperMethodWarningUtil.checkSuperMethod(method, RefactoringBundle.message("to.refactor"));
|
||||
if (psiMethod == null) return;
|
||||
final int parameterIndex = method.getParameterList().getParameterIndex(myParameter);
|
||||
if (!FileModificationService.getInstance().prepareFileForWrite(psiMethod.getContainingFile())) return;
|
||||
final ArrayList<ParameterInfoImpl> infos = new ArrayList<>();
|
||||
int i = 0;
|
||||
for (PsiParameter parameter : psiMethod.getParameterList().getParameters()) {
|
||||
final boolean changeType = i == parameterIndex;
|
||||
infos.add(ParameterInfoImpl.create(i++)
|
||||
.withName(parameter.getName())
|
||||
.withType(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[0]));
|
||||
processor.run();
|
||||
}
|
||||
}
|
||||
|
||||
protected PsiType getReturnType() {
|
||||
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
// "Change field 'x' type to 'byte'" "true"
|
||||
|
||||
record R(byte x) {
|
||||
void test() {
|
||||
byte b = x;
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Change field 'x' type to 'byte'" "true"
|
||||
|
||||
record R(byte x) {
|
||||
R(byte x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
void test() {
|
||||
byte b = x;
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
// "Change field 'x' type to 'byte'" "true"
|
||||
|
||||
record R(int x) {
|
||||
void test() {
|
||||
byte b = <caret>x;
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Change field 'x' type to 'byte'" "true"
|
||||
|
||||
record R(int x) {
|
||||
R(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
void test() {
|
||||
byte b = <caret>x;
|
||||
}
|
||||
}
|
||||
+9
@@ -16,8 +16,17 @@
|
||||
package com.intellij.java.codeInsight.daemon.quickFix;
|
||||
|
||||
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
|
||||
import com.intellij.testFramework.LightProjectDescriptor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import static com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase.JAVA_15;
|
||||
|
||||
public class VariableTypeTest extends LightQuickFixParameterizedTestCase {
|
||||
@Override
|
||||
protected @NotNull LightProjectDescriptor getProjectDescriptor() {
|
||||
return JAVA_15;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getBasePath() {
|
||||
return "/codeInsight/daemonCodeAnalyzer/quickFix/variableType";
|
||||
|
||||
Reference in New Issue
Block a user