コード作成ツール(反射を復習)


半日探してコードを生成するツールが探し出せなくて、自分で1つのjavabeanのコードの生成ツールを書きました
粗末な点は3つの方法toStringについて、データをクリアして、2つのbeanの複製
JavaEE方面のコード生成のツールを共有することを望みます


 package com.common;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
//toString clearData copy 
public class PrintBeanUtil {

	public static void main(String[] args) {
		printClearBeanMethod(PublicInfo.class);
		printToStringMethod(PublicInfo.class);
		printCopyBeanMethod(PublicInfo.class, PublicInfoTest.class);
	}

	//     set get 
	public static void printCopyBeanMethod(Class sourceCl, Class toCl) {
		Method[] sourceMethods = sourceCl.getMethods();
		Method[] toMethods = toCl.getMethods();
		String sourceName = sourceCl.getName().substring(sourceCl.getName().lastIndexOf(".") + 1);
		String targetName = toCl.getName().substring(toCl.getName().lastIndexOf(".") + 1);
		System.out.println("// get set " + sourceName + " " + targetName);
		System.out.println("//TODO");
		System.out.println("public void copySourceToTarget(" + sourceName + " source," + targetName + " target){");
		for (int i = 0; i < sourceMethods.length; i++) {
			Method sourceM = sourceMethods[i];
			if (sourceM.getName().startsWith("get") && Modifier.isPublic(sourceM.getModifiers())) {// public get 
				for (int j = 0; j < toMethods.length; j++) {
					Method toM = sourceMethods[j];
					if (toM.getName().startsWith("set") && Modifier.isPublic(toM.getModifiers())) {// public set 
						if (sourceM.getName().substring(3).equals(toM.getName().substring(3))) {//  
							System.out.println("	target." + toM.getName() + "(source." + sourceM.getName() + "()" + ");");
							break;
						}
					}
				}
			}
		}
		System.out.println("}");
	}

	/**
	 *  bean 
	 * 
	 * @param cl
	 */
	public static void printClearBeanMethod(Class cl) {
		Field[] fields = cl.getDeclaredFields();
		String sourceName = cl.getName().substring(cl.getName().lastIndexOf(".") + 1);
		System.out.println("//" + sourceName + " ");
		System.out.println("//TODO");
		System.out.println("public void clearData() {");
		for (int i = 0; i < fields.length; i++) {
			Field f = fields[i];
			String name = f.getName();
			if (!Modifier.isFinal(f.getModifiers())) {
				System.out.println("	this." + name + " = null;");
			}
		}
		System.out.println("}");
	}

	/**
	 *  bean toString 
	 * 
	 * @param cl
	 */
	public static void printToStringMethod(Class cl) {
		Field[] fields = cl.getDeclaredFields();
		System.out.println("//" + cl.getName().substring(cl.getName().lastIndexOf(".") + 1) + " toString ");
		System.out.println("//TODO");
		System.out.println("public String toString(){");
		System.out.print("	return ");
		for (int i = 0; i < fields.length; i++) {
			Field f = fields[i];
			// Class type = f.getType();
			String name = f.getName();
			// System.out.print(" " + Modifier.toString(f.getModifiers()));
			if (!Modifier.isStatic(f.getModifiers())) {
				System.out.print("\" " + name + "=\"+this." + name + "+");
			}

		}
		System.out.print("\"\";");
		System.out.println("");
		System.out.println("}");
	}
}