Skip navigation
Kirill_Grishin
Currently Being Moderated

Checking Type of java classes created with Scala

Dec 16, 2012 7:13 AM

Tags: #isinstanceof #scala

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.

 
Replies
  • Currently Being Moderated
    Dec 17, 2012 2:08 AM   in reply to Kirill_Grishin

    Let us verify. What does this tell you:

     

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

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 19, 2012 12:10 AM   in reply to Kirill_Grishin

    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"));

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 19, 2012 3:37 AM   in reply to Kirill_Grishin

    I was wondering whether it had something to do with the package. Is example a package name?

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 19, 2012 7:37 AM   in reply to Kirill_Grishin

    I wondered whether isInstanceOf expects just the class name, HelloWorld, and not the fully qualified name, example.HelloWorld. Will this work:

     

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

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

     

    I have been looking for clues on how isInstanceOf handles types defined with package names. I found very little in ColdFusion's documentation. So, I am looking into Java's documentation on instanceOf.

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 21, 2012 2:25 AM   in reply to Kirill_Grishin

    I have not been looking for a way to do it with Java's instanceof operator. That would be running away. I have been looking at how instanceof is used with fully qualified names, hoping to learn something from there. 

     

    However, I have only been able to confirm that your original code is correct, and should work! I am wondering whether it fails in your case because of the classpath.

     

    What is the absolute path of the directory that contains 'example.HelloWorld'? Go to the ColdFusion administrator. Click on the button for information. Is the directory in the classpath?

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 22, 2012 1:52 AM   in reply to Kirill_Grishin

    OK. Moving on, two tests:

     

    <p>

        1st test: Case sensitivity<br>

    <cfscript>

    helloworldapp = createObject("java","example.HelloWorld");

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

    </cfscript>

    </p>

     

    <p>

        2nd test: cfobject<br>

    <cfobject action="create" name="helloworldapp" type="java" class="example.HelloWorld">

    <cfoutput>#isInstanceOf(helloworldapp,"example.HelloWorld"))#</cfoutpu t>

    </p>

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 22, 2012 10:59 AM   in reply to Kirill_Grishin

    For want of a better idea, jot down the absolute path of the directory that contains /example/HelloWorld.class. Add that path to your loadPaths attribute. Incidentally, how do your dynamic settings this.javaSettings look like?

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 23, 2012 1:22 AM   in reply to Kirill_Grishin

    Thorough! Thanks.

     

    The test I last suggested is therefore:

     

    this.javaSettings.loadPaths = ['.\', '.\jars\','.\jars\cfjavatest.jar'];

     

    That should cover them all.

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 23, 2012 2:51 AM   in reply to Kirill_Grishin

    No! What's going on?

     

    Ok, let's now test one which we know is a yes. 

     

    myGateway = CreateObject("java","examples.JMS.JMSGateway");

    writeOutput(isInstanceOf(myGateway,"examples.JMS.JMSGateway"));

     

    If that gives a Yes, then the next test I would perform is a server restart(if that is possible). If it gives a No, then we know what we have in our hands.

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 23, 2012 3:19 AM   in reply to Kirill_Grishin

    Hang on a minute! I have this niggling feeling ColdFusion's isInstanceOf might be expecting the qualified name jars.example.HelloWorld.

     

    Just for giggles, run the original index.cfm page one directory up, in the jars directory. That is, make a copy to

     

    C:\ColdFusion10\cfusion\wwwroot\org\cfjava\jars\index.cfm

     

    and run it.

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 23, 2012 3:25 AM   in reply to Kirill_Grishin

    Kirill_Grishin wrote:

     

    That gave a YES

    That's the gateway test, yes?

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 23, 2012 3:38 AM   in reply to Kirill_Grishin

    An alternative test to the index.cfm relocation is to test by relocating cfjavatest.jar. Leave index.cfm in its original location. Return your Application.cfc to its original state. Then modify it as follows:

     

    this.javaSettings.loadPaths = ['.\', '.\cfjavatest.jar'];

     

    Move the file cfjavatest.jar one directory down to:

     

    C:\ColdFusion10\cfusion\wwwroot\org\cfjava\cfjavatest.jar

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 23, 2012 4:06 AM   in reply to Kirill_Grishin

    Kirill_Grishin wrote:

     

    Yes, the test for "examples.JMS.JMSGateway" gave a YES.

    That's a relief. So there is still hope.

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 28, 2012 3:42 AM   in reply to Kirill_Grishin

    Could you send me the JAR file? I have mailed you my e-mail address.

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 28, 2012 4:48 AM   in reply to Kirill_Grishin

    If you're up for it, I just thought of another test. At least for the time being. I am assuming the JAR file is at the original location,

     

    C:\ColdFusion10\cfusion\wwwroot\org\cfjava\jars\cfjavatest.jar

     

    In Application.cfc, comment out all the lines involving this.javaSettings. Go to the Java and JVM page in the Administrator. Add the following to the classpath:

     

    C:/ColdFusion10/cfusion/wwwroot/org/cfjava/jars/;C:/ColdFusion10/cfusi on/wwwroot/org/cfjava/jars/cfjavatest.jar

     

    Press the button to submit the changes. To be sure, restart ColdFusion.

     

    Any joy?

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 29, 2012 8:09 AM   in reply to Kirill_Grishin

    Thanks for the JAR file. Here follows my progress report.

     

    As a first test, I commented out the code this.javasettings=.... I unpacked the JAR file and copied its contents (example and scala directories) to C:\ColdFusion10\cfusion\wwwroot\WEB-INF\classes\. I restarted ColdFusion. When I run your original isInstanceOf code, I get a Yes.

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 30, 2012 12:58 AM   in reply to Kirill_Grishin

    I have been able to reproduce the issue (failure of isInstanceOf with dynamic Java settings) time after time. I am beginning to wonder whether you have uncovered a bug. However, I did find another positive result.

     

    Comment out the code this.javasettings=.... Move the file cfjavatest.jar to the directory,

     

    C:\ColdFusion10\cfusion\wwwroot\WEB-INF\lib\

     

    Restart ColdFusion. When you run the original isInstanceOf code, you get a Yes.

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 31, 2012 9:21 AM   in reply to Kirill_Grishin

    @Kirill_Grishin

    I decided to round this up before the end of the year. I am sorry I haven't been able to do better than find the above workarounds.

     

    IsInstanceOf(helloworldapp,"example.HelloWorld")) continues to return No for dynamic Java settings. I have reproduced the behaviour using every conceivable directory structure in the loadPaths attribute in this.javaSettings. As far as I can see, what you have discovered is a bug.

     

    Please consider filing a bug report. In fact, someone reported a similar bug (3121307) as far back as February 2012, when ColdFusion 10 was still in pre-release beta. Apparently that problem remains unsolved!

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points