39 lines
911 B
Plaintext
39 lines
911 B
Plaintext
Java 7's try-with-resources structure automatically handles closing the resources that the ``++try++`` itself opens. Thus, adding an explicit ``++close()++`` call is redundant and potentially confusing.
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
[source,java]
|
|
----
|
|
try (PrintWriter writer = new PrintWriter(process.getOutputStream())) {
|
|
String contents = file.contents();
|
|
writer.write(new Gson().toJson(new MyObject(contents)));
|
|
writer.flush();
|
|
writer.close(); // Noncompliant
|
|
}
|
|
----
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
[source,java]
|
|
----
|
|
try (PrintWriter writer = new PrintWriter(process.getOutputStream())) {
|
|
String contents = file.contents();
|
|
writer.write(new Gson().toJson(new MyObject(contents)));
|
|
writer.flush();
|
|
}
|
|
----
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::message.adoc[]
|
|
|
|
include::highlighting.adoc[]
|
|
|
|
endif::env-github,rspecator-view[]
|