You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by el...@apache.org on 2024/12/20 23:40:44 UTC
(maven-doap-plugin) branch master updated: Use try with resources (#28)
This is an automated email from the ASF dual-hosted git repository.
elharo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-doap-plugin.git
The following commit(s) were added to refs/heads/master by this push:
new 546e8dc Use try with resources (#28)
546e8dc is described below
commit 546e8dcab1776694cedd5a51e2de9ae88fa13060
Author: Elliotte Rusty Harold <el...@users.noreply.github.com>
AuthorDate: Fri Dec 20 23:40:38 2024 +0000
Use try with resources (#28)
* Use try with resources
---
.../org/apache/maven/plugin/doap/DoapUtil.java | 35 ++++++++--------------
.../org/apache/maven/plugin/doap/DoapMojoTest.java | 22 ++------------
.../maven/plugin/doap/stubs/DoapProjectStub.java | 18 ++++-------
3 files changed, 20 insertions(+), 55 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..52223e7 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;
@@ -414,7 +413,7 @@ public class DoapUtil {
/**
* Validate the given DOAP file.
*
- * @param doapFile not null and should exists.
+ * @param doapFile not null and should exist
* @return an empty list if the DOAP file is valid, otherwise a list of errors.
* @since 1.1
*/
@@ -458,29 +457,25 @@ 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
*/
+ @SuppressWarnings("checkstyle:emptyblock")
public static void fetchURL(Settings settings, URL url) throws IOException {
if (url == null) {
throw new IllegalArgumentException("The url is null");
}
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.
+ // Test if file exists
+ try (InputStream in = url.openStream()) {}
return;
}
@@ -688,24 +683,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.siveton@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());