Details

    • Type: Sub-task
    • Status: Closed
    • Priority: Minor
    • Resolution: Implemented
    • Affects Version/s: Trunk
    • Fix Version/s: Upcoming Release
    • Component/s: base
    • Labels:
      None

      Description

      • ComparableRange.java:70, HE_EQUALS_USE_HASHCODE

      HE: org.apache.ofbiz.base.lang.ComparableRange defines equals and uses Object.hashCode()

      This class overrides equals(Object), but does not override hashCode(), and inherits the implementation of hashCode() from java.lang.Object (which returns the identity hash code, an arbitrary value assigned to the object by the VM). Therefore, the class is very likely to violate the invariant that equal objects must have equal hashcodes.

      If you don't think instances of this class will ever be inserted into a HashMap/HashTable, the recommended hashCode implementation to use is:

      public int hashCode()

      { assert false : "hashCode not designed"; return 42; // any arbitrary constant will do }
      • ComparableRange.java:76, REC_CATCH_EXCEPTION
        REC: Exception is caught when Exception is not thrown in org.apache.ofbiz.base.lang.ComparableRange.equals(Object)

      This method uses a try-catch block that catches Exception objects, but Exception is not thrown within the try block, and RuntimeException is not explicitly caught. It is a common bug pattern to say try

      { ... }

      catch (Exception e)

      { something }

      as a shorthand for catching a number of types of exception each of whose catch blocks is identical, but this construct also accidentally catches RuntimeException as well, masking potential bugs.

      A better approach is to either explicitly catch the specific exceptions that are thrown, or to explicitly catch RuntimeException exception, rethrow it, and then catch all non-Runtime Exceptions, as shown below:

      try

      { ... }

      catch (RuntimeException e)

      { throw e; }

      catch (Exception e)

      { ... deal with all non-runtime exceptions ... }

        Activity

        Hide
        jacques.le.roux Jacques Le Roux added a comment -

        Thanks Dennis,

        Your patch is in trunk at revision: 1804556

        Show
        jacques.le.roux Jacques Le Roux added a comment - Thanks Dennis, Your patch is in trunk at revision: 1804556
        Hide
        Dennis Balkir Dennis Balkir added a comment -
        • implemented method hashCode() because method equals() was implemented
        • changed Exception to RuntimeException because no exception was thrown and the only occuring exception coud be a runtimeexception
        Show
        Dennis Balkir Dennis Balkir added a comment - implemented method hashCode() because method equals() was implemented changed Exception to RuntimeException because no exception was thrown and the only occuring exception coud be a runtimeexception

          People

          • Assignee:
            jacques.le.roux Jacques Le Roux
            Reporter:
            Dennis Balkir Dennis Balkir
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development