[java-refactoring] Inline delegating variable: an ability to keep the current variable name

Fixes IDEA-371649 Inline variable using the later name

GitOrigin-RevId: a31d332762af3721245af31841562ff48a0cb3a9
This commit is contained in:
Tagir Valeev
2025-05-09 18:02:40 +00:00
committed by intellij-monorepo-bot
parent 439a3ccff8
commit 1cadb8a667
29 changed files with 236 additions and 70 deletions
@@ -1,3 +1,4 @@
// "Inline variable" "true-preview"
package com.siyeh.igfixes.inline;
class ArrayInitializer {
@@ -1,3 +1,4 @@
// "Inline variable" "true-preview"
package com.siyeh.igfixes.inline;
class CastNeeded {
@@ -1,3 +1,4 @@
// "Inline variable" "true-preview"
class Comment {
Object x(String s) {
@@ -1,3 +1,4 @@
// "Inline variable" "true-preview"
import java.util.List;
public class Demo {
@@ -5,7 +6,7 @@ public class Demo {
public static void main(String[] args) {
var y = switch (1) {
case 1:
yield <caret>3;
<caret> yield 3;
default:
yield 4;
};
@@ -1,8 +1,9 @@
// "Inline variable" "true-preview"
import java.util.List;
public class Demo {
List<String> test(List<String> list) {
return <caret>list.stream()
<caret> return list.stream()
.map(String::toUpperCase)
.map(String::trim)
.toList();
@@ -1,3 +1,4 @@
// "Inline variable" "true-preview"
class C {
void m() throws Exception {
try (AutoCloseable r1 = null) {
@@ -1,3 +1,4 @@
// "Inline variable" "true-preview"
class C {
void m() throws Exception {
try (AutoCloseable r1 = null; AutoCloseable r3 = null) {
@@ -1,3 +1,4 @@
// "Inline variable" "true-preview"
class C {
void m() throws Exception {
try (AutoCloseable r1 = null) {
@@ -0,0 +1,19 @@
// "Inline variable|->Keep the 'vExposure' variable" "true-preview"
package com.example;
import java.util.ArrayList;
import java.util.List;
public class Demo {
static class ExposureSpecification {
}
public static void main(String[] args) {
List<ExposureSpecification> vExposures = new ArrayList<>();
for (ExposureSpecification vExposure : vExposures) {
// ... lines of code using exp ...
System.out.println(vExposure);
}
}
}
@@ -0,0 +1,19 @@
// "Inline variable|->Inline and rename 'vExposure' to 'exp'" "true-preview"
package com.example;
import java.util.ArrayList;
import java.util.List;
public class Demo {
static class ExposureSpecification {
}
public static void main(String[] args) {
List<ExposureSpecification> vExposures = new ArrayList<>();
for (ExposureSpecification exp : vExposures) {
// ... lines of code using exp ...
System.out.println(exp);
}
}
}
@@ -0,0 +1,20 @@
// "Inline variable|->Inline and rename 'vExposure' to 'exp'" "true-preview"
package com.example;
import java.util.ArrayList;
import java.util.List;
public class Demo {
static class ExposureSpecification {
}
public static void main(String[] args) {
List<ExposureSpecification> vExposures = new ArrayList<>();
for (ExposureSpecification exp : vExposures) {
System.out.println(exp);
// ... lines of code using exp ...
System.out.println(exp);
}
}
}
@@ -1,3 +1,4 @@
// "Inline variable" "true-preview"
package com.siyeh.igfixes.inline;
class ArrayInitializer {
@@ -1,3 +1,4 @@
// "Inline variable" "false"
class Base {
{
Descendant descendant = new Descendant();
@@ -1,3 +1,4 @@
// "Inline variable" "true-preview"
package com.siyeh.igfixes.inline;
class CastNeeded {
@@ -1,3 +1,4 @@
// "Inline variable" "true-preview"
class Comment {
Object x(String s) {
@@ -1,3 +1,4 @@
// "Inline variable" "true-preview"
import java.util.List;
public class Demo {
@@ -1,3 +1,4 @@
// "Inline variable" "true-preview"
class C {
void m() throws Exception {
try (AutoCloseable r1 = null; AutoCloseable <caret>r2 = r1) {
@@ -1,3 +1,4 @@
// "Inline variable" "true-preview"
class C {
void m() throws Exception {
try (AutoCloseable r1 = null; AutoCloseable <caret>r2 = r1; AutoCloseable r3 = null) {
@@ -1,3 +1,4 @@
// "Inline variable" "true-preview"
class C {
void m() throws Exception {
try (AutoCloseable r1 = null) {
@@ -0,0 +1,20 @@
// "Inline variable|->Keep the 'vExposure' variable" "true-preview"
package com.example;
import java.util.ArrayList;
import java.util.List;
public class Demo {
static class ExposureSpecification {
}
public static void main(String[] args) {
List<ExposureSpecification> vExposures = new ArrayList<>();
for (ExposureSpecification vExposure : vExposures) {
ExposureSpecification e<caret>xp = vExposure;
// ... lines of code using exp ...
System.out.println(exp);
}
}
}
@@ -0,0 +1,20 @@
// "Inline variable|->Inline and rename 'vExposure' to 'exp'" "true-preview"
package com.example;
import java.util.ArrayList;
import java.util.List;
public class Demo {
static class ExposureSpecification {
}
public static void main(String[] args) {
List<ExposureSpecification> vExposures = new ArrayList<>();
for (ExposureSpecification vExposure : vExposures) {
ExposureSpecification e<caret>xp = vExposure;
// ... lines of code using exp ...
System.out.println(exp);
}
}
}
@@ -0,0 +1,21 @@
// "Inline variable|->Inline and rename 'vExposure' to 'exp'" "true-preview"
package com.example;
import java.util.ArrayList;
import java.util.List;
public class Demo {
static class ExposureSpecification {
}
public static void main(String[] args) {
List<ExposureSpecification> vExposures = new ArrayList<>();
for (ExposureSpecification vExposure : vExposures) {
System.out.println(vExposure);
ExposureSpecification e<caret>xp = vExposure;
// ... lines of code using exp ...
System.out.println(exp);
}
}
}