talideon.com

Later, when I was released... spiritually, I mean...

November 29, 2004 at 2:24PM I’ve finally found an excuse to use reflection in Java.

I’ve a project here at work. I need to write a ColdFusion custom tag in Java to talk to a remote server. This tag needs to handle a bunch of different requests and responses, but putting all that code in the one class would be a bit messy, and then there’s the problem of what happens when I need to add new handler code to it.

So I’m using this as an excuse to use the Java Reflection API for the first time. It’s going to make the job of implementing all this much simpler, and I’m not just doing it just because I can.

The handlers simply register themselves with the tag class, which acts as a dispatcher, informing it of who it is (with a Class object), and what message it can handle. Then when the tag gets a request or needs to handle a response, it simply looks up the message to get back the corresponding class, and creates and instance (though I might do pooling later if I find a good reason), and pushes all the hard work onto the handler class.

This will rock!

Technorati Search Technorati Search Irish Bloggers

Comments

1 On February 15, 2005 at 11:33, Revence 27 wrote:

And I also found an excuse to use something similar in my ASP!

2 On February 15, 2005 at 16:17, Keith wrote:

You do know that neither ASP nor any of the major languages used with it have a reflection API? Ok, you might be able to fiddle around it in JScript, but I wouldn’t envy you.

The use of reflection as I described turned out to be superfluous, so I got rid of it in the end. Now I’m just doing some much simpler stuff with Class.forName().

Post a comment

All form information is optional, but it’s a good idea to fill in your name and email address if you want me to take your comment seriously.

Spammers, don’t bother posting crap down here. The site is set up so that legitimate search engines (Google, for instance) won’t index pages with comments on them. Posting crud here only means you’re wasting my time and patience. Shoo!

Real names, please. Please include!
Won’t be displayed. Please include!
Displayed, if present.