DynamicCast
Description
Converts an instantiated PowerBuilder proxy object to a proxy for
the passed-in proxy name.
Syntax
|
1 |
javavm.DynamicCast(powerobject proxyobject, readonly string proxyname) |
|
Argument |
Description |
|---|---|
|
javavm |
An instance of the JavaVM class |
|
proxyobject |
An instantiated PowerBuilder proxy |
|
proxyname |
A string containing the name of the proxy to be |
Return value
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:1234567891011121314151617181920212223242526272829DepartmentHome px_DeptHomeDepartment px_DeptEnumeration px_EmployeeListEmployee px_EmployeeSalaried px_SalariedEmployeeContract px_ContractEmployeeEJBConnection connconn = create ejbconnectiontryconn.connectToServer(properties)px_DeptHome = conn.lookup("DepartmentHome", &"Department", &"com.joesportinggoods.ejbs.DepartmentHome")px_Dept = px_DeptHome.findByPrimaryKey(as_DeptName)px_EmployeeList = px_Dept.getEmployees()DO WHILE px_EmployeeList.hasMoreElements()px_Employee = px_EmployeeList.nextElement()IF i_jvm.getActualClass(px_Employee) = &"com.joesportinggoods.ejbs.Salaried" THENpx_SalariedEmployee = &i_jvm.dynamicCast(px_Employee, "Salaried")px_SalariedEmployee.adjustSalary(al_increase)END IFLOOPcatch (Exception e)THROW CREATE ApplyRaiseExceptionend 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.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748ItemManagerHome px_ItemMgrHomeItemManager px_ItemMgrItem px_ItemList px_ItemListany any_Objectboolean ib_isAList = FALSEstring is_IFs[]string is_actualClasslong ll_rowTRYpx_ItemMgrHome = g_EJBConn.Lookup("ItemManagerHome", &"ItemManager","com.xapic.ItemManagerHome")px_ItemMgr = px_ItemMgrHome.create()any_Object = px_ItemMgr.getAllItems()// check if object implements java.util.List interfaceinteger iFOR i = 1 to g_javaVM.getInterfaces(any_Object, &is_IFs)IF is_IFs[i] = "java.util.List" THENib_isAList = TRUEEXITEND IFNEXT// if it is a listIF ib_isAList THENpx_ItemList = g_javaVM.dynamicCast(any_Object, &"list")// traverse the listFOR i = 0 TO px_ItemList.size() - 1// get item on the listany_Object = px_ItemList.get(i)// determine its class and dynamically cast itis_actualClass = &g_javaVM.getActualClass(any_Object)is_actualClass = Mid(is_actualClass, &LastPos(is_actualClass,".") + 1, &Len(is_actualClass))px_Item = g_javaVM.dynamicCast(any_Object, is_actualClass)// add item to datastorell_row = ads_Items.insertRow(0)ads_Items.object.id[ll_row] = px_Item.getID()ads_Items.object.type[ll_row] = is_actualClassNEXTEND IFCATCH (Throwable t)// Handle exceptionEND 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.
See also