package j2se.classloader; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** * <p>Project : J2SE的基础知识</p> * <p>Tile : Java 的反射机制</p> * <p>Description : * 1.每一个 *.class 文件 , 被 class loader , loader 到JVM 内存中 , * 都会有一个Class 对象 与之相对应 , 从而我们通过 Class 对象 , 就 * 可以得到该类的所有信息 , 比如 : 类中的方法 , 变量 , 参数的类型 等 * </p> * <p>Date : 2014-03-22</p> * @author * @version 1.0 */ public class TestReflection { /** * * @param args 类型为 : String[] * @return void */ public static void main(String[] args){ try { // static Class<?> forName(String className) 该方法将指定的类 , loader 到 JVM 的内存中 // Class<?> clazz = Class.forName("j2se.classloader.Test") ; // static Class<?> forName(String name, boolean initialize, ClassLoader loader) 该方法 // 使用指定的 class loader 来 将某个类 loader 到 JVM 的内存中 ; Class<?> clazz = Class.forName("j2se.classloader.Test", true , ClassLoader.getSystemClassLoader()) ; // T newInstance() 该方法通过调用类中无的 constructor 方法 来 实例化该类 ; // Object obj = clazz.newInstance() ; // Constructor<T> getConstructor(Class<?>... parameterTypes) 该方法可以获得指定参数类型的 构造方法的对象 // 用来获得 public 修饰符 的构造方法 ; // T newInstance(Object... initargs) 该方法 , 根据指定的参数来初始化某个类的实例 ; // Object obj = clazz.getConstructor(new Class[]{Integer.class}).newInstance(new Object[]{new Integer(1)}) ; // Object obj = clazz.getConstructor(Integer.class).newInstance(new Integer(1)) ; // Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) 该方法用来获得 除 private 修饰外的 构造方法 // 的对象 ; Object obj = clazz.getDeclaredConstructor(Integer.class).newInstance(1) ; System.out.println("---------获取成员变量,private的无法获取到--------------"); // Field[] getFields() 得到类的成员变量 , 但是不包括 private 修饰的变量 ; Field[] fs = clazz.getFields() ; for(Field f: fs){ System.out.println("---得到该 Test 类的成员变量信息 [ field_name = " + f.getName() + " , field_modified = " + f.getModifiers() + " , field_type = " + f.getType()+" ] " ); } System.out.println("---------获取成员变量,包括private的获取到--------------"); // Field[] getDeclaredFields() 得到类的成员变量 , 也包括 private 修饰的变量 ; fs = clazz.getDeclaredFields() ; for(Field f : fs){ System.out.println("---得到该 Test 类的成员变量信息 [ field_name = " + f.getName() + " , field_modified = " + f.getModifiers() + " , field_type = " + f.getType()+" ] " ); } System.out.println("---------获取方法名字,private的无法获取到--------------"); // Method[] getMethods() , 得到类中的方法 , 但是 除掉 private 修饰的方法 ; Method[] mds = clazz.getMethods() ; for(Method m : mds ){ System.out.println("---得到该 Test 类中的方法信息 [ method_name = " + m.getName() + " , method_modified = " + m.getModifiers() + " , method_type = " + m.getReturnType()+" ] " ); } System.out.println("---------获取方法名字,包括private的获取到--------------"); // Method[] getDeclaredMethods() 得到类中的方法 , 也包括 private 修饰的方法; mds = clazz.getDeclaredMethods() ; for(Method m : mds ){ System.out.println("---得到该 Test 类中的方法信息 [ method_name = " + m.getName() + " , method_modified = " + m.getModifiers() + " , method_type = " + m.getReturnType()+" ] " ); } System.out.println("--------------invoke()--------------------"); for(Method m : mds){ if(m.getName().equals("publicMethod")){ // Object invoke(Object obj, Object... args) , 利用反射来调用类中的方法 , 就是不能调用 private 修饰的方法 ; m.invoke(obj) ; } } String code = (String)clazz.getDeclaredMethod("publicMethodOfParam", String.class) .invoke(obj, " publicMethodOfParam method is invoke ") ; System.out.println("------------ publicMethodOfParam method return param value = " + code + "-------------"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * * <p> * Description: 声明一个类 , 通过 java 的反射 , 来得到该类的信息 ; * </p> * */ class Test { private Integer i ; public String str ; public Test test ; static { System.out.println("----------in Test static code block -----------"); } Test() { System.out.println("------------Init in Test constractor() method -------------"); } Test( Integer i ){ this.i = i ; System.out.println("------------Init in Test constractor(int i ) method -------------"); } public void publicMethod() { System.out.println("------- at publicMethod method ---------"); } private void privateMethod() { System.out.println("------ at privateMehthod method ---------"); } public String publicMethodOfParam(String str) { System.out.println("------- param str ==== " + str); return "0" ; } }
java 反射
最后发布:2014-03-23 21:01:32首次发布:2014-03-23 21:01:32