Skip to content

Commit

Permalink
Use try-with-resource
Browse files Browse the repository at this point in the history
and enable explicitlyClosedAutoCloseable=warning
  • Loading branch information
EcljpseB0T authored and jukzi committed Nov 30, 2023
1 parent 4226895 commit 88d5d57
Show file tree
Hide file tree
Showing 38 changed files with 71 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -606,9 +606,10 @@ private int[] getWeights(IMemento memento) {
public void partDeactivated(IWorkbenchPart part) {
String id = part.getSite().getId();
if (id.equals(getSite().getId())) {
try (StringWriter writer = new StringWriter()) {
XMLMemento memento = XMLMemento.createWriteRoot("VariablesViewMemento"); //$NON-NLS-1$
saveViewerState(memento);
StringWriter writer = new StringWriter();
XMLMemento memento = XMLMemento.createWriteRoot("VariablesViewMemento"); //$NON-NLS-1$
saveViewerState(memento);
try {
memento.save(writer);

IPreferenceStore store = DebugUIPlugin.getDefault().getPreferenceStore();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ org.eclipse.jdt.core.compiler.problem.discouragedReference=warning
org.eclipse.jdt.core.compiler.problem.emptyStatement=warning
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=warning
org.eclipse.jdt.core.compiler.problem.fallthroughCase=error
org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled
org.eclipse.jdt.core.compiler.problem.fieldHiding=warning
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ org.eclipse.jdt.core.compiler.problem.discouragedReference=warning
org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=warning
org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore
org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled
org.eclipse.jdt.core.compiler.problem.fieldHiding=ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ org.eclipse.jdt.core.compiler.problem.discouragedReference=error
org.eclipse.jdt.core.compiler.problem.emptyStatement=warning
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=warning
org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore
org.eclipse.jdt.core.compiler.problem.fatalOptionalError=enabled
org.eclipse.jdt.core.compiler.problem.fieldHiding=warning
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ org.eclipse.jdt.core.compiler.problem.discouragedReference=error
org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=warning
org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore
org.eclipse.jdt.core.compiler.problem.fatalOptionalError=enabled
org.eclipse.jdt.core.compiler.problem.fieldHiding=ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ org.eclipse.jdt.core.compiler.problem.discouragedReference=error
org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=warning
org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore
org.eclipse.jdt.core.compiler.problem.fatalOptionalError=enabled
org.eclipse.jdt.core.compiler.problem.fieldHiding=ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ org.eclipse.jdt.core.compiler.problem.discouragedReference=error
org.eclipse.jdt.core.compiler.problem.emptyStatement=warning
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=warning
org.eclipse.jdt.core.compiler.problem.fallthroughCase=warning
org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled
org.eclipse.jdt.core.compiler.problem.fieldHiding=warning
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ org.eclipse.jdt.core.compiler.problem.discouragedReference=warning
org.eclipse.jdt.core.compiler.problem.emptyStatement=warning
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=warning
org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore
org.eclipse.jdt.core.compiler.problem.fatalOptionalError=enabled
org.eclipse.jdt.core.compiler.problem.fieldHiding=warning
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ org.eclipse.jdt.core.compiler.problem.discouragedReference=error
org.eclipse.jdt.core.compiler.problem.emptyStatement=warning
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=warning
org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore
org.eclipse.jdt.core.compiler.problem.fatalOptionalError=enabled
org.eclipse.jdt.core.compiler.problem.fieldHiding=warning
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,13 @@ private Properties loadProperties(URL url) {
Properties result = new Properties();
if (url == null)
return result;
InputStream input = null;
try {
input = url.openStream();
try (InputStream input = url.openStream()) {
result.load(input);
} catch (IOException e) {
if (InternalPlatform.DEBUG_PLUGIN_PREFERENCES) {
InternalPlatform.message("Problem opening stream to preference customization file: " + url); //$NON-NLS-1$
e.printStackTrace();
}
} finally {
if (input != null)
try {
input.close();
} catch (IOException e) {
// ignore
}
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ org.eclipse.jdt.core.compiler.problem.discouragedReference=error
org.eclipse.jdt.core.compiler.problem.emptyStatement=warning
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=warning
org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore
org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled
org.eclipse.jdt.core.compiler.problem.fieldHiding=warning
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ org.eclipse.jdt.core.compiler.problem.discouragedReference=error
org.eclipse.jdt.core.compiler.problem.emptyStatement=warning
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=warning
org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore
org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled
org.eclipse.jdt.core.compiler.problem.fieldHiding=warning
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ org.eclipse.jdt.core.compiler.problem.discouragedReference=error
org.eclipse.jdt.core.compiler.problem.emptyStatement=warning
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=warning
org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore
org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled
org.eclipse.jdt.core.compiler.problem.fieldHiding=warning
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ org.eclipse.jdt.core.compiler.problem.discouragedReference=error
org.eclipse.jdt.core.compiler.problem.emptyStatement=warning
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=warning
org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore
org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled
org.eclipse.jdt.core.compiler.problem.fieldHiding=warning
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ org.eclipse.jdt.core.compiler.problem.discouragedReference=warning
org.eclipse.jdt.core.compiler.problem.emptyStatement=warning
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=warning
org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore
org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled
org.eclipse.jdt.core.compiler.problem.fieldHiding=warning
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,15 @@ public Object loadSnapshot(File rootLocation) throws IOException {

public void saveSnapshot(Object toSave, File rootLocation) throws IOException {
File summaryFile = new File(rootLocation, SNAPSHOT_FILE_NAME);
PrintWriter out = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(summaryFile))));
Map<?, ?> snapshot = (Map<?, ?>) toSave;
try {
try (PrintWriter out = new PrintWriter(
new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(summaryFile))))) {
Map<?, ?> snapshot = (Map<?, ?>) toSave;
for (Object element : snapshot.values()) {
FileSummary fileSummary = (FileSummary) element;
out.println(fileSummary.getPath());
out.println(fileSummary.getTimestamp());
out.println(fileSummary.getSize());
}
} finally {
out.close();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,11 +383,10 @@ public int getLocalPort() {

/** Example Server that will log and quit when contacted **/
public static void main(String[] args) throws Exception {
@SuppressWarnings("resource")
Server server = new Server();
System.out.println("Server startet on port: " + server.getLocalPort());
server.httpServerThread.join();
server.close();
try (Server server = new Server()) {
System.out.println("Server startet on port: " + server.getLocalPort());
server.httpServerThread.join();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ org.eclipse.jdt.core.compiler.problem.discouragedReference=warning
org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=warning
org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore
org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled
org.eclipse.jdt.core.compiler.problem.fieldHiding=ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ org.eclipse.jdt.core.compiler.problem.discouragedReference=warning
org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=warning
org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore
org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled
org.eclipse.jdt.core.compiler.problem.fieldHiding=ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ org.eclipse.jdt.core.compiler.problem.discouragedReference=warning
org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=warning
org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore
org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled
org.eclipse.jdt.core.compiler.problem.fieldHiding=ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ org.eclipse.jdt.core.compiler.problem.discouragedReference=error
org.eclipse.jdt.core.compiler.problem.emptyStatement=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=warning
org.eclipse.jdt.core.compiler.problem.fallthroughCase=error
org.eclipse.jdt.core.compiler.problem.fatalOptionalError=enabled
org.eclipse.jdt.core.compiler.problem.fieldHiding=ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ org.eclipse.jdt.core.compiler.problem.discouragedReference=warning
org.eclipse.jdt.core.compiler.problem.emptyStatement=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=warning
org.eclipse.jdt.core.compiler.problem.fallthroughCase=error
org.eclipse.jdt.core.compiler.problem.fatalOptionalError=enabled
org.eclipse.jdt.core.compiler.problem.fieldHiding=ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ org.eclipse.jdt.core.compiler.problem.discouragedReference=warning
org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=warning
org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore
org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled
org.eclipse.jdt.core.compiler.problem.fieldHiding=ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,11 @@ public <T> T getAdapter(Class<T> adapter) {
}
@Override
public String getCharset() throws CoreException {
InputStream contents = getContents();
try {
try (InputStream contents = getContents()) {
String charSet = TeamPlugin.getCharset(getName(), contents);
return charSet;
} catch (IOException e) {
throw new TeamException(new Status(IStatus.ERROR, TeamPlugin.ID, IResourceStatus.FAILED_DESCRIBING_CONTENTS, NLS.bind(Messages.CachedResourceVariant_1, new String[] { getFullPath().toString() }), e));
} finally {
try {
contents.close();
} catch (IOException e1) {
// Ignore
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,33 +120,24 @@ private void internalSetContents(InputStream stream, IProgressMonitor monitor) t
try {

// Open the cache file for writing
OutputStream out;
try {
if (state == UNINITIALIZED) {
out = new BufferedOutputStream(new FileOutputStream(ioFile));
} else {
try (OutputStream out = (state == UNINITIALIZED) ? new BufferedOutputStream(new FileOutputStream(ioFile))
// If the entry is READY, the contents must have been read in another thread.
// We still need to red the contents but they can be ignored since presumably they are the same
out = new ByteArrayOutputStream();
: new ByteArrayOutputStream()) {

// Transfer the contents
byte[] buffer = new byte[1024];
int read;
while ((read = stream.read(buffer)) >= 0) {
Policy.checkCanceled(monitor);
out.write(buffer, 0, read);
}
} catch (FileNotFoundException e) {
throw new TeamException(NLS.bind(Messages.RemoteContentsCache_fileError, new String[] { ioFile.getAbsolutePath() }), e);
}

// Transfer the contents
try {
try {
byte[] buffer = new byte[1024];
int read;
while ((read = stream.read(buffer)) >= 0) {
Policy.checkCanceled(monitor);
out.write(buffer, 0, read);
}
} finally {
out.close();
}
throw new TeamException(
NLS.bind(Messages.RemoteContentsCache_fileError, new String[] { ioFile.getAbsolutePath() }), e);
} catch (IOException e) {
// Make sure we don't leave the cache file around as it may not have the right contents
// Make sure we don't leave the cache file around as it may not have the right
// contents
cache.purgeFromCache(this);
throw e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,16 @@ public boolean compare(IResource e1, IFileRevision e2, IProgressMonitor monitor)
}

private boolean compareObjects(Object e1, Object e2, IProgressMonitor monitor) {
InputStream is1 = null;
InputStream is2 = null;
try {
monitor.beginTask(null, 100);
is1 = getContents(e1, Policy.subMonitorFor(monitor, 30));
is2 = getContents(e2, Policy.subMonitorFor(monitor, 30));
monitor.beginTask(null, 100);
try (InputStream is1 = getContents(e1, Policy.subMonitorFor(monitor, 30));
InputStream is2 = getContents(e2, Policy.subMonitorFor(monitor, 30))) {
return contentsEqual(Policy.subMonitorFor(monitor, 40), is1, is2, shouldIgnoreWhitespace());
} catch (IOException e) {
return false;
} catch (TeamException e) {
TeamPlugin.log(e);
return false;
} finally {
try {
try {
if (is1 != null) {
is1.close();
}
} finally {
if (is2 != null) {
is2.close();
}
}
} catch (IOException e) {
// Ignore
}
monitor.done();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ org.eclipse.jdt.core.compiler.problem.discouragedReference=warning
org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=warning
org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore
org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled
org.eclipse.jdt.core.compiler.problem.fieldHiding=ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1118,22 +1118,15 @@ private static String getEditorId(FileRevisionEditorInput editorInput) {
}

private static IContentType getContentType(FileRevisionEditorInput editorInput) {
IContentType type = null;
try {
InputStream contents = editorInput.getStorage().getContents();
try {
type = getContentType(editorInput.getFileRevision().getName(), contents);
} finally {
try {
contents.close();
} catch (IOException e) {
// ignore
}
}
try (InputStream contents = editorInput.getStorage().getContents()) {
return getContentType(editorInput.getFileRevision().getName(), contents);
} catch (IOException e) {
// ignore
} catch (CoreException e) {
TeamUIPlugin.log(IStatus.ERROR, NLS.bind("An error occurred reading the contents of file {0}", new String[] { editorInput.getName() }), e); //$NON-NLS-1$
TeamUIPlugin.log(IStatus.ERROR, NLS.bind("An error occurred reading the contents of file {0}", //$NON-NLS-1$
new String[] { editorInput.getName() }), e);
}
return type;
return null;
}

private static IContentType getContentType(String fileName, InputStream contents) {
Expand Down
Loading

0 comments on commit 88d5d57

Please sign in to comment.