|
@@ -0,0 +1,47 @@
|
|
|
+package cn.com.qmth.stmms.admin.utils;
|
|
|
+
|
|
|
+import java.lang.reflect.Field;
|
|
|
+
|
|
|
+import cn.com.qmth.stmms.biz.exam.bean.ExamVo;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 对象复制,属性copy代码生成
|
|
|
+ */
|
|
|
+public class CopyObjectCodeGenerator {
|
|
|
+
|
|
|
+ private static Object ob = new ExamVo();
|
|
|
+
|
|
|
+ private static String setName = "a";
|
|
|
+
|
|
|
+ private static String getName = "b";
|
|
|
+
|
|
|
+ public static String generateCopyCode(Object source) {
|
|
|
+ StringBuilder code = new StringBuilder();
|
|
|
+ Class<?> clazz = source.getClass();
|
|
|
+
|
|
|
+ // 获取所有字段
|
|
|
+ Field[] fields = clazz.getDeclaredFields();
|
|
|
+
|
|
|
+ for (Field field : fields) {
|
|
|
+ String fieldName = field.getName();
|
|
|
+ // 将字段名转换为驼峰命名格式,即首字母大写
|
|
|
+ String camelCaseName = Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
|
|
|
+
|
|
|
+ // 根据字段名生成 getter 和 setter 方法的代码
|
|
|
+ String getter = getName + ".get" + camelCaseName + "()";
|
|
|
+ String setter = setName + ".set" + camelCaseName + "(" + getter + ");";
|
|
|
+
|
|
|
+ code.append(setter).append("\n");
|
|
|
+ }
|
|
|
+
|
|
|
+ return code.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ // 示例对象
|
|
|
+ String code = generateCopyCode(ob);
|
|
|
+
|
|
|
+ System.out.println("生成的代码:");
|
|
|
+ System.out.println(code);
|
|
|
+ }
|
|
|
+}
|