Skip to content

Commit 9188546

Browse files
EcljpseB0Tjukzi
authored andcommitted
fix some 'Potential resource leak' warnings
1 parent c8b2caa commit 9188546

File tree

4 files changed

+81
-90
lines changed

4 files changed

+81
-90
lines changed

bundles/org.eclipse.ui.ide/extensions/org/eclipse/ui/dialogs/WizardNewFileCreationPage.java

+75-70
Original file line numberDiff line numberDiff line change
@@ -356,89 +356,94 @@ public IFile createNewFile() {
356356
final IPath containerPath = resourceGroup.getContainerFullPath();
357357
IPath newFilePath = containerPath.append(resourceGroup.getResource());
358358
final IFile newFileHandle = createFileHandle(newFilePath);
359-
final InputStream initialContents = getInitialContents();
359+
try (InputStream initialContents = getInitialContents()) {
360360

361-
createLinkTarget();
361+
createLinkTarget();
362362

363-
if (linkTargetPath != null) {
364-
URI resolvedPath = newFileHandle.getPathVariableManager().resolveURI(linkTargetPath);
365-
try {
366-
if (resolvedPath.getScheme() != null && resolvedPath.getSchemeSpecificPart() != null) {
367-
IFileStore store = EFS.getStore(resolvedPath);
368-
if (!store.fetchInfo().exists()) {
369-
MessageDialog dlg = new MessageDialog(getContainer().getShell(),
370-
IDEWorkbenchMessages.WizardNewFileCreationPage_createLinkLocationTitle, null,
371-
NLS.bind(IDEWorkbenchMessages.WizardNewFileCreationPage_createLinkLocationQuestion,
372-
linkTargetPath),
373-
MessageDialog.QUESTION_WITH_CANCEL, 0, IDialogConstants.YES_LABEL,
374-
IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL);
375-
int result = dlg.open();
376-
if (result == Window.OK) {
377-
store.getParent().mkdir(0, new NullProgressMonitor());
378-
OutputStream stream = store.openOutputStream(0, new NullProgressMonitor());
379-
stream.close();
363+
if (linkTargetPath != null) {
364+
URI resolvedPath = newFileHandle.getPathVariableManager().resolveURI(linkTargetPath);
365+
try {
366+
if (resolvedPath.getScheme() != null && resolvedPath.getSchemeSpecificPart() != null) {
367+
IFileStore store = EFS.getStore(resolvedPath);
368+
if (!store.fetchInfo().exists()) {
369+
MessageDialog dlg = new MessageDialog(getContainer().getShell(),
370+
IDEWorkbenchMessages.WizardNewFileCreationPage_createLinkLocationTitle, null,
371+
NLS.bind(IDEWorkbenchMessages.WizardNewFileCreationPage_createLinkLocationQuestion,
372+
linkTargetPath),
373+
MessageDialog.QUESTION_WITH_CANCEL, 0, IDialogConstants.YES_LABEL,
374+
IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL);
375+
int result = dlg.open();
376+
if (result == Window.OK) {
377+
store.getParent().mkdir(0, new NullProgressMonitor());
378+
try (OutputStream stream = store.openOutputStream(0, new NullProgressMonitor())) {
379+
// only try to open
380+
}
381+
}
382+
if (result == 2)
383+
return null;
380384
}
381-
if (result == 2)
382-
return null;
383385
}
386+
} catch (CoreException | IOException e) {
387+
MessageDialog.open(MessageDialog.ERROR, getContainer().getShell(),
388+
IDEWorkbenchMessages.WizardNewFileCreationPage_internalErrorTitle,
389+
NLS.bind(IDEWorkbenchMessages.WizardNewFileCreationPage_internalErrorMessage,
390+
e.getMessage()),
391+
SWT.SHEET);
392+
393+
return null;
384394
}
385-
} catch (CoreException | IOException e) {
395+
}
396+
397+
IRunnableWithProgress op = monitor -> {
398+
CreateFileOperation op1 = new CreateFileOperation(newFileHandle, linkTargetPath, initialContents,
399+
IDEWorkbenchMessages.WizardNewFileCreationPage_title);
400+
try {
401+
// see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=219901
402+
// directly execute the operation so that the undo state is
403+
// not preserved. Making this undoable resulted in too many
404+
// accidental file deletions.
405+
op1.execute(monitor, WorkspaceUndoUtil.getUIInfoAdapter(getShell()));
406+
} catch (final ExecutionException e) {
407+
getContainer().getShell().getDisplay().syncExec(() -> {
408+
if (e.getCause() instanceof CoreException) {
409+
ErrorDialog.openError(getContainer().getShell(), // Was
410+
// Utilities.getFocusShell()
411+
IDEWorkbenchMessages.WizardNewFileCreationPage_errorTitle, null, // no special
412+
// message
413+
((CoreException) e.getCause()).getStatus());
414+
} else {
415+
IDEWorkbenchPlugin.log(getClass(), "createNewFile()", e.getCause()); //$NON-NLS-1$
416+
MessageDialog.openError(getContainer().getShell(),
417+
IDEWorkbenchMessages.WizardNewFileCreationPage_internalErrorTitle,
418+
NLS.bind(IDEWorkbenchMessages.WizardNewFileCreationPage_internalErrorMessage,
419+
e.getCause().getMessage()));
420+
}
421+
});
422+
}
423+
};
424+
try {
425+
getContainer().run(true, true, op);
426+
} catch (InterruptedException e) {
427+
return null;
428+
} catch (InvocationTargetException e) {
429+
// Execution Exceptions are handled above but we may still get
430+
// unexpected runtime errors.
431+
IDEWorkbenchPlugin.log(getClass(), "createNewFile()", e.getTargetException()); //$NON-NLS-1$
386432
MessageDialog.open(MessageDialog.ERROR, getContainer().getShell(),
387433
IDEWorkbenchMessages.WizardNewFileCreationPage_internalErrorTitle,
388-
NLS.bind(IDEWorkbenchMessages.WizardNewFileCreationPage_internalErrorMessage, e.getMessage()),
434+
NLS.bind(IDEWorkbenchMessages.WizardNewFileCreationPage_internalErrorMessage,
435+
e.getTargetException().getMessage()),
389436
SWT.SHEET);
390437

391438
return null;
392439
}
393-
}
394440

395-
IRunnableWithProgress op = monitor -> {
396-
CreateFileOperation op1 = new CreateFileOperation(newFileHandle, linkTargetPath, initialContents,
397-
IDEWorkbenchMessages.WizardNewFileCreationPage_title);
398-
try {
399-
// see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=219901
400-
// directly execute the operation so that the undo state is
401-
// not preserved. Making this undoable resulted in too many
402-
// accidental file deletions.
403-
op1.execute(monitor, WorkspaceUndoUtil.getUIInfoAdapter(getShell()));
404-
} catch (final ExecutionException e) {
405-
getContainer().getShell().getDisplay().syncExec(() -> {
406-
if (e.getCause() instanceof CoreException) {
407-
ErrorDialog.openError(getContainer().getShell(), // Was
408-
// Utilities.getFocusShell()
409-
IDEWorkbenchMessages.WizardNewFileCreationPage_errorTitle, null, // no special
410-
// message
411-
((CoreException) e.getCause()).getStatus());
412-
} else {
413-
IDEWorkbenchPlugin.log(getClass(), "createNewFile()", e.getCause()); //$NON-NLS-1$
414-
MessageDialog.openError(getContainer().getShell(),
415-
IDEWorkbenchMessages.WizardNewFileCreationPage_internalErrorTitle,
416-
NLS.bind(IDEWorkbenchMessages.WizardNewFileCreationPage_internalErrorMessage,
417-
e.getCause().getMessage()));
418-
}
419-
});
420-
}
421-
};
422-
try {
423-
getContainer().run(true, true, op);
424-
} catch (InterruptedException e) {
425-
return null;
426-
} catch (InvocationTargetException e) {
427-
// Execution Exceptions are handled above but we may still get
428-
// unexpected runtime errors.
429-
IDEWorkbenchPlugin.log(getClass(), "createNewFile()", e.getTargetException()); //$NON-NLS-1$
430-
MessageDialog.open(MessageDialog.ERROR, getContainer().getShell(),
431-
IDEWorkbenchMessages.WizardNewFileCreationPage_internalErrorTitle,
432-
NLS.bind(IDEWorkbenchMessages.WizardNewFileCreationPage_internalErrorMessage,
433-
e.getTargetException().getMessage()),
434-
SWT.SHEET);
435-
436-
return null;
437-
}
438-
439-
newFile = newFileHandle;
441+
newFile = newFileHandle;
440442

441-
return newFile;
443+
return newFile;
444+
} catch (IOException e) {
445+
throw new RuntimeException(e);
446+
}
442447
}
443448

444449
/**

bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/misc/FileInfoAttributesMatcher.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package org.eclipse.ui.internal.ide.misc;
1717

1818
import java.io.IOException;
19-
import java.nio.file.FileSystems;
2019
import java.nio.file.Files;
2120
import java.nio.file.Path;
2221
import java.nio.file.attribute.BasicFileAttributes;
@@ -178,7 +177,7 @@ public static Argument decodeArguments(String argument) {
178177
*/
179178
private static long getFileCreationTime(String fullPath) {
180179
try {
181-
Path fileRef = FileSystems.getDefault().getPath(fullPath);
180+
Path fileRef = Path.of(fullPath);
182181
BasicFileAttributes attributes = Files.readAttributes(fileRef, BasicFileAttributes.class);
183182
return attributes.creationTime().toMillis();
184183
} catch (IOException e) {

bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/wizards/datatransfer/ZipLeveledStructureProvider.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public boolean closeArchive(){
205205
getZipFile().close();
206206
} catch (IOException e) {
207207
IDEWorkbenchPlugin.log(DataTransferMessages.ZipImport_couldNotClose
208-
+ getZipFile().getName(), e);
208+
+ zipFile.getName(), e);
209209
return false;
210210
}
211211
return true;

tests/org.eclipse.ui.tests.navigator/src/org/eclipse/ui/tests/navigator/util/ProjectUnzipUtil.java

+4-17
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414
package org.eclipse.ui.tests.navigator.util;
1515

1616
import java.io.File;
17-
import java.io.FileOutputStream;
1817
import java.io.IOException;
1918
import java.io.InputStream;
20-
import java.io.OutputStream;
2119
import java.net.URL;
20+
import java.nio.file.Files;
2221
import java.util.Enumeration;
2322
import java.util.zip.ZipEntry;
2423
import java.util.zip.ZipFile;
@@ -107,7 +106,9 @@ private void expandZip() throws CoreException, IOException {
107106
parentFile.mkdirs();
108107
if (!aFile.exists())
109108
aFile.createNewFile();
110-
copy(zipFile.getInputStream(entry), new FileOutputStream(aFile));
109+
try (InputStream in = zipFile.getInputStream(entry)) {
110+
Files.write(aFile.toPath(), in.readAllBytes());
111+
}
111112
if (entry.getTime() > 0)
112113
aFile.setLastModified(entry.getTime());
113114
}
@@ -123,20 +124,6 @@ private IPath computeLocation(String name) {
123124
return rootLocation.append(name);
124125
}
125126

126-
public static void copy(InputStream in, OutputStream out) throws IOException {
127-
byte[] buffer = new byte[1024];
128-
try {
129-
int n = in.read(buffer);
130-
while (n > 0) {
131-
out.write(buffer, 0, n);
132-
n = in.read(buffer);
133-
}
134-
} finally {
135-
in.close();
136-
out.close();
137-
}
138-
}
139-
140127
public void setRootLocation(IPath rootLocation) {
141128
this.rootLocation = rootLocation;
142129
}

0 commit comments

Comments
 (0)