Standard Analyses

SemmleCode Professional comes with a wide range of pre-packaged analyses, from architectural properties and metrics, to statement-level checks for likely bugs and violations of best practice.


Wide Class Hierarchies

This query shows the inheritance graph of classes that have more than four direct subclasses. Library classes and generated types are excluded from the statistics; anonymous classes do not count towards the total number of direct subclasses.

How to Interpret the Query Results

Running the query on the source code of the JBoss application server yields the following result, shown as a graph.

The screen shot is an extract of the class hierarchy of a class called ServiceMBeanSupport, which has no less than 117 direct subclasses.

How to Address the Query Results

Very wide class hierarchies can be an indication of sloppy object-oriented design. To increase encapsulation and ease extensibility, it might be better to introduce additional abstractions into the hierarchy.

Source Code
import default

/** a non-library, non-generated class that has more than four direct subclasses,
    excepting anonymous classes */
class WideHierarchyRoot extends Class {
  WideHierarchyRoot() { 
    this.fromSource() and
    not this instanceof GeneratedClass and
    count(RefType sub | sub = this.getASubtype() and 
                        not sub instanceof AnonymousClass) > 4
  }
}

from Class s, Class t
where t.getASupertype*() instanceof WideHierarchyRoot and
      t.fromSource() and
      s = t.getASubtype() and
      s.fromSource()
select t, t.getASubtype()
References