{"id":452,"date":"2003-07-17T22:23:03","date_gmt":"2003-07-18T02:23:03","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=452"},"modified":"2003-07-17T22:23:03","modified_gmt":"2003-07-18T02:23:03","slug":"java-collection-types-coldfusion","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2003\/07\/17\/java-collection-types-coldfusion\/","title":{"rendered":"Java Collection Types &amp; ColdFusion"},"content":{"rendered":"<p>At work one of our developers is programming an ecommerce store against a Java API written by a third party. Said Java API returns a <a href=\"http:\/\/java.sun.com\/j2se\/1.3\/docs\/api\/java\/util\/TreeMap.html\">TreeMap<\/a> object from a method and when you try to access that TreeMap object in any fashion, you&#8217;ll get an error, specifically:<\/p>\n<p><i>&#8220;Error casting an object of type java.lang.Integer to an incompatible type. This usually indicates a programming error in Java, although it could also mean you have tried to use a foreign object in a different way than it was designed.&#8221;<\/i><\/p>\n<p>What we&#8217;ve concluded is that ColdFusion attempts to cast any and all Java classes that implement the Map interface to:<\/p>\n<p>coldfusion.runtime.Struct<\/p>\n<p>which is of type <a href=\"http:\/\/java.sun.com\/j2se\/1.3\/docs\/api\/java\/util\/Hashtable.html\">java.util.Hashtable<\/a>.  Unfortunately, a TreeMap uses an <a href=\"http:\/\/java.sun.com\/j2se\/1.3\/docs\/api\/java\/lang\/Object.html\">Object<\/a> (in this case an Integer) as the key.  ColdFusion structures can only use strings as the key.  The takeaway is that this is a &#8216;feature&#8217; of ColdFusion, it&#8217;s nice that it attempts to transform collection type objects into the native ColdFusion structure type. In this specific case, it&#8217;s more of a hindrance.  The workaround is to create your own Java wrapper that takes the TreeMap, loops over all the keys, inserting each into a <a href=\"http:\/\/java.sun.com\/j2se\/1.3\/docs\/api\/java\/util\/Vector.html\">Vector<\/a> and then return the Vector to ColdFusion, which turns that into a structure.  <\/p>\n<p>In case you don&#8217;t feel comfortable going to sleep at night without seeing the code, here&#8217;s an example Java class that returns a simple TreeMap:<\/p>\n<p>import java.util.*;<br \/>\npublic class TreeMapTest {<br \/>\n&nbsp;&nbsp;&nbsp;public static TreeMap getTree() {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TreeMap t = new TreeMap();<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.put(new Integer(131000), &#8220;product&#8221;);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return t;<br \/>\n&nbsp;&nbsp;&nbsp;}<br \/>\n}<\/p>\n<p>Compile that and then drop it into your [cfusionmx]\\wwwroot\\WEB-INF\\lib\\ directory.  You can use this script to test it:<\/p>\n<p>&lt;cfscript&gt;<br \/>\n&nbsp;&nbsp;&nbsp;tmt = createObject(&#8220;java&#8221;, &#8220;TreeMapTest&#8221;);<br \/>\n&nbsp;&nbsp;&nbsp;WriteOutput(&#8220;tmt is of type &#8221; &amp; tmt.getClass());<br \/>\n&nbsp;&nbsp;&nbsp;treeMap = tmt.getTree();<br \/>\n&nbsp;&nbsp;&nbsp;WriteOutput(&#8220;treeMap is of type &#8221; &amp; treeMap.toString()); \/\/ you get an error here<br \/>\n&lt;\/cfscript&gt;<\/p>\n<p>I&#8217;d be very very interested in hearing about any ways that you know of to get around something like this without having to write a wrapper class.  And for you Macromedia ColdFusion guys, it might be a nice (albeit probably rarely used) feature to be able to maintain the Java type of an object by using some function, ( ie: maintainType() or some such).<\/p>\n<p>By the way, if you&#8217;re interested in knowing more about using Java and ColdFusion together, Terry Ford has written a great article here entitled <a href=\"http:\/\/www.macromedia.com\/devnet\/mx\/coldfusion\/articles\/java.html\">&#8220;Using Java and J2EE Elements in ColdFusion MX Applications&#8221;<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>At work one of our developers is programming an ecommerce store against a Java API written by a third party. Said Java API returns a TreeMap object from a method and when you try to access that TreeMap object in any fashion, you&#8217;ll get an error, specifically: &#8220;Error casting an object of type java.lang.Integer to &hellip; <a href=\"https:\/\/cephas.net\/blog\/2003\/07\/17\/java-collection-types-coldfusion\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Java Collection Types &amp; ColdFusion<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/452"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=452"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/452\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=452"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}