引言:
Zeroc Ice支持元语的句法,即metadata sytax。还是那句老话,“是利剑,也是毒药。”元语的使用,在 Ice manual 3.4 中有详细介绍,本文不做详述。只挑最有诱惑地Generics & Collections的元语做以说明。
正文:
(1)ArrayList<T>语法模型
["java:type:java.util.ArrayList<MyTypeClass>"] sequence<::MyTypeClass> arrayList;
这样slice2java编译后,便可在java侧生成基于ArrayList<MyTypeClass>的arraylist。
(2)实际应用
在实际使用时,大多数情况下“MyTypeClass”是自定义的,而且跟当前arraylist不在同一个包下。完善一下slice定义,可以如下。
// create by:hanjianxin // at:11-08-02 16:40, for SOS // resolve & comment by: zhaoningbo 11-08-03 #ifndef BOND__DEF #define BOND__DEF [["java:package:com.ccxe.bond.credit"]] module dto { struct BondIssueInformationCountDto{ string bondcode; string id; // ... string crOrgName; }; }; module service { // *Attention please: when we use 'metadata', we recommend // to use the full name of the Java Class. Just like the following! // That because 'slice2java' has a tiny bug, which may do not fill // the T , just in Generics<T>, with this full name. So ours code // goes red. ["java:type:java.util.ArrayList<com.ccxe.bond.credit.dto. BondIssueInformationCountDto>"] sequence<dto::BondIssueInformationCountDto> listBondIssueInformationCountDto; interface BondIssueInformationCountService{ //method listBondIssueInformationCountDto getCreditOrgListFromBondCredit(); }; }; #endif
需要注意几点:
1,第7行为包声明元语。(省了一堆module~。=)
2,第23行为本文ArrayList<T>声明元语。使用时请注意,元语体中的所有类型均为java侧的类型(因为使用的 “java:type:”),因此“BondIssueInformationCountDto”可写为 “BondIssueInformationCountDto”或者 “com.ccxe.bond.credit.dto.BondIssueInformationCountDto”,但绝不能为 “dto::BondIssueInformationCountDto”。原因是两处定义的识别者不同。
3,第23行,特别注意“BondIssueInformationCountDto”为了slice2java生成的java文件安全可靠,必须用类的 全名“com.ccxe.bond.credit.dto.BondIssueInformationCountDto”。否则生成的文件可能会出现如下 的结果,而报错,却又不能手工导包(slice2java设计原因)。
public static java.util.List<com.ccxe.bond.credit.dto. BondIssueInformationCountDto> read(IceInternal.BasicStream __is) { java.util.List<com.ccxe.bond.credit.dto. BondIssueInformationCountDto> __v; // full name, OK! // __v = new java.util.ArrayList // <com.ccxe.bond.credit.dto.BondIssueInformationCountDto>(); // short name, ERROR! no import package, can not resovle as a type. __v = new java.util.ArrayList<BondIssueInformationCountDto>(); // .... return __v; }
由上段代码可以看出,必须用类全名。
补充:
1,关于Ice slice, IceBox, IceGrid 及相关简单样例,你可以在google code上以svn免费获得。
2,在线浏览地址 numberIceDemo 。(本文样例路径:/numberIceSliceDemo/slice/com/number/temp/bondList.ice)