JAVA语言中的反射机制:
----Class类
hashCode(), equals(),clone(),toString(),getClass()等,其中的getClass()返回yige
Class 类型的对象。
时的 class和 interface,也用来表达 enum,array,primitive,Java Types 以及关键字void
,当加载一个类,或者当加载器(class loader)的defineClass()被JVM调用,便产生一个Class
对象,
的对象,接下来才能经由后者唤起为数十多个的反射API。
----运行时生成instance
构造方法,,如果想调用带参数的构造方法,就比较的麻烦,不能直接调用Class类中的newInstance()
,而是调用Constructor类中newInstance()方法,首先准备一个Class[]作为Constructor的参数类型。
然后调用该Class对象的getConstructor()方法获得一个专属的Constructor的对象,最后再准备一个
Object[]作为Constructor对象昂的newInstance()方法的实参。
Class类中的newInstance()方法是不带参数的,而Constructro类中的newInstance()方法是带参数的
需要提供必要的参数。
----运行时调用Method
Obeject[]放置自变量,然后调用Method对象的invoke(Object obj,Object[])方法。
----运行时调用Field内容
便可以直接调用Field的 get(Object obj)和set(Object obj,Object value)方法
- package
cn.com.reflection; -
- import
java.lang.reflect.Field; - import
java.lang.reflect.InvocationTargetExceptio n; - import
java.lang.reflect.Method; -
- public
class ReflectTester { -
-
-
public Object copy(Object obj) throws IllegalArgumentException , SecurityException, InstantiationException, IllegalAccessException, InvocationTargetExceptio n, NoSuchMethodException{ -
-
//获得对象的类型 -
Class classType=obj.getClass(); -
System.out.println("该对象的类型是:"+classType.toString()); -
-
//通过默认构造方法去创建一个新的对象,getConstructor的视其参数决定调用哪个构造方法 -
Object objectCopy=classType.getConstructor(new Class[]{}).newInstance(new Object[]{}); -
-
//获得对象的所有属性 -
Field[] fields=classType.getDeclaredFields(); -
-
for(int i=0;i -
//获取数组中对应的属性 -
Field field=fields[i]; -
-
String fieldName=field.getName(); -
String stringLetter=fieldName.substring(0, 1).toUpperCase(); -
-
//获得相应属性的getXXX和setXXX方法名称 -
String getName="get"+stringLetter+fieldName.substring(1); -
String setName="set"+stringLetter+fieldName.substring(1); -
-
//获取相应的方法 -
Method getMethod=classType.getMethod(getName, new Class[]{}); -
Method setMethod=classType.getMethod(setName, new Class[]{field.getType()}); -
-
//调用源对象的getXXX()方法 -
Object value=getMethod.invoke(obj, new Object[]{}); -
System.out.println(fieldName+" :"+value); -
-
//调用拷贝对象的setXXX()方法 -
setMethod.invoke(objectCopy,new Object[]{value}); -
-
-
} -
-
return objectCopy; -
-
} -
-
-
public static void main(String[] args) throws IllegalArgumentException , SecurityException, InstantiationException, IllegalAccessException, InvocationTargetExceptio n, NoSuchMethodException { -
Customer customer=new Customer(); -
customer.setName("hejianjie"); -
customer.setId(new Long(1234)); -
customer.setAge(19); -
-
Customer customer2=null; -
customer2=(Customer)new ReflectTester().copy(customer); -
System.out.println(customer.getName()+" "+customer2.getAge()+" "+customer2.getId()); -
-
System.out.println(customer); -
System.out.println(customer2); -
-
-
} -
- }
-
-
- class
Customer{ -
-
private Long id; -
-
private String name; -
-
private int age; -
-
-
public Customer(){ -
-
} -
-
public int getAge() { -
return age; -
} -
-
-
public void setAge(int age) { -
this.age = age; -
} -
-
-
public Long getId() { -
return id; -
} -
-
-
public void setId(Long id) { -
this.id = id; -
} -
-
-
public String getName() { -
return name; -
} -
-
-
public void setName(String name) { -
this.name = name; -
} -
- }
- package
cn.com.reflection; -
- import
java.lang.reflect.Array; -
- public
class ArrayTester1 { -
-
-
public static void main(String[] args) throws ClassNotFoundException { -
-
Class classType=Class.forName("java.lang.String"); -
-
Object array= Array.newInstance(classType,10); //指定数组的类型和大小 -
-
//对索引为5的位置进行赋值 -
Array.set(array, 5, "hello"); -
-
String s=(String)Array.get(array, 5); -
-
System.out.println(s); -
-
-
//循环遍历这个动态数组 -
for(int i=0;i<((String[])array).length;i++){ -
-
String str=(String)Array.get(array, i); -
-
System.out.println(str); -
} -
-
} -
- }