Java: fix variable renaming when inlining method and variable name prefixes or suffixes are configured (IDEA-376160)

GitOrigin-RevId: 560f1bf1a24d8d6195934d0abbb0745c63cf68d7
This commit is contained in:
Bas Leijdekkers
2025-07-29 17:34:50 +00:00
committed by intellij-monorepo-bot
parent c38b281fec
commit 4358c0c90d
9 changed files with 69 additions and 19 deletions
@@ -98,19 +98,29 @@ class InlineMethodHelper {
PsiCodeBlock block = Objects.requireNonNull(myMethodCopy.getBody());
boolean compactConstructor = JavaPsiRecordUtil.isCompactConstructor(myMethodCopy);
final int applicabilityLevel = PsiUtil.getApplicabilityLevel(myMethod, mySubstitutor, myCallArguments);
PsiParameter[] parameters = myMethod.getParameterList().getParameters();
// myMethodCopy does not have a parameter list for record compact constructors because it doesn't have a containing class
// see com.intellij.psi.impl.source.PsiMethodImpl.getParameterList
PsiVariable[] parameters = compactConstructor
? myMethod.getParameterList().getParameters()
: myMethodCopy.getParameterList().getParameters();
PsiLocalVariable[] parameterVars = new PsiLocalVariable[parameters.length];
for (int i = parameters.length - 1; i >= 0; i--) {
PsiParameter parameter = parameters[i];
String parameterName = parameter.getName();
String name = parameterName;
PsiVariable variable = parameters[i];
if (compactConstructor) {
PsiRecordComponent component = JavaPsiRecordUtil.getComponentForCanonicalConstructorParameter((PsiParameter)variable);
if (component != null) variable = JavaPsiRecordUtil.getFieldForComponent(component);
}
assert variable != null;
String variableName = variable.getName();
String name = variableName;
assert name != null;
name = myJavaCodeStyle.variableNameToPropertyName(name, VariableKind.PARAMETER);
name = myJavaCodeStyle.propertyNameToVariableName(name, VariableKind.LOCAL_VARIABLE);
if (!name.equals(parameterName)) {
if (!name.equals(variableName)) {
name = myJavaCodeStyle.suggestUniqueVariableName(name, block.getFirstChild(), true);
RefactoringUtil.renameVariableReferences(variable, name, new LocalSearchScope(block), true);
}
RefactoringUtil.renameVariableReferences(parameter, name, new LocalSearchScope(block), true);
PsiType paramType = parameter.getType();
PsiType paramType = variable.getType();
@NonNls String defaultValue;
if (paramType instanceof PsiEllipsisType ellipsisType) {
paramType = mySubstitutor.substitute(ellipsisType.toArrayType());
@@ -130,9 +140,9 @@ class InlineMethodHelper {
PsiDeclarationStatement declaration = myFactory.createVariableDeclarationStatement(name, paramType, initializer);
declaration = (PsiDeclarationStatement)block.addAfter(declaration, null);
parameterVars[i] = (PsiLocalVariable)declaration.getDeclaredElements()[0];
PsiUtil.setModifierProperty(parameterVars[i], PsiModifier.FINAL, parameter.hasModifierProperty(PsiModifier.FINAL));
PsiUtil.setModifierProperty(parameterVars[i], PsiModifier.FINAL, variable.hasModifierProperty(PsiModifier.FINAL));
if (compactConstructor) {
block.add(myFactory.createStatementFromText("this." + name + '=' + name + ';', myMethod));
block.add(myFactory.createStatementFromText("this." + variableName + '=' + name + ';', myMethod));
}
}
return parameterVars;
@@ -0,0 +1,11 @@
class InlineMethodExample {
public void displayMessage(String theMessage) {
System.out.println("The message is: " + theMessage);
}
public void testInline() {
String message = "Hello, world!";
display<caret>Message(message);
}
}
@@ -0,0 +1,7 @@
class InlineMethodExample {
public void testInline() {
String message = "Hello, world!";
System.out.println("The message is: " + message);
}
}
@@ -4,6 +4,9 @@ class RecordWithCompactConstructor {
}
record Person(String name, int age) {
Person {}
Person(String name, int age) {
this.name = name;
this.age = age;
}
}
}
@@ -4,6 +4,9 @@ class RecordWithCompactConstructor {
}
record Person(String name, int age) {
Person {}
Person(String name, int age) {
this.name = name;
this.age = age;
}
}
}
@@ -4,9 +4,7 @@ class RecordWithCompactConstructor {
}
record Person(String name, int age) {
Person(String name, int age) {
this.name = name;
this.age = age;
Person {
}
}
}
@@ -4,9 +4,7 @@ class RecordWithCompactConstructor {
}
record Person(String name, int age) {
Person(String name, int age) {
this.name = name;
this.age = age;
Person {
}
}
}
@@ -10,6 +10,7 @@ import com.intellij.openapi.ui.TestDialog;
import com.intellij.openapi.ui.TestDialogManager;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.JavaCodeStyleSettings;
import com.intellij.refactoring.BaseRefactoringProcessor;
import com.intellij.refactoring.inline.InlineMethodHandler;
import com.intellij.refactoring.inline.InlineMethodProcessor;
@@ -197,6 +198,15 @@ public class InlineMethodTest extends LightRefactoringTestCase {
public void testParamNameConflictsWithLocalVar() {
doTest();
}
public void testVariablePrefixesSuffixes() {
JavaCodeStyleSettings settings = JavaCodeStyleSettings.getInstance(getProject());
settings.LOCAL_VARIABLE_NAME_PREFIX = "l_";
settings.LOCAL_VARIABLE_NAME_SUFFIX = "_v";
settings.PARAMETER_NAME_PREFIX = "p_";
settings.PARAMETER_NAME_SUFFIX = "_r";
doTest();
}
public void testArrayTypeInferenceFromVarargs() {
doTest();
@@ -1,10 +1,11 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.java.refactoring.inline;
import com.intellij.JavaTestUtil;
import com.intellij.java.refactoring.LightRefactoringTestCase;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.JavaCodeStyleSettings;
import com.intellij.refactoring.BaseRefactoringProcessor;
import com.intellij.refactoring.inline.InlineObjectProcessor;
import com.intellij.testFramework.IdeaTestUtil;
@@ -22,7 +23,16 @@ public class InlineObjectTest extends LightRefactoringTestCase {
public void testInlinePointToString() { doTest(); }
public void testInlineBitString() { doTest(); }
public void testInlineSideEffect() { doTest(); }
public void testRecordWithCompactConstructor() { doTest(); }
public void testRecordWithCompactConstructor() {
JavaCodeStyleSettings settings = JavaCodeStyleSettings.getInstance(getProject());
settings.LOCAL_VARIABLE_NAME_PREFIX = "l_";
settings.LOCAL_VARIABLE_NAME_SUFFIX = "_v";
settings.PARAMETER_NAME_PREFIX = "p_";
settings.PARAMETER_NAME_SUFFIX = "_r";
doTest();
}
public void testRecordWithCanonicalConstructor() { doTest(); }
public void testInlineFileParentSrc() {
BaseRefactoringProcessor.ConflictsInTestsException.withIgnoredConflicts(this::doTest);