Tuesday, 7 August 2012

ClassViewer.cfc

G'day
This is just a quick follow-up to my previous entry about ClassViewer.java.



Here's the CF version:


/**
ClassViewer.cfc
@hint An almost pure CFML implementation of ClassViewer.class (see http://adamcameroncoldfusion.blogspot.co.uk/2012/08/classviewerjava.html).
@description
*/
component {
// these are just for formatting the output
variables._NL = createObject("java", "java.lang.System").getProperty("line.separator");
variables._INDENT = " ";
// Couldn't do it ALL with CFML, unfortunately. Need these to inspect the classes/objects
variables.class = createObject("java", "java.lang.Class");
variables.modifier = createObject("java", "java.lang.reflect.Modifier");
/**
@hint Returns a string containing information on the specified Java class. The string returned is best output within PRE tags.
@className A Java class name to return reflection data of.
*/
public String function viewClassByName(String className){
return viewClass(variables.class.forName(arguments.className));
}
/**
@hint Returns a string containing information on the specified object. The string returned is best output within PRE tags.
@object A Java Object.
*/
public String function viewObject(any object){
return viewClass(arguments.object.getClass());
}
/**
@hint Returns a string containing information on the specified class. The string returned is best output within PRE tags.
@class A Java Class object.
*/
public String function viewClass(any class){
var parentClass = arguments.class.getSuperclass();
// do the basic class modifiers like ""public static class MyClass""
var out = variables.modifier.toString(arguments.class.getModifiers()) & " class " & arguments.class.getName() & variables._NL;
var indent = variables._INDENT;
// now all the classes this one extends, in a slightly progessively indented fashion
var bExtends = true;
while (bExtends){
try {
var extends = parentClass.getName();
out &= indent & "extends " & extends & variables._NL;
parentClass = parentClass.getSuperclass();
indent &= variables._INDENT;
} catch (any e){
bExtends = false;
}
}
// and any interfaces it implements
var interfaces = arguments.class.getInterfaces();
if (arrayLen(interfaces) > 0) {
out &= variables._INDENT & "implements ";
for (var i=1; i <= arrayLen(interfaces); i++) {
if (i != 1) {
out &= ", ";
}
out &= interfaces[i].getName();
}
out &= variables._NL;
}
out &= "{" & variables._NL;
// that's the end of the main class declaration, the rest is what's within the class itself, constructors, methods, public properties (all are well signposted below)
out &= variables._INDENT & "/*** CONSTRUCTORS ***/" & variables._NL;
var constructors = arguments.class.getConstructors();
for (var c=1; c <= arrayLen(constructors); c++) {
out &= variables._INDENT & inspectConstructor(constructors[c]) & variables._NL & variables._NL;
}
out &= variables._NL;
out &= variables._INDENT & "/*** METHODS ***/" & variables._NL;
methods = arguments.class.getMethods();
for (var m=1; m <= arrayLen(methods); m++) {
out &= variables._INDENT & inspectMethod(methods[m]) & variables._NL & variables._NL;
}
out &= variables._NL;
out &= variables._INDENT & "/*** FIELDS ***/" & variables._NL;
fields = arguments.class.getFields();
for (var f=1; f <= arrayLen(fields); f++) {
out &= variables._INDENT & variables.modifier.toString(fields[f].getModifiers()) & " " & fields[f].getType().getName() & " " & fields[f].getName() & variables._NL;
}
out &= variables._NL & "}";
return out;
}
/**
@method A Java Method object.
@hint Returns a string containing information on the specified method
*/
private String function inspectMethod(any method) {
var out = variables.modifier.toString(arguments.method.getModifiers()) & " " & arguments.method.getReturnType().getName() & " " & arguments.method.getName() & "(";
params = arguments.method.getParameterTypes();
for (var p=1; p <= arrayLen(params); p++) {
if (p != 1) {
out &= ", ";
}
out &= params[p].getName();
}
out &= ")";
var exceptions = arguments.method.getExceptionTypes();
if (arrayLen(exceptions) > 0) {
out &= variables._NL;
out &= variables._INDENT & "throws ";
for (var e=1; e <= arrayLen(exceptions); e++) {
if (e != 1) {
out &= ", ";
}
out &= exceptions[e].getName();
}
}
return out;
}
/**
@constructor A Java Constructor object.
@hint Returns a string containing information on the specified constructor
*/
private String function inspectConstructor(any constructor){
var out = variables.modifier.toString(constructor.getModifiers()) & " " & constructor.getName() & "(";
var params = constructor.getParameterTypes();
for (var p=1; p <= arrayLen(params); p++) {
if (p != 1) {
out &= ", ";
}
out &= params[p].getName();
}
out &= ")";
return out;
}
}
view raw ClassViewer.cfc hosted with ❤ by GitHub
<!---testCfCv.cfm --->
<cfset objToInspect = []>
<cfset cv = new ClassViewer()>
<pre>
<cfoutput>#cv.viewObject(objToInspect)#</cfoutput>
</pre>
view raw testCfCv.cfm hosted with ❤ by GitHub
The API docs for it are as follows:

shared.CF.data_types.cv.ClassViewer
Component ClassViewer 


An almost pure CFML implementation of ClassViewer.class.

hierarchy:WEB-INF.cftags.component
      shared.CF.data_types.cv.ClassViewer
path:C:\ColdFusion10\cfusion\wwwroot\shared\CF\data_types\cv\ClassViewer.cfc
serializable:Yes
properties:
methods:viewClass, viewClassByName, viewObject, inspectConstructor*, inspectMethod*
* - private method

viewClass
public String viewClass ( any class ) 

Returns a string containing information on the specified class. The string returned is best output within PRE tags.

Output:
Parameters:
   class: any, optional, class - A Java Class object. 
viewClassByName
public String viewClassByName ( String className ) 

Returns a string containing information on the specified Java class. The string returned is best output within PRE tags.

Output:
Parameters:
   className: String, optional, className - A Java class name to return reflection data of. 
viewObject
public String viewObject ( any object ) 

Returns a string containing information on the specified object. The string returned is best output within PRE tags.

Output:
Parameters:
   object: any, optional, object - A Java Object. 
inspectConstructor*
private String inspectConstructor ( any constructor ) 

Returns a string containing information on the specified constructor

Output:
Parameters:
   constructor: any, optional, constructor - A Java Constructor object. 
inspectMethod*
private String inspectMethod ( any method ) 

Returns a string containing information on the specified method

Output:
Parameters:
   method: any, optional, method - A Java Method object. 

Feel free to do with this what you will.

Cheers.

--
Adam