LingerMind opened a new issue, #9836:
URL: https://github.com/apache/netbeans/issues/9836
### Apache NetBeans version
Apache NetBeans 32
### What happened
I ran Inline Method on m() at the call site inside a multi-declaration (int
a = 0, b = m();). The inlined method declares a local named c, and run()
already declares c in the same block, so the generated code declares c twice
and no longer compiles.
### Language / Project Type / NetBeans Component
Java SE application
### How to reproduce
1.Create a Java project.
2.Add the class below.
3.Place the caret on the m() call in int a = 0, b = m();.
4.Refactor > Inline, accept the preview.
5.Compile.
Before refactoring:
```
public class A {
static int m() {
int c = 9;
return c;
}
static void run() {
int a = 0, b = m();
int c = 3;
System.out.println("a=" + a + ", b=" + b + ", c=" + c);
}
public static void main(String[] args) {
run();
}
}
//output: a=0, b=9, c=3
```
After refactoring:
```
public class A {
static int m() {
int c = 9;
return c;
}
static void run() {
int a = 0, b;
int c = 9;
b = c;
int c = 3;
System.out.println("a=" + a + ", b=" + b + ", c=" + c);
}
public static void main(String[] args) {
run();
}
}
```
Compilation output:
```
A.java:12: error: variable c is already defined in method run()
int c = 3;
^
1 error
[A.java](https://github.com/user-attachments/files/35541689/A.java)
### Did this work correctly in an earlier version?
No / Don't know
### Operating System
Windows 11
### JDK
JDK 24
### Apache NetBeans packaging
Apache NetBeans binary zip
### Anything else
I believe this is erroneous because Java does not allow two locals with the
same name in one block, and inlining moved the declaration of c into the same
block as the existing c. The class compiled and printed a=0, b=9, c=3 before
the refactoring; after it, the program cannot be compiled at all.
Expected behaviour, either:
keep the inlined statements in a nested block:
```
int a = 0, b;
{ int c = 9; b = c; }
int c = 3;
System.out.println("a=" + a + ", b=" + b + ", c=" + c);
//output: a=0, b=9, c=3
```
or rename the inlined local (c → c1).
Both were verified to compile and produce a=0, b=9, c=3.
### Are you willing to submit a pull request?
No
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists