java反射

package reflect;

import java.lang.reflect.*;

public class ClassPraser {
	public static final String semicolon = ";\n";// 分号

	public static void prase(String classFullName) {
		Class clazz = null;
		try {
			clazz = Class.forName(classFullName);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("找不到该类:" + classFullName);
		}

		// 打印包信息
		StringBuilder packageStr = new StringBuilder();
		packageStr.append(clazz.getPackage());
		packageStr.append(semicolon);

		System.out.print(packageStr.toString());

		// 打印类修饰符
		int mod = clazz.getModifiers();
		
		System.out.print(getModifierName(mod));

		if (clazz.isInterface())
			System.out.print("interface ");
		else
			System.out.print("class ");

		// 打印类名称
		String simpleName = clazz.getSimpleName();
		System.out.print(simpleName);

		// 打印除Object以外的父类名称
		Class superClass = clazz.getSuperclass();
		if (superClass != null && !superClass.getSimpleName().equals("Object")) {
			System.out.print(" extends " + superClass.getSimpleName());
		}
		// 打印接口名称
		Class[] interfaces = clazz.getInterfaces();
		if (interfaces.length > 0) {
			System.out.print(" implements ");
		}
		int j = 0;
		for (Class interfaceType : interfaces) {
			System.out.print(interfaceType.getSimpleName());
			if (++j < interfaces.length)
				System.out.print(",");
		}

		// 类体开始
		System.out.println("{");

		// 打印构造函数
		Constructor[] constructor = clazz.getConstructors();
		for (Constructor cons : constructor) {
			StringBuilder consStr = new StringBuilder();
			consStr.append(getModifierName(cons.getModifiers()));
			consStr.append(cons.getName());
			consStr.append("(");

			Class[] parameterTypes = cons.getParameterTypes();

			int i = 0;
			for (Class para : parameterTypes) {
				consStr.append(para.getSimpleName());
				consStr.append(" arg" + ++i);
				if (i < parameterTypes.length)
					consStr.append(",");
			}

			consStr.append(");");
			System.out.println(consStr.toString());
		}

		// 打印类域

		// clazz.getFields();
		Field[] fields = clazz.getDeclaredFields();
		for (Field field : fields) {
			System.out.print(getModifierName(field.getModifiers()));
			System.out.print(field.getType().getSimpleName());
			System.out.print(field.getName());
			System.out.print(semicolon);
		}
		
		
		// 打印类方法
		Method[] methods = clazz.getDeclaredMethods();
		for (Method method : methods) {
			StringBuilder methodStr = new StringBuilder();
			methodStr.append(getModifierName(method.getModifiers()));
			
			methodStr.append(method.getReturnType().getSimpleName());
			
			methodStr.append(method.getName());
			methodStr.append("(");

			Class[] parameterTypes = method.getParameterTypes();

			int i = 0;
			for (Class para : parameterTypes) {
				methodStr.append(para.getSimpleName());
				methodStr.append(" arg" + ++i);
				if (i < parameterTypes.length)
					methodStr.append(",");
			}

			methodStr.append(");");
			System.out.println(methodStr);
			
		}
		// 类体结束
		System.out.println("}");
	}

	public static String getModifierName(int mod) {

		StringBuilder modifiers = new StringBuilder();

		if (Modifier.isStatic(mod))
			modifiers.append("static ");
		if (Modifier.isPublic(mod))
			modifiers.append("public ");
		if (Modifier.isPrivate(mod))
			modifiers.append("private ");
		if (Modifier.isProtected(mod))
			modifiers.append("protected ");
		if (Modifier.isFinal(mod))
			modifiers.append("final ");
		if (Modifier.isAbstract(mod))
			modifiers.append("Abstract ");
		if (Modifier.isSynchronized(mod))
			modifiers.append("Synchronized ");

		return modifiers.toString();
	}

	public static void main(String[] args) {
		prase("reflect.target.Dept");
		// prase("javax.sql.DataSource");
	}
}

 

你可能感兴趣的:(java,sql,J#)