Ask Ben: Instantiating Nested Java Classes In ColdFusion

Posted October 1, 2008 at 9:58 AM by Ben Nadel

Tags: ColdFusion, Ask Ben

I'm trying to create an instance of: java.awt.geom.Point2D.Double in coldfusion:

http://java.sun.com/j2se/1.5.0/docs/api/java/awt/geom/Point2D.html

Point2D.Double is a nested class inside of the abstract class Point2D. I have tried to instantiate the class, but get, "This fails because Coldfusion cannot find the class." And which does not work because Point2D is an abstract class and there is not a public constructor on which you can call PointClass.init(x,y). Right now, I've resorted to making my own Point class that wraps the Point2D.Double class so that I can instantiate it in Coldfusion. I don't think this is ideal and am looking for ideas about how to directly create a Point2D.Double class in Coldfusion. I'm also using Coldfusion 8.

Working with nested Java classes in ColdFusion is easy, as long as you know the secret hand shake (which is kept very hush hush). If you try to instantiate the nested classes using dot-notation in ColdFusion, as you have tried:

  • <!--- Create the point object. --->
  • <cfset objPoint = CreateObject(
  • "java",
  • "java.awt.geom.Point2D.Double"
  • ).Init(
  • JavaCast( "double", 5 ),
  • JavaCast( "double", 10 )
  • )
  • />

... you will get the following error:

Object Instantiation Exception. Class not found: java.awt.geom.Point2D.Double

The trick here is that when referring nested classes in the class path, you have to use the dollar sign ($) rather than the dot (.):

java.awt.geom.Point2D$Double

Knowing that, when we run this code:

  • <!--- Create the point object. --->
  • <cfset objPoint = CreateObject(
  • "java",
  • "java.awt.geom.Point2D$Double"
  • ).Init(
  • JavaCast( "double", 5 ),
  • JavaCast( "double", 10 )
  • )
  • />
  •  
  • <!--- Output the point string. --->
  • <cfdump var="#objPoint.ToString()#" />

... we get the following output:

Point2D.Double[5.0, 10.0]

... which indicates that the Point class was proparly instantiated and utilized. Hope that helps.



Reader Comments

Oct 1, 2008 at 10:33 AM // reply »
12 Comments

Thanks for the tip, Ben. Definitely useful.


Oct 1, 2008 at 10:35 AM // reply »
48 Comments

I didn't learn this trick until Mark Mandel showed it to me while I was at CFUnited this year (chatting him up on IM since he didn't make the trip). It is a huge life saver when you finally figure it out, and I had been meaning to blog it for a while now. Thanks for putting it out there for those that may not know it!


Oct 1, 2008 at 10:51 AM // reply »
10,638 Comments

@Todd,

Glad to help out with spreading the word :) I think I only found out when I first starting working with POI and realized that the available font Colors were nested classes. Before that, I had never seen this syntax.


Oct 1, 2008 at 12:35 PM // reply »
3 Comments

I heard that CF9 was going to support some sort of import when working with components the same as you can do in java when working with packages. I wonder how things like this will be effected.


Oct 1, 2008 at 1:14 PM // reply »
2 Comments

Thank you, thank you, thank you.

I've been looking for this for ages.


Oct 2, 2008 at 12:54 AM // reply »
132 Comments

This isn't so much a secret, it's rather just how Java inner and anonymous classes are implemented. To be compatible with older versions of Java, when they added the nested classes feature, they decided to just name mangle the classes and generate accessor functions to simulate accessing variables you didn't really have access to.

Essentially, when you nest classes, the compiler renames your nested class as the root class, with the nested class path separated by dollar signs.

org.my.company.AwesomeList$Node$Element$0

That's a class AwesomeList with a nested class Node, that has a nested class Element, that has an anonymous class inside it.

If your nested class accessed a private variable the compiler might generate a function public varname$0() or some such and implicitly call it whenever you accessed that variable in your code.

Technically you could name your own classes and methods this way with dollar signs, but that'd be bad since the dollar is reserved for code generation, and now used for inner classes.

This talks more about this:
<http://www.retrologic.com/innerclasses.doc7.html>

Java's full of these quirky implementation choices to maintain backwards compatibility. For instance, generics are also implemented using a compiler trick (type erasure).


Oct 2, 2008 at 12:40 PM // reply »
2 Comments

Elliot,

That may well be. But I'm not interested in Java except what it gives me withing CF. Same with any other component or tool. Give me the API that I can use within CF. If it doesn't have it, or if I have to spend weeks hunting for it, or spend weeks learning an entirely new language to do something that shouldn't take more than 20 minutes to do, it's not useful.

It may not be a secret to Java programmers, but I'm not one, nor am I interested in being one. C++ has gotten me anywhere I need to go.


Sep 4, 2009 at 4:22 PM // reply »
6 Comments

How did I not know about this until today?!? Man, you saved my bacon today Ben. Of course I had to do the whole banging-my-head-into-a-wall thing for about 6 hours before finding it!


Sep 6, 2009 at 10:39 AM // reply »
10,638 Comments

@Dave,

Ha ha ha ha, I hope I saved you some brain cells :)


Oct 5, 2010 at 12:25 AM // reply »
1 Comments

I kknow this post is old, but I would like to say thanks for pointing this out!


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 3, 2012 at 10:49 PM
How I Got Node.js Running On A Linux Micro Instance Using Amazon EC2
Wow this was really helpful! Only thing I would add is you need to update your .bash_profile after you edit the secure_path. This is what I did: $ . ~/.bash_profile Otherwise, NPM won't be found. ... read »
Feb 3, 2012 at 10:14 PM
Pushing Base64-Encoded Images Over HTML5 WebSockets With Pusher And ColdFusion
@Ben, Just wanted to let you know that pusher are soon to start limiting sizes on messages. This was the detail that came through in the Feb dispatch: "However, we will soon be limiting the s ... read »
Feb 3, 2012 at 5:05 PM
Regular Expressions Make CSV Parsing In ColdFusion So Much Easier (And Faster)
I tried using your RegEx in my C# program, but it was matching an extra empty-string at the end and so I would end up with an extra field that doesn't exist, so I changed it to this: (^|,)("(?: ... read »
Feb 3, 2012 at 3:47 PM
ColdFusion Supports HTTP Verbs PUT And DELETE (As Well As GET And POST)
Josh Cyr posted this on Twitter just a little bit ago. Thought it was appropriate. http://stackoverflow.com/questions/1619152/how-to-create-rest-urls-without-verbs/1619677#1619677 ... read »
Feb 3, 2012 at 2:28 PM
Changing The Execution Context Of Your Self-Executing Function Blocks In JavaScript
@Michael, You definitely make a good point (and extra points for quoting movies - I love movies). When you use a return() statement to define the object's public API, it does provide a consistent a ... read »
Feb 3, 2012 at 2:04 PM
Changing The Execution Context Of Your Self-Executing Function Blocks In JavaScript
To quote Jurassic Park: "Just because you can doesn't mean you should". I completely, utterly disagree with the thought that this is more readable. Consider the current module pattern: if ... read »
Feb 3, 2012 at 1:10 PM
REST API Design Rulebook By Mark Masse
@Jordan, Yeah, WRML was created by Mark Masse (author of the book). I also found it to be a bit convoluted. I suppose it is intended to allow the Client to be able to programmaticaly respond to cha ... read »
Feb 3, 2012 at 1:08 PM
ColdFusion Supports HTTP Verbs PUT And DELETE (As Well As GET And POST)
@Jason, To be honest, I don't have good answers for that kinds of stuff. And, to the point, that is specifically why I *really* liked the REST API Design Rulebook by Mark Masse - he just cuts throu ... read »