|
|

楼主 |
发表于 2013/8/20 16:52:25
|
显示全部楼层
深入浅出Peoplesoft之Java使用
在Peoplesoft中使用java对象通过CreateJavaObject实例化对象,ObjectDoMethod来调用实例化对象中的方法。我把以前测试用的方法分享给大家。
在peoplecode中调用java使用的反射机制。所以在使用过程中有哪些问题,可以在eclipse中使用反射机制模拟peoplecode调用。如下:
1、为了能快速使用Java中的类,首先在eclipse中对要使用的对象用反射机制模拟实现程序开发。
在使用Java反射机制调用函数的时候,函数的参数类型一定要正确。参数类型可以再eclipse里看。
例如:
package com.lashou.services;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
public class PSDebug {
public static void main(String[] args) throws ClassNotFoundException,
SecurityException, NoSuchMethodException, IllegalArgumentException,
IllegalAccessException, InvocationTargetException,
InstantiationException {
HashMap emp = new HashMap();
ArrayList empList = new ArrayList();
empList.add("123");
empList.add("234");
emp.put("dept", new ArrayList());
ArrayList empList1 = new ArrayList();
empList1.add("123");
empList1.add("234");
((ArrayList) emp.get("dept")).add(empList);
((ArrayList) emp.get("dept")).add(empList1);
ArrayList el = (ArrayList) emp.get("dept");
for (Iterator it2 = el.iterator(); it2.hasNext();) {
System.out.println(it2.next());
}
Class hmCls = Class.forName("java.util.HashMap");
Constructor cons = hmCls.getConstructor(new Class[] {});
Object obj = cons.newInstance();
// System.out.println(cons.newInstance(null));
/*
* Method[] addMethod = hmCls.getDeclaredMethods(); for(Method
* m:addMethod){
*
* System.out.println(m.getName()); }
*/
Method putMethod = hmCls.getMethod("put", new Class[] { Object.class,
Object.class });
System.out.println(putMethod);
putMethod.invoke(obj, new Object[] { "dept", empList1 });
Method getMethod = hmCls.getMethod("get", new Class[] { Object.class });
System.out.println(getMethod);
Object deptArrayList = getMethod.invoke(obj, new Object[] { "dept" });
Class al = deptArrayList.getClass();
Method[] alMethod = al.getDeclaredMethods();
for (Method m : alMethod) {
System.out.println(m.getName());
}
Method sizeMethod = al.getMethod("size", null);
System.out.println(sizeMethod.invoke(deptArrayList, null));
String.valueOf(b)
}
}
2。根据以上的实现方式用PS中函数以及对象实现调用过程
JavaObject 返回的是java对象,这个对象可以直接调用Java方法
ObjectDoMethod 可以调用 java Object对象的方法,比如Object obj = (Object)new ArrayList(); 这样Object的实现是ArrayList ,那么ObjectDoMethod 就可以调用ArrayList的方法了,这个函数为了解决返回对象是Object时候使用的,因为Object对象没有子类的方法
CreateJavaObject 相对Java中 new Object();这个函数调用会实例一个对象
具体的实现(HashMap 和 ArrayList)如下:
Local JavaObject &DeptHashMap = CreateJavaObject("java.util.HashMap");
&DeptHashMap.put("Dept1", CreateJavaObject("java.util.ArrayList"));
Local JavaObject &DeptArray = CreateJavaObject("java.util.ArrayList");
&DeptArray.add("A");
&DeptArray.add("B");
Local JavaObject &DeptArray1 = CreateJavaObject("java.util.ArrayList");
&DeptArray1.add("C");
&DeptArray1.add("D");
Local JavaObject &HmClass = GetJavaClass("java.lang.Class").forName("java.util.HashMap");
Local JavaObject &HmConstructor = ObjectDoMethod(&HmClass, "getConstructor", CreateJavaObject("java.lang.Class[]"));
MessageBox(0, "", 0, 0, String(&HmClass));
MessageBox(0, "", 0, 0, String(&HmConstructor));
Local JavaObject &HmInstance = ObjectDoMethod(&HmConstructor, "newInstance", Null);
MessageBox(0, "", 0, 0, String(&HmInstance));
Local JavaObject &putMethod = &HmClass.getMethod("put", CreateJavaObject("java.lang.Class[]", GetJavaClass("java.lang.Object"), GetJavaClass("java.lang.Object")));
MessageBox(0, "", 0, 0, String(&putMethod));
&putMethod.invoke(&HmInstance, CreateJavaObject("java.lang.Object[]", "DEPT", &DeptArray));
Local JavaObject &getMethod = &HmClass.getMethod("get", CreateJavaObject("java.lang.Class[]", GetJavaClass("java.lang.Object")));
Local JavaObject &DeptArrayList = &getMethod.invoke(&HmInstance, CreateJavaObject("java.lang.Object[]", "DEPT"));
Local JavaObject &DeptArrayListClass = ObjectDoMethod(&DeptArrayList, "getClass");
MessageBox(0, "", 0, 0, String(&DeptArrayListClass));
Local JavaObject &DeptSizeMethod = &DeptArrayListClass.getMethod("size", Null);
Local string &DeptSize = ObjectDoMethod(CreateJavaObject("java.lang.String"), "valueOf", &DeptSizeMethod.invoke(&DeptArrayList, Null));
MessageBox(0, "", 0, 0, &DeptSize);
希望这个对大家有帮助.
|
|