Skip to content

Commit

Permalink
Use try with reources
Browse files Browse the repository at this point in the history
  • Loading branch information
elharo committed Dec 16, 2024
1 parent 0551698 commit 37e1ec4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 54 deletions.
34 changes: 12 additions & 22 deletions src/main/java/org/apache/maven/plugin/doap/DoapUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
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;
Expand Down Expand Up @@ -458,10 +457,10 @@ public static boolean isValidEmail(String str) {
}

/**
* 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
Expand All @@ -472,16 +471,13 @@ public static void fetchURL(Settings settings, URL url) throws IOException {
}

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...
Expand Down Expand Up @@ -688,24 +684,18 @@ private static String getLowerCaseString(I18N i18n, String key) {
*/
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);
}
}

Expand Down
22 changes: 2 additions & 20 deletions src/test/java/org/apache/maven/plugin/doap/DoapMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -405,27 +405,9 @@ public void testGeneratedExtraDoap() throws Exception {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -34,9 +35,9 @@
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:[email protected]">Vincent Siveton</a>
Expand All @@ -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());
Expand Down

0 comments on commit 37e1ec4

Please sign in to comment.