11

I have a next code:

private Stream<Field> getStreamWithAccessibleFields(final Object object) {
    return Arrays.stream(object.getClass()
            .getDeclaredFields()).peek(field -> field.setAccessible(true));
}

Sonar throws me an issue: [MINOR] Close this "Stream". squid:S2095. Can anybody give an advice, how I can handle this problem?

azurefrog
  • 10,785
  • 7
  • 42
  • 56
Sergey Zim
  • 143
  • 1
  • 5
  • So Sonar does a double fault. First, there are streams which might not require closing, second, closing a resource that is returned to the caller would be wrong for any kind of `AutoCloseable`… – Holger Apr 29 '16 at 15:15
  • Still, I’d prefer the bulk operation for efficiency: `Field[] fields=object.getClass() .getDeclaredFields(); AccessibleObject.setAccessible(fields, true); return Arrays.stream(fields);` – Holger Apr 29 '16 at 15:17

1 Answers1

11

This is a false positive that is already fixed and soon to be released with SonarQube Java 3.14.

For further reference, please check SONARJAVA-1478.