package com.wesley.framework.decoration; /** * @author it.zl 封装解析Object之后的一些参数 */ public class DecoratorParam { private Class<?> cls; private StringBuffer clsString = new StringBuffer(); public DecoratorParam(Class<?> cls) { super(); this.cls = cls; } public DecoratorParam(Class<?> cls, StringBuffer clsString) { super(); this.cls = cls; this.clsString = clsString; } public Class<?> getCls() { return cls; } public void setCls(Class<?> cls) { this.cls = cls; } public StringBuffer getClsString() { return clsString; } public void setClsString(StringBuffer clsString) { this.clsString = clsString; } }
package com.wesley.framework.decoration; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.lang.reflect.Field; import java.lang.reflect.Method; import com.meadville.entity.Tlog; public class DecoratorUtil { private final static String PACKAGE = "com.XXX.entity.decoration"; private final static String LINE = "\n"; private final static String NULL = "\t"; public static void main(String[] args) { DecoratorUtil.outputDecoratorBuffer(DecoratorUtil .getDecoratorBuffer(new Tlog())); } /** * @param param * 输出通过解析Object的信息 */ public synchronized static void outputDecoratorBuffer(DecoratorParam param) { FileWriter fw = null; String packageDirName = System.getProperty("user.dir") + "/src/" + PACKAGE.replace('.', '/'); try { File file = new File(packageDirName + "\\" + param.getCls().getSimpleName() + "Dec.java"); file.createNewFile(); fw = new FileWriter(file); fw.getEncoding().getBytes("utf-8"); fw.write(param.getClsString().toString()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { fw.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * @param obj * @return 通过Object来生成java,类似于反射 */ public synchronized static DecoratorParam getDecoratorBuffer(Object obj) { Class<?> cls = obj.getClass(); Field[] fields = cls.getDeclaredFields(); StringBuffer sb = new StringBuffer(); String modelName = cls.getSimpleName(); String decName = cls.getSimpleName() + "Dec"; sb.append("package ").append(PACKAGE).append(";").append(LINE); sb.append("import ").append(cls.getCanonicalName()).append(";") .append(LINE); sb.append("//http://numen06.iteye.com").append(LINE); sb.append("@SuppressWarnings(\"serial\")").append(LINE); sb.append("public class ").append(decName) .append(" extends DecoratorModel<").append(modelName) .append(",").append(decName).append("> {").append(LINE); sb.append(NULL).append("public ").append(decName) .append("() {super();}").append(LINE); sb.append(NULL).append("public ").append(decName).append("(") .append(modelName).append(" model) {super(model);}") .append(LINE); for (Field f : fields) { String getmethod = "get" + f.getName().substring(0, 1).toUpperCase() + f.getName().substring(1); String setmethod = "set" + f.getName().substring(0, 1).toUpperCase() + f.getName().substring(1); try { Method gmothod = cls.getMethod(getmethod, new Class<?>[0]); Method smothod = cls.getMethod(setmethod, gmothod.getReturnType()); sb.append(NULL).append("public ") .append(gmothod.getReturnType().getName()).append(" ") .append(gmothod.getName()).append("() {").append(LINE); sb.append(NULL).append(NULL).append("return super.model.") .append(getmethod).append("();").append(LINE); sb.append(NULL).append("}").append(LINE); sb.append(NULL).append("public void").append(" ") .append(smothod.getName()).append("(") .append(smothod.getParameterTypes()[0].getName()) .append(" ").append(f.getName()).append(") {") .append(LINE); sb.append(NULL).append(NULL).append("super.model.") .append(smothod.getName()).append("(") .append(f.getName()).append(");").append(LINE); sb.append(NULL).append("}").append(LINE); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } sb.append("}"); // System.out.println(sb.toString()); return new DecoratorParam(cls, sb); } }