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

Factory methods should be used when available

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Use a factory method to create this "xxx".
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way
    • Targeted languages:
      Java
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      20min
    • FindBugs:
      XFB_XML_FACTORY_BYPASS

      Description

      The use of factory methods lets you abstract the job you need to do from the specific tool implementations needed to do it with, and helps insulate you from changes.

      This rule raises an issue when instances of these are instantiated directly:

      • javax.xml.parsers.DocumentBuilder
      • javax.xml.parsers.SAXParser
      • javax.xml.transform.Transformer
      • org.xml.sax.XMLReader
      • org.xml.sax.XMLFilter
      • org.w3c.dom.*

      Noncompliant Code Example

      DocumentBuilder builder = new DocumentBuilderImpl();  // Noncompliant
      

      Compliant Solution

      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = factory.newDocumentBuilder();
      
      

        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: