ClassInfoUtil.java

4257 단어 자바


/*
 * ClassInfoUtil.java 
 * xiaocao000
 * 2009-1-2
 */
package com.xiaocao000.lang;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * @author xiaocao000
 */
public final class ClassInfoUtil
{
	/**
	 *       ,      。
	 */
	private ClassInfoUtil()
	{
	}

	private static Class<?> clazz = null;
	private static Class<?> superClazz = null;
	// private static Class<? extends Object> implInterface = null;

	private static final String LINE_SEPARATOR = System
			.getProperty("line.separator");

	private static void init(Class<?> clazz)
	{
		ClassInfoUtil.clazz = clazz;
		ClassInfoUtil.superClazz = ClassInfoUtil.clazz.getSuperclass();
	}

	public static void printClassInfo(String clazz)
			throws ClassNotFoundException
	{
		printClassInfo(Class.forName(clazz));
	}

	public static void printClassInfo(Class<?> clazz)
			throws ClassNotFoundException
	{
		System.out.println(getClassInfo(clazz));
	}

	public static String getClassInfo(Class<?> clazz)
	{
		init(clazz);
		StringBuffer sb = new StringBuffer();
		sb.append("class " + clazz.getName());

		if (superClazz != null && !superClazz.equals(Object.class))
		{
			System.out.print("extends " + superClazz.getName());
		}

		sb.append(LINE_SEPARATOR + "{" + LINE_SEPARATOR);
		sb.append(getFields(clazz));
		sb.append(getConstructors(clazz));
		sb.append(getMethods(clazz));
		sb.append(LINE_SEPARATOR + "}" + LINE_SEPARATOR);
		sb.append(LINE_SEPARATOR);
		return sb.toString();
	}

	@SuppressWarnings("unchecked")
	public static String getConstructors(Class<?> clazz)
	{
		StringBuffer sb = new StringBuffer();
		Constructor[] constructors = clazz.getDeclaredConstructors();

		for (Constructor constructor : constructors)
		{
			sb.append(Modifier.toString(constructor.getModifiers()));
			sb.append(" " + constructor.getName() + "(");
			Class<?>[] paramTypes = constructor.getParameterTypes();

			for (int i = 0; i < paramTypes.length; i++)
			{
				if (i > 0)
				{
					sb.append(",");
				}

				sb.append(paramTypes[i].getName());
			}

			sb.append(");" + LINE_SEPARATOR);

		}
		sb.append(LINE_SEPARATOR);
		return sb.toString();
	}

	public static String getFields(Class<?> clazz)
	{
		StringBuffer sb = new StringBuffer();
		for (Field field : clazz.getDeclaredFields())
		{
			Class<?> type = field.getType();
			sb.append(Modifier.toString(field.getModifiers()));
			sb.append(" " + type.getName());
			sb.append(" " + field.getName());
			sb.append(";" + LINE_SEPARATOR);

		}
		sb.append(LINE_SEPARATOR);
		return sb.toString();
	}

	public static String getMethods(Class<?> clazz)
	{
		StringBuffer sb = new StringBuffer();
		for (Method method : clazz.getDeclaredMethods())
		{
			Class<?> returnType = method.getReturnType();
			sb.append(Modifier.toString(method.getModifiers()));
			sb.append(" " + returnType.getName());
			sb.append(" " + method.getName() + "(");

			Class<?>[] paramTypes = method.getParameterTypes();
			for (int i = 0; i < paramTypes.length; i++)
			{
				if (i > 0)
				{
					sb.append(",");
				}

				sb.append(paramTypes[i].getName());

			}
			sb.append(");" + LINE_SEPARATOR);
		}
		sb.append(LINE_SEPARATOR);
		return sb.toString();
	}

}



좋은 웹페이지 즐겨찾기