实现JavaBean与ActionScript的VO之间的转换.

 

 

    ActionScript与Java一样是面向对象的开发程序.二者之间的相同之处很多.

 

    有的时候,我们在编写JavaBean后,需要创建对应的AS对象.在数量很少的情况下,也许并不觉得繁琐.但是当你的JavaBean存在几十个,数百个的时候,是否希望有一种方式能够自动生成AS的vo?

 

    这里将为你展示,我所编写的时间Java与ActionScript之间自动生成的代码.由于程序刚写完,并未完善,故比较粗糙,请勿见笑.如果哪位朋友有更好的转换方法,请邮箱([email protected])发送给我,鄙人在此十分感谢!

 

package cn.vicky.utils; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.lang.reflect.Field; import java.math.BigDecimal; import java.util.Date; /** * @author Vicky * @emial [email protected] */ public class Java2Flex { // java的代码目录 private static String JAVA_SRC_PATH = "G:/游戏项目/game_webServer/src"; // flex代码目录 private static String FLEX_SRC_PATH = "G:/游戏项目/game_webServer/src"; // POJO的包路径 private static String JAVA_PACKAGE = "cn.vicky.pojo"; // 将要生成的as的value object包路径 private static String FLEX_PACKAGE = "cn.vicky.pojo"; private static String EXAMPLE = "Example"; public Java2Flex() { } @SuppressWarnings("static-access") public Java2Flex(String java_src_path, String flex_src_path, String java_package, String flex_package) { this.JAVA_SRC_PATH = java_src_path; this.FLEX_SRC_PATH = flex_src_path; this.JAVA_PACKAGE = java_package; this.FLEX_PACKAGE = flex_package; } /** * @param args */ public static void main(String[] args) { String srcPath = JAVA_PACKAGE.replaceAll("//.", "/"); File dir = new File(JAVA_SRC_PATH + "//" + srcPath); File[] files = dir.listFiles(); String name = ""; for (File file : files) { if (!file.isDirectory() && file.isFile()) { name = file.getName(); if (name.lastIndexOf(EXAMPLE + ".java") == -1) { name = name.replaceAll(".java", ""); try { Class<?> clazz = Class.forName(JAVA_PACKAGE + "." + name); createFile(clazz); } catch (Exception e) { e.printStackTrace(); } } } } } private static void createFile(Class<?> clz) throws IOException { String flexSrcPath = FLEX_PACKAGE.replaceAll("//.", "/"); String fileName = clz.getSimpleName(); // User File dir = new File(FLEX_SRC_PATH + "//" + flexSrcPath); if (!dir.exists()) { dir.mkdirs(); } File f = new File(dir, fileName + ".as"); // User.as if (f.exists()) { f.delete(); } f.createNewFile(); FileWriter fw = new FileWriter(f); fw.write("package " + FLEX_PACKAGE + "{/n"); // fw.write("/t[Bindable]/n"); // [Bindable] fw.write("/tpublic class " + fileName + "{/n"); // public class User{ fw.write("/t/tpublic function " + fileName + "(){}/n"); // System.out.println(fileName + ":" + clz.getDeclaredFields().length); for (Field fd : clz.getDeclaredFields()) { fw.write(createField(fd)); // 创建属性 } fw.write("/t}/n}"); fw.close(); } private static String createField(Field fd) { String r = "/t/tpublic var "; Class<?> type = fd.getType(); String name = fd.getName(); if (name.equals("serialVersionUID")) { return ""; } System.out.println("type : " + type); System.out.println("type.getName() : " + type.getName()); if (type.equals(Long.class) || type.equals("long") || type.equals(Double.class) || type.equals("double") || type.equals(Integer.class) || type.equals("int") || type.equals(Short.class) || type.equals(BigDecimal.class)) { r = r + name + ":Number;"; } else if (type.equals(String.class)) { r = r + name + ":String;"; } else if (type.equals(Date.class)) { r = r + name + ":Date;"; } else if (type.getName().equals("java.lang.Object")) { r = r + name + ":Object;"; } else if (type.getName().equals("java.util.List") || type.getName().equals("java.util.Set") || type.getName().equals("java.util.Vector")) { r = r + name + ":Array = new Array();"; } else if (type.getName() != null && !type.getName().equals("")) { r = r + name + ":" + getFileType(type.getName()) + ";"; } else { System.out.println(type.getName()); } return r + "/n"; } public static String getFileType(String type) { if (type.contains(".")) { type = type.substring(type.lastIndexOf(".") + 1, type.length()); } return type; } }

 

编写测试的JavaBean

** * 玩家表 * * @author Vicky */ @Entity @Table(name = "lesogo_user") public class User implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @Column(length = 20, unique = true, nullable = false) private String user_name; @Column(length = 32, nullable = false) private String user_password; @Column(length = 2, nullable = false) private Integer user_rank = 1; @Column(length = 5, nullable = false) private Integer user_experience = 0; @Column(length = 10, nullable = false) private Integer user_money = 0; @Column(length = 5, nullable = false) private Integer user_charm = 0; @Column(length = 3, nullable = false) private Integer user_skills = 1; @Column(length = 20, nullable = false) private String user_trbal; @ManyToOne(cascade = { CascadeType.REFRESH }, optional = true, fetch = FetchType.LAZY) @JoinColumn(name = "user_village_id") private Villages user_village_id; @Temporal(TemporalType.TIMESTAMP) @Column(nullable = false) private Date user_create_time = new Date(); @Column(length = 19) private String user_login_time; @Column(length = 1, nullable = false) private Integer user_isLogin = 0; @OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "friendships_owner") private Set<Friendships> userID = new HashSet<Friendships>(); @OneToMany(cascade = { CascadeType.REFRESH }, fetch = FetchType.LAZY, mappedBy = "friendships_friend") private Set<Friendships> friendID = new HashSet<Friendships>(); @OneToOne(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY) @JoinColumn(name = "user_forbidden_id") private Forbidden user_forbidden_id; // getter & setter

生成ActionScript

package cn.vicky.pojo{ public class User{ public function User(){} public var id:Number; public var user_name:String; public var user_password:String; public var user_rank:Number; public var user_experience:Number; public var user_money:Number; public var user_charm:Number; public var user_skills:Number; public var user_trbal:String; public var user_village_id:Villages; public var user_create_time:Date; public var user_login_time:String; ... }

你可能感兴趣的:(java,String,user,Flex,Integer,actionscript)