Skip to content

Commit

Permalink
Converts try finally block to try-with-resources. Closes #162.
Browse files Browse the repository at this point in the history
  • Loading branch information
carstenartur authored and joehni committed Aug 11, 2020
1 parent 3cec78d commit 67c2dbb
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 53 deletions.
12 changes: 3 additions & 9 deletions xstream/src/java/com/thoughtworks/xstream/XStreamer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2006, 2007, 2014, 2016, 2019 XStream Committers.
* Copyright (C) 2006, 2007, 2014, 2016, 2019, 2020 XStream Committers.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
Expand Down Expand Up @@ -106,13 +106,10 @@ public String toXML(final XStream xstream, final Object obj) throws ObjectStream
*/
public void toXML(final XStream xstream, final Object obj, final Writer out) throws IOException {
final XStream outer = new XStream();
final ObjectOutputStream oos = outer.createObjectOutputStream(out);
try {
try (final ObjectOutputStream oos = outer.createObjectOutputStream(out)) {
oos.writeObject(xstream);
oos.flush();
xstream.toXML(obj, out);
} finally {
oos.close();
}
}

Expand Down Expand Up @@ -270,8 +267,7 @@ public <T> T fromXML(final HierarchicalStreamDriver driver, final Reader xml, fi
outer.addPermission(permission);
}
final HierarchicalStreamReader reader = driver.createReader(xml);
final ObjectInputStream configIn = outer.createObjectInputStream(reader);
try {
try (ObjectInputStream configIn = outer.createObjectInputStream(reader)) {
final XStream configured = (XStream)configIn.readObject();
final ObjectInputStream in = configured.createObjectInputStream(reader);
try {
Expand All @@ -281,8 +277,6 @@ public <T> T fromXML(final HierarchicalStreamDriver driver, final Reader xml, fi
} finally {
in.close();
}
} finally {
configIn.close();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright (C) 2008, 2014 XStream Committers.
* Copyright (C) 2008, 2014, 2020 XStream Committers.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
*
* Created on 18. November 2008 by Joerg Schaible
*/
package com.thoughtworks.xstream.persistence;
Expand All @@ -31,7 +31,7 @@

/**
* Abstract base class for file based persistence strategies.
*
*
* @author Guilherme Silveira
* @author Joerg Schaible
* @since 1.3.1
Expand Down Expand Up @@ -64,15 +64,15 @@ protected boolean isValid(final File dir, final String name) {

/**
* Given a filename, the unescape method returns the key which originated it.
*
*
* @param name the filename
* @return the original key
*/
protected abstract K extractKey(String name);

/**
* Given a key, the escape method returns the filename which shall be used.
*
*
* @param key the key
* @return the desired and escaped filename
*/
Expand Down Expand Up @@ -156,13 +156,10 @@ public boolean equals(final Object obj) {
private void writeFile(final File file, final Object value) {
try {
final FileOutputStream out = new FileOutputStream(file);
final Writer writer = encoding != null
try (final Writer writer = encoding != null
? new OutputStreamWriter(out, encoding)
: new OutputStreamWriter(out);
try {
: new OutputStreamWriter(out)) {
xstream.toXML(value, writer);
} finally {
writer.close();
}
} catch (final IOException e) {
throw new StreamException(e);
Expand All @@ -176,13 +173,12 @@ private File getFile(final String filename) {
private V readFile(final File file) {
try {
final FileInputStream in = new FileInputStream(file);
final Reader reader = encoding != null ? new InputStreamReader(in, encoding) : new InputStreamReader(in);
try {
try (final Reader reader = encoding != null
? new InputStreamReader(in, encoding)
: new InputStreamReader(in)) {
@SuppressWarnings("unchecked")
final V value = (V)xstream.fromXML(reader);
return value;
} finally {
reader.close();
}
} catch (final FileNotFoundException e) {
// not found... file.exists might generate a sync problem
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2003, 2004, 2005 Joe Walnes.
* Copyright (C) 2006, 2007, 2008, 2012, 2014, 2016, 2017, 2018 XStream Committers.
* Copyright (C) 2006, 2007, 2008, 2012, 2014, 2016, 2017, 2018, 2020 XStream Committers.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
Expand Down Expand Up @@ -269,19 +269,13 @@ public void testPathWithSpecialCharacters() {
public void testPathOfNonDefaultFileSystem() throws IOException {
final Map<String, String> env = new HashMap<>();
env.put("create", "true");
final URI uri = URI.create("jar:"
+ Paths.get("target/lib/proxytoys-0.2.1.jar").toAbsolutePath().toUri().toString());
final URI uri = URI
.create("jar:" + Paths.get("target/lib/proxytoys-0.2.1.jar").toAbsolutePath().toUri().toString());

FileSystem zipfs = null;
try {
zipfs = FileSystems.newFileSystem(uri, env);
try (final FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
final String entry = "/com/thoughtworks/proxy/kit/SimpleReference.class";
final Path path = zipfs.getPath(entry);
assertBothWays(path, "<path>" + uri.toString() + "!" + entry + "</path>");
} finally {
if (zipfs != null) {
zipfs.close();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2008, 2018 XStream Committers.
* Copyright (C) 2008, 2018, 2020 XStream Committers.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
Expand Down Expand Up @@ -93,7 +93,7 @@ public void testCanUsePersistenceCollectionAsConverter() throws IOException {

final SampleLists<Object, ?> lists = new SampleLists<>();
lists.good.add("Guilherme");
lists.good.add(new Integer(1970));
lists.good.add(Integer.valueOf(1970));
lists.good.add(new Software("Codehaus", "XStream"));

final String expected = ""
Expand All @@ -111,11 +111,8 @@ public void testCanUsePersistenceCollectionAsConverter() throws IOException {
assertEquals(lists.good, serialized.good);

// retrieve value from external file
final FileInputStream inputStream = new FileInputStream(new File(dir, "int@2.xml"));
try {
try (final FileInputStream inputStream = new FileInputStream(new File(dir, "int@2.xml"))) {
assertEquals(lists.good.get(2), xstream.fromXML(inputStream));
} finally {
inputStream.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ public void testObjectOutputStream() throws Exception {

final Parent parent = new Parent("ze-name", new Child(1));
final StringWriter stringWriter = new StringWriter();
final ObjectOutputStream os = xstream.createObjectOutputStream(stringWriter);
os.writeObject(parent);
os.close();
try (ObjectOutputStream os = xstream.createObjectOutputStream(stringWriter)) {
os.writeObject(parent);
}
final String actualXml = stringWriter.getBuffer().toString();
assertEquals(expectedXml, actualXml);
}
Expand Down Expand Up @@ -217,9 +217,9 @@ public void testCanHandleRawBytes() throws IOException, ClassNotFoundException {
+ "</root>";

final StringWriter stringWriter = new StringWriter();
final ObjectOutputStream os = xstream.createObjectOutputStream(stringWriter, "root");
os.writeObject(new RawString("XStream"));
os.close();
try (ObjectOutputStream os = xstream.createObjectOutputStream(stringWriter, "root")) {
os.writeObject(new RawString("XStream"));
}
final String actualXml = stringWriter.getBuffer().toString();
assertEquals(expectedXml, actualXml);

Expand Down
17 changes: 9 additions & 8 deletions xstream/src/test/com/thoughtworks/xstream/XStreamTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2003, 2004, 2005, 2006 Joe Walnes.
* Copyright (C) 2006, 2007, 2011, 2014, 2017, 2018 XStream Committers.
* Copyright (C) 2006, 2007, 2011, 2014, 2017, 2018, 2020 XStream Committers.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
Expand Down Expand Up @@ -246,6 +246,7 @@ public void testAccessToUnderlyingDom4JImplementation() throws Exception {
xstream.alias("person", Person.class);

final Dom4JDriver driver = new Dom4JDriver();
@SuppressWarnings("resource")
final Person person = (Person)xstream.unmarshal(driver.createReader(new StringReader(xml)));

assertEquals("jason", person.firstName);
Expand Down Expand Up @@ -362,14 +363,14 @@ public void testUnmarshalsWhenAllImplementationsAreSpecifiedUsingAClassIdentifie

public void testObjectOutputStreamCloseTwice() throws IOException {
final ObjectOutputStream oout = xstream.createObjectOutputStream(new StringWriter());
oout.writeObject(new Integer(1));
oout.writeObject(Integer.valueOf(1));
oout.close();
oout.close();
}

public void testObjectOutputStreamCloseAndFlush() throws IOException {
final ObjectOutputStream oout = xstream.createObjectOutputStream(new StringWriter());
oout.writeObject(new Integer(1));
oout.writeObject(Integer.valueOf(1));
oout.close();
try {
oout.flush();
Expand All @@ -381,10 +382,10 @@ public void testObjectOutputStreamCloseAndFlush() throws IOException {

public void testObjectOutputStreamCloseAndWrite() throws IOException {
final ObjectOutputStream oout = xstream.createObjectOutputStream(new StringWriter());
oout.writeObject(new Integer(1));
oout.writeObject(Integer.valueOf(1));
oout.close();
try {
oout.writeObject(new Integer(2));
oout.writeObject(Integer.valueOf(2));
fail("Closing and writing should throw a StreamException");
} catch (final StreamException e) {
// ok
Expand Down Expand Up @@ -416,9 +417,9 @@ private File createTestFile() throws FileNotFoundException, IOException, Unsuppo
final File dir = new File("target/test-data");
dir.mkdirs();
final File file = new File(dir, "test.xml");
final FileOutputStream fos = new FileOutputStream(file);
fos.write(xml.getBytes("UTF-8"));
fos.close();
try (final FileOutputStream fos = new FileOutputStream(file)) {
fos.write(xml.getBytes("UTF-8"));
}
return file;
}
}

0 comments on commit 67c2dbb

Please sign in to comment.