try-with-resources - incorrect can have 'final' modifier

Answered

When using try-with-resource IntelliJ recommends using a final modifier on the variable even though the variable is already final because of the try-by-resource.

When using final, IntelliJ doesn't complain, but Checkstyle correctly gives out a warning. Is there a way to disable/fix this behavior?

Example code:

public void init() {
try (InputStreamReader reader = fileProvider.getFileReader()) {
final CSVParser parser;
parser = CSVFormat.DEFAULT.withDelimiter(';').parse(reader);

for (final CSVRecord record : parser) {
final String attributeName = record.get(0);
final String allwedValuesString = record.get(1);
final String[] allowedValues = allwedValuesString.split(",");
allowedValuesMap.put(attributeName, Arrays.asList(allowedValues));
}
} catch (final Exception ex) {
LogHelper.putMDC(MDC_KEY_FILENAME, fileProvider.getFileName());
LogHelper.error(LOGGER, "Error occurred while loading file", ex);
LogHelper.removeMDC(MDC_KEY_FILENAME);
throw new RuntimeException("Error occurred while loading file " + fileProvider.getFileName(), ex);
}
}

0
2 comments

Hello,

Current behaviour seems to be intended, please see the issue:

https://youtrack.jetbrains.com/issue/IDEA-182740

Does disabling "report variables which are implicit finals" option of the inspection (Java | Code style issues | Local variable or parameter can be final) solve the issue for you?

1
Avatar
Permanently deleted user

Thanks, that helped indeed

0

Please sign in to leave a comment.