Friday, February 19, 2010

cfcommons Reflection API

what is reflection?

Reflection is the ability for a program to introspect details about itself (the source code), understand it and even modify it. 

is it possible in ColdFusion?

Yes, don't get it twisted :P

really, how?

In ColdFusion functions like  getComponentMetaData() and getMetaData() that will provide you with source level information such as method names, arguments name, type information, annotations, class hierarchy  etc. So when you call one of those methods you are essentially using reflection. You are asking the object to reflect upon itself and let you, the developer, know about it.

ok, but how is this powerful?

Simple - let's take cfcommon's lightweight automatic dependency injection plugin called Neodymium. The problem was how do we provide a lightweight automatic dependency injection plugin with these goals in mind:

  • It must be lighting FAST
  • Absolutely zero XML
  • It must be lighting FAST
  • Absolutely zero bean configuration
  • It must be lighting FAST
  • Absolutely no other feature other than dependency injection to bloat the code
  • It must be lighting FAST

By leveraging cfcommon's reflection API, at application start/reload we are able to to reflect upon all classes (cfc's) in a designated directory (package) check to see that if they have a type attribute associated to it then we will automatically inject those dependencies into the object factory. If there was a doNotInject annotation then we simply do nothing.

See how powerful that is? Extremely. Oh and did we mention it's freaking fast?

if ColdFusion already gives us the ability, why use cfcommons? 

Although ColdFusion's reflective functions are helpful, there is a lot of uneccessary code you need to write to get at specific information. cfcommon's Reflection API provides you with object oriented classes that help you get at what you need with one method call. Some examples:
  • Need to check if a class is annotated? use isAnnotated()
  • Need to get all annotated methods of a class? use getAnnotatedMethods()
  • Want to know if a method has an annotation? use hasAnnotation()
  • Need the superclass? use getSuperClass()

sky's is the limit

The goal of the cfcommons project is to push the limits forward for ColdFusion. It's a call to developers to think outside the box and to help provide stable, reusable  modules using a powerful platform. Reflection is just one of many modules available to you. 

So give it a try it! Can you think of a problem that you can solve via reflective methods?

i want more information!

A must read about Reflection API WITH examples - reflection documentation.


Brian Carr,  the mastermind behind cfcommons,  can always be reached via:
  • email :
  • twitter: @cfcommons
You can also reach me at or on twitter (@mickydionisio).

Thanks peeps!

No comments: