I’ve been recently been working with some undocumented features of ColdFusion. Unfortunately, because of a lack of documentation I’ve been feeling my way in the dark by using the trusty cfdump tag.
However, cfdump doesn’t produce all that much useful information when you dump Java objects. You get the package and class name, its methods (in an unintelligible form), and its fields. You don’t get the class’ superclass, its implemented interfaces, its constructors or any useful information on its methods.
If you’re lucky the class is one of the standard java classes and you can easily look the information up in the Javadocs. If you’re unlucky you have almost no information on the object and what it does.
That is, until now! I spent a good portion of today writing a CFC which uses reflection to inspect any arbitrary class. The CFC returns a structure of information on the class. I then went on to write a handy user interface for the CFC with Model-Glue. (If you go to the link, be sure try out a class name of coldfusion.sql.QueryTable. This is the class name for a ColdFusion Query.)
You can download the whole thing here. To use it you will need to download Model-Glue, extract it, and create a ColdFusion mapping to the Model-Glue folder.
The reflector.cfc is the heart of the application. It can be found under the /reflector/model/reflection directory.
Reflector.cfc provides the following methods:
Boolean isClassName(name) This returns a Boolean value indicating if the name argument is a class name.
Boolean isPackageName(name) This returns a Boolean value indicating if the name argument is a package name.
Array getAllPackageNames() This returns an array of all available packages. I’m not much use, but I’m here.
Struct getClassInfoFromObject(object) This method returns information on the object passed in. The object can actually be a primitive value, or any other object. This simply calls the getClassInfo(name) method.
Struct getClassInfo(name) This method returns information on the class identified by the name. This is case sensitive. This pools information from all of the following methods.
Array getSuperclasses(Class) This returns an array of names of super classes for the java.lang.Class object passed in.
Array getNestedClasses(Class) This returns an array of names of nested inner classes for the java.lang.Class object passed in.
Array getInterfaces(Class) This returns an array of structures describing interfaces which implemented by the java.lang.Class object passed in.
Array getFieldsInfo(Class) This returns an array of structures describing interfaces on the fields available in the java.lang.Class object passed in.
Array getConstructorsInfo(Class) This returns an array of structures describing constructors available in the java.lang.Class object passed in.
Array getMethodsInfo(Class) This returns an array of structures describing methods available in the java.lang.Class object passed in.
In general, you’ll probably only want to use the getClassInfo(name) method. Normally I would describe the exact structure of the struct. However, it’s time for me to take care of my son. I suggest you take a look at the source code for the reflection.cfc and how I use it in the Model-Glue user interface.
If enough people ask I’ll document more details.
Comments on: "ColdFusion Java Object Reflector" (4)
Hi Doug,
I wrote something similar a year or two back:
http://www.spike.org.uk/projects/junk/classdumper/classdumper.cfm
If you put a fully qualified class name in the text box it will give you a bunch of summary info for the methods in that class.
I use it quite a bit 🙂
LikeLike
I have used the following UDF in the past to interospect a Java Class.
http://cflib.org/udf.cfm?ID=1076
LikeLike
Dough, you should change line 16 in Controller.cfc to:
<cfset event.setValue("applicationMapping", GetModelGlue().getConfigSetting("applicationMapping")) />
LikeLike
My ClassViewer can view not only local objects but you can load custom java objects remotely using my classloader.
http://www.cfide.org/classViewer.cfm
LikeLike