Skip to content

Commit 7309af7

Browse files
authored
use try with resources (#62)
* use try with resources * order imports
1 parent 05ad8b8 commit 7309af7

File tree

3 files changed

+16
-54
lines changed

3 files changed

+16
-54
lines changed

src/main/java/org/apache/maven/plugins/dependency/analyze/AnalyzeDuplicateMojo.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* under the License.
2020
*/
2121

22+
import java.io.IOException;
2223
import java.io.Reader;
2324
import java.util.ArrayList;
2425
import java.util.Collections;
@@ -37,8 +38,8 @@
3738
import org.apache.maven.plugins.annotations.Mojo;
3839
import org.apache.maven.plugins.annotations.Parameter;
3940
import org.apache.maven.project.MavenProject;
40-
import org.codehaus.plexus.util.IOUtil;
4141
import org.codehaus.plexus.util.ReaderFactory;
42+
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
4243

4344
/**
4445
* Analyzes the <code>&lt;dependencies/&gt;</code> and <code>&lt;dependencyManagement/&gt;</code> tags in the
@@ -85,21 +86,13 @@ public void execute()
8586

8687
MavenXpp3Reader pomReader = new MavenXpp3Reader();
8788
Model model = null;
88-
Reader reader = null;
89-
try
89+
try ( Reader reader = ReaderFactory.newXmlReader( project.getFile() ) )
9090
{
91-
reader = ReaderFactory.newXmlReader( project.getFile() );
9291
model = pomReader.read( reader );
93-
reader.close();
94-
reader = null;
9592
}
96-
catch ( Exception e )
93+
catch ( IOException | XmlPullParserException e )
9794
{
98-
throw new MojoExecutionException( "IOException: " + e.getMessage(), e );
99-
}
100-
finally
101-
{
102-
IOUtil.close( reader );
95+
throw new MojoExecutionException( "Exception: " + e.getMessage(), e );
10396
}
10497

10598
Set<String> duplicateDependencies = Collections.emptySet();

src/main/java/org/apache/maven/plugins/dependency/fromDependencies/BuildClasspathMojo.java

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import org.apache.maven.project.MavenProjectHelper;
4646
import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
4747
import org.apache.maven.shared.transfer.repository.RepositoryManager;
48-
import org.codehaus.plexus.util.IOUtil;
4948
import org.codehaus.plexus.util.StringUtils;
5049

5150
/**
@@ -332,32 +331,24 @@ private void storeClasspathFile( String cpString, File out )
332331
// make sure the parent path exists.
333332
out.getParentFile().mkdirs();
334333

335-
Writer w = null;
336-
try
334+
try ( Writer w = new BufferedWriter( new FileWriter( out ) ) )
337335
{
338-
w = new BufferedWriter( new FileWriter( out ) );
339336
w.write( cpString );
340-
w.close();
341-
w = null;
342337
getLog().info( "Wrote classpath file '" + out + "'." );
343338
}
344339
catch ( IOException ex )
345340
{
346341
throw new MojoExecutionException( "Error while writing to classpath file '" + out + "': " + ex.toString(),
347342
ex );
348343
}
349-
finally
350-
{
351-
IOUtil.close( w );
352-
}
353344
}
354345

355346
/**
356347
* Reads into a string the file specified by the mojo param 'outputFile'. Assumes, the instance variable
357348
* 'outputFile' is not null.
358349
*
359-
* @return the string contained in the classpathFile, if exists, or null otherwise.
360-
* @throws IOException in case of an error.
350+
* @return the string contained in the classpathFile, if it exists, or null otherwise
351+
* @throws IOException in case of an error
361352
*/
362353
protected String readClasspathFile()
363354
throws IOException
@@ -373,26 +364,15 @@ protected String readClasspathFile()
373364
return null;
374365
}
375366
StringBuilder sb = new StringBuilder();
376-
BufferedReader r = null;
377-
378-
try
367+
try ( BufferedReader r = new BufferedReader( new FileReader( outputFile ) ) )
379368
{
380-
r = new BufferedReader( new FileReader( outputFile ) );
381-
382369
for ( String line = r.readLine(); line != null; line = r.readLine() )
383370
{
384371
sb.append( line );
385372
}
386373

387-
r.close();
388-
r = null;
389-
390374
return sb.toString();
391375
}
392-
finally
393-
{
394-
IOUtil.close( r );
395-
}
396376
}
397377

398378
/**

src/main/java/org/apache/maven/plugins/dependency/utils/DependencyUtil.java

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.apache.maven.artifact.Artifact;
3030
import org.apache.maven.artifact.ArtifactUtils;
3131
import org.apache.maven.plugin.logging.Log;
32-
import org.codehaus.plexus.util.IOUtil;
3332
import org.codehaus.plexus.util.StringUtils;
3433

3534
/**
@@ -227,30 +226,19 @@ private static String getDependencyId( Artifact artifact, boolean removeVersion
227226
public static synchronized void write( String string, File file, boolean append, Log log )
228227
throws IOException
229228
{
230-
file.getParentFile().mkdirs();
229+
file.getParentFile().mkdirs();
231230

232-
FileWriter writer = null;
233-
234-
try
231+
try ( FileWriter writer = new FileWriter( file, append ) )
235232
{
236-
writer = new FileWriter( file, append );
237-
238233
writer.write( string );
239-
240-
writer.close();
241-
writer = null;
242-
}
243-
finally
244-
{
245-
IOUtil.close( writer );
246234
}
247235
}
248236

249237
/**
250238
* Writes the specified string to the log at info level.
251239
*
252240
* @param string the string to write
253-
* @param log where to log information.
241+
* @param log where to log information
254242
* @throws IOException if an I/O error occurs
255243
*/
256244
public static synchronized void log( String string, Log log )
@@ -279,9 +267,10 @@ public static String[] tokenizer( String str )
279267
}
280268

281269
/**
282-
* clean up configuration string before it can be tokenized
283-
* @param str The str which should be cleaned.
284-
* @return cleaned up string.
270+
* Clean up configuration string before it can be tokenized.
271+
*
272+
* @param str the string which should be cleaned
273+
* @return cleaned up string
285274
*/
286275
public static String cleanToBeTokenizedString( String str )
287276
{

0 commit comments

Comments
 (0)