Thursday, June 19, 2003

Iterating over an Enumeration with CFMX

I tried writing some CFML code to iterate over a java.util.Enumeration, a fairly common thing to do in java, but was plagued with java.lang.IllegalAccessException's.

IllegalAccess exceptions usually occurr when you try to call a method using reflection (which CFMX does) that doesn't exist. However I'm quite sure that hasMoreElements() is a method of java.util.Enumeration. In fact if I cfdump the propNames variable the hasMoreElements method shows up! Here's the code I'm using:

<cfset system = CreateObject("java", "java.lang.System")>
<!--- properties is a java.util.Properties object --->
<cfset properties = system.getProperties()>

<!--- propNames is a java.util.Enumeration --->
<cfset propNames = properties.propertyNames()>

<cfoutput>
 <cfloop condition="propNames.hasMoreElements()">
	<cfset propName = propNames.nextElement()>
	#propName# = #system.getProperty(propName)#<br />
 </cfloop>
</cfoutput>

I suspect the problem has something to do with CFMX not realizing that propNames is an Enumeration, I wish that JavaCast accepted more types besides the primitives.

Here's the stack trace I'm receiving, I'm using CFMX U3:

java.lang.IllegalAccessException
	at java.lang.reflect.Method.invoke(Native Method)
	at coldfusion.runtime.StructBean.invoke(Unknown Source)
	at coldfusion.runtime.CfJspPage._invoke(Unknown Source)
	at Statement12.evaluate(Unknown Source)
	at coldfusion.runtime.CFPage.evaluateCondition(Unknown Source)
	at cftesta2ecfm779424527.runPage(C:\web\testa.cfm:19)
	at coldfusion.runtime.CfJspPage.invoke(Unknown Source)
	at coldfusion.tagext.lang.IncludeTag.doStartTag(Unknown Source)
	at coldfusion.filter.CfincludeFilter.invoke(Unknown Source)
	at coldfusion.filter.ApplicationFilter.invoke(Unknown Source)
	at coldfusion.filter.PathFilter.invoke(Unknown Source)
	at coldfusion.filter.LicenseFilter.invoke(Unknown Source)
	at coldfusion.filter.ExceptionFilter.invoke(Unknown Source)
	at coldfusion.filter.BrowserDebugFilter.invoke(Unknown Source)
	at coldfusion.filter.ClientScopePersistenceFilter.invoke(Unknown Source)
	at coldfusion.filter.BrowserFilter.invoke(Unknown Source)
	at coldfusion.filter.GlobalsFilter.invoke(Unknown Source)
	at coldfusion.filter.DatasourceFilter.invoke(Unknown Source)
	at coldfusion.CfmServlet.service(Unknown Source)
	at jrun.servlet.ServletInvoker.invoke(ServletInvoker.java:91)
	at jrun.servlet.JRunInvokerChain.invokeNext(JRunInvokerChain.java:42)
	at jrun.servlet.JRunRequestDispatcher.invoke(JRunRequestDispatcher.java:226)
	at jrun.servlet.ServletEngineService.dispatch(ServletEngineService.java:527)
	at jrun.servlet.http.WebService.invokeRunnable(WebService.java:172)
	at jrunx.scheduler.ThreadPool$DownstreamMetrics.invokeRunnable(ThreadPool.java:348)
	at jrunx.scheduler.ThreadPool$ThreadThrottle.invokeRunnable(ThreadPool.java:451)
	at jrunx.scheduler.ThreadPool$UpstreamMetrics.invokeRunnable(ThreadPool.java:294)
	at jrunx.scheduler.WorkerThread.run(WorkerThread.java:66)

I may end up reporting this one to Macromedia as a bug, unless I'm missing something here.