Bridging the gap to Fusion through our PeopleSoft Solutions Extenders
Grey Sparling PeopleSoft Expert's Corner
Oracle Blogs
 Subscribe Now!
Interact with the experts here at Grey Sparling Solutions, Inc.

Saturday, July 05, 2008

Casting Java objects in PeopleCode

When using PeopleCode and Java mixed together, one thing that will cause problems for you is the inability to do any casting of Java objects from the PeopleCode side. For example, if you were storing some Java objects in a HashMap.

Local JavaObject &map = CreateJavaObject("java.util.HashMap");
Local JavaObject &int = CreateJavaObject("java.lang.Integer", 28);
&map.put("TEST", &int);


Later on, when you want to get the object back out and use it for something

Local JavaObject &int2 = &map.get("TEST");
Warning(&int2.intValue());

You'll get an error "Java method intValue not found for class java.lang.Object.".

This is because the PeopleCode runtime is using the return type for the "get" method on the HashMap object to decide what object it has. Fair enough, that's how Java works as well. If you're writing Java, you'd do something like this.

Integer int2 = (Integer)map.get("TEST");

The (Integer) part changes (or casts) the returned value of the get method from java.lang.Object to Integer. Unfortunately, there is no way to do this in PeopleCode, so you'd have to write (and distribute to the application servers) some Java glue code that could handle the casting, but that's a bit of a headache.

We've shown in previous blog entries how to use Java reflection from PeopleCode to work around this, but Java 5 brings a new option for us to try. PeopleTools 8.49 (the current version of PeopleTools as of the moment) is the first version of PeopleTools to use Java 5.

As a side note, Java 1.4 end of life is October 30, 2008, so those of you looking for a good reason to justify a PeopleTools upgrade to your management, here it is :-)

So what helps us in Java 5 with casting? There is a new method in java.lang.Class called cast, which according to the doc, "Casts an object to the class or interface represented by this Class object.".

Great!

So we update the code

Local JavaObject &int2 = &map.get("TEST");
Local JavaObject &int3 = &int.getClass().cast(&int2);
Warning(&int3.intValue());

PeopleCode knows that the &int object is of type java.lang.Integer so we can get it's Class object and use that to cast our object that came back out of the HashMap and we'll be all set, right?

Nope. Same error as before. "Java method intValue not found for class java.lang.Object.".

Labels: , ,

0Comments:

Post a Comment

<< Home