Friday, October 04, 2002

How CFMX creates the filename for the compiled version of your CF code

In previous bog entries I've talked about compilation and precompilation of CFMX code into Java. And I've shared how all CF templates get compiled into the one [cfusionmx]\wwwroot\WEB-INF\cfclasses directory. 

Ever wonder how CF creates that file name? A CF template named Setsession.cfm might lead to a class file named cfsetsession2ecfm1011928409.class.  The "cf" is always there, while the next part is the file name and extension, but with "2e" where the period in the filename was. The number that follows represents a hash of the file/folder name.

Here are the details from Mike Nimer at Macromedia:
How CFMX creates the .class name, or how you can find the name of the .class file you want to delete

The formula is: "cf" + filename + hashCode

The fully canonicalized filename is used, with "/" as the directory separator. The following substitutions are made in the filename:
1.) / becomes __
2.) any other character which would be illegal in a Java identifier is represented as 2 hex digits

hashCode is the hashCode() of the File object which represents this file as documented at

If the hashCode() is negative, it is exclusive-OR'd with 0xFFFFFFFF to get the value used by ColdFusion.
It seems that hashcode call uses the filename AND the folder, as even two files in the same directory could end up with hashcode values that are different. 

You can prove this by creating two templates in the same folder, with distinctive names that you can readily identify, and then save and execute those pages, and view the latest class files created in the cfclasses folder. Those distinctively named files in the same folder may show having the same or different hashcodes. 

So sadly, this is like a one-way hash: cf can figure out what file exists in the cfclasses for a given filename/folder it wants to run, but we can't look at that hashcode and figure out what folder that file was in. At least we can see the name: though again you may well see many classes with the same name, for different folders in which a given filename may live. 

And note that for components (CFCs), note that there is a class for each method in that cfc (technically there is also a class for each cffunction in a cfm, as well). So you could have far more class files than you may expect if you somehow had an idea of how many cfm templates you had. :-)

No comments: