I was chatting with a coworker today about random ColdFusion topics, and we ended up talking about the Java that underlies CF. He pointed me towards a few good resources for "peeking under the hood" that really stood out.

Simon Whatley's blog entry about finding out what makes up CF arrays and structures makes use of Java reflection to follow the inheritance path for those two object types. After reading it, I decided to write up a quick function for dumping the full class hierarchy of any Java object.

Note: Other UDFs do exist that do this, including the rather complete getJavaMetaData() function on CFLib.

view plain print about
1function getClassHierarchy( obj ) {
2    var local = structNew();
3    local.hierarchy = arrayNew(1);
5    local.currentClass = arguments.obj.getClass();
6    arrayAppend(local.hierarchy, local.currentClass.getName());
8    /* All java objects ultimately extend java.lang.Object, so stop once we reach it. */
9    while (local.currentClass.getName() NEQ "java.lang.Object") {
10        local.currentClass = local.currentClass.getSuperClass();
11        arrayAppend(local.hierarchy, local.currentClass.getName());
12    }
14    return local.hierarchy;

The getClass() and getSuperClass() methods are the heavy lifters of the Java reflection API. The getSuperClass() method allows us to traverse up the inheritance chain until we reach java.lang.Object, the base class for all Java objects.

The results of this function are very informative. For example, the class hierarchy for a ColdFusion struct (using ColdFusion 7.0.2) is:

  • coldfusion.runtime.Struct
  • coldfusion.util.FastHashtable
  • java.util.Hashtable
  • java.util.Dictionary
  • java.lang.Object

Just a little peek into what makes CF tick.