Skip to content

Commit b0fd3f2

Browse files
committed
code cleanup - use try-with-resources
1 parent 80a2c46 commit b0fd3f2

File tree

12 files changed

+28
-161
lines changed

12 files changed

+28
-161
lines changed

visualvm/applicationviews/src/org/graalvm/visualvm/application/views/monitor/ApplicationMonitorModel.java

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -348,37 +348,19 @@ public void save(Snapshot snapshot) {
348348

349349
private static void saveChartSupport(SimpleXYChartSupport chartSupport, File file) {
350350
if (chartSupport == null) return;
351-
352-
OutputStream os = null;
353351

354-
try {
355-
os = new FileOutputStream(file);
352+
try (OutputStream os = new FileOutputStream(file)){
356353
chartSupport.saveValues(os);
357354
} catch (Exception e) {
358355
// TODO: log it
359-
} finally {
360-
try {
361-
if (os != null) os.close();
362-
} catch (Exception e) {
363-
// TODO: log it
364-
}
365356
}
366357
}
367358

368359
private static void loadChartSupport(SimpleXYChartSupport chartSupport, File file) {
369-
InputStream is = null;
370-
371-
try {
372-
is = new FileInputStream(file);
360+
try (InputStream is = new FileInputStream(file)) {
373361
chartSupport.loadValues(is);
374362
} catch (Exception e) {
375363
// TODO: log it
376-
} finally {
377-
try {
378-
if (is != null) is.close();
379-
} catch (Exception e) {
380-
// TODO: log it
381-
}
382364
}
383365
}
384366

visualvm/applicationviews/src/org/graalvm/visualvm/application/views/threads/PersistenceSupport.java

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -63,50 +63,31 @@ static void saveDataManager(VisualVMThreadsDataManager dm, Storage storage) {
6363
if (dm == null) return;
6464

6565
File dir = storage.getDirectory();
66-
OutputStream os = null;
6766

68-
try {
69-
os = new FileOutputStream(new File(dir, THREADS_DATA_FILE));
67+
try (OutputStream os = new FileOutputStream(new File(dir, THREADS_DATA_FILE))) {
7068
saveDataManager(dm, os);
7169
storage.setCustomProperty(SNAPSHOT_VERSION, CURRENT_SNAPSHOT_VERSION);
7270
} catch (Exception e) {
7371
// TODO: log it
74-
} finally {
75-
try {
76-
if (os != null) os.close();
77-
} catch (Exception e) {
78-
// TODO: log it
79-
}
8072
}
8173
}
8274

8375
static VisualVMThreadsDataManager loadDataManager(Storage storage) {
8476
File dir = storage.getDirectory();
85-
InputStream is = null;
8677

87-
try {
88-
is = new FileInputStream(new File(dir, THREADS_DATA_FILE));
78+
try (InputStream is = new FileInputStream(new File(dir, THREADS_DATA_FILE))) {
8979
return loadDataManager(is);
9080
} catch (Exception e) {
9181
// TODO: log it
9282
return null;
93-
} finally {
94-
try {
95-
if (is != null) is.close();
96-
} catch (Exception e) {
97-
// TODO: log it
98-
}
9983
}
10084
}
10185

10286

10387
private synchronized static void saveDataManager(VisualVMThreadsDataManager dm, OutputStream os) throws IOException {
104-
DataOutputStream dos = null;
105-
try {
106-
synchronized(dm) {
107-
int tcount = dm.getThreadsCount();
108-
109-
dos = new DataOutputStream(os);
88+
synchronized(dm) {
89+
int tcount = dm.getThreadsCount();
90+
try (DataOutputStream dos = new DataOutputStream(os)) {
11091

11192
dos.writeUTF(THREADS_SNAPSHOT_HEADER); // Snapshot format
11293
dos.writeInt(THREADS_SNAPSHOT_VERSION); // Snapshot version
@@ -127,8 +108,6 @@ private synchronized static void saveDataManager(VisualVMThreadsDataManager dm,
127108
}
128109
}
129110
}
130-
} finally {
131-
if (dos != null) dos.close();
132111
}
133112
}
134113

visualvm/appui/src/org/graalvm/visualvm/modules/appui/keystore/CacertsKeyStoreProvider.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
import java.io.File;
2929
import java.io.FileInputStream;
30-
import java.io.IOException;
3130
import java.security.KeyStore;
3231
import java.util.logging.Level;
3332
import java.util.logging.Logger;
@@ -55,25 +54,17 @@ private static String getCacerts() {
5554
@Override
5655
public KeyStore getKeyStore() {
5756
KeyStore keyStore = null;
58-
FileInputStream is = null;
5957

60-
try {
61-
File file = new File(getCacerts());
62-
if (!file.exists()) {
63-
return null;
64-
}
58+
File file = new File(getCacerts());
59+
if (!file.exists()) {
60+
return null;
61+
}
6562

63+
try (FileInputStream is = new FileInputStream(file)){
6664
keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
67-
is = new FileInputStream(file);
6865
keyStore.load(is, null);
6966
} catch (Exception ex) {
7067
Logger.getLogger("global").log(Level.INFO, ex.getMessage(), ex);
71-
} finally {
72-
try {
73-
if (is != null) is.close();
74-
} catch (IOException ex) {
75-
assert false : ex;
76-
}
7768
}
7869
return keyStore;
7970
}

visualvm/appui/src/org/graalvm/visualvm/modules/appui/keystore/VisualVMKeyStoreProvider.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
package org.graalvm.visualvm.modules.appui.keystore;
2727

28-
import java.io.IOException;
2928
import java.io.InputStream;
3029
import java.security.KeyStore;
3130
import java.util.logging.Level;
@@ -46,20 +45,12 @@ public final class VisualVMKeyStoreProvider implements KeyStoreProvider {
4645

4746
public KeyStore getKeyStore() {
4847
KeyStore keyStore = null;
49-
InputStream is = null;
5048

51-
try {
52-
is = getClass().getResourceAsStream(KS_FILE_PATH);
49+
try (InputStream is = getClass().getResourceAsStream(KS_FILE_PATH)) {
5350
keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
5451
keyStore.load (is, KS_DEFAULT_PASSWORD.toCharArray());
5552
} catch (Exception ex) {
5653
Logger.getLogger ("global").log(Level.INFO, ex.getMessage(), ex);
57-
} finally {
58-
try {
59-
if (is != null) is.close();
60-
} catch (IOException ex) {
61-
assert false : ex;
62-
}
6354
}
6455
return keyStore;
6556
}

visualvm/charts/src/org/graalvm/visualvm/charts/xy/XYStorage.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,10 @@ public synchronized void addValues(long timestamp, long[] values) {
8383
}
8484

8585
public synchronized void saveValues(OutputStream os) throws IOException {
86-
DataOutputStream dos = null;
87-
try {
86+
try (DataOutputStream dos = new DataOutputStream(os)) {
8887
int icount = values.length;
8988
int vcount = getTimestampsCount();
9089

91-
dos = new DataOutputStream(os);
92-
9390
dos.writeUTF(SNAPSHOT_HEADER); // Snapshot format
9491
dos.writeInt(SNAPSHOT_VERSION); // Snapshot version
9592
dos.writeInt(icount); // Items count
@@ -100,8 +97,6 @@ public synchronized void saveValues(OutputStream os) throws IOException {
10097
for (int iidx = 0; iidx < icount; iidx++)
10198
dos.writeLong(getValue(iidx, vidx));
10299
}
103-
} finally {
104-
if (dos != null) dos.close();
105100
}
106101
}
107102

visualvm/graalvm/src/org/graalvm/visualvm/graalvm/libgraal/MemoryModel.java

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -218,40 +218,18 @@ private static void saveChartSupport(SimpleXYChartSupport chartSupport, File fil
218218
return;
219219
}
220220

221-
OutputStream os = null;
222-
223-
try {
224-
os = new FileOutputStream(file);
221+
try (OutputStream os = new FileOutputStream(file)) {
225222
chartSupport.saveValues(os);
226223
} catch (Exception e) {
227224
LOGGER.log(Level.INFO, "saveChartSupport", e); // NOI18N
228-
} finally {
229-
try {
230-
if (os != null) {
231-
os.close();
232-
}
233-
} catch (Exception e) {
234-
LOGGER.log(Level.INFO, "saveChartSupport", e); // NOI18N
235-
}
236225
}
237226
}
238227

239228
private static void loadChartSupport(SimpleXYChartSupport chartSupport, File file) {
240-
InputStream is = null;
241-
242-
try {
243-
is = new FileInputStream(file);
229+
try (InputStream is = new FileInputStream(file)) {
244230
chartSupport.loadValues(is);
245231
} catch (Exception e) {
246232
LOGGER.log(Level.INFO, "loadChartSupport", e); // NOI18N
247-
} finally {
248-
try {
249-
if (is != null) {
250-
is.close();
251-
}
252-
} catch (Exception e) {
253-
LOGGER.log(Level.INFO, "loadChartSupport", e); // NOI18N
254-
}
255233
}
256234
}
257235

visualvm/libs.profiler/profiler.api/src/org/graalvm/visualvm/lib/profiler/spi/ProfilerStorageProvider.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,9 @@ protected void loadProperties(Properties properties, FileObject storage) throws
193193

194194
protected void saveProperties(Properties properties, FileObject storage) throws IOException {
195195
synchronized (this) {
196-
OutputStream os = storage.getOutputStream();
197-
BufferedOutputStream bos = new BufferedOutputStream(os);
198-
try {
196+
try (OutputStream os = storage.getOutputStream();
197+
BufferedOutputStream bos = new BufferedOutputStream(os)) {
199198
properties.storeToXML(bos, ""); // NOI18N
200-
} finally {
201-
if (bos != null) bos.close();
202199
}
203200
}
204201
}

visualvm/libs.profiler/profiler.snaptracer/src/org/graalvm/visualvm/lib/profiler/snaptracer/impl/export/DataExport.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,8 @@ private static void doExportData(final TableModel model, final String title,
9696

9797
TracerSupportImpl.getInstance().perform(new Runnable() {
9898
public void run() {
99-
Writer writer = null;
10099
TracerProgressObject progress = null;
101-
try {
102-
writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); // NOI18N
100+
try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8")) { // NOI18N
103101
ExportBatch batch = null;
104102

105103
if (filter == XML_FILTER)
@@ -123,8 +121,6 @@ else if (filter == CSV_FILTER)
123121
progress.finish();
124122
}
125123
LOGGER.log(Level.INFO, "Exporting data failed", t); // NOI18N
126-
} finally {
127-
if (writer != null) try { writer.close(); } catch (Exception e) {}
128124
}
129125
}
130126
});

visualvm/pluginimporter/src/org/graalvm/visualvm/pluginimporter/ClusterUpdateProvider.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,7 @@ private static Collection<File> readModules (File cluster) {
172172

173173
private static void readConfigFile (File cf, Map<String, String> attr) {
174174
Document document = null;
175-
InputStream is = null;
176-
try {
177-
is = new BufferedInputStream (new FileInputStream (cf));
175+
try (InputStream is = new BufferedInputStream (new FileInputStream (cf))) {
178176
InputSource xmlInputSource = new InputSource (is);
179177
document = XMLUtil.parse (xmlInputSource, false, false, null, EntityCatalog.getDefault ());
180178
} catch (SAXException saxe) {
@@ -184,14 +182,6 @@ private static void readConfigFile (File cf, Map<String, String> attr) {
184182
} catch (IOException ioe) {
185183
LOG.log(Level.INFO, "Error while reading " + cf);
186184
LOG.log(Level.WARNING, ioe.getLocalizedMessage (), ioe);
187-
} finally {
188-
if (is != null) {
189-
try {
190-
is.close ();
191-
} catch (IOException e){
192-
//ignore
193-
}
194-
}
195185
}
196186

197187
assert document.getDocumentElement () != null : "File " + cf + " must contain document element.";

visualvm/pluginimporter/src/org/graalvm/visualvm/pluginimporter/PluginImporter.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -325,18 +325,13 @@ private static File locateUpdateTracking(String cnb, File cluster) {
325325

326326
private static Node getUpdateTrackingConf(File moduleUpdateTracking) {
327327
Document document = null;
328-
InputStream is;
329-
try {
330-
is = new BufferedInputStream(new FileInputStream(moduleUpdateTracking));
328+
try (InputStream is = new BufferedInputStream(new FileInputStream(moduleUpdateTracking))) {
331329
InputSource xmlInputSource = new InputSource(is);
332330
document = XMLUtil.parse(xmlInputSource, false, false, null, org.openide.xml.EntityCatalog.getDefault());
333-
is.close();
334331
} catch (SAXException saxe) {
335332
LOG.log(Level.WARNING, "SAXException when reading " + moduleUpdateTracking + ", cause: " + saxe);
336333
//for issue #217118 investigation what is corrupted and how
337-
FileReader reader = null;
338-
try {
339-
reader = new FileReader(moduleUpdateTracking);
334+
try (FileReader reader = new FileReader(moduleUpdateTracking)){
340335
char[] text = new char[1024];
341336
String fileContent = "";
342337
while (reader.read(text) > 0) {
@@ -345,14 +340,6 @@ private static Node getUpdateTrackingConf(File moduleUpdateTracking) {
345340
LOG.log(Level.WARNING, "SAXException in file:\n------FILE START------\n " + fileContent + "\n------FILE END-----\n");
346341
} catch (Exception ex) {
347342
//don't need to fail in logging
348-
} finally {
349-
if (reader != null) {
350-
try {
351-
reader.close();
352-
} catch (IOException ex) {
353-
//don't need any info from logging fail
354-
}
355-
}
356343
}
357344
return null;
358345
} catch (IOException ioe) {

0 commit comments

Comments
 (0)