You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by "edrobal (via GitHub)" <gi...@apache.org> on 2025/11/28 13:45:42 UTC

[I] Code conversion error [netbeans]

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

   ### Apache NetBeans version
   
   Apache NetBeans 28
   
   ### What happened
   
   Code conversion truncate code
   
   ### Language / Project Type / NetBeans Component
   
   java class library
   
   ### How to reproduce
   
   Initial code is 
           ByteArrayOutputStream outStream = new ByteArrayOutputStream();
           int len;
   
           byte[] buffer = new byte[1024];
   
           while ((len = input.read(buffer)) != -1) {
               outStream.write(buffer, 0, len);
           }
           input.close();
   
           return outStream.toByteArray();
   Netbeans show convert to try-with-ressources and after code is
   
           try (input) {
               outStream = new ByteArrayOutputStream();
               int len;
               byte[] buffer = new byte[1024];
               while ((len = input.read(buffer)) != -1) {
                   outStream.write(buffer, 0, len);
               }
           }
   
   
   ### Did this work correctly in an earlier version?
   
   No / Don't know
   
   ### Operating System
   
   Linux 6.4.0-150600.23.65-default 
   
   ### JDK
   
   Java: 24.0.2; OpenJDK 64-Bit Server VM 24.0.2+12
   
   ### Apache NetBeans packaging
   
   Apache NetBeans binary zip
   
   ### Anything else
   
   _No response_
   
   ### 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: notifications-unsubscribe@netbeans.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


Re: [I] Code conversion error [netbeans]

Posted by "mbien (via GitHub)" <gi...@apache.org>.
mbien commented on issue #9046:
URL: https://github.com/apache/netbeans/issues/9046#issuecomment-3591543032

   works for me:
   
   ```java
       public static void test(InputStream input) throws Exception {
           ByteArrayOutputStream outStream = new ByteArrayOutputStream();
           int len;
   
           byte[] buffer = new byte[1024];
   
           while ((len = input.read(buffer)) != -1) {
               outStream.write(buffer, 0, len);
           }
           input.close();
       }
   ```
   
   ->
   
   ```java
       public static void test(InputStream input) throws Exception {
           try (input) {
               ByteArrayOutputStream outStream = new ByteArrayOutputStream();
               int len;
               
               byte[] buffer = new byte[1024];
               
               while ((len = input.read(buffer)) != -1) {
                   outStream.write(buffer, 0, len);
               }
           }
       }
   ```


-- 
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: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


Re: [I] Code conversion error [netbeans]

Posted by "matthiasblaesing (via GitHub)" <gi...@apache.org>.
matthiasblaesing commented on issue #9046:
URL: https://github.com/apache/netbeans/issues/9046#issuecomment-3592744061

   @mbien the return part is crucial here. The example above should be this:
   
   ```java
   import java.io.ByteArrayOutputStream;
   import java.io.InputStream;
   
   public class Test {
   
       public static byte[] test(InputStream input) throws Exception {
           ByteArrayOutputStream outStream = new ByteArrayOutputStream();
           int len;
   
           byte[] buffer = new byte[1024];
   
           while ((len = input.read(buffer)) != -1) {
               outStream.write(buffer, 0, len);
           }
           input.close();
   
           return outStream.toByteArray();
       }
   }
   ```
   
   And the refactoring hint/fix leads to:
   
   ```java
   import java.io.ByteArrayOutputStream;
   import java.io.InputStream;
   
   public class Test {
   
       public static byte[] test(InputStream input) throws Exception {
           try (input) {
               outStream = new ByteArrayOutputStream();
               int len;
               byte[] buffer = new byte[1024];
               while ((len = input.read(buffer)) != -1) {
                   outStream.write(buffer, 0, len);
               }
           }
   
           return outStream.toByteArray();
       }
   }
   ```
   
   The hint seems to correctly detect, that the variable `outStream` must be declared outside the try-with-resource block, but fails to insert the declaration before the block.


-- 
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: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


Re: [I] Code conversion error [netbeans]

Posted by "matthiasblaesing (via GitHub)" <gi...@apache.org>.
matthiasblaesing closed issue #9046: Code conversion error
URL: https://github.com/apache/netbeans/issues/9046


-- 
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: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists