Search code examples
javaintellij-ideatry-with-resources

How can I use intellij to refactor a field into a try-with-resources


This question is similar to In IntelliJ IDEA, how to surround with try-with-resources? but the difference is, I have a field and not a local variable. My code looks like this:

    preparedStatement = connect.prepareStatement(sqlQuery);
    preparedStatement.setInt(1, 1);
    preparedStatement.setString(2, "foo");
    preparedStatement.setString(3, "bar");
    preparedStatement.setString(4, "baz");
    preparedStatement.setDouble(5, 0);
    preparedStatement.setDouble(6, 0);
    preparedStatement.setString(7, foovar);
    preparedStatement.setString(8, barvar);
    preparedStatement.executeUpdate();

    preparedStatement = connect.prepareStatement(sqlQuery);
    preparedStatement.setInt(1, 1);
    preparedStatement.setString(2, "foo2");
    preparedStatement.setString(3, "bar2");
    preparedStatement.setString(4, "baz2");
    preparedStatement.setDouble(5, 0);
    preparedStatement.setDouble(6, 0);
    preparedStatement.setString(7, foovar2);
    preparedStatement.setString(8, barvar2);
    preparedStatement.executeUpdate();

I would like to wrap each one of these so it looks like this::

    try (PreparedStatement preparedStatement = connect.prepareStatement(sqlQuery)) {
        preparedStatement = connect.prepareStatement(sqlQuery);
        preparedStatement.setInt(1, 1);
        preparedStatement.setString(2, "foo");
        preparedStatement.setString(3, "bar");
        preparedStatement.setString(4, "baz");
        preparedStatement.setDouble(5, 0);
        preparedStatement.setDouble(6, 0);
        preparedStatement.setString(7, foovar);
        preparedStatement.setString(8, barvar);
        preparedStatement.executeUpdate();
}

    try (PreparedStatement preparedStatement = connect.prepareStatement(sqlQuery)) {
        preparedStatement = connect.prepareStatement(sqlQuery);
        preparedStatement.setInt(1, 1);
        preparedStatement.setString(2, "foo2");
        preparedStatement.setString(3, "bar2");
        preparedStatement.setString(4, "baz2");
        preparedStatement.setDouble(5, 0);
        preparedStatement.setDouble(6, 0);
        preparedStatement.setString(7, foovar2);
        preparedStatement.setString(8, barvar2);
        preparedStatement.executeUpdate();
}

And I'd really like to do this by only using IDE refactoring shortcuts. Is this possible? The most challenging part, in my mind, is turning this field into a local variable. Once I've done that, I think I could figure out the rest.


Solution

  • I don't believe this is possible, since this refactoring can have adverse effects for any other user of the connection.

    Given that your connection is at a higher scope than method, it stands to reason that there may be other downstream users of this connection instance. With that in mind, if there is the possibility of other downstream users, closing the connection from underneath their feet would result in unexpected or aberrant behavior.

    IntelliJ is declaring here that its code analyzer cannot reliably guarantee that this variable isn't being used by any other downstream consumer. You may want to request this as a feature.

    I personally like it not allowing this to happen, since it saves the developer from doing something incredibly stupid without them realizing it.