JavaVM:
DynamicCast method
Description
Converts
an instantiated PowerBuilder proxy object to a proxy for the passed–in
proxy name.
Syntax
|
1 |
<span>javavm</span>.DynamicCast(powerobject <span>proxyobject</span><span></span>, readonly string <span>proxyname</span>) |
|
Argument |
Description |
|---|---|
|
javavm |
An instance of the JavaVM class |
|
proxyobject |
An instantiated PowerBuilder proxy object |
|
proxyname |
A string containing the name of the proxy |
Return Values
Powerobject. A new proxy object for the
Java class referenced by proxyname. This method
returns null if the proxy cannot be created.
Examples
In the following example, the object returned from
the nextElement method is represented by a proxy
for the Employee class. The GetActualClass method
is used to determine whether the object is actually a SalariedEmployee, and
if it is, the proxy px_Employee is
down cast to the proxy px_SalariedEmployee so
that the adjustSalary method can be called:
|
1 |
DepartmentHome   px_DeptHome<br>Department       px_Dept<br>Enumeration      px_EmployeeList<br>Employee         px_Employee<br>Salaried         px_SalariedEmployee<br>Contract         px_ContractEmployee<br>EJBConnection    conn<br> <br>conn = create ejbconnection<br>try<br>   conn.connectToServer(properties)<br>   px_DeptHome = conn.lookup("DepartmentHome",  &<br>      "Department",   &<br>      "com.joesportinggoods.ejbs.DepartmentHome")<br>   px_Dept = px_DeptHome.findByPrimaryKey(as_DeptName)<br> <br>   px_EmployeeList = px_Dept.getEmployees()<br>   DO WHILE px_EmployeeList.hasMoreElements()<br>     px_Employee = px_EmployeeList.nextElement()<br>     IF i_jvm.<span>getActualClass</span>(px_Employee) =  &<br>        "com.joesportinggoods.ejbs.Salaried" THEN<br>         px_SalariedEmployee =   &<br>            i_jvm.<span>dynamicCast</span>(px_Employee, "Salaried")<br>         px_SalariedEmployee.adjustSalary(al_increase)<br>     END IF<br>   LOOP<br>catch (Exception e)<br>   THROW CREATE ApplyRaiseException<br>end try |
In this example, getAllItems returns
a java.lang.Object in the EJB declaration, which maps to the PowerBuilder Any data
type. The call to GetInterfaces determines whether
what is returned is a java.util.List. If it is,
a call to DynamicCast obtains a proxy for List,
which is used to obtain the size of the list before using its Get method
to obtain the elements of the list. A method such as getAllItems can
be used in many situations, such as to get a list of part numbers
for any type of product.
|
1 |
ItemManagerHome px_ItemMgrHome<br>ItemManager px_ItemMgr<br>Item px_Item<br>List px_ItemList<br>any any_Object<br>boolean ib_isAList = FALSE<br>string is_IFs[]<br>string is_actualClass<br>long ll_row<br> <br>TRY<br>   px_ItemMgrHome =      g_EJBConn.Lookup("ItemManagerHome", &<br>     "ItemManager","com.xapic.ItemManagerHome")<br>   px_ItemMgr = px_ItemMgrHome.create()<br>   any_Object = px_ItemMgr.getAllItems()<br>// check if object implements java.util.List interface<br>   integer i<br>   FOR i = 1 to g_javaVM.getInterfaces(any_Object, &<br>     is_IFs)<br>     IF is_IFs[i] = "java.util.List" THEN<br>        ib_isAList = TRUE<br>        EXIT<br>     END IF<br>   NEXT<br>   // if it is a list<br>   IF ib_isAList THEN<br>     px_ItemList = g_javaVM.dynamicCast(any_Object, &<br>        "list")<br>     // traverse the list<br>      FOR i = 0 TO px_ItemList.size() - 1<br>        // get item on the list<br>        any_Object = px_ItemList.get(i)<br>         // determine its class and dynamically cast it<br>        is_actualClass = &<br>           g_javaVM.getActualClass(any_Object)<br>        is_actualClass = Mid(is_actualClass, &<br>           LastPos(is_actualClass,".") + 1, &<br>           Len(is_actualClass))<br>        px_Item = g_javaVM.dynamicCast(any_Object,            is_actualClass)<br>        // add item to datastore<br>        ll_row = ads_Items.insertRow(0)<br>        ads_Items.object.id[ll_row] = px_Item.getID()<br>        ads_Items.object.type[ll_row] = is_actualClass<br>      NEXT<br>   END IF<br>CATCH (Throwable t)<br>   // Handle exception<br>END TRY |
Usage
There are two scenarios in which a Java object returned from
a call to an EJB method can be represented by a proxy that does
not provide the methods you need:
-
If the class
of a Java object returned from an EJB method call is dynamically
generated, PowerBuilder uses a proxy for the first interface implemented
by the Java class. -
The prototype of an EJB method that actually returns someclass can
be defined to return a class that someclass extends
or implements.For example, the prototype of a method that actually returns
an object of type java.util.ArrayList can be
defined to return java.util.Collection instead. (The java.util.ArrayList class
inherits from java.util.AbstractList, which inherits
from java.util.AbstractCollection, which implements java.util.Collection.)
If the method prototype has a return type of java.util.Collection,
PowerBuilder uses a proxy for java.util.Collection.
The DynamicCast method allows you to cast
the returned proxy object to a proxy for the interface you require,
or for the actual class of the object returned at runtime so that
the methods of that object can be used.
You can obtain the actual class of the object using the GetActualClass method. You
can also use the DynamicCast method with the GetSuperClass method, which
returns the immediate parent of the Java class, and the GetInterfaces method,
which writes a list of interfaces implemented by the class to an
array of strings.
For example, consider the following class:
|
1 |
public class java.util.LinkedList extends java.util.AbstractSequentialList implements java.util.List, java.lang.Cloneable, java.io.Serializable |
GetActualClass returns java.util.LinkedList, GetSuperClass returns java.util.AbstractSequentialList,
and GetInterfaces returns 3 and writes three strings
to the referenced string array: java.util.List, java.lang.Cloneable,
and java.io.Serializable.