Java程序工具之发射机制类信息打印器(Java版本)

/**
 * @author Sking
 * 使用反射机制打印类信息
 */
package component.ReadAPI;
import java.lang.reflect.*;
import java.util.*;

public class GenericReflectionTest
{
   public static void main(String[] args)
   {
      String name;
      if (args.length > 0) 
         name = args[0];
      else 
      {
         Scanner in = new Scanner(System.in);
         System.out.println("Enter class name (e.g. java.util.Date): ");
         name = in.next();//从控制台读取指定类
      }
      try
      {  
         Class<?> cl = Class.forName(name);//返回指定类对应的Class对象
         printClass(cl);//打印类声明
         for (Method m : cl.getDeclaredMethods())//打印类的所有方法
            printMethod(m);
      }
      catch (ClassNotFoundException e)
      {
         e.printStackTrace();
      }
   }

   public static void printClass(Class<?> cl)
   {
      System.out.print(cl);//打印类名,格式为:class 类的完全限定名
      printTypes(cl.getTypeParameters(), "<", ", ", ">");//类的类型参数列表
      Type sc = cl.getGenericSuperclass(); //返回直接父类的类型
      if (sc != null) 
      {
         System.out.print(" extends ");
         printType(sc);
      }
      printTypes(cl.getGenericInterfaces(), " implements ", ", ", "");
      System.out.println();
   }

   public static void printMethod(Method m)
   {
      String name = m.getName();//方法名
    //打印访问修饰符标志的字符串
      System.out.print(Modifier.toString(m.getModifiers()));
      System.out.print(" ");
      printTypes(m.getTypeParameters(), "<", ", ", "> ");//方法类型参数列表
      printType(m.getGenericReturnType());//打印返回类型
      System.out.print(" ");
      System.out.print(name);//打印方法名
      System.out.print("(");
      printTypes(m.getGenericParameterTypes(), "", ", ", "");//形参类型列表
      System.out.println(")");
   }
 
   /**
    * 按照指定格式打印数组参数指定的类型列表
    * @param types 类型数组
    * @param pre 格式前缀
    * @param sep 类型之间的分隔符
    * @param suf 格式后缀
    */
   public static void printTypes(Type[] types, String pre, String sep, 
                  String suf)
   {
      if (types.length > 0) System.out.print(pre);//打印前缀
      for (int i = 0; i < types.length; i++)
      {
         if (i > 0) System.out.print(sep);
         printType(types[i]);
      }
      if (types.length > 0) System.out.print(suf);
   }

   public static void printType(Type type)
   {
      if (type instanceof Class) //对象类型
      {
         Class<?> t = (Class<?>) type;
         System.out.print(t.getName());
      }
      else if (type instanceof TypeVariable)//类型变量
      {
         TypeVariable<?> t = (TypeVariable<?>) type;
         System.out.print(t.getName());
         printTypes(t.getBounds(), " extends ", " & ", "");
      }
      else if (type instanceof WildcardType)//通配符类型表达式
      {
         WildcardType t = (WildcardType) type;
         System.out.print("?");
         printTypes(t.getLowerBounds(), " extends ", " & ", "");
         printTypes(t.getUpperBounds(), " super ", " & ", "");
      }
      else if (type instanceof ParameterizedType)//参数化类型
      {
         ParameterizedType t = (ParameterizedType) type;
         Type owner = t.getOwnerType();
         if (owner != null) { printType(owner); System.out.print("."); }
         printType(t.getRawType());
         printTypes(t.getActualTypeArguments(), "<", ", ", ">");         
      }
      else if (type instanceof GenericArrayType)//数组类型
      {
         GenericArrayType t = (GenericArrayType) type;
         System.out.print("");
         printType(t.getGenericComponentType());
         System.out.print("[]");
      }     
   }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值