反射机制是Java的一个重要性,它使得Java语言具有了动态特性。比如说,可以在代码中动态地获取某个类的信息,生成它的实例、获取其成员变量、调用它的方法。下面通过几个示例来演示反射机制的作用与用法。
示例1.获取类的信息
import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Type; /* * @Date 20160223 * 演示了如何通过反射机制获取指定类的构造方法、超类、接口、域和方法 */ public class ClassInfoCollector { public void run() throws Exception { Class c = Class.forName("java.lang.Long"); Class [] cs = {java.lang.String.class}; System.out.println(" ------------------------------- "); Constructor cs1 = c.getConstructor(cs); System.out.println("1、通过参数获取指定Class对象的构造方法:"); System.out.println(cs1.toString()); Constructor cs2 = c.getDeclaredConstructor(cs); System.out.println("2、通过参数获取指定Class对象所表示的类或接口的构造方法:"); System.out.println(cs2.toString()); Constructor cs3 = c.getEnclosingConstructor(); System.out.println("3、获取本地或匿名类Constructor 对象,它表示基础类的立即封闭构造方法。"); if (cs3 != null) System.out.println(cs3.toString()); else System.out.println("-- 没有获取到任何构造方法!"); Constructor[] css = c.getConstructors(); System.out.println("4、获取指定Class对象的所有构造方法:"); for (int i = 0; i < css.length; i++) { System.out.println(css[i].toString()); } System.out.println(" ------------------------------- "); Type [] tys1 = c.getGenericInterfaces(); System.out.println("1、返回直接实现的接口:"); for (int i = 0; i < tys1.length; i++) { System.out.println(tys1[i].toString()); } Type type1 = c.getGenericSuperclass(); System.out.println("2、返回直接超类:"); System.out.println(type1.toString()); Class [] cls1 = c.getClasses(); System.out.println("3、返回超类和所有实现的接口:"); for (int i = 0; i < cls1.length; i++) { System.out.println(cls1[i].toString()); } Class [] cls2 = c.getInterfaces(); System.out.println("4、实现的接口"); for (int i = 0; i < cls2.length; i++) { System.out.println(cls2[i].toString()); } System.out.println(" ------------------------------- "); Field [] fds1 = c.getFields(); System.out.println("1、类或接口的所有可访问公共字段:"); for (int i = 0; i < fds1.length; i++) { System.out.println(fds1[i].toString()); } Field [] fds2 = c.getDeclaredFields(); System.out.println("2、类或接口所声明的所有字段:"); for (int i = 0; i < fds2.length; i++) { System.out.println(fds2[i].toString()); } Field fd1 = c.getField("MIN_VALUE"); System.out.println("3、类或接口的指定已声明指定公共成员字段:"); System.out.println(fd1.toString()); Field fd2 = c.getDeclaredField("serialVersionUID"); System.out.println("4、类或接口的指定已声明指定字段:"); System.out.println(fd2.toString()); System.out.println(" ------------------------------- "); Method [] md1 = c.getMethods(); System.out.println("1、返回类所有的公共成员方法:"); for (int i = 0; i < md1.length; i++) { System.out.println(md1[i].toString()); } Method [] md2 = c.getDeclaredMethods(); System.out.println("2、返回类所有的成员方法:"); for (int i = 0; i < md2.length; i++) { System.out.println(md2[i].toString()); } Method m3 = c.getMethod("longValue", new Class[]{}); System.out.println("3、返回指定公共成员方法:"); System.out.println(m3.toString()); } public static void main(String args[]) throws Exception { ClassInfoCollector cic = new ClassInfoCollector(); cic.run(); } }
输出如下:
-------------------------------
1、通过参数获取指定Class对象的构造方法:
public java.lang.Long(java.lang.String) throws java.lang.NumberFormatException
2、通过参数获取指定Class对象所表示的类或接口的构造方法:
public java.lang.Long(java.lang.String) throws java.lang.NumberFormatException
3、获取本地或匿名类Constructor 对象,它表示基础类的立即封闭构造方法。
-- 没有获取到任何构造方法!
4、获取指定Class对象的所有构造方法:
public java.lang.Long(long)
public java.lang.Long(java.lang.String) throws java.lang.NumberFormatException
-------------------------------
1、返回直接实现的接口:
java.lang.Comparable<java.lang.Long>
2、返回直接超类:
class java.lang.Number
3、返回超类和所有实现的接口:
4、实现的接口
interface java.lang.Comparable
-------------------------------
1、类或接口的所有可访问公共字段:
public static final long java.lang.Long.MIN_VALUE
public static final long java.lang.Long.MAX_VALUE
public static final java.lang.Class java.lang.Long.TYPE
public static final int java.lang.Long.SIZE
public static final int java.lang.Long.BYTES
2、类或接口所声明的所有字段:
public static final long java.lang.Long.MIN_VALUE
public static final long java.lang.Long.MAX_VALUE
public static final java.lang.Class java.lang.Long.TYPE
private final long java.lang.Long.value
public static final int java.lang.Long.SIZE
public static final int java.lang.Long.BYTES
private static final long java.lang.Long.serialVersionUID
3、类或接口的指定已声明指定公共成员字段:
public static final long java.lang.Long.MIN_VALUE
4、类或接口的指定已声明指定字段:
private static final long java.lang.Long.serialVersionUID
-------------------------------
1、返回类所有的公共成员方法:
public static int java.lang.Long.numberOfLeadingZeros(long)
public static int java.lang.Long.numberOfTrailingZeros(long)
public static int java.lang.Long.bitCount(long)
public boolean java.lang.Long.equals(java.lang.Object)
public java.lang.String java.lang.Long.toString()
public static java.lang.String java.lang.Long.toString(long,int)
public static java.lang.String java.lang.Long.toString(long)
public int java.lang.Long.hashCode()
public static int java.lang.Long.hashCode(long)
public static long java.lang.Long.min(long,long)
public static long java.lang.Long.max(long,long)
public static long java.lang.Long.reverseBytes(long)
public int java.lang.Long.compareTo(java.lang.Object)
public int java.lang.Long.compareTo(java.lang.Long)
public static java.lang.Long java.lang.Long.getLong(java.lang.String,java.lang.Long)
public static java.lang.Long java.lang.Long.getLong(java.lang.String,long)
public static java.lang.Long java.lang.Long.getLong(java.lang.String)
public byte java.lang.Long.byteValue()
public short java.lang.Long.shortValue()
public int java.lang.Long.intValue()
public long java.lang.Long.longValue()
public float java.lang.Long.floatValue()
public double java.lang.Long.doubleValue()
public static java.lang.Long java.lang.Long.valueOf(long)
public static java.lang.Long java.lang.Long.valueOf(java.lang.String,int) throws java.lang.NumberFormatException
public static java.lang.Long java.lang.Long.valueOf(java.lang.String) throws java.lang.NumberFormatException
public static java.lang.String java.lang.Long.toHexString(long)
public static int java.lang.Long.compare(long,long)
public static java.lang.Long java.lang.Long.decode(java.lang.String) throws java.lang.NumberFormatException
public static long java.lang.Long.reverse(long)
public static long java.lang.Long.sum(long,long)
public static int java.lang.Long.compareUnsigned(long,long)
public static long java.lang.Long.divideUnsigned(long,long)
public static long java.lang.Long.highestOneBit(long)
public static long java.lang.Long.lowestOneBit(long)
public static long java.lang.Long.parseLong(java.lang.String,int) throws java.lang.NumberFormatException
public static long java.lang.Long.parseLong(java.lang.String) throws java.lang.NumberFormatException
public static long java.lang.Long.remainderUnsigned(long,long)
public static long java.lang.Long.rotateLeft(long,int)
public static long java.lang.Long.rotateRight(long,int)
public static int java.lang.Long.signum(long)
public static java.lang.String java.lang.Long.toBinaryString(long)
public static java.lang.String java.lang.Long.toOctalString(long)
public static java.lang.String java.lang.Long.toUnsignedString(long)
public static java.lang.String java.lang.Long.toUnsignedString(long,int)
public static long java.lang.Long.parseUnsignedLong(java.lang.String) throws java.lang.NumberFormatException
public static long java.lang.Long.parseUnsignedLong(java.lang.String,int) throws java.lang.NumberFormatException
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
2、返回类所有的成员方法:
public static int java.lang.Long.numberOfLeadingZeros(long)
public static int java.lang.Long.numberOfTrailingZeros(long)
public static int java.lang.Long.bitCount(long)
public boolean java.lang.Long.equals(java.lang.Object)
public java.lang.String java.lang.Long.toString()
public static java.lang.String java.lang.Long.toString(long,int)
public static java.lang.String java.lang.Long.toString(long)
public int java.lang.Long.hashCode()
public static int java.lang.Long.hashCode(long)
public static long java.lang.Long.min(long,long)
public static long java.lang.Long.max(long,long)
public static long java.lang.Long.reverseBytes(long)
public int java.lang.Long.compareTo(java.lang.Object)
public int java.lang.Long.compareTo(java.lang.Long)
public static java.lang.Long java.lang.Long.getLong(java.lang.String,java.lang.Long)
public static java.lang.Long java.lang.Long.getLong(java.lang.String,long)
public static java.lang.Long java.lang.Long.getLong(java.lang.String)
public byte java.lang.Long.byteValue()
public short java.lang.Long.shortValue()
public int java.lang.Long.intValue()
public long java.lang.Long.longValue()
public float java.lang.Long.floatValue()
public double java.lang.Long.doubleValue()
public static java.lang.Long java.lang.Long.valueOf(long)
public static java.lang.Long java.lang.Long.valueOf(java.lang.String,int) throws java.lang.NumberFormatException
public static java.lang.Long java.lang.Long.valueOf(java.lang.String) throws java.lang.NumberFormatException
public static java.lang.String java.lang.Long.toHexString(long)
public static int java.lang.Long.compare(long,long)
public static java.lang.Long java.lang.Long.decode(java.lang.String) throws java.lang.NumberFormatException
static void java.lang.Long.getChars(long,int,char[])
public static long java.lang.Long.reverse(long)
static int java.lang.Long.stringSize(long)
public static long java.lang.Long.sum(long,long)
public static int java.lang.Long.compareUnsigned(long,long)
public static long java.lang.Long.divideUnsigned(long,long)
public static long java.lang.Long.highestOneBit(long)
public static long java.lang.Long.lowestOneBit(long)
public static long java.lang.Long.parseLong(java.lang.String,int) throws java.lang.NumberFormatException
public static long java.lang.Long.parseLong(java.lang.String) throws java.lang.NumberFormatException
public static long java.lang.Long.remainderUnsigned(long,long)
public static long java.lang.Long.rotateLeft(long,int)
public static long java.lang.Long.rotateRight(long,int)
public static int java.lang.Long.signum(long)
public static java.lang.String java.lang.Long.toBinaryString(long)
public static java.lang.String java.lang.Long.toOctalString(long)
public static java.lang.String java.lang.Long.toUnsignedString(long)
public static java.lang.String java.lang.Long.toUnsignedString(long,int)
static java.lang.String java.lang.Long.toUnsignedString0(long,int)
static int java.lang.Long.formatUnsignedLong(long,int,char[],int,int)
public static long java.lang.Long.parseUnsignedLong(java.lang.String) throws java.lang.NumberFormatException
public static long java.lang.Long.parseUnsignedLong(java.lang.String,int) throws java.lang.NumberFormatException
private static java.math.BigInteger java.lang.Long.toUnsignedBigInteger(long)
3、返回指定公共成员方法:
public long java.lang.Long.longValue()
=========================
可以看到,能够获取的信息非常全面。
示例2.动态调用类的方法
import java.lang.reflect.Method; import java.lang.reflect.Type; /* * @Date 20160223 * 演示了如何通过反射机制调用指定类的方法,以add()和echo()为例 */ public class MethodInvoker { public int add(int param1, int param2) { return param1 + param2; } public String echo(String msg) { return "echo: " + msg; } public static void main(String[] args) throws Exception { Class<?> classType = MethodInvoker.class; Object invokeTester = classType.newInstance(); //获取InvokeTester类的add()方法 Method addMethod = classType.getMethod("add", new Class[]{int.class, int.class}); //调用invokeTester对象上的add()方法 Object result = addMethod.invoke(invokeTester, new Object[]{new Integer(100), new Integer(200)}); System.out.println((Integer) result); //获取InvokeTester类的echo()方法 Method echoMethod = classType.getMethod("echo", new Class[]{String.class}); //调用invokeTester对象的echo()方法 result = echoMethod.invoke(invokeTester, new Object[]{"Hello"}); System.out.println((String) result); } }
输出如下:
300
echo: Hello
===================================
在示例2中,首先使用classType.newInstance()生成了该类的对象,然后获取了其add和echo方法,最后调用了这两个方法。
示例3.访问对象的成员变量
import java.lang.reflect.Field; /* * @Date 20160223 * 演示了如何通过java.lang.reflect.Field类的get和set方法动态调整对象的域 */ public class FieldModifier { public double x; public double y; private double z; public static void main(String args[]) throws NoSuchFieldException, IllegalAccessException { Class c = FieldModifier.class; Field xf = c.getField("x"); Field yf = c.getField("y"); Field zf = c.getDeclaredField("z"); FieldModifier obj = new FieldModifier(); //变更成员x值 System.out.println("变更前x=" + xf.get(obj)); xf.set(obj, 1.1); System.out.println("变更后x=" + xf.get(obj)); //变更成员y值 System.out.println("变更前y=" + yf.get(obj)); yf.set(obj, 2.1); System.out.println("变更后y=" + yf.get(obj)); //变更成员z值 System.out.println("变更前z=" + zf.get(obj)); zf.set(obj, 3.1); System.out.println("变更后z=" + zf.get(obj)); } }
输出如下:
变更前x=0.0
变更后x=1.1
变更前y=0.0
变更后y=2.1
变更前z=0.0
变更后z=3.1
==================
示例3通过Field的get和set方法访问成员变量,包括私有成员。
示例4.动态创建和调整数组
import java.lang.reflect.Array; /* * @Date 20160223 * 演示了如何通过java.lang.reflect.Array动态创建和调整数组 * */ public class DynamicArrayTest2 { public static void main(String args[]) { int[] dims = new int[]{5, 10, 15};//指定即将创造的数组在每一维度上的长度 //创建一个具有指定的组件类型和维度的新数组。 Object array = Array.newInstance(Integer.TYPE, dims); Object arrayObj = Array.get(array, 3);//第array[][][3]个5*10的整型[][]数组 Class<?> cls = arrayObj.getClass().getComponentType(); System.out.println(cls); arrayObj = Array.get(arrayObj, 5);//第array[][5][3]个整型[]数组 Array.setInt(arrayObj, 10, 37);//第array[10][5][3]个整型数,设置其值为37 int arrayCast[][][] = (int[][][]) array; System.out.println(arrayCast[3][5][10]); } }
输出如下:
class [I
37
=========================
利用java.lang.reflect.Array来实现数组的创建与操作。