LingerMind opened a new issue, #9774:
URL: https://github.com/apache/netbeans/issues/9774

   ### Apache NetBeans version
   
   Apache NetBeans 31
   
   ### What happened
   
   Inline Method on a bound method reference replaces the reference with a 
lambda and drops the qualifier expression entirely. In this case the qualifier 
is a method call with a side effect, so the refactored program produces a 
different result.
   
   
   ### Language / Project Type / NetBeans Component
   
   Java SE application
   
   ### How to reproduce
   
   
[InlineMethod_S005_T07.java](https://github.com/user-attachments/files/34233053/InlineMethod_S005_T07.java)
   1. Create a Java SE project and add a file named InlineMethod_S005_T07.java 
with the
      code below (InlineMethod_S005_T07 is the test-case ID from my refactoring
      comparison suite; the class is self-contained).
   2. Put the caret on getValue in the method reference 
getArrayHolder()::getValue.
   3. Refactor-> Inline ,and accept the defaults.
   4. Run the class.
   
   Before reflactoring:
   
   import java.util.function.IntSupplier;
   public class InlineMethod_S005_T07 {
       private static int[] values = new int[]{1, 2, 3};
       static class ArrayHolder {
           int getValue() {
               return values[0];
           }
       }
       static ArrayHolder getArrayHolder() {
           values[0] = 99;
           return new ArrayHolder();
       }
       static int run() {
           IntSupplier supplier = getArrayHolder()::getValue;
           return supplier.getAsInt();
       }
       public static void main(String[] args) {
           System.out.println(run());
       }
   }
   //output: 99
   
   After reflactoring:
   
   import java.util.function.IntSupplier;
   public class InlineMethod_S005_T07 {
       private static int[] values = new int[]{1, 2, 3};
       static class ArrayHolder {
       }
       static ArrayHolder getArrayHolder() {
           values[0] = 99;
           return new ArrayHolder();
       }
       static int run() {
           IntSupplier supplier = () -> {
               return values[0];
           };
           return supplier.getAsInt();
       }
       public static void main(String[] args) {
           System.out.println(run());
       }
   }
   //output: 1
   
   ### Did this work correctly in an earlier version?
   
   Apache NetBeans 31
   
   ### Operating System
   
   Windows 11
   
   ### JDK
   
   JDK 24
   
   ### Apache NetBeans packaging
   
   Apache NetBeans binary zip
   
   ### Anything else
   
   Possible directions:
   1. Evaluate the qualifier once into a temporary and inline the body against 
it, e.g.
          ArrayHolder tmp = getArrayHolder();
          IntSupplier supplier = () -> { return tmp.getValue(); };
   2. Or refuse/warn when the qualifier is not a simple effectively-final 
variable.
   3. Keep the current path only for unbound references (Type::method).
   The same code path is used for every bound method reference, so this is not 
specific to the test case. I can provide the full project or a screen recording 
if useful.
   
   ### 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

Reply via email to