反射入门实例

/**
 * @File name:  TestReflection.java
 * @Create on:  2007-5-17
 * @Author   :  chenjun
 */
package com.ghostandghost.chenjun.reflection;

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

public class TestReflection {
 
 public static void main(String[] args) throws SecurityException, IllegalArgumentException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException
 {
  reflection("com.ghostandghost.chenjun.reflection.testClass");
 }
 
 private static void reflection(String str) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
  //通过传入字符串获得类
                          Class cl=Class.forName(str);
  
                          getClassMethods(str, cl); 
        
                          getClassConstructors(str, cl);
         
                          getClassFields(str, cl);
         
                          isType(str, cl);          
         
                          instanceAndExecute(cl);
 }

 private static void instanceAndExecute(Class cl) throws InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException {
  System.out.println("*************************实例化,执行方法begin****************************************");
        
         //实例化获得的类
        Object oj=cl.newInstance(); 
        //方法参数类型类列表(方法名称,参数都可以动态获得,见上方法)
  Class partypes[] = new Class[1];
  partypes[0] = Class.forName("java.lang.String");  
  Method meth = cl.getMethod("setA",partypes);
  Object arglist[] = new Object[1];
  arglist[0] = "aabbb";
        //执行方法
  meth.invoke(oj, arglist);
  meth= cl.getMethod("getA");
  //执行方法
  System.out.println(meth.invoke(oj));
  System.out.println(oj); 
  System.out.println("*************************实例化,执行方法end******************************************");
  System.out.println();
        System.out.println();
 }

 private static void isType(String str, Class cl) {
  //判断类类型
         System.out.println("*************************类型判断begin****************************************");
         boolean b1 = cl.isInstance(new Integer(37));
         System.out.println(str+"和Integer  类是不是同一个类:"+b1);      
         boolean b2 = cl.isInstance(new testClass());        
         System.out.println(str+"和testClass类是不是同一个类:"+b2);
         System.out.println("*************************类型判断end******************************************");
         System.out.println();
         System.out.println();
 }

 private static void getClassFields(String str, Class cl) {
  System.out.println("**********"+str+"的所有字段begin*************");
         Field fieldlist[] = cl.getDeclaredFields();
         for (int i = 0; i < fieldlist.length; i++) {
          System.out.println("################################################################################################");
             Field fld = fieldlist[i];
             System.out.println("name = " + fld.getName());
             System.out.println("decl class = " + fld.getDeclaringClass());
             System.out.println("type = " + fld.getType());
             int mod = fld.getModifiers();
             System.out.println("modifiers = " + Modifier.toString(mod));
             System.out.println("################################################################################################");
             System.out.println("");
         }
         System.out.println("**********"+str+"的所有字段end***************");
         System.out.println("");
         System.out.println("");
 }

 private static void getClassConstructors(String str, Class cl) {
  //       获得类的构造方法,打印出来
           System.out.println("**********"+str+"的所有构造方法begin*************");
           Constructor ctorlist[] = cl.getDeclaredConstructors();
           for (int i = 0; i < ctorlist.length; i++) {
            System.out.println("################################################################################################");
               Constructor ct = ctorlist[i];
               System.out.println(ct.toString());
               System.out.println("name = " + ct.getName());
               System.out.println("decl class = " + ct.getDeclaringClass());
               Class pvec[] = ct.getParameterTypes();
               for (int j = 0; j < pvec.length; j++)
                   System.out.println("param #" + j + " " + pvec[j]);
               Class evec[] = ct.getExceptionTypes();
               for (int j = 0; j < evec.length; j++)
                   System.out.println("exc #" + j + " " + evec[j]);
               System.out.println("################################################################################################");
               System.out.println();
           }
           System.out.println("**********"+str+"的所有构造方法end***************");
           System.out.println();
           System.out.println();
 }

 private static void getClassMethods(String str, Class cl) {
  //获得类的方法,打印出来
   Method m[] = cl.getDeclaredMethods();
   System.out.println("**********"+str+"的所有方法begin*************");
         for (int i = 0; i < m.length; i++)
         {
          System.out.println("################################################################################################");
          System.out.println(m[i].toString());
          System.out.println(m[i].getName());
          System.out.println("decl class = " + m[i].getDeclaringClass());
             Class pvec[] = m[i].getParameterTypes();
             for (int j = 0; j < pvec.length; j++)
                 System.out.println("param #" + j + " " + pvec[j]);
             Class evec[] = m[i].getExceptionTypes();
             for (int j = 0; j < evec.length; j++)
                 System.out.println("exc #" + j + " " + evec[j]);

          System.out.println(m[i].getReturnType());
          System.out.println("################################################################################################");
          System.out.println();
         }
            
         System.out.println("**********"+str+"的所有方法end***************");
         System.out.println();
         System.out.println();
 }
}

class testClass
{
  
  String a;
  String b;
  
  int c,d;
  
  public int add(int c,int d)
  {
   return c+d;
  }
  public String getA() {
   return a;
  }
  public void setA(String a) {
   this.a = a;
  }
  public String getB() {
   return b;
  }
  public void setB(String b) {
   this.b = b;
  }
  
  public String function()
  {
   return null;
   
  }
  public testClass() {
   super();
  }
  public testClass(String a, String b) {
   super();
   this.a = a;
   this.b = b;
  }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值