Skip to main content
Known Participant
December 16, 2012
Question

Checking Type of java classes created with Scala

  • December 16, 2012
  • 1 reply
  • 4981 views

Hi.

I've created a simple class in Scala and packed it into a .jar file. I have no problems loading it into ColdFusion and running its methods.

Here is a copy of cfdump of the created object:

object of example.HelloWorld
Class Nameexample.HelloWorld
MethodsMethodReturn Type
Country()example.HelloWorld$Country$
cn()example.HelloWorld$Country
countriesList()scala.collection.immutable.List
countriesMap()scala.collection.immutable.Map
getCountryByCode(java.lang.String)scala.Option
ja()example.HelloWorld$Country
ru()example.HelloWorld$Country
us()example.HelloWorld$Country

But I have problems with isInstanceOf function with this object. If I run:

HelloWorldApp = CreateObject("java","example.HelloWorld");

writeOutput(isInstanceOf(HelloWorldApp,"example.HelloWorld"));

I get false as a result, but it should be true, right? The class does get loaded properly by CreateObject function.

Is there a workaround? The cfdump tag somehow manages to get the proper type of the object which it displays in the Class Name field of the dump table.

Thank you.

This topic has been closed for replies.

1 reply

BKBK
Community Expert
Community Expert
December 17, 2012

Let us verify. What does this tell you:

<cfoutput>#HelloWorldApp.getClass().getName()#</cfoutput>

Known Participant
December 19, 2012

I am sorry for late response. The result of that code is "example.HelloWorld", so yes, it does return the correct name of Scala type. I also tried this approach for scala.Option type and it seems to be working.

Though, for example, the return type of getCountryByCode(java.lang.String) function is defined as scala.Option, the actual result's type is either scala.Some ot scala.None$ (which of cause are subtypes of  scala.Option), which makes it possible to distinguish if the result of the computation was a fail or not.

Thank you for your help. It seems like I will have to use this approach for checking types.

BKBK
Community Expert
Community Expert
December 19, 2012

I am surprised that isInstanceOf misses the HelloWorldApp instance of "example.HelloWorld". That seems buggy to me. Does it do better with the default constructor? That is

HelloWorldApp = CreateObject("java","example.HelloWorld").init();

writeOutput(isInstanceOf(HelloWorldApp,"example.HelloWorld"));