Menu

#1370 false positive for unused method using method references in java 8

3.x
closed-duplicate
None
5
2018-08-16
2015-03-22
No

findbugs complains incorrectly that a private static method is not used. However this method is actually called using the new method reference syntax of Java 8. The workaround here is simple: replace the method reference with an actual method call. Example:

    public List<String> readLocations(final List<String> inputs) {
        return inputs.stream()
                .map(MyClass::createPath)
                // here comes more code..;
    }

    private static Path createPath(final String input) {
        // doesn't matter
        return null;
    }

The workaround replaces

.map(MyClass::createPath)

with

.map(input -> createPath(input))

Discussion

  • Tagir Valeev

    Tagir Valeev - 2015-03-28
    • status: open --> open-works-for-me
    • assigned_to: Tagir Valeev
     
  • Sebastian Hoß

    Sebastian Hoß - 2015-03-29

    You are right! I was using version 3.0.0 of the findbugs-maven-plugin. Switching to version 3.0.1 which was just released, fixes this problem. Thanks a lot!

     
  • Tagir Valeev

    Tagir Valeev - 2015-03-29
    • status: open-works-for-me --> closed-duplicate
     
  • SRINIVAS MACHERLA

    In case of overloaded methods still findbugs reports same error. Its working only after I change the method name.

    For below scenario, findbug reports UPM_UNCALLED_PRIVATE_METHOD.

        public List<String> readLocations(final List<String> inputs) {
            return inputs.stream()
                    .map(MyClass::createPath)
                    // here comes more code..;
        }
    
        private Path createPath(final String input) {
            // doesn't matter
            return createPath(input, 0);
        }
    
    private Path createPath(final String input, final int d) {
            // doesn't matter
            return null;
        }
    

    I'm able to unblock my self changing createPath(final String input) method name.

     

    Last edit: SRINIVAS MACHERLA 2018-08-16

Log in to post a comment.

MongoDB Logo MongoDB