• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

解析class的工具类

JAVA相关 水墨上仙 2523次浏览

解析class的工具类


package com.google.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;

public class AnalyticClassInfo
{

	private static String getModifier(int modifier)
	{
		String result = "";
		switch (modifier)
		{
		case Modifier.PRIVATE:
			result = "private";
			break;
		case Modifier.PUBLIC:
			result = "public";
			break;
		case Modifier.PROTECTED:
			result = "protected";
			break;
		case Modifier.ABSTRACT:
			result = "abstract";
			break;
		case Modifier.FINAL:
			result = "final";
			break;
		case Modifier.NATIVE:
			result = "native";
			break;
		case Modifier.STATIC:
			result = "static";
			break;
		case Modifier.SYNCHRONIZED:
			result = "synchronized";
			break;
		case Modifier.STRICT:
			result = "strict";
			break;
		case Modifier.TRANSIENT:
			result = "transient";
			break;
		case Modifier.VOLATILE:
			result = "volatile";
			break;
		case Modifier.INTERFACE:
			result = "interface";
			break;
		default:
			break;
		}
		return result;
	}

	private static void printClassDefinition(Class clz)
	{
		String clzModifier = getModifier(clz.getModifiers());
		if (clzModifier != null && !clzModifier.equals(""))
		{
			clzModifier += "  ";
		}

		String superClz = "";
		if(clz.getSuperclass()!=null)
		{
			superClz = clz.getSuperclass().getName();
			if (superClz != null && !superClz.equals(""))
			{
				superClz = " extends " + superClz;
			}
		}
		

		Class[] interfaces = clz.getInterfaces();
		String inters = "";
		for (int i = 0; i < interfaces.length; i++)
		{
			if (i == 0)
			{
				inters += " implements ";
			}
			inters += interfaces[i].getName();
		}

		System.out.println(clzModifier + clz.getName() + " " + superClz + " "
				+ inters);

		System.out.println("{");

		System.out.println("fields:");
		Field[] fields = clz.getDeclaredFields();
		for (int i = 0; i < fields.length; i++)
		{
			String modifier = getModifier(fields[i].getModifiers());
			if (modifier != null && !modifier.equals(""))
			{
				modifier = modifier + " ";
			}
			String fieldName = fields[i].getName();
			String fieldType = fields[i].getType().getName();
			System.out.println("    " + modifier + fieldType + " " + fieldName
					+ ";");
		}

		System.out.println("methods:");

		Method[] methods = clz.getDeclaredMethods();
		for (int i = 0; i < methods.length; i++)
		{
			Method method = methods[i];

			String modifier = getModifier(method.getModifiers());
			if (modifier != null && !modifier.equals(""))
			{
				modifier = modifier + " ";
			}

			String methodName = method.getName();

			Class returnClz = method.getReturnType();
			String retrunType = returnClz.getName();

			Class[] clzs = method.getParameterTypes();
			String paraList = "(";
			for (int j = 0; j < clzs.length; j++)
			{
				paraList += clzs[j].getName();
				if (j != clzs.length - 1)
				{
					paraList += ", ";
				}
			}
			paraList += ")";

			clzs = method.getExceptionTypes();
			String exceptions = "";
			for (int j = 0; j < clzs.length; j++)
			{
				if (j == 0)
				{
					exceptions += "throws ";
				}

				exceptions += clzs[j].getName();

				if (j != clzs.length - 1)
				{
					exceptions += ", ";
				}
			}

			exceptions += ";";

			String methodPrototype = modifier + retrunType + " " + methodName
					+ paraList + exceptions;

			System.out.println("    " + methodPrototype);

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

	
	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		// TODO Auto-generated method stub

//		printClassDefinition(List.class);
//		printClassDefinition(Proxy.class);
		printClassDefinition(ArrayList.class);
	}

	
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明解析class的工具类
喜欢 (0)
加载中……