-
Notifications
You must be signed in to change notification settings - Fork 725
SONARJAVA-6514 Fix S2093 FN: JDK 26 autoclosables #5700
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
Merged
+210
−4
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
172 changes: 172 additions & 0 deletions
172
java-checks-test-sources/default/src/main/java/checks/TryWithResourcesCheck_java_26.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| package checks; | ||
|
|
||
| import java.io.IOException; | ||
| import java.sql.Array; | ||
| import java.sql.Blob; | ||
| import java.sql.CallableStatement; | ||
| import java.sql.Clob; | ||
| import java.sql.Connection; | ||
| import java.sql.NClob; | ||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
| import java.sql.SQLXML; | ||
|
|
||
| class TryWithResourcesCheck_java_26 { | ||
|
|
||
| void processFromProcessBuilderIsCloseableAsOfJava26() throws IOException { | ||
| Process p = null; | ||
| try { // Noncompliant {{Change this "try" to a try-with-resources.}} | ||
| p = new ProcessBuilder("ls").start(); | ||
| p.waitFor(); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| } finally { | ||
| if (p != null) { | ||
| p.close(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void processFromRuntimeExec() throws IOException { | ||
| Process p = null; | ||
| try { // Noncompliant | ||
| p = Runtime.getRuntime().exec(new String[] {"ls"}); | ||
| p.waitFor(); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| } finally { | ||
| p.close(); | ||
| } | ||
| } | ||
|
|
||
| void blobFromConnectionFactory(Connection connection) throws SQLException { | ||
| Blob blob = null; | ||
| try { // Noncompliant | ||
| blob = connection.createBlob(); | ||
| blob.setBytes(1L, new byte[] {0}); | ||
| } finally { | ||
| if (blob != null) { | ||
| blob.close(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void clobFromResultSetGetter(ResultSet rs) throws SQLException { | ||
| Clob clob = null; | ||
| try { // Noncompliant | ||
| clob = rs.getClob("payload"); | ||
| clob.length(); | ||
| } finally { | ||
| if (clob != null) { | ||
| clob.close(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void arrayFromResultSetGetter(ResultSet rs) throws SQLException { | ||
| Array array = null; | ||
| try { // Noncompliant | ||
| array = rs.getArray(1); | ||
| array.getBaseType(); | ||
| } finally { | ||
| if (array != null) { | ||
| array.close(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void sqlxmlFromConnectionFactory(Connection connection) throws SQLException { | ||
| SQLXML xml = null; | ||
| try { // Noncompliant | ||
| xml = connection.createSQLXML(); | ||
| xml.setString("<root/>"); | ||
| } finally { | ||
| if (xml != null) { | ||
| xml.close(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void blobFromCallableStatement(CallableStatement cs) throws SQLException { | ||
| Blob blob = null; | ||
| try { // Noncompliant | ||
| blob = cs.getBlob(1); | ||
| blob.length(); | ||
| } finally { | ||
| if (blob != null) { | ||
| blob.close(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void clobFromConnectionFactory(Connection connection) throws SQLException { | ||
| Clob clob = null; | ||
| try { // Noncompliant | ||
| clob = connection.createClob(); | ||
| clob.length(); | ||
| } finally { | ||
| if (clob != null) { | ||
| clob.close(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void nclobFromConnectionFactory(Connection connection) throws SQLException { | ||
| NClob nclob = null; | ||
| try { // Noncompliant | ||
| nclob = connection.createNClob(); | ||
| nclob.length(); | ||
| } finally { | ||
| if (nclob != null) { | ||
| nclob.close(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void arrayFromConnectionFactory(Connection connection) throws SQLException { | ||
| Array array = null; | ||
| try { // Noncompliant | ||
| array = connection.createArrayOf("VARCHAR", new String[] {"a", "b"}); | ||
| array.getBaseType(); | ||
| } finally { | ||
| if (array != null) { | ||
| array.close(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void nclobFromResultSetGetter(ResultSet rs) throws SQLException { | ||
| NClob nclob = null; | ||
| try { // Noncompliant | ||
| nclob = rs.getNClob("payload"); | ||
| nclob.length(); | ||
| } finally { | ||
| if (nclob != null) { | ||
| nclob.close(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void sqlxmlFromResultSetGetter(ResultSet rs) throws SQLException { | ||
| SQLXML xml = null; | ||
| try { // Noncompliant | ||
| xml = rs.getSQLXML("payload"); | ||
| xml.getString(); | ||
| } finally { | ||
| if (xml != null) { | ||
| xml.close(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void compliantTryWithResources(Connection connection) throws IOException, SQLException { | ||
| try (Process p = new ProcessBuilder("ls").start(); | ||
| Blob blob = connection.createBlob()) { | ||
| p.waitFor(); | ||
| blob.setBytes(1L, new byte[] {0}); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| } | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.