This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.
1) Create Java file package pck; public class IntroduceVariable { /*static */ long currentTime = 42; void foo() { System.out.println(System.currentTimeMillis()); // #7 System.out.println(System.currentTimeMillis()); System.currentTimeMillis(); long i = currentTime; System.out.println(currentTime); class MyLocalClass { String s = "" + currentTime; { System.out.println(System.currentTimeMillis()); System.out.println(currentTime); } } } } 2) Place cursor within currentTimeMillis() method call on line 7, right click->Refactor->Introduce->Variable... Confirm selection of "System.currentTimeMillis()". Introduce Variable dialog Name: currentTime Replace All Occurences (4) - check OK The resulting code (with my comments on wrong replacements) is: package pck; public class IntroduceVariable { /*static */ long currentTime = 42; void foo() { long currentTime = System.currentTimeMillis(); System.out.println(currentTime); // #7 System.out.println(currentTime); currentTime; // Not a statement. Expression statements cannot be replaced by a variable name. long i = currentTime; // This should be this.currentTime (if the field is instance field), in case of static field -> IntroduceVariable.currentTime System.out.println(currentTime); // This should be this.currentTime (if the field is instance field), in case of static field -> IntroduceVariable.currentTime class MyLocalClass { String s = "" + currentTime; //This should be IntroduceVariable.this.currentTime (if the field is instance field), in case of static field -> IntroduceVariable.currentTime { System.out.println(currentTime); // OK System.out.println(currentTime); //This should be IntroduceVariable.this.currentTime (if the field is instance field), in case of static field -> IntroduceVariable.currentTime } } } } When fixing this, please considered also possibility that class MyLocalClass can declare its own field currentTime: class MyLocalClass { String currentTime = "Hello"; String s = "" + currentTime; ... }
Breaks code, bumping priority.
One more example class you can use for testing so that the scope qualifier of outer classes is prefixed properly. Enjoy: package pck; public class CheckFieldShadowing { int i = 9; class InnerClass extends ParentClassForShadowing { class InnerInnerClass { void foo() { // Introduce variable for 'System.currentTimeMillis()', // use names: 'x', 'y', 'z,',' i' System.out.println(System.currentTimeMillis()); System.out.println(x); System.out.println(y); System.out.println(z); System.out.println(i); } } } } class ParentClassForShadowing extends SuperParentClass implements SuperInterface { public int x; } class SuperParentClass { protected int y; } interface SuperInterface { static int z = 3; }
I don't think I can fix the behaviour change without some additional metadata (the same case would be a Math.random() call assigned to a variable). But replacement of the method call statement with expression (which is not a proper statement) can be fixed. Not a P2, though.
Adding dependency; I need to find the to-be-shadowed definitions.
Fixed in jet-main#a494d90e587c
Integrated into 'main-silver', will be available in build *201606180002* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress) Changeset: http://hg.netbeans.org/main-silver/rev/a494d90e587c User: Svata Dedic <sdedic@netbeans.org> Log: #256404: UI warns if a symbol should be shadowed; refactors references, if the user insists on shadowing