Java反射机制深入

(Reflection)定义:在Java运行时环境中,对于任意一个类,对于任意一个对象,Java的反射机制能够动态获取类的信息以及动态调用对象的方法。
Java反射机制主要提供一下功能:
a.在运行时判断任意一个对象所属的类
b.在运行时构造任意一个类的对象
c.在运行时判断任意一个类所具有的成员变量和方法
d.在运行时调用任意一个对象的方法

在JDK中,主要由以下类来实现Java反射机制,位于java.lang.reflect包中:
Class类:代表一个类
Field类:代表类的成员属性或者成为类的属性
Method类:代表类的方法
Constructor类:代表类的构造方法
Array类:提供了动态创建数组,以及访问数组元素的静态方法

一、通过Class类获取成员变量、成员方法、接口、超类、构造方法等
在java.lang.Object类中定义了getClass()方法,对于任意一个Java对象,都可以通过此方法获取对象的类型。Class类是Reflection API中的核心类,它有以下方法
getName():获取类的完整名字
getFields():获取类的public类型的属性
getDeclaredFields():获取类的所有属性
getMethods():获取类的public类型的方法
getDeclaredMethods():获取类的所有方法
getMethod(String name,Class[] parameterTypes):获取类的特定方法,name指定方法名称,parameterTypes参数指定方法的参数类型
getConstructors():获取类的public类型的构造方法
getConstructor(Class[] parameterTypes):获取类的特定构造方法,parameterTypes指定构造方法的参数类型
newInstance():通过类的不带参数的构造函数build这个类的一个对象


A:Class
package com.javareflect.base.classdemo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;

public class RefConstructor {

public static void main(String[] args) throws Exception {
RefConstructor ref = new RefConstructor();
ref.getConstructor();
}

@SuppressWarnings({ "unchecked", "rawtypes" })
public void getConstructor() throws Exception {
Class c = null;
c = Class.forName("java.lang.Long");
Class[] cs = { java.lang.String.class };
Constructor cs1 = c.getConstructor(cs);
System.out.println("通过参数获取指定Class对象的构造方法:");
System.out.println(cs1.toString());

Constructor cs2 = c.getDeclaredConstructor(cs);
System.out.println("通过参数获取指定Class对象所表示的类或接口的构造方法");
System.out.println(cs2.toString());

Constructor cs3 = c.getEnclosingConstructor();
System.out.println("获取本地或匿名类Constructor对象,它表示基础类的立即封闭构造方法");
if (cs3 != null)
System.out.println(cs3.toString());
else
System.out.println("没有获取到任何构造方法");

Constructor[] cs4 = c.getConstructors();
for (int i = 0; i < cs4.length; i++) {
System.out.println(cs4[i].toString());
}

Type t1 = c.getGenericSuperclass();
System.out.println("返回超类:"+t1.toString());

Class[] cis = c.getClasses();
System.out.println("返回超类和所有实现的接口:");
for(int i =0;i<cis.length;i++){
System.out.println(cis[i].toString());
}

Class[] cls = c.getInterfaces();
        System.out.println("4、实现的接口");
for(int i =0;i<cls.length;i++){
System.out.println(cls[i].toString());
}

Field fs1[] = c.getFields();
        System.out.println("1、类或接口的所有可访问公共字段:");
        for (int i = 0; i < fs1.length; i++) {
            System.out.println(fs1[i].toString());
        }
       
        Field f1 = c.getField("MIN_VALUE");
        System.out.println("2、类或接口的指定已声明指定公共成员字段:");
        System.out.println(f1.toString());
       
        Field fs2[] = c.getDeclaredFields();
        System.out.println("3、类或接口所声明的所有字段:");
        for (int i = 0; i < fs2.length; i++) {
            System.out.println(fs2[i].toString());
        }
       
        Field f2 = c.getDeclaredField("serialVersionUID");
        System.out.println("4、类或接口的指定已声明指定字段:");
        System.out.println(f2.toString());

        System.out.println("\n-------------------------------\n");
       
        Method m1[] = c.getMethods();
        System.out.println("1、返回类所有的公共成员方法:");
        for (int i = 0; i < m1.length; i++) {
            System.out.println(m1[i].toString());
        }

        Method m2 = c.getMethod("longValue", new Class[]{});
        System.out.println("2、返回指定公共成员方法:");
        System.out.println(m2.toString());

        Method[] ms = c.getDeclaredMethods();
        System.out.println("2、返回指定所有成员方法:");
        for (int i = 0; i < ms.length; i++) {
            System.out.println(ms[i].toString());
        }
       
}

}

猜你喜欢

转载自hehefan.iteye.com/blog/2307201