From 402a89eb0310a2b20d2b48aa44fc734f4f5c000a Mon Sep 17 00:00:00 2001 From: Zhihong Yu Date: Fri, 30 Jun 2023 14:56:03 -0700 Subject: [PATCH] Use try-with-resources to close reader / writer --- .../script/SingleLineSqlScriptExtractor.java | 3 +-- .../org/hibernate/orm/antlr/AntlrHelper.java | 23 ++++++++----------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/script/SingleLineSqlScriptExtractor.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/script/SingleLineSqlScriptExtractor.java index d2ba726368..a8a65900f3 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/script/SingleLineSqlScriptExtractor.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/script/SingleLineSqlScriptExtractor.java @@ -32,8 +32,7 @@ public class SingleLineSqlScriptExtractor implements SqlScriptCommandExtractor { public List extractCommands(Reader reader, Dialect dialect) { final List statementList = new LinkedList<>(); - final BufferedReader bufferedReader = new BufferedReader( reader ); - try { + try (final BufferedReader bufferedReader = new BufferedReader( reader )) { for ( String line = bufferedReader.readLine(); line != null; line = bufferedReader.readLine() ) { final String trimmedLine = line.trim(); diff --git a/local-build-plugins/src/main/java/org/hibernate/orm/antlr/AntlrHelper.java b/local-build-plugins/src/main/java/org/hibernate/orm/antlr/AntlrHelper.java index 02c054a8b2..f9c29c2090 100644 --- a/local-build-plugins/src/main/java/org/hibernate/orm/antlr/AntlrHelper.java +++ b/local-build-plugins/src/main/java/org/hibernate/orm/antlr/AntlrHelper.java @@ -51,23 +51,20 @@ public class AntlrHelper { tmpFile.getParentFile().mkdirs(); tmpFile.createNewFile(); - final BufferedReader reader = new BufferedReader( new FileReader( generatedJavaFile ) ); - final BufferedWriter writer = new BufferedWriter( new FileWriter( tmpFile ) ); + try (final BufferedReader reader = new BufferedReader( new FileReader( generatedJavaFile ) ); + final BufferedWriter writer = new BufferedWriter( new FileWriter( tmpFile ) )) { - boolean found = false; - String currentLine; + boolean found = false; + String currentLine; - while ( ( currentLine = reader.readLine() ) != null ) { - if ( ! found && currentLine.startsWith( "// Generated from" ) ) { - found = true; - continue; + while ((currentLine = reader.readLine()) != null) { + if (!found && currentLine.startsWith("// Generated from")) { + found = true; + continue; + } + writer.write(currentLine + System.lineSeparator()); } - writer.write( currentLine + System.lineSeparator() ); } - - writer.close(); - reader.close(); - generatedJavaFile.delete(); tmpFile.renameTo( generatedJavaFile ); }