Java反射机制(Reflection)
1、什么是反射机制?
简而言之,java反射机制就是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象都能够调用它的任意一个方法;这种动态获取信息以及动态调用对象的方法的功能称为java的反射机制,个人理解就是一个通过一个这样的工具透彻的了解自己哈
在平常的编程中不会用到,but it’s the backbone for most of the Java, J2EE frameworks
Reflection is the process of examining or modifying the runtime behaviour of a class at runtime.
The java.lang.Class class provides many methods that can be used to get metadata, examine and change the runtime behaviour of a class.
Where is it used?
The Reflection API is mainly used in:
|
2、反射相关的API
<1>java.lang
Class<T>:表示一个正在运行的Java应用程序中的类和接口,是Reflection的起源
The java.lang.Class class performs mainly two tasks:
- provides methods to get the metadata of a class at runtime.
- provides methods to examine and change the runtime behaviour of a class.
Commonly used methods of Class class:
| Method | Description |
|---|---|
| 1) public String getName() | returns the class name |
| 2) public static Class forName(String className)throws ClassNotFoundException | loads the class and returns the reference of Class class. |
| 3) public Object newInstance()throws InstantiationException,IllegalAccessException | creates new instance. |
| 4) public boolean isInterface() | checks if it is interface. |
| 5) public boolean isArray() | checks if it is array. |
| 6) public boolean isPrimitive() | checks if it is primitive. |
| 7) public Class getSuperclass() | returns the superclass class reference. |
| 8) public Field[] getDeclaredFields()throws SecurityException | returns the total number of fields of this class. |
| 9) public Method[] getDeclaredMethods()throws SecurityException | returns the total number of methods of this class. |
| 10) public Constructor[] getDeclaredConstructors()throws SecurityException | returns the total number of constructors of this class. |
| 11) public Method getDeclaredMethod(String name,Class[] parameterTypes)throws NoSuchMethodException,SecurityException | returns the method class instance. |
<2>java.lang.reflect
Field、Method、Constructor、Array等
Constructor类:
1、Constructor getConstructor(Class[] params)
获得使用特殊的参数类型的公共构造函数
2、Constructor[] getConstructors()
获得类的所有公共构造函数
3、Constructor getDeclaredConstructor(Class[] params)
获得使用特定参数类型的构造函数(不限制访问级别)
4、Constructor[] getDeclaredConstructors()
获得类的所有构造函数(不限制访问级别)
Class 类
生成类的四种方法:使用对象的getClass();使用static method Class.forName() (最常被使用);使用.class语法;如果是java的封装类型,使用TYPE语法
对于上述Constructor类和Class 类使用,用代码来说明:
1 package com.reflect; 2 3 public class Person { 4 5 private String name; 6 private int age; 7 8 public Person() 9 { 10 11 } 12 13 public Person(String name, int age) 14 { 15 this.name = name; 16 this.age = age; 17 } 18 19 Person(int age) 20 { 21 this.age = age; 22 } 23 24 private Person(String name) 25 { 26 this.name = name; 27 } 28 29 public String getName() { 30 return name; 31 } 32 33 public void setName(String name) { 34 this.name = name; 35 } 36 37 public int getAge() { 38 return age; 39 } 40 41 public void setAge(int age) { 42 this.age = age; 43 } 44 45 @Override 46 public String toString() { 47 return "Person [name=" + this.getName() + ", age=" + this.getAge() + "]"; 48 } 49 50 51 }
1 package com.reflect; 2 3 import java.lang.reflect.Constructor; 4 5 public class ConstructorDemo { 6 7 public static void main(String[] args) 8 { 9 try { 10 Class<?> c = Class.forName("com.reflect.Person");//此处类名一定要写完整 11 //获得类的所有公共构造函数 12 System.out.println("所有公共构造函数"); 13 Constructor[] constructors = c.getConstructors(); 14 for(int i = 0; i < constructors.length; i++) 15 { 16 System.out.println(constructors[i].toGenericString()); 17 } 18 19 //获得使用特殊的参数类型的公共构造函数 20 System.out.println("所有指定参数公共构造函数"); 21 //Constructor constru = c.getConstructor(new Class[]{String.class, Integer.class}); 22 Constructor constru = c.getConstructor(new Class[]{String.class, int.class}); 23 24 System.out.println(constru.toGenericString()); 25 26 // 27 System.out.println("获得使用特定参数类型的构造函数(不限制访问级别)"); 28 Constructor constru1 = c.getDeclaredConstructor(new Class[]{String.class}); 29 System.out.println(constru1.toGenericString()); 30 // 31 System.out.println("获得类的所有构造函数(不限制访问级别)"); 32 Constructor[] constru2 = c.getDeclaredConstructors(); 33 for(int i = 0; i < constru2.length; i++) 34 { 35 System.out.println(constru2[i].toGenericString()); 36 } 37 38 39 } catch (ClassNotFoundException e) { 40 // TODO Auto-generated catch block 41 e.printStackTrace(); 42 } catch (NoSuchMethodException e) { 43 // TODO Auto-generated catch block 44 e.printStackTrace(); 45 } catch (SecurityException e) { 46 // TODO Auto-generated catch block 47 e.printStackTrace(); 48 } 49 50 51 } 52 53 54 55 }
所有公共构造函数
public com.reflect.Person(java.lang.String,int)
public com.reflect.Person()
所有指定参数公共构造函数
public com.reflect.Person(java.lang.String,int)
获得使用特定参数类型的构造函数(不限制访问级别)
private com.reflect.Person(java.lang.String)
获得类的所有构造函数(不限制访问级别)
private com.reflect.Person(java.lang.String)
com.reflect.Person(int)
public com.reflect.Person(java.lang.String,int)
public com.reflect.Person()
1 package com.reflect; 2 3 public class ClassDemo { 4 5 public static void main(String[] args) 6 { 7 Class<?> c1 = null; 8 Class<?> c2 = null; 9 Class<?> c3 = null; 10 Class<?> c4 = null; 11 12 //方式一:使用对象的getClass() 13 Person p = new Person(); 14 c1 = p.getClass(); 15 16 //方式二:使用static method Class.forName() (最常被使用) 17 try { 18 c2 = Class.forName("com.reflect.Person");//此处类名一定要写完整 19 } catch (ClassNotFoundException e) { 20 // TODO Auto-generated catch block 21 e.printStackTrace(); 22 } 23 24 //方式三:使用.class语法 25 c3 = Person.class; 26 27 //方式四:如果是java的封装类型,使用TYPE语法 28 c4 = Integer.TYPE; 29 30 System.out.println(c1.getName()); 31 System.out.println(c2.getName()); 32 System.out.println(c3.getName()); 33 System.out.println(c4.getName()); 34 35 36 } 37 }
运行结果:
com.reflect.Person
com.reflect.Person
com.reflect.Person
int

浙公网安备 33010602011771号