You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by "neilcsmith-net (via GitHub)" <gi...@apache.org> on 2024/03/04 10:26:39 UTC

Re: [PR] fix try with resources on null file [netbeans-mavenutils-nbm-maven-plugin]

neilcsmith-net commented on code in PR #153:
URL: https://github.com/apache/netbeans-mavenutils-nbm-maven-plugin/pull/153#discussion_r1510935202


##########
nb-repository-plugin/src/main/java/org/apache/netbeans/nbm/repository/PopulateRepositoryMojo.java:
##########
@@ -1105,17 +1104,24 @@ else if ( wr instanceof ModuleWrapperMaven )
 //        mavenModel.setBuild(build);
 
         File fil = null;
-        try ( FileWriter writer = new FileWriter( fil ) )
+        try 
         {
-            MavenXpp3Writer xpp = new MavenXpp3Writer();
-            fil = Files.createTempFile( "maven", ".pom" ).toFile();
+            fil = Files.createTempFile("maven", ".pom").toFile();
             fil.deleteOnExit();
-            xpp.write( writer, mavenModel );
-        }
-        catch ( IOException ex )
+        } catch (IOException ex) 
         {
             ex.printStackTrace();
-        }        
+        }
+        if (fil != null) {

Review Comment:
   Would this not be better as nested `try`?
   
   ```
   try {
     File file = Files.createTempFile("maven", ".pom").toFile();
     file.deleteOnExit();
     try (FileWriter writer = new FileWriter(file)) {
        // write
     }
   } catch (IOException ex) {
     // handle all exceptions
   }
   ```
   
   It's a shame try-with-resources doesn't seem to handle intermediate declarations that are not `AutoCloseable`.



-- 
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