Chinaunix首页 | 论坛 | 博客
  • 博客访问: 610375
  • 博文数量: 96
  • 博客积分: 1464
  • 博客等级: 上尉
  • 技术积分: 1539
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-12 23:24
文章分类

全部博文(96)

文章存档

2013年(29)

2012年(53)

2011年(14)

分类: Java

2012-03-07 18:06:45

下面为java反射技术的一个应用:
下面的代码打印了一个类的全部信息。这个程序将提醒用户输入类名,然后输出类中的所有构造器的签名、方法和所有属性

  1. package myProgram;

  2. import java.util.*;
  3. import java.lang.reflect.*;

  4. /**
  5.  * This program uses reflection to print all features of a class
  6.  * @version 1.1 2004-02-21
  7.  * @author Cay Horstmann
  8.  * Copier **
  9.  * copy time 2012-03-06
  10.  *
  11.  */
  12. public class ReflectionTest {
  13.     public static void main(String[] args){
  14.         //read class name from command line args or user input
  15.         String name;
  16.         if(args.length > 0)
  17.             name = args[0];
  18.         else{
  19.             Scanner in = new Scanner(System.in);
  20.             System.out.println("Enter class (e.g. java.util.Date):");
  21.             name = in.next();
  22.         }
  23.         
  24.         try{
  25.             //print class name and surpclass name (if != Object)
  26.             Class c1 = Class.forName(name);
  27.             Class superc1 = c1.getSuperclass();
  28.             String modifiers = Modifier.toString(c1.getModifiers());
  29.             if(modifiers.length() > 0)
  30.                 System.out.println(modifiers + " ");
  31.             System.out.println("class "+name);
  32.             if(superc1 != null && superc1 != Object.class)
  33.                 System.out.println("extends "+superc1.getName());
  34.             
  35.             System.out.print("\n{\n");
  36.             printConstructors(c1);
  37.             System.out.println();
  38.             printMethods(c1);
  39.             System.out.println();
  40.             printFields(c1);
  41.             System.out.println("}");
  42.         }catch(ClassNotFoundException e){
  43.             e.printStackTrace();
  44.         }
  45.         System.exit(0);
  46.     }
  47.     
  48.     
  49.     /**
  50.      * Print all constructors of a class
  51.      * @param c1 a class
  52.      */
  53.     public static void printConstructors(Class c1){
  54.         Constructor[] constructors = c1.getDeclaredConstructors();
  55.         
  56.         for(Constructor c : constructors){
  57.             String name = c.getName();
  58.             System.out.print(" ");
  59.             String modifiers = Modifier.toString(c.getModifiers());
  60.             if(modifiers.length() > 0)
  61.                 System.out.print(modifiers + " ");
  62.             System.out.print(name + "(");
  63.             
  64.             //print parameter types
  65.             Class[] paramTypes = c.getParameterTypes();
  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.             System.out.println(");");
  72.         }
  73.     }
  74.     
  75.     /**
  76.      * Prints all methods of a class
  77.      * @param c1 a class
  78.      */
  79.     public static void printMethods(Class c1){
  80.         Method[] methods = c1.getDeclaredMethods();
  81.         
  82.         for(Method m : methods){
  83.             Class retType = m.getReturnType();
  84.             String name = m.getName();
  85.             
  86.             System.out.println(" ");
  87.             //print modifiers,return type,and method name
  88.             String modifiers = Modifier.toString(m.getModifiers());
  89.             if(modifiers.length() > 0)
  90.                 System.out.print(modifiers + " ");
  91.             System.out.print(retType.getName()+" "+name+"(");
  92.             
  93.             //print parameter types
  94.             Class[] paramTypes = m.getParameterTypes();
  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.             System.out.println(");");
  101.         }
  102.     }
  103.     
  104.     /**
  105.      * prints all fields of a class
  106.      * @param c1 a class
  107.      */
  108.     public static void printFields(Class c1){
  109.         Field[] fields = c1.getDeclaredFields();
  110.         
  111.         for(Field f : fields){
  112.             Class type = f.getType();
  113.             String name = f.getName();
  114.             System.out.print(" ");
  115.             String modifiers = Modifier.toString(f.getModifiers());
  116.             if(modifiers.length() > 0)
  117.                 System.out.print(modifiers + " ");
  118.             System.out.println(type.getName() + " "+ name+ ";");
  119.         }
  120.     }
  121.     
  122. }

结果
  1. Enter class (e.g. java.util.Date):
  2. java.lang.Double (此行为输入的值)
  3. public final
  4. class java.lang.Double
  5. extends java.lang.Number

  6. {
  7.   public java.lang.Double(java.lang.String);
  8.   public java.lang.Double(double);

  9.   
  10. public int hashCode();
  11.   
  12. public static native long doubleToRawLongBits(double);
  13.   
  14. public static long doubleToLongBits(double);
  15.   
  16. public static native double longBitsToDouble(long);
  17.   
  18. public boolean equals(java.lang.Object);
  19.   
  20. public volatile int compareTo(java.lang.Object);
  21.   
  22. public int compareTo(java.lang.Double);
  23.   
  24. public static java.lang.String toString(double);
  25.   
  26. public java.lang.String toString();
  27.   
  28. public static java.lang.String toHexString(double);
  29.   
  30. public static int compare(double,double);
  31.   
  32. public static java.lang.Double valueOf(double);
  33.   
  34. public static java.lang.Double valueOf(java.lang.String);
  35.   
  36. public static boolean isNaN(double);
  37.   
  38. public boolean isNaN();
  39.   
  40. public byte byteValue();
  41.   
  42. public double doubleValue();
  43.   
  44. public float floatValue();
  45.   
  46. public int intValue();
  47.   
  48. public boolean isInfinite();
  49.   
  50. public static boolean isInfinite(double);
  51.   
  52. public long longValue();
  53.   
  54. public short shortValue();
  55.   
  56. public static double parseDouble(java.lang.String);

  57.   public static final double POSITIVE_INFINITY;
  58.   public static final double NEGATIVE_INFINITY;
  59.   public static final double NaN;
  60.   public static final double MAX_VALUE;
  61.   public static final double MIN_NORMAL;
  62.   public static final double MIN_VALUE;
  63.   public static final int MAX_EXPONENT;
  64.   public static final int MIN_EXPONENT;
  65.   public static final int SIZE;
  66.   public static final java.lang.Class TYPE;
  67.   private final double value;
  68.   private static final long serialVersionUID;
  69. }











阅读(2122) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~