java反射
在 java.lang.Object 类中定义了 getClass()方法, 因此对于任意一个 Java 对象, 都可以通过此方法获得对象的类型。
1.获得Class对象的三种方式
Class<?> c1 = Class.forName("java.lang.Boolean");
Class<?> c2 = Boolean.class;//类名.class
Class<?> c3 = 对象.getClass();
2.Class 类是 Reflection API 中的核心类,它有以下方法。
-
getName():获得类的完整名字。
- getFields():获得类的 public 类型的属性。
- getDeclaredFields():获得类的所有属性。
- getMethods():获得类的 public 类型的方法。
- getDeclaredMethods():获得类的所有方法。
- getMethod(String name, Class[] parameterTypes):获得类的特定方法,name参数指定方法的名字,parameterTypes参数指定方法的参数类型。
- getConstrutors():获得类的 public类型的构造方法。
- getConstrutor(Class[] parameterTypes):获得类的特定构造方法, parameterTypes参数指定构造方法的参数类型。
- newInstance():通过类的不带参数的构造方法创建这个类的一个对象。
-
public Method getDeclaredMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
-
public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
-
public Method getMethod(String name,
Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
-
public Field getField(String name) throws NoSuchFieldException, SecurityException
java.lang.reflect.Method的重要方法:
Object |
invoke(Object obj, Object... args)
Invokes the underlying method represented by this
Method object, on the specified object with the specified parameters. |
java.lang.reflect.Constructor<T>的重要方法:
public T newInstance(Object... initargs) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
package com.dd; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Constructor; import java.lang.reflect.Method; class Person{ private String name; public Person(){ this(""); } public Person(String name){ this.name = name; } public String getName(){ return name; } public void setName(String name){ this.name = name; } } public class Test { public static void main(String[] args) throws InstantiationException, IllegalAccessException { // TODO Auto-generated method stub Class<?> c1 = null; Class<?> c2 = null; Class<?> c3 = null; c2 = Person.class; c3 = new Person("ttdd").getClass(); try{ c1 = Class.forName("com.dd.Person"); c3 = Class.forName("java.lang.Boolean"); }catch(ClassNotFoundException e){ e.printStackTrace(); } System.out.println(c2.getName()); System.out.println(c1.getCanonicalName()); Field[] fields = c1.getDeclaredFields(); for(Field f : fields){ System.out.println(f); } Method[] methods = c3.getDeclaredMethods(); for(Method m : methods){ System.out.println(m); } Constructor[] constructors = c2.getDeclaredConstructors(); for(Constructor c : constructors){ System.out.println(c); } Person p = (Person) c2.newInstance(); Field f = null; try { f = c2.getDeclaredField("name"); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } f.setAccessible(true); System.out.println(p.getName()); f.set(p, "wf"); System.out.println(p.getName()); Method m = null; Method sm = null; try { m = c2.getDeclaredMethod("getName"); sm = c2.getDeclaredMethod("setName", String.class); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { sm.invoke(p, "asdfasdf"); System.out.println(m.invoke(p)); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }