diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/nls/changes/CreateFileChange.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/nls/changes/CreateFileChange.java
index b79fbab2eaf..be261248a4b 100644
--- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/nls/changes/CreateFileChange.java
+++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/nls/changes/CreateFileChange.java
@@ -44,9 +44,8 @@
import org.eclipse.jdt.core.IJavaModelStatusConstants;
import org.eclipse.jdt.core.JavaModelException;
-import org.eclipse.jdt.internal.corext.util.Messages;
-
import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels;
+import org.eclipse.jdt.internal.corext.util.Messages;
public class CreateFileChange extends ResourceChange {
@@ -149,8 +148,6 @@ public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
@Override
public Change perform(IProgressMonitor pm) throws CoreException, OperationCanceledException {
-
- InputStream is= null;
try {
pm.beginTask(NLSChangesMessages.createFile_creating_resource, 3);
@@ -164,8 +161,7 @@ public Change perform(IProgressMonitor pm) throws CoreException, OperationCancel
pm.worked(1);
return composite.perform(new SubProgressMonitor(pm, 1));
} else { */
- try {
- is= new ByteArrayInputStream(fSource.getBytes(fEncoding));
+ try (InputStream is= new ByteArrayInputStream(fSource.getBytes(fEncoding))) {
file.create(is, false, new SubProgressMonitor(pm, 1));
if (fStampToRestore != IResource.NULL_STAMP) {
file.revertModificationStamp(fStampToRestore);
@@ -179,17 +175,12 @@ public Change perform(IProgressMonitor pm) throws CoreException, OperationCancel
} catch (UnsupportedEncodingException e) {
throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
}
- } finally {
- try {
- if (is != null)
- is.close();
- } catch (IOException ioe) {
+ } catch (IOException ioe) {
throw new JavaModelException(ioe, IJavaModelStatusConstants.IO_EXCEPTION);
- } finally {
- pm.done();
- }
+ } finally {
+ pm.done();
}
- }
+}
protected IFile getOldFile(IProgressMonitor pm) throws OperationCanceledException {
pm.beginTask("", 1); //$NON-NLS-1$
diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/nls/changes/CreateTextFileChange.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/nls/changes/CreateTextFileChange.java
index caad05e6cc0..c8ef45ba5ec 100644
--- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/nls/changes/CreateTextFileChange.java
+++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/nls/changes/CreateTextFileChange.java
@@ -43,22 +43,14 @@ public String getTextType() {
public String getCurrentContent() throws JavaModelException {
IFile file= getOldFile(new NullProgressMonitor());
- if (! file.exists())
+ if (!file.exists())
return ""; //$NON-NLS-1$
- InputStream stream= null;
- try{
- stream= file.getContents();
+ try (InputStream stream= file.getContents()) {
String encoding= file.getCharset();
String c= NLSUtil.readString(stream, encoding);
- return (c == null) ? "": c; //$NON-NLS-1$
- } catch (CoreException e){
+ return (c == null) ? "" : c; //$NON-NLS-1$
+ } catch (IOException | CoreException e) {
throw new JavaModelException(e, IJavaModelStatusConstants.CORE_EXCEPTION);
- } finally {
- try {
- if (stream != null)
- stream.close();
- } catch (IOException x) {
- }
}
}
diff --git a/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/model/JUnitModel.java b/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/model/JUnitModel.java
index c3683ebd0b9..40f88fcd06e 100644
--- a/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/model/JUnitModel.java
+++ b/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/model/JUnitModel.java
@@ -41,6 +41,7 @@
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
+import org.eclipse.jdt.junit.ITestRunListener;
import org.eclipse.jdt.junit.TestRunListener;
import org.eclipse.core.runtime.Assert;
@@ -65,7 +66,6 @@
import org.eclipse.jdt.internal.junit.Messages;
import org.eclipse.jdt.internal.junit.launcher.JUnitLaunchConfigurationConstants;
import org.eclipse.jdt.internal.junit.model.TestElement.Status;
-import org.eclipse.jdt.junit.ITestRunListener;
/**
* Central registry for JUnit test runs.
@@ -495,23 +495,12 @@ public static void importIntoTestRunSession(File swapFile, TestRunSession testRu
* @throws CoreException if an error occurred
*/
public static void exportTestRunSession(TestRunSession testRunSession, File file) throws CoreException {
- FileOutputStream out= null;
- try {
- out= new FileOutputStream(file);
- exportTestRunSession(testRunSession, out);
-
+ try (FileOutputStream out= new FileOutputStream(file)) {
+ exportTestRunSession(testRunSession, out);
} catch (IOException | TransformerConfigurationException e) {
throwExportError(file, e);
} catch (TransformerException e) {
throwExportError(file, e);
- } finally {
- if (out != null) {
- try {
- out.close();
- } catch (IOException e2) {
- JUnitCorePlugin.log(e2);
- }
- }
}
}
diff --git a/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/FileTool.java b/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/FileTool.java
index 0a6a703a6e0..746234da9b7 100644
--- a/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/FileTool.java
+++ b/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/FileTool.java
@@ -62,25 +62,10 @@ public static void unzip(ZipFile zipFile, File dstDir) throws IOException {
String entryName = entry.getName();
File file = new File(dstDir, changeSeparator(entryName, '/', File.separatorChar));
file.getParentFile().mkdirs();
- InputStream src = null;
- OutputStream dst = null;
- try {
- src = zipFile.getInputStream(entry);
- dst = new FileOutputStream(file);
+ try (InputStream src= zipFile.getInputStream(entry);
+ OutputStream dst= new FileOutputStream(file)) {
transferData(src, dst);
- } finally {
- if(dst != null){
- try {
- dst.close();
- } catch(IOException e){
- }
- }
- if(src != null){
- try {
- src.close();
- } catch(IOException e){
- }
- }
+ } catch (IOException e) {
}
}
} finally {
@@ -115,25 +100,9 @@ public static String changeSeparator(String path, char oldSeparator, char newSep
*/
public static void transferData(File source, File destination) throws IOException {
destination.getParentFile().mkdirs();
- InputStream is = null;
- OutputStream os = null;
- try {
- is = new FileInputStream(source);
- os = new FileOutputStream(destination);
+ try (InputStream is = new FileInputStream(source);
+ OutputStream os = new FileOutputStream(destination)) {
transferData(is, os);
- } finally {
- if(os != null){
- try {
- os.close();
- } catch(IOException e){
- }
- }
- if(is != null){
- try {
- is.close();
- } catch(IOException e){
- }
- }
}
}
diff --git a/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/TextPerformanceTestCase.java b/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/TextPerformanceTestCase.java
index 2a0ceed9e89..7e75038949b 100644
--- a/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/TextPerformanceTestCase.java
+++ b/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/TextPerformanceTestCase.java
@@ -16,9 +16,10 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
-import java.text.SimpleDateFormat;
+import java.time.Instant;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
-import java.util.Date;
import java.util.List;
import java.util.Locale;
@@ -61,7 +62,7 @@ protected void tearDown() throws Exception {
}
static boolean DEBUG= false;
- private static final SimpleDateFormat DATE_FORMAT= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS Z", Locale.US);
+ private static final DateTimeFormatter DATE_FORMAT= DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS Z", Locale.US).withZone(ZoneId.systemDefault());
/** containing plug-in id */
@@ -127,7 +128,7 @@ protected void setUp() throws Exception {
EditorsUI.getPreferenceStore().putValue(SpellingService.PREFERENCE_SPELLING_ENABLED, IPreferenceStore.FALSE);
if (DEBUG)
- System.out.println(DATE_FORMAT.format(new Date()) + ": " + getClass().getName() + "." + getName());
+ System.out.println(DATE_FORMAT.format(Instant.now()) + ": " + getClass().getName() + "." + getName());
}
/*
diff --git a/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/RenameTypeTests.java b/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/RenameTypeTests.java
index 03ca9f763ee..a86c3cd784e 100644
--- a/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/RenameTypeTests.java
+++ b/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/RenameTypeTests.java
@@ -1400,14 +1400,11 @@ private void helperQualifiedName(String oldName, String newName, String textFile
ICompilationUnit newcu= getPackageP().getCompilationUnit(newName + ".java");
assertEqualLines("invalid renaming", getFileContents(getOutputTestFileName(newName)), newcu.getSource());
- InputStreamReader reader= new InputStreamReader(file.getContents(true));
StringBuilder newContent= new StringBuilder();
- try {
+ try (InputStreamReader reader= new InputStreamReader(file.getContents(true))) {
int ch;
- while((ch= reader.read()) != -1)
- newContent.append((char)ch);
- } finally {
- reader.close();
+ while ((ch= reader.read()) != -1)
+ newContent.append((char) ch);
}
String definedContent= getFileContents(getTestPath() + getName() + TEST_OUTPUT_INFIX + textFileName);
assertEqualLines("invalid updating", definedContent, newContent.toString());
@@ -1526,14 +1523,11 @@ public void testSimilarElements05() throws Exception {
helper3("SomeClass", "SomeDifferentClass", true, true, true, "test.html");
- InputStreamReader reader= new InputStreamReader(file.getContents(true));
StringBuilder newContent= new StringBuilder();
- try {
+ try (InputStreamReader reader= new InputStreamReader(file.getContents(true))) {
int ch;
while((ch= reader.read()) != -1)
newContent.append((char)ch);
- } finally {
- reader.close();
}
String definedContent= getFileContents(getTestPath() + "testSimilarElements05/out/test.html");
assertEqualLines("invalid updating test.html", newContent.toString(), definedContent);
diff --git a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/template/java/TemplateSet.java b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/template/java/TemplateSet.java
index 8d9e091accb..e9acfd110d8 100644
--- a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/template/java/TemplateSet.java
+++ b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/template/java/TemplateSet.java
@@ -87,22 +87,10 @@ public TemplateSet(String templateTag, ContextTypeRegistry registry) {
* @see #addFromStream(InputStream, boolean)
*/
public void addFromFile(File file, boolean allowDuplicates) throws CoreException {
- InputStream stream= null;
-
- try {
- stream= new FileInputStream(file);
+ try (InputStream stream= new FileInputStream(file)) {
addFromStream(stream, allowDuplicates);
-
} catch (IOException e) {
throwReadException(e);
-
- } finally {
- try {
- if (stream != null)
- stream.close();
- } catch (IOException e) {
- // just exit
- }
}
}
@@ -202,22 +190,10 @@ private String getAttributeValue(NamedNodeMap attributes, String name) {
* @see #saveToStream(OutputStream)
*/
public void saveToFile(File file) throws CoreException {
- OutputStream stream= null;
-
- try {
- stream= new FileOutputStream(file);
+ try (OutputStream stream= new FileOutputStream(file)) {
saveToStream(stream);
-
} catch (IOException e) {
throwWriteException(e);
-
- } finally {
- try {
- if (stream != null)
- stream.close();
- } catch (IOException e) {
- // just exit
- }
}
}
diff --git a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/util/History.java b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/util/History.java
index 58d440c324e..a13d7a44a3a 100644
--- a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/util/History.java
+++ b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/util/History.java
@@ -168,19 +168,10 @@ public synchronized void load() {
IPath stateLocation= JavaPlugin.getDefault().getStateLocation().append(fFileName);
File file= stateLocation.toFile();
if (file.exists()) {
- InputStreamReader reader= null;
- try {
- reader = new InputStreamReader(new FileInputStream(file), "utf-8");//$NON-NLS-1$
+ try (InputStreamReader reader= new InputStreamReader(new FileInputStream(file), "utf-8")) {//$NON-NLS-1$
load(new InputSource(reader));
} catch (IOException | CoreException e) {
JavaPlugin.log(e);
- } finally {
- try {
- if (reader != null)
- reader.close();
- } catch (IOException e) {
- JavaPlugin.log(e);
- }
}
}
}
@@ -188,22 +179,12 @@ public synchronized void load() {
public synchronized void save() {
IPath stateLocation= JavaPlugin.getDefault().getStateLocation().append(fFileName);
File file= stateLocation.toFile();
- OutputStream out= null;
- try {
- out= new FileOutputStream(file);
+ try (OutputStream out= new FileOutputStream(file)) {
save(out);
} catch (IOException | CoreException | TransformerFactoryConfigurationError e) {
// The XML library can be misconficgured (e.g. via
// -Djava.endorsed.dirs=C:\notExisting\xerces-2_7_1)
JavaPlugin.log(e);
- } finally {
- try {
- if (out != null) {
- out.close();
- }
- } catch (IOException e) {
- JavaPlugin.log(e);
- }
}
}
diff --git a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/changes/DeletePackageFragmentRootChange.java b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/changes/DeletePackageFragmentRootChange.java
index 456672a6845..243959e86ec 100644
--- a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/changes/DeletePackageFragmentRootChange.java
+++ b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/changes/DeletePackageFragmentRootChange.java
@@ -155,23 +155,21 @@ private static String getFileContents(IFile file) throws CoreException {
private static int getFileLength(IFile file) throws CoreException {
// Cannot use file buffers here, since they are not yet in sync at this point.
- InputStream contents= file.getContents();
- InputStreamReader reader;
- try {
- reader= new InputStreamReader(contents, file.getCharset());
- } catch (UnsupportedEncodingException e) {
- JavaPlugin.log(e);
- reader= new InputStreamReader(contents);
- }
- try {
- return (int) reader.skip(Integer.MAX_VALUE);
- } catch (IOException e) {
- throw new CoreException(new Status(IStatus.ERROR, Corext.getPluginId(), e.getMessage(), e));
- } finally {
+ try (InputStream contents= file.getContents()) {
+ InputStreamReader reader;
+ try {
+ reader= new InputStreamReader(contents, file.getCharset());
+ } catch (UnsupportedEncodingException e) {
+ JavaPlugin.log(e);
+ reader= new InputStreamReader(contents);
+ }
try {
+ return (int) reader.skip(Integer.MAX_VALUE);
+ } finally {
reader.close();
- } catch (IOException e) {
}
+ } catch (IOException e) {
+ throw new CoreException(new Status(IStatus.ERROR, Corext.getPluginId(), e.getMessage(), e));
}
}
diff --git a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/reorg/JavaElementTransfer.java b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/reorg/JavaElementTransfer.java
index cf4cd752f98..68a42d5cbd5 100644
--- a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/reorg/JavaElementTransfer.java
+++ b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/reorg/JavaElementTransfer.java
@@ -73,9 +73,8 @@ protected void javaToNative(Object data, TransferData transferData) {
* (String) handle identifier
*/
- try {
- ByteArrayOutputStream out= new ByteArrayOutputStream();
- DataOutputStream dataOut= new DataOutputStream(out);
+ try (ByteArrayOutputStream out= new ByteArrayOutputStream();
+ DataOutputStream dataOut= new DataOutputStream(out)) {
//write the number of elements
dataOut.writeInt(javaElements.length);
@@ -86,8 +85,6 @@ protected void javaToNative(Object data, TransferData transferData) {
}
//cleanup
- dataOut.close();
- out.close();
byte[] bytes= out.toByteArray();
super.javaToNative(bytes, transferData);
} catch (IOException e) {
diff --git a/org.eclipse.jdt.ui/ui refactoring/org/eclipse/jdt/internal/ui/refactoring/nls/search/NLSSearchResultRequestor.java b/org.eclipse.jdt.ui/ui refactoring/org/eclipse/jdt/internal/ui/refactoring/nls/search/NLSSearchResultRequestor.java
index b4e69ee6703..6a4b1edb331 100644
--- a/org.eclipse.jdt.ui/ui refactoring/org/eclipse/jdt/internal/ui/refactoring/nls/search/NLSSearchResultRequestor.java
+++ b/org.eclipse.jdt.ui/ui refactoring/org/eclipse/jdt/internal/ui/refactoring/nls/search/NLSSearchResultRequestor.java
@@ -22,14 +22,21 @@
import java.util.Iterator;
import java.util.Set;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+
+import org.eclipse.core.resources.IFile;
+
import org.eclipse.core.filebuffers.FileBuffers;
import org.eclipse.core.filebuffers.ITextFileBuffer;
import org.eclipse.core.filebuffers.ITextFileBufferManager;
import org.eclipse.core.filebuffers.LocationKind;
-import org.eclipse.core.resources.IFile;
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.IStatus;
+
+import org.eclipse.jface.text.Position;
+
+import org.eclipse.search.ui.text.Match;
+
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IJavaElement;
@@ -44,13 +51,13 @@
import org.eclipse.jdt.core.compiler.InvalidInputException;
import org.eclipse.jdt.core.search.SearchMatch;
import org.eclipse.jdt.core.search.SearchRequestor;
+
import org.eclipse.jdt.internal.corext.refactoring.nls.PropertyFileDocumentModel;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
+
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.JavaUIStatus;
import org.eclipse.jdt.internal.ui.util.StringMatcher;
-import org.eclipse.jface.text.Position;
-import org.eclipse.search.ui.text.Match;
class NLSSearchResultRequestor extends SearchRequestor {
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/jarimport/JarImportWizardPage.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/jarimport/JarImportWizardPage.java
index 71e500d981b..c8bc6136e81 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/jarimport/JarImportWizardPage.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/jarimport/JarImportWizardPage.java
@@ -376,9 +376,7 @@ protected void handleJarFileChanged() {
setPageComplete(true);
return;
}
- InputStream stream= null;
- try {
- stream= zip.getInputStream(entry);
+ try (InputStream stream= zip.getInputStream(entry)) {
data.setRefactoringHistory(RefactoringCore.getHistoryService().readRefactoringHistory(stream, JavaRefactoringDescriptor.JAR_MIGRATION | JavaRefactoringDescriptor.JAR_REFACTORING));
} catch (IOException exception) {
setErrorMessage(JarImportMessages.JarImportWizardPage_no_refactorings);
@@ -389,14 +387,6 @@ protected void handleJarFileChanged() {
setErrorMessage(JarImportMessages.JarImportWizardPage_no_refactorings);
setPageComplete(false);
return;
- } finally {
- if (stream != null) {
- try {
- stream.close();
- } catch (IOException exception) {
- // Do nothing
- }
- }
}
} finally {
if (zip != null) {
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/spelling/SpellCheckEngine.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/spelling/SpellCheckEngine.java
index 85af5993357..7409c44ef1d 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/spelling/SpellCheckEngine.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/spelling/SpellCheckEngine.java
@@ -427,13 +427,10 @@ private synchronized void resetUserDictionary() {
return;
final URL url= new URL("file", null, filePath); //$NON-NLS-1$
- InputStream stream= url.openStream();
- if (stream != null) {
- try {
+ try (InputStream stream= url.openStream()) {
+ if (stream != null) {
fUserDictionary= new PersistentSpellDictionary(url);
fChecker.addDictionary(fUserDictionary);
- } finally {
- stream.close();
}
}
} catch (IOException exception) {
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/spelling/engine/PersistentSpellDictionary.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/spelling/engine/PersistentSpellDictionary.java
index a2482bf9a41..bed69ae615c 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/spelling/engine/PersistentSpellDictionary.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/spelling/engine/PersistentSpellDictionary.java
@@ -52,20 +52,18 @@ public void addWord(final String word) {
if (isCorrect(word))
return;
- FileOutputStream fileStream= null;
- try {
- Charset charset= Charset.forName(getEncoding());
- ByteBuffer byteBuffer= charset.encode(word + "\n"); //$NON-NLS-1$
- int size= byteBuffer.limit();
- final byte[] byteArray;
- if (byteBuffer.hasArray())
- byteArray= byteBuffer.array();
- else {
- byteArray= new byte[size];
- byteBuffer.get(byteArray);
- }
+ Charset charset= Charset.forName(getEncoding());
+ ByteBuffer byteBuffer= charset.encode(word + "\n"); //$NON-NLS-1$
+ int size= byteBuffer.limit();
+ final byte[] byteArray;
+ if (byteBuffer.hasArray())
+ byteArray= byteBuffer.array();
+ else {
+ byteArray= new byte[size];
+ byteBuffer.get(byteArray);
+ }
- fileStream= new FileOutputStream(fLocation.getPath(), true);
+ try (FileOutputStream fileStream= new FileOutputStream(fLocation.getPath(), true)) {
// Encoding UTF-16 charset writes a BOM. In which case we need to cut it away if the file isn't empty
int bomCutSize= 0;
@@ -76,12 +74,6 @@ public void addWord(final String word) {
} catch (IOException exception) {
JavaPlugin.log(exception);
return;
- } finally {
- try {
- if (fileStream != null)
- fileStream.close();
- } catch (IOException e) {
- }
}
hashWord(word);
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/ClassPathDetector.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/ClassPathDetector.java
index 85a300339f6..4b0cb7fb649 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/ClassPathDetector.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/ClassPathDetector.java
@@ -186,19 +186,12 @@ private IPath detectOutputFolder() throws CoreException {
for (IResource iResource : fClassFiles) {
IFile file= (IFile) iResource;
IClassFileReader reader= null;
- InputStream content= null;
- try {
- content= file.getContents();
+ try (InputStream content= file.getContents()) {
reader= ToolFactory.createDefaultClassFileReader(content, IClassFileReader.CLASSFILE_ATTRIBUTES);
- } finally {
- try {
- if (content != null)
- content.close();
- } catch (IOException e) {
- throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.ERROR,
- Messages.format(NewWizardMessages.ClassPathDetector_error_closing_file, BasicElementLabels.getPathLabel(file.getFullPath(), false)),
- e));
- }
+ } catch (IOException e) {
+ throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.ERROR,
+ Messages.format(NewWizardMessages.ClassPathDetector_error_closing_file, BasicElementLabels.getPathLabel(file.getFullPath(), false)),
+ e));
}
if (reader == null) {
continue; // problematic class file
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/jarpackager/JarWriter.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/jarpackager/JarWriter.java
index f64ec6c9d5d..e627dcf80b1 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/jarpackager/JarWriter.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/jarpackager/JarWriter.java
@@ -40,12 +40,12 @@
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels;
import org.eclipse.jdt.internal.corext.util.Messages;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.jarpackager.JarPackagerMessages;
import org.eclipse.jdt.internal.ui.jarpackager.JarPackagerUtil;
-import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels;
/**
* Creates a JAR file for the given JAR package data.
@@ -124,26 +124,13 @@ public void close() throws CoreException {
* in the status object.
*/
public void write(IFile resource, IPath destinationPath) throws CoreException {
- ByteArrayOutputStream output= new ByteArrayOutputStream();
- BufferedInputStream contentStream= null;
- try {
- contentStream= new BufferedInputStream(resource.getContents(false));
+ try (ByteArrayOutputStream output= new ByteArrayOutputStream();
+ BufferedInputStream contentStream= new BufferedInputStream(resource.getContents(false))) {
int chunkSize= 4096;
byte[] readBuffer= new byte[chunkSize];
int count;
while ((count= contentStream.read(readBuffer, 0, chunkSize)) != -1)
output.write(readBuffer, 0, count);
- } catch (IOException ex) {
- throw JarPackagerUtil.createCoreException(ex.getLocalizedMessage(), ex);
- } finally {
- try {
- if (contentStream != null)
- contentStream.close();
- } catch (IOException ex) {
- throw JarPackagerUtil.createCoreException(ex.getLocalizedMessage(), ex);
- }
- }
- try {
IPath fileLocation= resource.getLocation();
long lastModified= System.currentTimeMillis();
File file= null;
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/jarpackager/JarWriter2.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/jarpackager/JarWriter2.java
index 406b2505955..0b4c188ba44 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/jarpackager/JarWriter2.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/jarpackager/JarWriter2.java
@@ -39,12 +39,12 @@
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels;
import org.eclipse.jdt.internal.corext.util.Messages;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.jarpackager.JarPackagerMessages;
import org.eclipse.jdt.internal.ui.jarpackager.JarPackagerUtil;
-import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels;
/**
* Creates a JAR file for the given JAR package data.
@@ -172,17 +172,11 @@ protected void addFile(IFile resource, IPath path, File correspondingFile) throw
// Set modification time
newEntry.setTime(lastModified);
- InputStream contentStream = resource.getContents(false);
-
- try {
+ try (InputStream contentStream = resource.getContents(false)) {
fJarOutputStream.putNextEntry(newEntry);
int count;
while ((count= contentStream.read(readBuffer, 0, readBuffer.length)) != -1)
fJarOutputStream.write(readBuffer, 0, count);
- } finally {
- if (contentStream != null)
- contentStream.close();
-
/*
* Commented out because some JREs throw an NPE if a stream
* is closed twice. This works because
@@ -204,21 +198,17 @@ protected void addFile(IFile resource, IPath path, File correspondingFile) throw
* @throws CoreException if the resource can-t be accessed
*/
private void calculateCrcAndSize(JarEntry jarEntry, IFile resource, byte[] readBuffer) throws IOException, CoreException {
- InputStream contentStream = resource.getContents(false);
- int size = 0;
- CRC32 checksumCalculator= new CRC32();
- int count;
- try {
+ try (InputStream contentStream= resource.getContents(false)) {
+ int size= 0;
+ CRC32 checksumCalculator= new CRC32();
+ int count;
while ((count= contentStream.read(readBuffer, 0, readBuffer.length)) != -1) {
checksumCalculator.update(readBuffer, 0, count);
- size += count;
+ size+= count;
}
- } finally {
- if (contentStream != null)
- contentStream.close();
+ jarEntry.setSize(size);
+ jarEntry.setCrc(checksumCalculator.getValue());
}
- jarEntry.setSize(size);
- jarEntry.setCrc(checksumCalculator.getValue());
}
/**
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/jarpackager/JarWriter3.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/jarpackager/JarWriter3.java
index d1419ca6c5f..3f2844de687 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/jarpackager/JarWriter3.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/jarpackager/JarWriter3.java
@@ -317,32 +317,14 @@ else if (delta < 0)
return -1;
return 0;
});
- File file= null;
- OutputStream output= null;
- try {
- file= File.createTempFile("history", null); //$NON-NLS-1$
- output= new BufferedOutputStream(new FileOutputStream(file));
- try {
- RefactoringCore.getHistoryService().writeRefactoringDescriptors(proxies, output, RefactoringDescriptor.NONE, false, monitor);
- try {
- output.close();
- output= null;
- } catch (IOException exception) {
- // Do nothing
- }
- writeMetaData(data, file, path);
- } finally {
- if (output != null) {
- try {
- output.close();
- } catch (IOException exception) {
- // Do nothing
- }
- }
- }
+ File file= File.createTempFile("history", null); //$NON-NLS-1$
+ try (OutputStream output= new BufferedOutputStream(new FileOutputStream(file))) {
+ RefactoringCore.getHistoryService().writeRefactoringDescriptors(proxies, output, RefactoringDescriptor.NONE, false, monitor);
+ writeMetaData(data, file, path);
+ } catch (IOException exception) {
+ // Do nothing
} finally {
- if (file != null)
- file.delete();
+ file.delete();
}
}
@@ -470,21 +452,18 @@ private void writeMetaData(final JarPackageData data, final File file, final IPa
entry.setMethod(ZipEntry.DEFLATED);
else {
entry.setMethod(ZipEntry.STORED);
- JarPackagerUtil.calculateCrcAndSize(entry, new BufferedInputStream(new FileInputStream(file)), buffer);
+ try (BufferedInputStream stream= new BufferedInputStream(new FileInputStream(file))) {
+ JarPackagerUtil.calculateCrcAndSize(entry, stream, buffer);
+ }
}
entry.setTime(System.currentTimeMillis());
- final InputStream stream= new BufferedInputStream(new FileInputStream(file));
- try {
+ try (InputStream stream= new BufferedInputStream(new FileInputStream(file))) {
fJarOutputStream.putNextEntry(entry);
int count;
while ((count= stream.read(buffer, 0, buffer.length)) != -1)
fJarOutputStream.write(buffer, 0, count);
- } finally {
- try {
- stream.close();
- } catch (IOException exception) {
- // Do nothing
- }
+ } catch (IOException exception) {
+ // Do nothing
}
}
}
diff --git a/org.eclipse.ltk.core.refactoring/src/org/eclipse/ltk/internal/core/refactoring/history/RefactoringHistoryManager.java b/org.eclipse.ltk.core.refactoring/src/org/eclipse/ltk/internal/core/refactoring/history/RefactoringHistoryManager.java
index 9df01638e3a..5bf3f09b274 100644
--- a/org.eclipse.ltk.core.refactoring/src/org/eclipse/ltk/internal/core/refactoring/history/RefactoringHistoryManager.java
+++ b/org.eclipse.ltk.core.refactoring/src/org/eclipse/ltk/internal/core/refactoring/history/RefactoringHistoryManager.java
@@ -708,14 +708,13 @@ public static void writeRefactoringSession(final OutputStream stream, final Refa
}
final Document result= transformer.getResult();
writeNode(stream, result);
- }
+ }
private static void writeNode(final OutputStream stream, Document document) {
- OutputStreamWriter outputStreamWriter= new OutputStreamWriter(stream, Charset.forName("UTF-8")); //$NON-NLS-1$
- @SuppressWarnings("resource")
- DOMWriter writer= new DOMWriter(outputStreamWriter);
- writer.printDocument(document);
- writer.flush();
+ try (DOMWriter writer= new DOMWriter(new OutputStreamWriter(stream, Charset.forName("UTF-8")))){ //$NON-NLS-1$
+ writer.printDocument(document);
+ writer.flush();
+ }
}
/** The cached session descriptor, or null
*/
@@ -1188,24 +1187,17 @@ void setComment(final RefactoringDescriptorProxy proxy, final String comment, fi
* if an error occurs while adding the history entry
*/
private void writeHistoryEntry(final IFileStore file, final Document document, final IProgressMonitor monitor, final String task) throws CoreException {
- OutputStream output= null;
- try {
- monitor.beginTask(task, 2);
- file.getParent().mkdir(EFS.NONE, new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
- output= new BufferedOutputStream(file.openOutputStream(EFS.NONE, new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL)));
+ monitor.beginTask(task, 2);
+ file.getParent().mkdir(EFS.NONE, new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
+ try (OutputStream output= new BufferedOutputStream(file.openOutputStream(EFS.NONE, new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL)))) {
writeNode(output, document);
- } finally {
- fCachedDocument= null;
- fCachedPath= null;
- fCachedDescriptor= null;
- fCachedStore= null;
- if (output != null) {
- try {
- output.close();
- } catch (IOException exception) {
- // Do nothing
- }
- }
+ } catch (IOException exception) {
+ // Do nothing
+ } finally {
+ fCachedDocument= null;
+ fCachedPath= null;
+ fCachedDescriptor= null;
+ fCachedStore= null;
monitor.done();
}
}
diff --git a/org.eclipse.ltk.ui.refactoring/src/org/eclipse/ltk/internal/ui/refactoring/model/RefactoringHistoryMerger.java b/org.eclipse.ltk.ui.refactoring/src/org/eclipse/ltk/internal/ui/refactoring/model/RefactoringHistoryMerger.java
index 75e47b6bb93..c9feb9bf59d 100644
--- a/org.eclipse.ltk.ui.refactoring/src/org/eclipse/ltk/internal/ui/refactoring/model/RefactoringHistoryMerger.java
+++ b/org.eclipse.ltk.ui.refactoring/src/org/eclipse/ltk/internal/ui/refactoring/model/RefactoringHistoryMerger.java
@@ -58,29 +58,13 @@ public boolean canMergeWithoutAncestor() {
@Override
public IStatus merge(final OutputStream output, final String encoding, final IStorage ancestor, final IStorage target, final IStorage other, final IProgressMonitor monitor) throws CoreException {
- InputStream targetStream= null;
- InputStream sourceStream= null;
- try {
- targetStream= target.getContents();
- sourceStream= target.getContents();
+ try (InputStream targetStream= target.getContents();
+ InputStream sourceStream= target.getContents()) {
performMerge(output, targetStream, sourceStream);
} catch (CoreException exception) {
return new Status(IStatus.ERROR, RefactoringUIPlugin.getPluginId(), 1, RefactoringUIMessages.RefactoringHistoryMerger_error_auto_merge, exception);
- } finally {
- if (targetStream != null) {
- try {
- targetStream.close();
- } catch (IOException exception) {
- // Do nothing
- }
- }
- if (sourceStream != null) {
- try {
- sourceStream.close();
- } catch (IOException exception) {
- // Do nothing
- }
- }
+ } catch (IOException exception) {
+ // Do nothing
}
return Status.OK_STATUS;
}
diff --git a/org.eclipse.ltk.ui.refactoring/src/org/eclipse/ltk/internal/ui/refactoring/scripting/ApplyRefactoringScriptWizardPage.java b/org.eclipse.ltk.ui.refactoring/src/org/eclipse/ltk/internal/ui/refactoring/scripting/ApplyRefactoringScriptWizardPage.java
index b9aec28087f..5b3d1b4f3b6 100644
--- a/org.eclipse.ltk.ui.refactoring/src/org/eclipse/ltk/internal/ui/refactoring/scripting/ApplyRefactoringScriptWizardPage.java
+++ b/org.eclipse.ltk.ui.refactoring/src/org/eclipse/ltk/internal/ui/refactoring/scripting/ApplyRefactoringScriptWizardPage.java
@@ -175,9 +175,7 @@ private void handleLocationChanged() {
setPageComplete(false);
return;
}
- InputStream stream= null;
- try {
- stream= new BufferedInputStream(new FileInputStream(file));
+ try (InputStream stream= new BufferedInputStream(new FileInputStream(file))) {
fWizard.setRefactoringHistory(RefactoringCore.getHistoryService().readRefactoringHistory(stream, RefactoringDescriptor.NONE));
} catch (IOException exception) {
setErrorMessage(ScriptingMessages.ApplyRefactoringScriptWizardPage_error_cannot_read);
@@ -187,14 +185,6 @@ private void handleLocationChanged() {
setErrorMessage(ScriptingMessages.ApplyRefactoringScriptWizardPage_invalid_format);
setPageComplete(false);
return;
- } finally {
- if (stream != null) {
- try {
- stream.close();
- } catch (IOException exception) {
- // Do nothing
- }
- }
}
}
diff --git a/org.eclipse.ltk.ui.refactoring/src/org/eclipse/ltk/internal/ui/refactoring/scripting/CreateRefactoringScriptWizard.java b/org.eclipse.ltk.ui.refactoring/src/org/eclipse/ltk/internal/ui/refactoring/scripting/CreateRefactoringScriptWizard.java
index 3d4b0ca2fe7..75043341068 100644
--- a/org.eclipse.ltk.ui.refactoring/src/org/eclipse/ltk/internal/ui/refactoring/scripting/CreateRefactoringScriptWizard.java
+++ b/org.eclipse.ltk.ui.refactoring/src/org/eclipse/ltk/internal/ui/refactoring/scripting/CreateRefactoringScriptWizard.java
@@ -137,9 +137,7 @@ private boolean performExport() {
final MessageDialog message= new MessageDialog(getShell(), getShell().getText(), null, Messages.format(ScriptingMessages.CreateRefactoringScriptWizard_overwrite_query, new String[] { ScriptingMessages.CreateRefactoringScriptWizard_merge_button, ScriptingMessages.CreateRefactoringScriptWizard_overwrite_button}), MessageDialog.QUESTION, new String[] { ScriptingMessages.CreateRefactoringScriptWizard_merge_button, ScriptingMessages.CreateRefactoringScriptWizard_overwrite_button, IDialogConstants.CANCEL_LABEL}, 0);
final int result= message.open();
if (result == 0) {
- InputStream stream= null;
- try {
- stream= new BufferedInputStream(new FileInputStream(file));
+ try (InputStream stream= new BufferedInputStream(new FileInputStream(file))) {
final RefactoringDescriptorProxy[] existing= RefactoringCore.getHistoryService().readRefactoringHistory(stream, RefactoringDescriptor.NONE).getDescriptors();
final Set set= new HashSet<>(Arrays.asList(existing));
set.addAll(Arrays.asList(fRefactoringDescriptors));
@@ -151,25 +149,19 @@ private boolean performExport() {
} catch (CoreException exception) {
handleCoreException(exception);
return false;
- } finally {
- if (stream != null) {
- try {
- stream.close();
- } catch (IOException exception) {
- // Do nothing
- }
- }
+ } catch (IOException closeException) {
+ // Do nothing
}
} else if (result == 2)
return false;
}
- OutputStream stream= null;
try {
File parentFile= file.getParentFile();
if (parentFile != null)
parentFile.mkdirs();
- stream= new BufferedOutputStream(new FileOutputStream(file));
- writeRefactoringDescriptorProxies(writable, stream);
+ try (OutputStream stream= new BufferedOutputStream(new FileOutputStream(file))) {
+ writeRefactoringDescriptorProxies(writable, stream);
+ }
return true;
} catch (CoreException exception) {
handleCoreException(exception);
@@ -177,14 +169,8 @@ private boolean performExport() {
} catch (FileNotFoundException exception) {
MessageDialog.openError(getShell(), RefactoringUIMessages.ChangeExceptionHandler_refactoring, exception.getLocalizedMessage());
return false;
- } finally {
- if (stream != null) {
- try {
- stream.close();
- } catch (IOException exception) {
- // Do nothing
- }
- }
+ } catch (IOException closeException) {
+ // Do nothing
}
} else if (fUseClipboard) {
try {