使用Java反射机制得到类的结构信息

使用Java反射机制得到类的结构信息

关键字: java反射 类的结构

使用Java反射机制得到类的结构信息
代码如下:
Java代码 复制代码
  1. package com.youdao.wm;   
  2.   
  3. import java.lang.reflect.Constructor;   
  4. import java.lang.reflect.Field;   
  5. import java.lang.reflect.Method;   
  6. import java.lang.reflect.Modifier;   
  7.   
  8. /**  
  9.  * java反射机制测试:打印类的构造函数、方法、属性的信息  
  10.  *   
  11.  * @author jacky  
  12.  *   
  13.  */  
  14. public class ReflectionTest {   
  15.   
  16.     public static void main(String[] args) {   
  17.         // 要打印的类名   
  18.         String name = "java.lang.Double";   
  19.         try {   
  20.             // 得到此类对应的Class   
  21.             Class c1 = Class.forName(name);   
  22.             // 得到此类的父类   
  23.             Class superc1 = c1.getSuperclass();   
  24.             System.out.print("class " + name);   
  25.             // 如果超类不为空且不是Object   
  26.             if (superc1 != null && superc1 != Object.class) {   
  27.                 System.out.print("extends " + superc1.getName());   
  28.             }   
  29.   
  30.             System.out.print("/n{/n");   
  31.   
  32.             // 打印此类的构造函数信息   
  33.             printConstructors(c1);   
  34.             System.out.println();   
  35.   
  36.             // 打印此类的方法信息   
  37.             printMethods(c1);   
  38.             System.out.println();   
  39.   
  40.             // 打印此类的属性信息   
  41.             printFields(c1);   
  42.   
  43.             System.out.print("/n}/n");   
  44.         } catch (ClassNotFoundException e) {   
  45.             // TODO Auto-generated catch block   
  46.             e.printStackTrace();   
  47.         }   
  48.     }   
  49.   
  50.     /**  
  51.      * 打印Class对应类的构造函数信息  
  52.      *   
  53.      * @param c1  
  54.      *            Class  
  55.      */  
  56.     public static void printConstructors(Class c1) {   
  57.         Constructor[] constructors = c1.getDeclaredConstructors();   
  58.   
  59.         for (Constructor c : constructors) {   
  60.             String name = c.getName();   
  61.             System.out.print("   " + Modifier.toString(c.getModifiers()));   
  62.             System.out.print(" " + name + "(");   
  63.   
  64.             Class[] paramTypes = c.getParameterTypes();   
  65.   
  66.             for (int j = 0; j < paramTypes.length; j++) {   
  67.                 if (j > 0)   
  68.                     System.out.print(",");   
  69.                 System.out.print(paramTypes[j].getName());   
  70.             }   
  71.   
  72.             System.out.println(");");   
  73.         }   
  74.     }   
  75.   
  76.     /**  
  77.      * 打印Class对应类的所有方法信息  
  78.      *   
  79.      * @param c1  
  80.      *            Class  
  81.      */  
  82.     public static void printMethods(Class c1) {   
  83.         Method[] methods = c1.getDeclaredMethods();   
  84.   
  85.         for (Method method : methods) {   
  86.             Class retType = method.getReturnType();   
  87.   
  88.             String name = method.getName();   
  89.   
  90.             System.out.print("   " + Modifier.toString(method.getModifiers()));   
  91.             System.out.print(" " + retType.getName() + " " + name + "(");   
  92.   
  93.             Class[] paramTypes = method.getParameterTypes();   
  94.   
  95.             for (int j = 0; j < paramTypes.length; j++) {   
  96.                 if (j > 0)   
  97.                     System.out.print(", ");   
  98.                 System.out.print(paramTypes[j].getName());   
  99.             }   
  100.   
  101.             System.out.println(");");   
  102.         }   
  103.   
  104.     }   
  105.   
  106.     /**  
  107.      * 打印Class对应类的属性信息  
  108.      *   
  109.      * @param c1  
  110.      *            Class  
  111.      */  
  112.     public static void printFields(Class c1) {   
  113.         Field[] fields = c1.getDeclaredFields();   
  114.   
  115.         for (Field f : fields) {   
  116.             Class classType = f.getType();   
  117.   
  118.             System.out.print("   " + Modifier.toString(f.getModifiers()));   
  119.             System.out.print(" " + classType.getName() + " " + f.getName());   
  120.             System.out.println(";/n");   
  121.         }   
  122.     }   
  123.   
  124. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值