Skip to content

Commit

Permalink
Merge pull request apache#7076 from pepness/ioutils-deprecated
Browse files Browse the repository at this point in the history
Remove usage of package `org.codehaus.plexus.util.IOUtil`.
- Use `try-with-resources` instead of `IOUtil.close()` method
- Use `readAllBytes()` instead of `IOUtil.toByteArray()`
- Bump `javac.source` and `javac.target` to version 11 in module
  `maven.checkstyle`. Needed for method `readAllBytes()` and to use a
  variable inside `try-with-resources`
- Add `Maven` as a display category to the module `maven.checkstyle`
- Use one wrapped stream instead of two
- Use native JDK method `Files.copy` instead of NetBeans method
  `FileUtil.copy`
  • Loading branch information
pepness authored Feb 24, 2024
2 parents 01b8f9a + f88417a commit 61d4cd1
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.regex.Pattern;
import org.netbeans.api.annotations.common.SuppressWarnings;
import org.netbeans.modules.maven.api.NbMavenProject;
import org.codehaus.plexus.util.IOUtil;
import org.netbeans.api.project.Project;
import org.netbeans.spi.java.queries.AccessibilityQueryImplementation;
import org.netbeans.spi.project.ProjectServiceProvider;
Expand Down Expand Up @@ -166,17 +165,13 @@ private static List<Pattern> loadPublicPackagesPatterns(Project project) {
} else {
FileObject obj = project.getProjectDirectory().getFileObject(MANIFEST_PATH);
if (obj != null) {
InputStream in = null;
try {
in = obj.getInputStream();
try (InputStream in = obj.getInputStream()) {
Manifest man = new Manifest();
man.read(in);
String value = man.getMainAttributes().getValue(ATTR_PUBLIC_PACKAGE);
toRet = prepareManifestPublicPackagesPatterns(value);
} catch (Exception ex) {
Exceptions.printStackTrace(ex);
} finally {
IOUtil.close(in);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.netbeans.api.annotations.common.NonNull;
Expand Down Expand Up @@ -367,18 +366,14 @@ private Tuple cacheOrLoad() {
private Manifest getManifest(FileObject root) {
FileObject manifestFo = root.getFileObject("META-INF/MANIFEST.MF");
if (manifestFo != null) {
InputStream is = null;
try {
is = manifestFo.getInputStream();
return new Manifest(is);
try (InputStream is = manifestFo.getInputStream()) {
Manifest manifest = new Manifest(is);
return manifest;
} catch (IOException ex) {
//Exceptions.printStackTrace(ex);
} finally {
IOUtil.close(is);
}
}
return null;

}

private static class MavenWhiteListImplementation implements WhiteListImplementation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
Expand All @@ -32,7 +34,6 @@
import org.apache.maven.model.Dependency;
import org.apache.maven.model.PluginManagement;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.eclipse.aether.repository.RemoteRepository;
import org.netbeans.modules.apisupport.project.api.EditableManifest;
Expand Down Expand Up @@ -103,14 +104,10 @@ public void run() {
if (packageName != null) {
String path = packageName.replace(".", "/") + "/Bundle.properties";
mf.setAttribute("OpenIDE-Module-Localizing-Bundle", path, null);
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(new File(src, "manifest.mf")));
mf.write(bos);
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(new File(src, "manifest.mf")))) {
mf.write(os);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
} finally {
IOUtil.close(bos);
}

}
Expand All @@ -121,20 +118,13 @@ public void run() {
String path = packageName.replace(".", File.separator);
File res = new File(src, path);
res.mkdirs();
OutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(new File(res, "Bundle.properties")));
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(new File(res, "Bundle.properties")))) {
Properties p = new Properties();
p.store(bos, EMPTY_BUNDLE_FILE);

p.store(os, EMPTY_BUNDLE_FILE);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
} finally {
IOUtil.close(bos);
}

}

}
}

Expand Down
3 changes: 2 additions & 1 deletion java/maven.checkstyle/nbproject/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
javac.source=1.8
javac.source=11
javac.target=11
javac.compilerargs=-Xlint -Xlint:-serial
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
OpenIDE-Module-Display-Category=Maven
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.project.ProjectBuildingException;
import org.codehaus.plexus.util.IOUtil;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectUtils;
import org.netbeans.modules.maven.api.Constants;
Expand Down Expand Up @@ -93,14 +92,8 @@ private FileObject copyToCacheDir(InputStream in) throws IOException {
if (file == null) {
file = cacheDir.createData("checkstyle-checker", "xml");
}
InputStream inst = in;
OutputStream outst = null;
try {
outst = file.getOutputStream();
try (in; OutputStream outst = file.getOutputStream()) {
FileUtil.copy(in, outst);
} finally {
IOUtil.close(inst);
IOUtil.close(outst);
}
return file;
}
Expand Down Expand Up @@ -159,10 +152,8 @@ private Properties convert() {
RP.post(new Runnable() {
@Override
public void run() {
InputStream urlis = null;
try {
urlis = url.openStream();
byte[] arr = IOUtil.toByteArray(urlis);
try (InputStream urlis = url.openStream()) {
byte[] arr = urlis.readAllBytes();
synchronized (AuxPropsImpl.this) {
//#174401
ByteArrayInputStream bais = new ByteArrayInputStream(arr);
Expand All @@ -171,8 +162,6 @@ public void run() {
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
IOUtil.close(urlis);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package org.netbeans.modules.maven.format.checkstyle;

import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.StringUtils;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -103,7 +102,7 @@ public InputSource resolveEntity(String publicId, String systemId) throws SAXExc
return is;
}
});
try {
try (checkstyleStream) {
Document doc = bldr.build(checkstyleStream);
Element root = doc.getRootElement();
processModule(root, "", props);
Expand All @@ -114,8 +113,6 @@ public InputSource resolveEntity(String publicId, String systemId) throws SAXExc
Exceptions.printStackTrace(ex);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
} finally {
IOUtil.close(checkstyleStream);
}
return props;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.jdom2.DefaultJDOMFactory;
import org.jdom2.Document;
Expand Down Expand Up @@ -415,10 +414,6 @@ private static void writeNbActionsModel(final Project project, final FileObject
@Override
public void run() throws IOException {
JDOMFactory factory = new DefaultJDOMFactory();

InputStream inStr = null;
FileLock lock = null;
OutputStreamWriter outStr = null;
try {
Document doc;
if (mapping.getActions().isEmpty()) { //#224450 don't write empty nbactions.xml files
Expand All @@ -434,19 +429,19 @@ public void run() throws IOException {
doc = factory.document(factory.element("actions")); //NOI18N
} else {
//TODO..
inStr = fo.getInputStream();
SAXBuilder builder = new SAXBuilder();
doc = builder.build(inStr);
inStr.close();
inStr = null;
try (InputStream inStr = fo.getInputStream()) {
SAXBuilder builder = new SAXBuilder();
doc = builder.build(inStr);
}
}
lock = fo.lock();
NetbeansBuildActionJDOMWriter writer = new NetbeansBuildActionJDOMWriter();
String encoding = mapping.getModelEncoding() != null ? mapping.getModelEncoding() : "UTF-8"; //NOI18N
outStr = new OutputStreamWriter(fo.getOutputStream(lock), encoding);
Format form = Format.getRawFormat().setEncoding(encoding);
form = form.setLineSeparator(System.getProperty("line.separator")); //NOI18N
writer.write(mapping, doc, outStr, form);
try (FileLock lock = fo.lock();
OutputStreamWriter outStr = new OutputStreamWriter(fo.getOutputStream(lock), encoding);) {
NetbeansBuildActionJDOMWriter writer = new NetbeansBuildActionJDOMWriter();
Format form = Format.getRawFormat().setEncoding(encoding);
form = form.setLineSeparator(System.getProperty("line.separator")); //NOI18N
writer.write(mapping, doc, outStr, form);
}
} catch (JDOMException exc){
//throw (IOException) new IOException("Cannot parse the nbactions.xml by JDOM.").initCause(exc); //NOI18N
//TODO this would need it's own problem provider, but how to access it in project lookup if all are merged into one?
Expand All @@ -461,13 +456,6 @@ public void run() throws IOException {
impl.addReport(rep);
}
Logger.getLogger(CustomizerProviderImpl.class.getName()).log(Level.INFO, exc.getMessage(), exc);
} finally {
IOUtil.close(inStr);
IOUtil.close(outStr);
if (lock != null) {
lock.releaseLock();
}

}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -54,7 +55,6 @@
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.cli.CommandLineUtils;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.netbeans.api.annotations.common.NonNull;
Expand Down Expand Up @@ -1029,7 +1029,6 @@ private File checkAvailability(String ver, VersionRange vr, InputOutput ioput) {
} else {
f.mkdirs();
ioput.getOut().println("NetBeans: Downloading and unzipping Maven version " + ver);
ZipInputStream str = null;
try {
//this url pattern works for all versions except the last one 3.2.3
//which is only under <mirror>/apache/maven/maven-3/3.2.3/binaries/
Expand All @@ -1048,24 +1047,19 @@ private File checkAvailability(String ver, VersionRange vr, InputOutput ioput) {
LOGGER.log(Level.WARNING, "wasn''t able to download maven binaries, version {0}", ver);
return null;
}
str = new ZipInputStream(is);
ZipEntry entry;
while ((entry = str.getNextEntry()) != null) {
//base it of f not child as the zip contains the maven base folder
File fileOrDir = new File(f, entry.getName());
if (entry.isDirectory()) {
fileOrDir.mkdirs();
} else {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(fileOrDir);
FileUtil.copy(str, fos);
} finally {
IOUtil.close(fos);
}
// correct way to set executable flag?
if ("bin".equals(fileOrDir.getParentFile().getName()) && !fileOrDir.getName().endsWith(".conf")) {
fileOrDir.setExecutable(true);
try (ZipInputStream str = new ZipInputStream(is)) {
ZipEntry entry;
while ((entry = str.getNextEntry()) != null) {
//base it of f not child as the zip contains the maven base folder
File fileOrDir = new File(f, entry.getName());
if (entry.isDirectory()) {
fileOrDir.mkdirs();
} else {
Files.copy(str, fileOrDir.toPath());
// correct way to set executable flag?
if ("bin".equals(fileOrDir.getParentFile().getName()) && !fileOrDir.getName().endsWith(".conf")) {
fileOrDir.setExecutable(true);
}
}
}
}
Expand All @@ -1088,8 +1082,6 @@ private File checkAvailability(String ver, VersionRange vr, InputOutput ioput) {
} catch (IOException ex1) {
Exceptions.printStackTrace(ex1);
}
} finally {
IOUtil.close(str);
}
}
return null;
Expand Down

0 comments on commit 61d4cd1

Please sign in to comment.