• 『FLEX』Java的pojo对象转成Flex的vo对象


    在使用openamf的类映射功能时,一般需要在as端也定义和java pojo类似的对象,用于类的映射。

    有关使用openamf的类映射,请见之前的《openAMF自定义类映射(custom-class-mapping)的使用

    如果手工去写这些和java对应的flex vo对象比较麻烦,这里用java写了个简单的as vo对象生成的工具,代码如下:

    /**
    *
    */
    package com.nstar.base.util;

    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.lang.reflect.Field;

    import org.apache.commons.lang.StringUtils;

    /**
    * @author Administrator
    *
    */
    public class VoUtils {
    public VoUtils(){}

    //根据java类对象的类型返回vo类型
    public static String getClassType(Class c){
    String typeName = c.getSimpleName();
    if (typeName.equals("String") || typeName.equals("Date")) {
    return typeName;
    }else if (typeName.equals("BigDecimal") || typeName.equals("Decimal") ||
    typeName.equals("Double")) {
    return "Number";
    }else if (typeName.equals("Integer")) {
    return "int";
    }else if (typeName.equals("Boolean")) {
    return "boolean";
    }else {
    return "*";//其它类型的设置为未知类型
    }
    }
    //重复c字符count次,用于格式化生成的as文件
    public static String repeat(String c, int count){
    String temp = "";
    for(int i = 0;i<count ;i++){
    temp += c;
    }
    return temp;
    }
    /**
    * 生成as文件
    * @param pojoName java对象名称
    * @param packageName flex中vo对象的包名
    * @throws ClassNotFoundException
    * @throws IOException
    */
    public static void generateAsFile(String pojoName, String packageName, String folder)
    throws ClassNotFoundException, IOException {
    Class c = Class.forName(pojoName);
    Field[] fields =c.getDeclaredFields();
    //as的vo对象名称结尾加上VO标志
    File f = new File(folder+c.getSimpleName()+"VO.as");
    BufferedWriter bw = new BufferedWriter(
    new FileWriter(f));
    //包名,没有设置包名就取和java pojo一样的包名
    if (StringUtils.isEmpty(packageName)){
    bw.write("package "+c.getPackage().getName()+"\n{\n");
    }else{
    bw.write("package "+packageName+"\n{\n");
    }
    //写类
    bw.write(repeat(" ",4)+"public class "+c.getSimpleName()+"VO\n");
    bw.write(repeat(" ",4)+"{\n");
    //写属性
    for(int i=0;i<fields.length;i++){
    Class fieldType = fields[i].getType();
    String typeName = getClassType(fieldType);
    bw.write(repeat(" ",8)+"private var _"+fields[i].getName()+":"+typeName+";\n");
    }
    bw.write("\n\n\n");
    //写空的构造函数
    bw.write(repeat(" ",8)+"public function "+c.getSimpleName()+"VO(){}\n\n");
    //写 setter/getter 方法
    for(int i=0;i<fields.length;i++){
    Class fieldType = fields[i].getType();
    String typeName = getClassType(fieldType);
    //setter
    bw.write(repeat(" ",8)+"public function set "+fields[i].getName()+"(value:"+typeName+"):void{\n");
    bw.write(repeat(" ",12)+"this._"+fields[i].getName()+" = value;\n");
    bw.write(repeat(" ",8)+"}\n\n");
    //getter
    bw.write(repeat(" ",8)+"public function get "+fields[i].getName()+"():"+typeName+"{\n");
    bw.write(repeat(" ",12)+"return this._"+fields[i].getName()+";\n");
    bw.write(repeat(" ",8)+"}\n\n\n");
    }
    bw.write(repeat(" ",4)+"}\n");
    bw.write("}");
    bw.close();
    }

    //测试,写了个测试
    public static void main(String[] args) throws ClassNotFoundException, IOException {
    // TODO Auto-generated method stub
    String[] pojos = {
    "com.nstar.model.orderexpress.Area",
    "com.nstar.model.orderexpress.FoodType",
    "com.nstar.model.orderexpress.Holiday",
    "com.nstar.model.orderexpress.ItemSpec",
    "com.nstar.model.orderexpress.ItemSpecType",
    "com.nstar.model.orderexpress.Menu2Rest",
    "com.nstar.model.orderexpress.OrderInfo",
    "com.nstar.model.orderexpress.OrderInfoExp",
    "com.nstar.model.orderexpress.OrderMenu",
    "com.nstar.model.orderexpress.OrderState",
    "com.nstar.model.orderexpress.OrderType",
    "com.nstar.model.orderexpress.Repository",
    "com.nstar.model.orderexpress.RepositoryType",
    "com.nstar.model.orderexpress.RestInfo",
    "com.nstar.model.orderexpress.UserInfo",
    "com.nstar.model.orderexpress.UserLevel",
    "com.nstar.model.orderexpress.UserType"
    };
    for(int i=0;i<pojos.length;i++){
    //Class c = Class.forName(pojos[i]);
    //System.out.println("registerClassAlias('"+pojos[i]+"',com.nstar.orderexpress.vo."+c.getSimpleName()+"VO);");
    VoUtils.generateAsFile(pojos[i],"com.nstar.orderexpress.vo","C:/temp/");
    }
    }

    }

    测试时可以随便找个Java对象来测试,下面是其中生成的一个文件(OrderTypeVO)的内容:

    package com.nstar.orderexpress.vo
    {
    public class OrderTypeVO
    {
    private var _orderTypeId:String;
    private var _name:String;
    private var _remarks:String;

    public function OrderTypeVO(){}

    public function set orderTypeId(value:String):void{
    this._orderTypeId = value;
    }

    public function get orderTypeId():String{
    return this._orderTypeId;
    }

    public function set name(value:String):void{
    this._name = value;
    }

    public function get name():String{
    return this._name;
    }

    public function set remarks(value:String):void{
    this._remarks = value;
    }

    public function get remarks():String{
    return this._remarks;
    }

    }
    }

  • 相关阅读:
    A题
    CTF--web BugKu-ctf-web(1-10)
    CTF--web 攻防世界web题 robots backup
    CTF--web 攻防世界web题 get_post
    BUUCTF-Crypyo-No.1
    攻防世界-新手篇(Mise)~~~
    BJDCTF-2020-WRITEUP---TiKi小组
    TKCTF-学校内部的校赛
    BUUCTF-BJD(更新V1.0)
    线程池(1)-参数
  • 原文地址:https://www.cnblogs.com/pyrmkj/p/2503660.html
Copyright © 2020-2023  润新知