Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-2212

Method references should be used

    XMLWordPrintable

    Details

    • Type: Finding Detection
    • Status: Closed
    • Resolution: Duplicate
    • Labels:
    • Message:
      Use "xxx" instead of this lambda.
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Targeted languages:
      Java
    • Constant Cost:
      10min

      Description

      Method references are specialized lambda expressions for methods that already have a name. They take the form of objectReference::methodName or ClassName::methodName. When a lambda does nothing but call an existing method, it can be much clearer to use a method reference instead.

      Noncompliant Code Example

      Set roster = transferElements(rosterSource, 
            () -> { return new HashSet<>(); }  // Noncompliant
      );
      

      Compliant Solution

      Set roster = transferElements(rosterSource, HashSet::new);
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              ann.campbell.2 Ann Campbell
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: