issue #5752: Activate checks related to java.io.Closeable#5753
Conversation
|
|
||
| mkdir -p target/classes | ||
| mkdir -p target/eclipse | ||
| mkdir -p target/classes target/test-classes target/eclipse |
There was a problem hiding this comment.
To solve warning incorrect classpath: src/checkstyle/target/test-classes on clean environment
Codecov Report
@@ Coverage Diff @@
## master #5753 +/- ##
========================================
Coverage 100% 100%
- Complexity 7381 7386 +5
========================================
Files 296 296
Lines 16123 16138 +15
Branches 3254 3255 +1
========================================
+ Hits 16123 16138 +15
Continue to review full report at Codecov.
|
| * @return a fresh new {@code AuditListener} | ||
| * @exception IOException when provided output location is not found | ||
| */ | ||
| @SuppressWarnings("resource") |
There was a problem hiding this comment.
Please explain suppress reason in javadoc.
Why IDEA does not have there suppression ? Is it a hug in Eclipse analyser?
There was a problem hiding this comment.
I do not think this is a bug. The algorithms that use IDEA and Eclipse are different. And they can behave differently.
There was a problem hiding this comment.
ok, call it new feature request, enhancement, any type of ticket against Eclipse to be better.
If IDEA found way to make less false positives, why Eclipse should be behind ?
If Eclipse is less configurable/smart ... why we should activate it ? Does it make sense to activate only IDEA ?
Do we get some benefit from Eclipse only(not detected by IDEA) ?
|
|
||
| /** | ||
| * Output stream to hold the test results. | ||
| * @noinspection resource |
There was a problem hiding this comment.
Please explain suppress reason in javadoc.
|
|
||
| /** | ||
| * Output stream to hold the test results. | ||
| * @noinspection resource |
There was a problem hiding this comment.
Please explain suppress reason in javadoc.
| <option value="ReuseOfLocalVariable" /> | ||
| <!-- The output stream created by Main.createListener will be closed later. This is | ||
| by design. A warning issued by Eclipse JDT need to be suppressed. --> | ||
| <option value="resource" /> |
There was a problem hiding this comment.
Please make comment more general. As we sometime postpone close for resource by design, not only for this particular case - Main.createListener
There was a problem hiding this comment.
sorry to enforce bureaucracy, but it is proven by practice, principle to do "done" for each point.
It is just final recheck that all items from numerous are done.
romani
left a comment
There was a problem hiding this comment.
I need to review this further:
| <!-- we are ok to use auto-unboxing as we use modern java --> | ||
| <inspection_tool class="AutoCloseableResource" enabled="true" level="ERROR" enabled_by_default="true" > | ||
| <option name="ignoreFromMethodCall" value="true" /> | ||
| </inspection_tool> |
There was a problem hiding this comment.
comment for myself.
from IDEA:
Reports AutoCloseable instances which are not used in a try-with-resources statement, also known as Automatic
Resource Management. This means that the open resource before/in try, close in finally style which was used
before try-with-resources was available is also reported. This inspection is meant to replace all opened but not
safely closed inspections when developing in Java 7 and higher.
Use the first table below to specify which AutoCloseable subclasses should be ignored by this inspection. Specify
AutoCloseable subclasses here which do not need to be closed.
Use the second table below to specify which methods returning AutoCloseable will be ignored when called.
Use the first checkbox below to ignore an AutoCloseable if it the result of a method call. When enabled, the results
of factory methods will also be ignored.
Use the second checkbox below to specify that the inspection should not warn if an AutoCloseable instance is
passed as a method call argument. If enabled the inspection assumes the resource is closed in the called method. Method calls inside a finally block with close in the name and an AutoCloseable argument will not be ignored.
There was a problem hiding this comment.
Yes, it is related to the ignoreFromMethodCall option.
By turning it off the IDEA reports the line
out = Files.newOutputStream(Paths.get(outputLocation));
as violation in the same way as Eclipse does. One more: the suppression
@SuppressWarnings("resource")
Works for IDEA in the same way as for Eclipse. So it is possible to set ignoreFromMethodCall to false.
|
After setting This is unexpected and should be investigated. |
|
@pbludov ,
ignoreFromMethodCall=true allow too much. Please merge my draft branch https://github.com/checkstyle/checkstyle/tree/i5752-close-resources to your code, together update will be good to merge I think. |
|
I'm afk till 14 may, sorry. |
done. |
| /** | ||
| * Creates the audit listener. | ||
| * Creates the audit listener. The JDT compiler issues the warning explicitlyClosedAutoCloseable | ||
| * for {@code Files.newOutputStream}, so it need to be suppressed. |
There was a problem hiding this comment.
this javadoc is public, let be more accurate here.
please remove "The JDT compiler issues the warning explicitlyClosedAutoCloseable ...." and make it like This method create in AuditListener open stream for validation data, it must be closed by {@link RootModule} (default implementation is {@link Checker}) by launching {@link AuditListener#auditFinished(AuditEvent)}
| <option name="ignoredTypes" value="java.io.ByteArrayOutputStream,java.util.stream.Stream,java.util.stream.IntStream,java.util.stream.LongStream,java.util.stream.DoubleStream,com.puppycrawl.tools.checkstyle.internal.utils.CloseAndFlushTestByteArrayOutputStream" /> | ||
| <!-- False positives for the Closable returned from a builder method (which return "this"). | ||
| Mock stubs shouldn't be closed. --> | ||
| <option name="METHOD_MATCHER_CONFIG" value="java.util.Formatter,format,java.io.Writer,append,com.google.common.base.Preconditions,checkNotNull,org.hibernate.Session,close,java.io.PrintWriter,printf,org.mockito.ArgumentCaptor,getValue,org.mockito.Mockito,mock|verify,org.mockito.stubbing.Stubber,when" /> |
There was a problem hiding this comment.
getValue
it is not ok. We can resolve this bad code.
please review commits, sorry if they are messy, I can not squash in main repo:
2b0ac1b
9c6aa47
I refactored createListener to create streams when required getOutputStream(outputLocation) and getOutputStreamOptions(String outputLocation).
If it still messy, I will recreate branch.
If you have some concerns with such implementation, we can merge this PR, but discuss update in separate issue.
There was a problem hiding this comment.
done. Your code has been squashed to the PR.
|
eclipse is not happy. |
done |
| * | ||
| * This method creates in AuditListener an open stream for validation data, it must be closed by | ||
| * {@link RootModule} (default implementation is {@link Checker}) by calling | ||
| * {@link AuditListener#auditFinished(AuditEvent)}. |
There was a problem hiding this comment.
Please move explanation on why stream is not closed to getOutputStream(String outputLocation) .
Unfortunately there is no way to keep reason of suppression in same annotation, so javadoc is only place to keep reason of suppression
| Mock stubs shouldn't be closed. --> | ||
| <option name="METHOD_MATCHER_CONFIG" value="java.util.Formatter,format,java.io.Writer,append,com.google.common.base.Preconditions,checkNotNull,org.hibernate.Session,close,java.io.PrintWriter,printf,org.mockito.Mockito,mock|verify,org.mockito.stubbing.Stubber,when" /> | ||
| </inspection_tool> | ||
| <!-- we are ok to use auto-unboxing as we use modern java --> |
There was a problem hiding this comment.
incorrect indentation, -1 space
| <!-- we are ok to use auto-unboxing as we use modern java --> | ||
| <inspection_tool class="AutoCloseableResource" enabled="true" level="ERROR" enabled_by_default="true"> | ||
| <!-- These classes do not contain any resources that need to be closed. --> | ||
| <option name="ignoredTypes" value="java.io.ByteArrayOutputStream,java.util.stream.Stream,java.util.stream.IntStream,java.util.stream.LongStream,java.util.stream.DoubleStream,com.puppycrawl.tools.checkstyle.internal.utils.CloseAndFlushTestByteArrayOutputStream" /> |
There was a problem hiding this comment.
can we split all new lines so they are less than 100 characters where possible?
There was a problem hiding this comment.
if this is not obvious how to make, we can postpone it for later.
There was a problem hiding this comment.
done. If the CI fails, i'll revert this.
There was a problem hiding this comment.
The line wrapping reverted.
| <option name="ignoredTypes" value="java.io.ByteArrayOutputStream,java.util.stream.Stream,java.util.stream.IntStream,java.util.stream.LongStream,java.util.stream.DoubleStream,com.puppycrawl.tools.checkstyle.internal.utils.CloseAndFlushTestByteArrayOutputStream" /> | ||
| <!-- False positives for the Closable returned from a builder method (which return "this"). | ||
| Mock stubs shouldn't be closed. --> | ||
| <option name="METHOD_MATCHER_CONFIG" value="java.util.Formatter,format,java.io.Writer,append,com.google.common.base.Preconditions,checkNotNull,org.hibernate.Session,close,java.io.PrintWriter,printf,org.mockito.Mockito,mock|verify,org.mockito.stubbing.Stubber,when" /> |
There was a problem hiding this comment.
if this is not obvious how to make, we can postpone it for later.
| public void testNullInfoStreamOptions() { | ||
| try { | ||
| final DefaultLogger logger = new DefaultLogger(new ByteArrayOutputStream(), null); | ||
| assertNotNull("Null instance", logger); |
There was a problem hiding this comment.
isn't this assertion unnecessary since eventually we will hit fail below? Also when can a new instance return null?
There was a problem hiding this comment.
Without this assertion, the warning "The allocated object is never used" appears. See
https://travis-ci.org/checkstyle/checkstyle/jobs/378234050#L761
We need to use the logger somehow to make the eclipse compiler happy.
There was a problem hiding this comment.
This won't work:
@Test
public void testNullOutputStreamOptions() {
try {
new XMLLogger(outStream, null);
fail("Exception was expected");
}
catch (IllegalArgumentException exception) {
assertEquals("Invalid error message", "Parameter outputStreamOptions can not be null",
exception.getMessage());
}
}
If you have any idea how to do it right, let us know.
There was a problem hiding this comment.
happiness for all - is tough.
This nuance nobody will remember, it far from obvious.
What if we will put in assert message "Null instance; assert required to calm down eclipse's 'The allocated object is never used' violation" or just make single line comment above assert ?
There was a problem hiding this comment.
What if we will put in assert message "Null instance; assert required to calm down eclipse's 'The allocated object is never used' violation" or just make single line comment above assert ?
done
There was a problem hiding this comment.
This sounds familiar which we had to do with other tests.
| try { | ||
| final DefaultLogger logger = new DefaultLogger(new ByteArrayOutputStream(), | ||
| AutomaticBean.OutputStreamOptions.CLOSE, new ByteArrayOutputStream(), null); | ||
| assertNotNull("Null instance", logger); |
| public void testNullOutputStreamOptions() { | ||
| try { | ||
| final XMLLogger logger = new XMLLogger(outStream, null); | ||
| assertNotNull("Null instance", logger); |
| * @param messageFileName message file name. | ||
| * @param expected an array of expected messages. | ||
| * @throws Exception might happen | ||
| */ |
There was a problem hiding this comment.
Why is this documentation needed when no code was changed? This is a test area.
There was a problem hiding this comment.
I believe that there was a suppression. Later, the code was refactored. The comment is removed now.
| OutputStreamOptions errorStreamOptions, | ||
| AuditEventFormatter messageFormatter) { | ||
| if (infoStreamOptions == null) { | ||
| throw new IllegalArgumentException("Parameter infoStreamOptions can not be null"); |
There was a problem hiding this comment.
Just curious, why are we adding these IAEs and do we need them in other areas?
There was a problem hiding this comment.
Pitest. It says "the result may be replaced with null here
private static AutomaticBean.OutputStreamOptions getOutputStreamOptions(String outputLocation) {
final AutomaticBean.OutputStreamOptions result;
if (outputLocation == null) {
result = AutomaticBean.OutputStreamOptions.NONE; // THIS line was red
}
else {
result = AutomaticBean.OutputStreamOptions.CLOSE;
}
return result;
}
The expression
closeStream = outputStreamOptions == OutputStreamOptions.CLOSE;
work just fine when the parameter outputStreamOptions is null.
It turns out that we can use an enumeration with just one value! The second value will be null.
This is logical, but wrong. I explicitly forbid null for these parameters.
|
@pbludov , please take a look at items to improve and confim. If smth is not clear let us know. |
rnveach
left a comment
There was a problem hiding this comment.
Can be merged when CI passes.
|
Merged, thanks a lot to all |


Issue #5752
Force use of
try-with-resources