程序员人生 网站导航

android自动创建生成model或者po文件

栏目:php教程时间:2015-03-24 08:27:25

在开发进程中发现model或po层的数据只是参数不同,其他都是1样,而每次编写的时候一样的事情需要花费很多的时间去写或拷贝。虽然有些能够快速生成,但是还是需要时间,比如Parcelable接口实现的时候需要写1些东西,现在使用1个对文件编写的类来自动生成文件相信能减少更多的时间。乃至如果开发的接口够规范的话还能直接解析接口生成model或po文件。


源码以下:

import java.io.File; import java.io.FileOutputStream; import java.io.RandomAccessFile; import java.util.Date; /** * 在指定位置生成model或po文件 * * @author xiaoyi * * 2015年3月6日 */ public class CreateMode { /** * 指定文件路径 */ public static String dirPath = "C:Userschengxuyuan2Desktopmodel"; /** * 指定文件格式 */ public static String byteType = "GBK"; /** * 在指定位置判断并创建文件和文件夹 * * @param dir * 指定的文件夹 * @param file * 指定的文件 * @return * @throws Exception */ public static void createFile(File dir, File file) throws Exception { try { // 在指定位置判断并创建文件夹 if (dir.exists()) { System.out.println("model文件夹存在"); } else { System.out.println("model文件夹不存在,创建model"); dir.mkdir();// 不存在则创建 } // 在指定位置判断并创建文件 if (file.exists()) { System.out.println(file.getName() + "文件存在"); } else { System.out .println(file.getName() + "文件不存在,新建" + file.getName()); file.createNewFile();// 不存在则创建 } } catch (Exception e) { System.out.println(file.getName() + "文件创建失败!"); e.printStackTrace(); } } /** * 对指定文件中写入指定内容 * * @param content * 写入内容 * @param dirPath * 文件路径 * @param fileName * 文件名称 * @param byteType * 写入格式 GBK,UTF⑻... * @return * @throws Exception */ public static boolean writeTxtFile(StringBuilder content, String dirPath, String fileName, String byteType) throws Exception { File dir = new File(dirPath); File file = new File(dir, fileName); createFile(dir, file); RandomAccessFile mm = null; boolean flag = false; FileOutputStream o = null; try { o = new FileOutputStream(file); o.write(content.toString().getBytes(byteType)); o.close(); flag = true; } catch (Exception e) { System.out.println("文件内容写入失败!"); e.printStackTrace(); } finally { if (mm != null) { mm.close(); } } return flag; } /** * 创建model或po * * @param packageName * 项目包名 比如:com.xy.pocket_book.po * @param remark * 对该po的说明备注 * @param author * 作者 * @param time * 创建时间 * @param otherInfo * 其他说明 * @param className * 类名 * @param impI * 需要实现的接口 Parcelable or Serializable * @param variable * 成员变量 * @return */ private static void createModel(String packageName, String remark, String author, String time, String otherInfo, String className, String impI, String... variable) { StringBuilder builder = new StringBuilder(); // +项目包路径 builder.append("package ").append(packageName).append("; "); // +援用包 builder.append("import android.os.Parcel; ").append( "import android.os.Parcelable; "); // +po注释 作者 日期等 builder.append("/** ").append(" * ").append(remark).append(" "); builder.append(" * ").append(" "); builder.append(" * ").append("@author ").append(author).append(" "); builder.append(" * ").append(" "); builder.append(" * ").append(time).append(" "); builder.append(" * ").append(" "); builder.append(" * ").append(otherInfo).append(" "); builder.append(" * ").append(" "); builder.append(" */ ").append(" "); // +类名称 接口 builder.append("public class ").append(className) .append(" implements ").append(impI).append(" { "); // +变量 for (int i = 0; i < variable.length; i++) { builder.append(" private String ").append(variable[i]) .append("; "); } builder.append(" "); // +空构造 builder.append(" public ").append(className).append("() {} "); // +参数构造 builder.append(" public ").append(className).append("("); for (int i = 0; i < variable.length; i++) { if (i == variable.length - 1) { builder.append("String ").append(variable[i]); } else { builder.append("String ").append(variable[i]).append(","); } } builder.append(") { "); builder.append(" super(); "); for (int i = 0; i < variable.length; i++) { builder.append(" this.").append(variable[i]).append(" = ") .append(variable[i]).append("; "); } builder.append(" } "); // +toString builder.append(" @Override "); builder.append(" public String toString() { "); builder.append(" return "").append(className).append(" ["); for (int i = 0; i < variable.length; i++) { if (i == variable.length - 1) { // name=" + name + " builder.append(variable[i]).append("=" + ") .append(variable[i]).append(" + ""); } else if (i == 0) { // name=" + name + ", builder.append(variable[i]).append("=" + ") .append(variable[i]).append(" + ","); } else { builder.append(" ").append(variable[i]).append("=" + ") .append(variable[i]).append(" + ","); } } builder.append("]"; "); builder.append(" } "); // +get for (int i = 0; i < variable.length; i++) { String str1 = variable[i].substring(0, 1); String str2 = variable[i].substring(1, variable[i].length()); builder.append(" public String get").append(str1.toUpperCase()) .append(str2).append("() { "); builder.append(" return ").append(variable[i]).append("; "); builder.append(" } "); } // +set for (int i = 0; i < variable.length; i++) { String str1 = variable[i].substring(0, 1); String str2 = variable[i].substring(1, variable[i].length()); builder.append(" public void set").append(str1.toUpperCase()) .append(str2).append("(String ").append(variable[i]) .append(") { "); builder.append(" this.").append(variable[i]).append(" = ") .append(variable[i]).append("; "); builder.append(" } "); } // 如果使用Parcelable接口 添加两个方法1个实现 if (impI.equals("Parcelable")) { // 第1个方法 builder.append(" @Override "); builder.append(" public int describeContents() { "); builder.append(" return 0; "); builder.append(" } "); // 第2个方法 builder.append(" @Override "); builder.append(" public void writeToParcel(Parcel dest, int flags) { "); for (int i = 0; i < variable.length; i++) { builder.append(" dest.writeString(").append(variable[i]) .append("); "); } builder.append(" } "); // 第3个实现 builder.append(" private static final Parcelable.Creator<") .append(className).append("> CREATOR = new Creator<") .append(className).append(">() { "); builder.append(" @Override "); builder.append(" public ").append(className) .append("[] newArray(int size) { "); builder.append(" return new ").append(className) .append("[size]; "); builder.append(" } "); builder.append(" @Override "); builder.append(" public ").append(className) .append(" createFromParcel(Parcel source) { "); builder.append(" ").append(className) .append(" mSource = new ").append(className) .append("(); "); for (int i = 0; i < variable.length; i++) { builder.append(" mSource.").append(variable[i]) .append(" = source.readString(); "); } builder.append(" return mSource; "); builder.append(" } "); builder.append(" }; "); builder.append("};"); } // 写入文件 try { writeTxtFile(builder, dirPath, className + ".java", byteType); System.out.println("文件写入成功!"); } catch (Exception e) { System.out.println("文件写入失败!"); e.printStackTrace(); } } public static void main(String[] args) { // 创建model或po测试 createModel("com.xy.pocket_book.po", "测试备注", "xiaoyi", new Date().toLocaleString(), "其他信息备注", "BillPo", "Parcelable", "id", "title", "parent_id", "other", "info", "remark"); } }


自动生成的源码:

package com.xy.pocket_book.po; import android.os.Parcel; import android.os.Parcelable; /** * 测试备注 * * @author xiaoyi * * 2015⑶⑹ 9:44:30 * * 其他信息备注 * */ public class BillPo implements Parcelable { private String id; private String title; private String parent_id; private String other; private String info; private String remark; public BillPo() {} public BillPo(String id,String title,String parent_id,String other,String info,String remark) { super(); this.id = id; this.title = title; this.parent_id = parent_id; this.other = other; this.info = info; this.remark = remark; } @Override public String toString() { return "BillPo [id=" + id + ", title=" + title + ", parent_id=" + parent_id + ", other=" + other + ", info=" + info + ",remark=" + remark + "]"; } public String getId() { return id; } public String getTitle() { return title; } public String getParent_id() { return parent_id; } public String getOther() { return other; } public String getInfo() { return info; } public String getRemark() { return remark; } public void setId(String id) { this.id = id; } public void setTitle(String title) { this.title = title; } public void setParent_id(String parent_id) { this.parent_id = parent_id; } public void setOther(String other) { this.other = other; } public void setInfo(String info) { this.info = info; } public void setRemark(String remark) { this.remark = remark; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(id); dest.writeString(title); dest.writeString(parent_id); dest.writeString(other); dest.writeString(info); dest.writeString(remark); } private static final Parcelable.Creator<BillPo> CREATOR = new Creator<BillPo>() { @Override public BillPo[] newArray(int size) { return new BillPo[size]; } @Override public BillPo createFromParcel(Parcel source) { BillPo mSource = new BillPo(); mSource.id = source.readString(); mSource.title = source.readString(); mSource.parent_id = source.readString(); mSource.other = source.readString(); mSource.info = source.readString(); mSource.remark = source.readString(); return mSource; } }; };



------分隔线----------------------------
------分隔线----------------------------

最新技术推荐