Skip to content

Commit c8b2caa

Browse files
EcljpseB0Tjukzi
authored andcommitted
PlistFileWriter: fix 'Potential resource leak' warnings
1 parent 2a6fe0e commit c8b2caa

File tree

3 files changed

+36
-32
lines changed

3 files changed

+36
-32
lines changed

bundles/org.eclipse.urischeme/src/org/eclipse/urischeme/internal/registration/PlistFileWriter.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ public class PlistFileWriter {
5858

5959
/**
6060
* Creates an instance of the PlistFileWriter. Throws an
61-
* {@link IllegalStateException} if the given {@link Reader} does not provide
61+
* {@link IllegalStateException} if the supplied {@link Reader} does not provide
6262
* .plist file.
6363
*
64-
* @param reader The file reader of the .plist file
64+
* @param reader The supplier of file reader of the .plist file
6565
*
6666
* @throws IllegalArgumentException if file cannot be understood as .plist file
6767
*/
68-
public PlistFileWriter(Reader reader) {
68+
public PlistFileWriter(IOSupplier<Reader> reader) {
6969
this.document = getDom(reader);
7070
this.array = getOrCreateBundleUrlTypesAndArray();
7171
}
@@ -167,13 +167,18 @@ public void removeScheme(String scheme) {
167167
arrayNode.removeChild(dict);
168168
}
169169

170+
@FunctionalInterface
171+
public interface IOSupplier<T> {
172+
T get() throws IOException;
173+
}
174+
170175
/**
171-
* Writes the content (xml) of the .plist file to the given {@link Writer}
176+
* Writes the content (xml) of the .plist file to the supplied {@link Writer}
172177
*
173-
* @param writer The Writer to which the xml should be written to, e.g.
174-
* {@link BufferedWriter}
178+
* @param writer The Supplier for a Writer to which the xml should be written
179+
* to, e.g. {@link BufferedWriter}
175180
*/
176-
public void writeTo(Writer writer) {
181+
public void writeTo(IOSupplier<Writer> writer) {
177182
boolean hasDict = false;
178183
for (int i = 0; i < array.getChildNodes().getLength(); i++) {
179184
Node child = array.getChildNodes().item(i);
@@ -197,8 +202,8 @@ public void writeTo(Writer writer) {
197202
transformDocument(writer);
198203
}
199204

200-
private void transformDocument(Writer writer) {
201-
try (writer) {
205+
private void transformDocument(IOSupplier<Writer> supplier) {
206+
try (Writer writer = supplier.get()) {
202207
DOMSource source = new DOMSource(this.document);
203208
@SuppressWarnings("restriction")
204209
TransformerFactory f = org.eclipse.core.internal.runtime.XmlProcessorFactory
@@ -209,11 +214,12 @@ private void transformDocument(Writer writer) {
209214
}
210215
}
211216

212-
@SuppressWarnings("restriction")
213-
private Document getDom(Reader reader) {
214-
try (reader) {
215-
return org.eclipse.core.internal.runtime.XmlProcessorFactory
216-
.parseWithErrorOnDOCTYPE(new InputSource(reader));
217+
private Document getDom(IOSupplier<Reader> supplier) {
218+
try (Reader reader = supplier.get()) {
219+
@SuppressWarnings("restriction")
220+
Document withErrorOnDOCTYPE = org.eclipse.core.internal.runtime.XmlProcessorFactory
221+
.parseWithErrorOnDOCTYPE(new InputSource(reader));
222+
return withErrorOnDOCTYPE;
217223
} catch (ParserConfigurationException | IOException | SAXException e) {
218224
throw new IllegalArgumentException(e);
219225
}

bundles/org.eclipse.urischeme/src/org/eclipse/urischeme/internal/registration/RegistrationMacOsX.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
*******************************************************************************/
1111
package org.eclipse.urischeme.internal.registration;
1212

13-
import java.io.IOException;
1413
import java.util.ArrayList;
1514
import java.util.Collection;
1615
import java.util.List;
@@ -117,8 +116,8 @@ private String determineHandlerLocation(String lsRegisterDump, String scheme) {
117116
.orElse(""); //$NON-NLS-1$
118117
}
119118

120-
private PlistFileWriter getPlistFileWriter(String plistPath) throws IOException {
121-
return new PlistFileWriter(fileProvider.newReader(plistPath));
119+
private PlistFileWriter getPlistFileWriter(String plistPath) {
120+
return new PlistFileWriter(() -> fileProvider.newReader(plistPath));
122121
}
123122

124123
@Override
@@ -131,8 +130,7 @@ private void registerAppWithLsregister(String pathToEclipseApp) throws Exception
131130
processExecutor.execute(LSREGISTER, RECURSIVE, pathToEclipseApp);
132131
}
133132

134-
private void changePlistFile(Collection<IScheme> toAdd, Collection<IScheme> toRemove, String pathToEclipseApp)
135-
throws IOException {
133+
private void changePlistFile(Collection<IScheme> toAdd, Collection<IScheme> toRemove, String pathToEclipseApp) {
136134
String plistPath = pathToEclipseApp + PLIST_PATH_SUFFIX;
137135

138136
PlistFileWriter writer = getPlistFileWriter(plistPath);
@@ -145,7 +143,7 @@ private void changePlistFile(Collection<IScheme> toAdd, Collection<IScheme> toRe
145143
writer.removeScheme(scheme.getName());
146144
}
147145

148-
writer.writeTo(fileProvider.newWriter(plistPath));
146+
writer.writeTo(() -> fileProvider.newWriter(plistPath));
149147
}
150148

151149
/**

tests/org.eclipse.tests.urischeme/src/org/eclipse/urischeme/internal/registration/TestUnitPlistFileWriter.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public void doesntRemoveCommentAfterEndArrayTag() {
8282
String xml = getPlistStartXmlSnippet() + " <key>CFBundleURLTypes</key>\n" + " <array>\n"
8383
+ getSchemeXmlSnippet("other") + " </array>\n" + "<!--comment-->\n" + getPlistEndXmlSnippet();
8484

85-
PlistFileWriter writer = new PlistFileWriter(new StringReader(xml));
85+
PlistFileWriter writer = new PlistFileWriter(() -> new StringReader(xml));
8686

8787
writer.addScheme("adt", "adtScheme");
8888

@@ -97,7 +97,7 @@ public void doesntRemoveCommentAfterEndArrayTag() {
9797
public void doesntRemoveCommentBeforeEndArrayTag() {
9898
String xml = getPlistStartXmlSnippet() + " <key>CFBundleURLTypes</key>\n" + " <array>\n"
9999
+ getSchemeXmlSnippet("other") + "<!--comment-->" + " </array>\n" + getPlistEndXmlSnippet();
100-
PlistFileWriter writer = new PlistFileWriter(new StringReader(xml));
100+
PlistFileWriter writer = new PlistFileWriter(() -> new StringReader(xml));
101101

102102
writer.addScheme("adt", "adtScheme");
103103

@@ -183,25 +183,25 @@ public void removesEmptyCFBundleURLTypesEntry() {
183183

184184
@Test(expected = IllegalArgumentException.class)
185185
public void throwsExceptionOnEmptyDocument() {
186-
new PlistFileWriter(new StringReader(""));
186+
new PlistFileWriter(() -> new StringReader(""));
187187
}
188188

189189
@Test(expected = IllegalStateException.class)
190190
public void throwsExceptionOnWrongPlistFile() {
191191
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" + "<plist version=\"1.0\"/>";
192-
new PlistFileWriter(new StringReader(xml));
192+
new PlistFileWriter(() -> new StringReader(xml));
193193
}
194194

195195
@Test(expected = IllegalStateException.class)
196196
public void throwsExceptionOnWrongXmlFile() {
197197
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" + "<foo/>";
198-
new PlistFileWriter(new StringReader(xml));
198+
new PlistFileWriter(() -> new StringReader(xml));
199199
}
200200

201201
@Test(expected = IllegalArgumentException.class)
202202
public void throwsExceptionOnNonXmlFile() {
203203
String xml = "foo bar";
204-
new PlistFileWriter(new StringReader(xml));
204+
new PlistFileWriter(() -> new StringReader(xml));
205205
}
206206

207207
@Test
@@ -232,17 +232,17 @@ private void assertSchemesInOrder(PlistFileWriter writer, String... schemes) {
232232
}
233233

234234
private void assertXml(String xml, PlistFileWriter writer) {
235-
StringWriter stringWriter = new StringWriter();
236-
writer.writeTo(stringWriter);
237-
assertEquals(xml, stringWriter.toString().replaceAll("\r\n", "\n"));
235+
StringWriter stringWriters[] = new StringWriter[1];
236+
writer.writeTo(() -> (stringWriters[0] = new StringWriter()));
237+
assertEquals(xml, stringWriters[0].toString().replaceAll("\r\n", "\n"));
238238
}
239239

240240
private PlistFileWriter getWriter() {
241-
return new PlistFileWriter(new StringReader(getXml()));
241+
return new PlistFileWriter(() -> new StringReader(getXml()));
242242
}
243243

244244
private PlistFileWriter getWriterWithSchemes(String... schemes) {
245-
return new PlistFileWriter(new StringReader(getXml(schemes)));
245+
return new PlistFileWriter(() -> new StringReader(getXml(schemes)));
246246
}
247247

248248
private String getXml() {
@@ -257,7 +257,7 @@ private String getXml(String[] schemes) {
257257
}
258258
}
259259
return getPlistStartXmlSnippet() + " <key>CFBundleURLTypes</key>\n" + " <array>\n"
260-
+ snippets.append(" </array>\n").append(getPlistEndXmlSnippet()).toString();
260+
+ snippets.append(" </array>\n").append(getPlistEndXmlSnippet()).toString();
261261
}
262262

263263
private String getPlistStartXmlSnippet() {

0 commit comments

Comments
 (0)