Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use try-with-resource #604

Merged
merged 4 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apitools/org.eclipse.pde.api.tools/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.pde.api.tools;singleton:=true
Bundle-Version: 1.3.0.qualifier
Bundle-Version: 1.3.100.qualifier
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.25.0,4.0.0)",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008, 2009 IBM Corporation and others.
* Copyright (c) 2008, 2023 IBM Corporation and others.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI - updating this year is optional; I personally find it a waste of effort. If you like to do this, that is fine but in case you also find it non-value add, you can skip this step.

*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -101,7 +101,11 @@ protected void setName(String newname) {
* @throws CoreException
*/
protected void abort(String message, Throwable e) throws CoreException {
throw new CoreException(Status.error(message, e));
throw abortException(message, e);
}

protected CoreException abortException(String message, Throwable e) {
return new CoreException(Status.error(message, e));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2009, 2013 IBM Corporation and others.
* Copyright (c) 2009, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -94,31 +94,20 @@ public int hashCode() {
@Override
public byte[] getContents() throws CoreException {
StubArchiveApiTypeContainer archive = (StubArchiveApiTypeContainer) getParent();
ZipFile zipFile = archive.open();
ZipEntry entry = zipFile.getEntry(getName());
InputStream stream = null;
if (entry != null) {
try {
stream = zipFile.getInputStream(entry);
} catch (IOException e) {
abort("Failed to open class file: " + getTypeName() + " in archive: " + archive.fLocation, e); //$NON-NLS-1$ //$NON-NLS-2$
return null;
}
try {
return stream.readAllBytes();
} catch (IOException ioe) {
abort("Unable to read class file: " + getTypeName(), ioe); //$NON-NLS-1$
return null; // never gets here
} finally {
try {
stream.close();
} catch (IOException e) {
ApiPlugin.log(e);
try (ZipFile zipFile = archive.open()) {
ZipEntry entry = zipFile.getEntry(getName());
if (entry != null) {
try (InputStream stream = zipFile.getInputStream(entry)) {
return stream.readAllBytes();
}
}
throw abortException("Class file not found: " + getTypeName() + " in archive: " + archive.fLocation, //$NON-NLS-1$ //$NON-NLS-2$
null);
} catch (IOException e) {
ApiPlugin.log(e);
throw abortException(
"Failed to read class file: " + getTypeName() + " in archive: " + archive.fLocation, e); //$NON-NLS-1$ //$NON-NLS-2$
}
abort("Class file not found: " + getTypeName() + " in archive: " + archive.fLocation, null); //$NON-NLS-1$ //$NON-NLS-2$
return null;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2007, 2021 IBM Corporation and others.
* Copyright (c) 2007, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -1983,15 +1983,14 @@ public static void guntar(String zipPath, String destDirPath) throws TarExceptio
new File(destDirPath, fileDir).mkdirs();
// write file
File outFile = new File(destDirPath, filePath);
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outFile));
int n = 0;
InputStream inputStream = tarFile.getInputStream(zEntry);
BufferedInputStream stream = new BufferedInputStream(inputStream);
while ((n = stream.read(buf)) >= 0) {
outputStream.write(buf, 0, n);
try (BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outFile))) {
int n = 0;
try (BufferedInputStream stream = new BufferedInputStream(tarFile.getInputStream(zEntry))) {
while ((n = stream.read(buf)) >= 0) {
outputStream.write(buf, 0, n);
}
}
}
outputStream.close();
stream.close();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion ui/org.eclipse.pde.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %name
Bundle-SymbolicName: org.eclipse.pde.core; singleton:=true
Bundle-Version: 3.17.0.qualifier
Bundle-Version: 3.17.100.qualifier
Bundle-Activator: org.eclipse.pde.internal.core.PDECore
Bundle-Vendor: %provider-name
Bundle-Localization: plugin
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2005, 2012 IBM Corporation and others.
* Copyright (c) 2005, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -14,6 +14,7 @@
package org.eclipse.pde.internal.core;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Arrays;
import java.util.Comparator;
Expand Down Expand Up @@ -113,12 +114,14 @@ private boolean isValid(IConfigurationElement elem) {
value = elem.getAttribute("definition"); //$NON-NLS-1$
String symbolicName = elem.getDeclaringExtension().getContributor().getName();
URL url = getResourceURL(symbolicName, value);
try {
if (url != null && url.openStream().available() > 0) {
return true;
if (url != null) {
try (InputStream s = url.openStream()) {
if (s.available() > 0) {
return true;
}
} catch (IOException e) {
// file does not exist
}
} catch (IOException e) {
// file does not exist
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2017 IBM Corporation and others.
* Copyright (c) 2000, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -212,8 +212,8 @@ private String getPluginID() {
File OSGiFile = new File(file.getParentFile(), ICoreConstants.BUNDLE_FILENAME_DESCRIPTOR);

if (OSGiFile.exists()) {
try {
Map<String, String> headers = ManifestElement.parseBundleManifest(new FileInputStream(OSGiFile), new HeaderMap<>());
try (FileInputStream manifestStream = new FileInputStream(OSGiFile)) {
Map<String, String> headers = ManifestElement.parseBundleManifest(manifestStream, new HeaderMap<>());
String value = headers.get(Constants.BUNDLE_SYMBOLICNAME);
if (value == null) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion ui/org.eclipse.pde.ui/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %name
Bundle-SymbolicName: org.eclipse.pde.ui; singleton:=true
Bundle-Version: 3.14.0.qualifier
Bundle-Version: 3.14.100.qualifier
Bundle-Activator: org.eclipse.pde.internal.ui.PDEPlugin
Bundle-Vendor: %provider-name
Bundle-Localization: plugin
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2021 IBM Corporation and others.
* Copyright (c) 2000, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -183,11 +183,12 @@ private static IEditorPart openWorkspacePlugin(IFile pluginFile) {
private static IEditorPart openExternalPlugin(File location, String filename) {
IEditorInput input = null;
if (location.isFile()) {
try {
ZipFile zipFile = new ZipFile(location);
if (zipFile.getEntry(filename) != null)
try (ZipFile zipFile = new ZipFile(location)) {
if (zipFile.getEntry(filename) != null) {
input = new JarEntryEditorInput(new JarEntryFile(zipFile, filename));
}
} catch (IOException e) {
// ignore
}
} else {
File file = new File(location, filename);
Expand Down