Pete Freitag Pete Freitag

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()>

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

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:

	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(
	at jrun.servlet.JRunInvokerChain.invokeNext(
	at jrun.servlet.JRunRequestDispatcher.invoke(
	at jrun.servlet.ServletEngineService.dispatch(
	at jrun.servlet.http.WebService.invokeRunnable(
	at jrunx.scheduler.ThreadPool$DownstreamMetrics.invokeRunnable(
	at jrunx.scheduler.ThreadPool$ThreadThrottle.invokeRunnable(
	at jrunx.scheduler.ThreadPool$UpstreamMetrics.invokeRunnable(

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

Like this? Follow me ↯

Iterating over an Enumeration with CFMX was first published on June 19, 2003.

FuseGuard Web App Firewall for ColdFusion

The FuseGuard Web Application Firewall for ColdFusion & CFML is a high performance, customizable engine that blocks various attacks against your ColdFusion applications.


Hi Pete,

It's a CF5 and CFMX6 bug that others have also found. I know now workaround. But you can upgrade to CFMX6.1. You might still sometimes find the bug, but iterating over props.propertyNames() and props.keys() works for me.

ps = CreateObject("java", "java.util.Properties");
ps.setProperty("clau", "valor");
ps.setProperty("clau1", "valor1");
ps.setProperty("clau2", "valor2");
ps.setProperty("clau3", "valor3");

// funciona en CFMX6.1
keys = ps.keys();
while (keys.hasMoreElements()){
k = keys.nextElement();
v = ps.getProperty(k);
WriteOutput(k & " = " & v & "<br/>");
by Alexander Bunkenburg on 02/04/2004 at 10:59:47 AM UTC
This code now works as of CFMX 6.1
by Pete Freitag on 10/29/2004 at 2:56:48 PM UTC