This is an automated email from the ASF dual-hosted git repository. elharo pushed a commit to branch io in repository https://gitbox.apache.org/repos/asf/maven-doap-plugin.git
commit 37e1ec489603ea48267fde625bff27e57020f206 Author: Elliotte Rusty Harold <elh...@ibiblio.org> AuthorDate: Mon Dec 16 07:51:45 2024 -0500 Use try with reources --- .../org/apache/maven/plugin/doap/DoapUtil.java | 34 ++++++++-------------- .../org/apache/maven/plugin/doap/DoapMojoTest.java | 22 ++------------ .../maven/plugin/doap/stubs/DoapProjectStub.java | 18 ++++-------- 3 files changed, 20 insertions(+), 54 deletions(-) diff --git a/src/main/java/org/apache/maven/plugin/doap/DoapUtil.java b/src/main/java/org/apache/maven/plugin/doap/DoapUtil.java index 4a62e86..d4cf72a 100644 --- a/src/main/java/org/apache/maven/plugin/doap/DoapUtil.java +++ b/src/main/java/org/apache/maven/plugin/doap/DoapUtil.java @@ -69,7 +69,6 @@ import org.codehaus.plexus.interpolation.ObjectBasedValueSource; import org.codehaus.plexus.interpolation.PrefixedObjectValueSource; import org.codehaus.plexus.interpolation.PropertiesBasedValueSource; import org.codehaus.plexus.interpolation.RegexBasedInterpolator; -import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.introspection.ClassMap; import org.codehaus.plexus.util.xml.XMLWriter; @@ -458,10 +457,10 @@ public class DoapUtil { } /** - * Fetch an URL + * Fetch a URL. * - * @param settings the user settings used to fetch the url with an active proxy, if defined. - * @param url the url to fetch + * @param settings the user settings used to fetch the URL with an active proxy, if defined + * @param url the URL to fetch * @throws IOException if any * @see #DEFAULT_TIMEOUT * @since 1.1 @@ -472,16 +471,13 @@ public class DoapUtil { } if ("file".equals(url.getProtocol())) { - InputStream in = null; - try { - in = url.openStream(); - in.close(); - in = null; - } finally { - IOUtil.close(in); + // [ERROR] src/main/java/org/apache/maven/plugin/doap/DoapUtil.java:[474,53] (blocks) EmptyBlock: Empty try + // block. + try (InputStream in = url.openStream()) { + return; + } catch (IOException ex) { + return; } - - return; } // http, https... @@ -688,24 +684,18 @@ public class DoapUtil { */ private static String getPluginVersion() { Properties pomProperties = new Properties(); - InputStream is = null; - try { - is = DoapUtil.class.getResourceAsStream( - "/META-INF/maven/org.apache.maven.plugins/" + "maven-doap-plugin/pom.properties"); + + try (InputStream is = DoapUtil.class.getResourceAsStream( + "/META-INF/maven/org.apache.maven.plugins/" + "maven-doap-plugin/pom.properties")) { if (is == null) { return "<unknown>"; } pomProperties.load(is); - is.close(); - is = null; - return pomProperties.getProperty("version", "<unknown>"); } catch (IOException e) { return "<unknown>"; - } finally { - IOUtil.close(is); } } diff --git a/src/test/java/org/apache/maven/plugin/doap/DoapMojoTest.java b/src/test/java/org/apache/maven/plugin/doap/DoapMojoTest.java index 31c673c..d359160 100644 --- a/src/test/java/org/apache/maven/plugin/doap/DoapMojoTest.java +++ b/src/test/java/org/apache/maven/plugin/doap/DoapMojoTest.java @@ -405,27 +405,9 @@ public class DoapMojoTest extends AbstractMojoTestCase { assertTrue(readed.contains("<labs:status>active</labs:status>")); } - /** - * @param file - * @return - * @throws IOException if any - */ private String readFile(File file) throws IOException { - String result = null; - - FileReader reader = null; - try { - // platform encoding - reader = new FileReader(file); - - result = IOUtil.toString(reader); - - reader.close(); - reader = null; - } finally { - IOUtil.close(reader); + try (FileReader reader = new FileReader(file)) { + return IOUtil.toString(reader); } - - return result; } } diff --git a/src/test/java/org/apache/maven/plugin/doap/stubs/DoapProjectStub.java b/src/test/java/org/apache/maven/plugin/doap/stubs/DoapProjectStub.java index c256647..b799fd2 100644 --- a/src/test/java/org/apache/maven/plugin/doap/stubs/DoapProjectStub.java +++ b/src/test/java/org/apache/maven/plugin/doap/stubs/DoapProjectStub.java @@ -19,6 +19,7 @@ package org.apache.maven.plugin.doap.stubs; import java.io.File; +import java.io.IOException; import java.util.Collections; import java.util.List; @@ -34,9 +35,9 @@ import org.apache.maven.model.Organization; import org.apache.maven.model.Scm; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; import org.apache.maven.plugin.testing.stubs.MavenProjectStub; -import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.ReaderFactory; import org.codehaus.plexus.util.xml.XmlStreamReader; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; /** * @author <a href="mailto:vincent.sive...@gmail.com">Vincent Siveton</a> @@ -49,20 +50,13 @@ public class DoapProjectStub extends MavenProjectStub { */ public DoapProjectStub() { MavenXpp3Reader pomReader = new MavenXpp3Reader(); - XmlStreamReader reader = null; - try { - reader = ReaderFactory.newXmlReader(new File( - new File(super.getBasedir(), "/src/test/resources/unit/doap-configuration/"), - "doap-configuration-plugin-config.xml")); + try (XmlStreamReader reader = ReaderFactory.newXmlReader(new File( + new File(super.getBasedir(), "/src/test/resources/unit/doap-configuration/"), + "doap-configuration-plugin-config.xml"))) { model = pomReader.read(reader); setModel(model); - - reader.close(); - reader = null; - } catch (Exception e) { + } catch (IOException | XmlPullParserException e) { throw new RuntimeException(e); - } finally { - IOUtil.close(reader); } setGroupId(model.getGroupId());