-
Notifications
You must be signed in to change notification settings - Fork 38.9k
use try-with-resources statement
#35046
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
use try-with-resources statement
#35046
Conversation
.../src/main/java/org/springframework/jms/listener/AbstractPollingMessageListenerContainer.java
Show resolved
Hide resolved
4e2e1be to
9aa6812
Compare
Signed-off-by: Vincent Potucek <vpotucek@me.com>
9aa6812 to
d8d4b6e
Compare
bclozel
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for raising this, but as this review shows, these stylistic changes are mostly breaking the behavior or adding little value.
| continue; | ||
| } | ||
| InputStream is = cl.getResourceAsStream(c.getName().replace('.', '/') + ".class"); | ||
| if (is == null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't change files in the cglib package, this part is manually shaded into our repository and we shouldn't change the source if we want to easily keep up with the original.
| Set bridges = (Set) entry.getValue(); | ||
| try { | ||
| InputStream is = classLoader.getResourceAsStream(owner.getName().replace('.', '/') + ".class"); | ||
| if (is == null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same
| name.replace('.','/') + ".class" | ||
| ); | ||
|
|
||
| if (is == null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same
| } | ||
| return size; | ||
| } | ||
| finally { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert. The exception thrown while closing the stream is not caught/logged anymore.
See this message for a discussion on possible workarounds. I think we should keep the current code.
| try { | ||
| con.close(); | ||
| } | ||
| catch (SQLException ex) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same. Please revert.
| } | ||
| try { | ||
| handleListenerException(ex); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please revert this new line change.
| if (ex instanceof JMSException jmsException) { | ||
| throw jmsException; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please revert this new line change.
| TransactionSynchronizationManager.unbindResource(obtainConnectionFactory()); | ||
| } | ||
| observation.stop(); | ||
| scope.close(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a neutral change. The scope is now closed before the observation is stopped and the transaction finished. This will change the behavior and will have unintended consequences.
Please revert.
| in.close(); | ||
| } | ||
| catch (Throwable ex) { | ||
| // ignore, see SPR-12999 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert. This will catch all Throwable thrown by the code block, not just when the resource is closed.
| responseHeaders.setContentLength(rangeLength); | ||
|
|
||
| InputStream in = region.getResource().getInputStream(); | ||
| // We cannot use try-with-resources here for the InputStream, since we have |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is literally called out in this comment...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes need to stay open i know this behavior from maven. should give more detail then as ignored exc for good reason.
|
Given the state of this PR, I don't think it's worth spending time on this - we're more likely to break behavior than anything. |
|
yes of course will SOC. if this changes anything its a interesting to invest, as its a valid feature used all around already in this code base: Line 414 in d8ac3ff
|
|
if can not use java like supposed to, it seems smell. |
Pankraz76
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thx. for detailed review. yes this need special attention.
| responseHeaders.setContentLength(rangeLength); | ||
|
|
||
| InputStream in = region.getResource().getInputStream(); | ||
| // We cannot use try-with-resources here for the InputStream, since we have |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes need to stay open i know this behavior from maven. should give more detail then as ignored exc for good reason.
Java 7 introduced the try-with-resources statement. This statement ensures that each resource is closed at the end of the statement. It avoids the need of explicitly closing the resources in a finally block. Additionally exceptions are better handled: If an exception occurred both in the try block and finally block, then the exception from the try block was suppressed. With the try-with-resources statement, the exception thrown from the try-block is preserved.
try-with-resourcesstatement openrewrite/rewrite-static-analysis#591try-with-resourcesstatement inLookupWagonMojoapache/maven#2426try-with-resourcesstatement quarkusio/quarkus#48364