在前几篇使用mina实现rpc调用时,会存在方法中要求返回类型是数组的情况,
如定义一个方法,
- public Properties[] getProps();
假设Object中的内容为:
- public Properties[] getProps() {
- Properties[] parray = new Properties[3];
- parray[0] = new Properties();
- parray[0].put("444","234234");
- parray[0].put("vvvv",new Vector());
- parray[0].put("aaaaa",new Apple());
- parray[1] = new Properties();
- parray[1].put("asdfasdf","234234");
- parray[1].put("vvaddfasdvv",new Vector());
- parray[1].put("aaadfadsfaaa",new Apple());
- parray[2] = new Properties();
- parray[2].put("asdfaadsfasdfasfsdf","234234");
- parray[2].put("vvaddfasdfasdfasfdasdvv",new Vector());
- parray[2].put("aaadfaasdfafdasdfasfdfddsfaaa",new Apple());
- return parray;
- }
获得对象后要需要利用反射还原为数组结构。
- if(handler.resultObject!=null){
- Object obj = handler.resultObject;
- if (obj.getClass().isArray()) {
- System.out.println("it is a array."+Array.getLength(obj));
- System.out.println("type="+obj.getClass().getComponentType());
- Properties[] props = new Properties[Array.getLength(obj)];
- for(int i=0;i<Array.getLength(obj);i++){
- props[i] = (Properties) Array.get(obj,i);
- System.out.println(i+"===="+props[i]);
- }
- }
- }
输出:
- it is a array.3
- type=class java.util.Properties
- 0===={vvvv=[], 444=234234, aaaaa=mina.common.Apple@26e431}
- 1===={vvaddfasdvv=[], asdfasdf=234234, aaadfadsfaaa=mina.common.Apple@14f8dab}
- 2===={vvaddfasdfasdfasfdasdvv=[], aaadfaasdfafdasdfasfdfddsfaaa=mina.common.Apple@1ddebc3, asdfaadsfasdfasfsdf=234234}