根据实体生成action文件
package utils; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; import java.lang.reflect.Field; import java.util.List; import entity.WorkExperience; /** * 生成action工具类 * * @author huang * */ public class FileCreateUtil { /** * 生成get方法 * * @param field * @return */ public static StringBuffer get(Field field) { StringBuffer sb = new StringBuffer(); String name = field.getName(); String type = field.getType().getSimpleName(); sb.append("public "); sb.append(type + " "); sb.append("get" + name.substring(0, 1).toUpperCase() + name.substring(1)); sb.append("()"); sb.append("{\n "); sb.append("return "); sb.append("this."); sb.append(name + ";" + "\n"); sb.append("}\n"); return sb; } /** * 生成set方法 * * @param field * @return */ public static StringBuffer set(Field field) { StringBuffer sb = new StringBuffer(); String name = field.getName(); String type = field.getType().getSimpleName(); sb.append("public "); sb.append("void "); sb.append("set" + name.substring(0, 1).toUpperCase() + name.substring(1)); sb.append("("); sb.append(type + " " + name); sb.append(")"); sb.append("{\n"); sb.append("this." + name + "=" + name + ";" + "\n"); sb.append("}\n"); return sb; } /** * 生成私有属性 * * @param fields * @return */ public static StringBuffer fields(List<Field> fields) { StringBuffer sb = new StringBuffer(); if (fields != null && fields.size() > 0) { for (Field f : fields) { String name = f.getName(); String type = f.getType().getSimpleName(); sb.append("private " + type + " " + name + ";" + "\n"); } } return sb; } /** * 文件主体 * * @param clazz * @param absolutePackagePath * @return */ public static StringBuffer classMain(Class clazz, String absolutePackagePath) { StringBuffer sb = new StringBuffer(); sb.append("package " + absolutePackagePath + ";\n"); List<Field> fields = EntityUtil.getCommonFields(clazz); if (fields != null && fields.size() > 0) { for (Field field : fields) { if (field.getType().equals(java.util.Date.class)) { sb.append("import java.util.Date;\n"); break; } } } // sb.append("import " + clazz.getName() + ";\n"); sb.append("import service.Service;\n"); sb.append("import service.CriteriaService;\n"); String simpleName = clazz.getSimpleName(); sb .append("public class " + simpleName + "Action extends BaseAction{\n"); sb.append(fields(fields)); if (fields != null && fields.size() > 0) { for (Field field : fields) { sb.append(get(field)); sb.append(set(field)); } } sb.append("private Service service;\n"); sb.append("private CriteriaService cservice;\n"); sb.append("public Service getService(){\n return this.service;\n }\n"); sb .append("public CriteriaService getCservice(){\n return this.cservice;\n }\n"); sb .append("public void setService(Service service){\n this.service=service;\n }\n"); sb .append("public void setCservice(CriteriaService cservice){\n this.cservice=cservice;\n }\n"); sb.append("}"); System.out.println(sb.toString()); return sb; } /** * 创建文件 * * @param clazz * @throws Exception * */ public static void makeFile(Class clazz, String genPackagepath, String absolutePackagePath) throws Exception { File file = new File(""); String actionPackage = file.getAbsolutePath(); String simpleName = clazz.getSimpleName(); String dir = actionPackage + "/src/" + genPackagepath; /** * 生成文件夹 */ File dirFile = new File(dir + "/"); if (!dirFile.exists()) { dirFile.mkdir(); } /** * 生成文件 */ File f = new File(dir + simpleName + "Action.java"); StringBuffer sb = classMain(clazz, absolutePackagePath); if (!f.exists()) { Writer writer = null; try { writer = new OutputStreamWriter(new FileOutputStream(f), "utf-8"); writer.write(sb.toString()); writer.close(); } catch (IOException e) { e.printStackTrace(); } } else { throw new Exception("对不起,文件已存在"); } } /** * 使用方法 * * @param s */ public static void main(String[] s) { try { makeFile(WorkExperience.class, "/action/", "action"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package utils; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; /** * 实体的工具类 * * @author huang * */ public class EntityUtil { /** * 返回树节点含有指定注解的属性名称 * * @return */ @SuppressWarnings("unchecked") private static String fieldName(Object object, Class anno, Boolean inhert) { String name = ""; Class clazz = object.getClass(); List<Field> fields = new ArrayList<Field>(); Field[] fs1 = clazz.getDeclaredFields(); if (inhert == true) { Field[] fs = clazz.getSuperclass().getDeclaredFields(); if (fs != null && fs.length > 0) { for (Field f : fs) { fields.add(f); } } for (Field f : fs1) { fields.add(f); } } else { for (Field f : fs1) { fields.add(f); } } if (fields.size() > 0) { for (Field f : fields) { if (f.isAnnotationPresent(anno)) { name = f.getName(); break; } } } return name; } /** * 获取属性的方法 * * @return */ @SuppressWarnings("unchecked") private static String fieldMethod(Object object, Class anno, Boolean inhert) { String methodName = "get" + fieldName(object, anno, inhert).substring(0, 1).toUpperCase() + fieldName(object, anno, inhert).substring(1); return methodName; } /** * 返回属性的值 * * @return * @throws SecurityException * @throws NoSuchMethodException * @throws IllegalArgumentException * @throws IllegalAccessException * @throws InvocationTargetException */ @SuppressWarnings("unchecked") public static Object fieldValue(Object object, Class anno, Boolean inhert) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { Class clazz = object.getClass(); String idMethod = fieldMethod(object, anno, inhert); Method method = clazz.getMethod(idMethod); Object value = method.invoke(object) == null ? "" : method .invoke(object); return value; } /** * 获取类中所有的属性 * * @param clazz * @return */ public static List<Field> getClassFields(Class clazz) { List<Field> listFields = new ArrayList<Field>(); Field[] fields = clazz.getDeclaredFields(); Field[] fs = null; if (clazz.getSuperclass() != Object.class) { fs = clazz.getSuperclass().getDeclaredFields(); } if (fs != null && fs.length > 0) { for (Field f : fs) { listFields.add(f); } } for (Field f : fields) { listFields.add(f); } return listFields; } /** * 返回类中的基本数据类型的属性 * * @param clazz * @return */ public static List<Field> getCommonFields(Class clazz) { List<Field> fs = getClassFields(clazz); List<Field> list = new ArrayList<Field>(); if (fs != null && fs.size() > 0) { for (Field f : fs) { if (f.getType().equals(java.lang.String.class)) { list.add(f); } if (f.getType().equals(java.lang.Double.class)) { list.add(f); } if (f.getType().equals(java.util.Date.class)) { list.add(f); } if (f.getType().equals(java.lang.Long.class)) { list.add(f); } if (f.getType().equals(java.lang.Integer.class)) { list.add(f); } if (f.getType().equals(java.lang.Float.class)) { list.add(f); } if (f.getType().equals(java.lang.Byte.class)) { list.add(f); } if (f.getType().equals(int.class)) { list.add(f); } if (f.getType().equals(float.class)) { list.add(f); } if (f.getType().equals(double.class)) { list.add(f); } if (f.getType().equals(float.class)) { list.add(f); } if (f.getType().equals(boolean.class)) { list.add(f); } if (f.getType().equals(byte.class)) { list.add(f); } } } return list; } }
其主要是省去了手动生成action文件的麻烦,根据实体的相关属性,生成具有简单依赖关系的action