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

The diamond operator ("<>") should be used

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Replace the type specification in this constructor call with the diamond operator ("<>"). [(sonar.java.source not set. Assuming 7 or greater.)]
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way
    • Covered Languages:
      Java
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      1min
    • Analysis Scope:
      Main Sources, Test Sources
    • Implementation details:

      Description

      Java 7 introduced the diamond operator (<>) to reduce the verbosity of generics code. For instance, instead of having to declare a List's type in both its declaration and its constructor, you can now simplify the constructor declaration with <>, and the compiler will infer the type.

      Note that this rule is automatically disabled when the project's sonar.java.source is lower than 7.

      Noncompliant Code Example

      List<String> strings = new ArrayList<String>();  // Noncompliant
      Map<String,List<Integer>> map = new HashMap<String,List<Integer>>();  // Noncompliant
      

      Compliant Solution

      List<String> strings = new ArrayList<>();
      Map<String,List<Integer>> map = new HashMap<>();
      

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated: