现在的位置: 首页 > 综合 > 正文

动态类加载及反射演示类

2018年01月23日 ⁄ 综合 ⁄ 共 4817字 ⁄ 字号 评论关闭
  1. package org.mytest;
  2. import java.lang.reflect.Field;
  3. import java.lang.reflect.InvocationTargetException;
  4. import java.lang.reflect.Method;
  5. /*
  6.  * 动态类加载及反射演示类
  7.  */
  8. public class DynamicClassLoader {
  9.     private String attribute = "我是DynamicClassLoader类的属性!";
  10.     public String str2 = "hi";
  11.     public String getAttribute() {
  12.         return attribute;
  13.     }
  14.     public DynamicClassLoader() {
  15.     }
  16.     public void setAttribute(String attribute) {
  17.         this.attribute = attribute;
  18.     }
  19.     public int getAttribute2() {
  20.         return 110;
  21.     }
  22.     protected void protectedMethod() {
  23.     }
  24.     private void privateMethod() {
  25.     }
  26.     public static void main(String s[]) throws ClassNotFoundException,
  27.             InstantiationException, IllegalAccessException,
  28.             IllegalArgumentException, InvocationTargetException {
  29.         // //////////////////////动态加载//////////////////
  30.         DynamicClassLoader dcl0 = new DynamicClassLoader();// 这是我们通常创建一个类实例的做法--new
  31.         System.out.println("new类实例,并打印实例dcl0的属性 = " + dcl0.getAttribute());
  32.         Class class1 = Class.forName("org.mytest.DynamicClassLoader");
  33.         DynamicClassLoader dcl1 = (DynamicClassLoader) class1.newInstance();
  34.         System.out.println("Class.forName动态加载获得的类实例,并打印实例dcl1的属性 = " + dcl1.getAttribute());
  35.         Class class2 = ClassLoader.getSystemClassLoader().loadClass("org.mytest.DynamicClassLoader");
  36.         DynamicClassLoader dcl2 = (DynamicClassLoader) class2.newInstance();
  37.         System.out.println("ClassLoader.loadClass动态加载获得的类实例,并打印实例dcl2的属性 = "    + dcl2.getAttribute());
  38.         System.out.println("dcl1==dcl2:" + (dcl1==dcl2));
  39.         System.out.println("dcl1==dcl0:" + (dcl1==dcl0));
  40.         System.out.println("dcl2==dcl0:" + (dcl1==dcl0));
  41.         
  42.         System.out.println("dcl0.getClass().getClassLoader():" + (dcl0.getClass().getClassLoader()));
  43.         System.out.println("dcl1.getClass().getClassLoader():" + (dcl1.getClass().getClassLoader()));
  44.         System.out.println("dcl2.getClass().getClassLoader():" + (dcl2.getClass().getClassLoader()));
  45.         //讲解如果要求按照业务逻辑定义自己的对象比对策略(如hibernate的po可以仅仅比对
  46.         //主键、主键等则两个po等),则重写(覆盖)equals方法和hashcode方法
  47.         
  48.         // ///////////////方法反射//////////////////
  49.         DynamicClassLoader d = new DynamicClassLoader();
  50.         Object o = (Object) d;
  51.         System.out.println("o.getClass() = " + o.getClass());
  52.         Method[] methods = o.getClass().getMethods();
  53.         System.out.println("methods.length = " + methods.length);
  54.         for (int i = 0; i < methods.length; i++) {
  55.             System.out.print("method" + (i + 1) + ":/t");
  56.             System.out.println(methods[i]);
  57.         }
  58.         System.out.println("---------------------");
  59.         Method[] methods2 = o.getClass().getDeclaredMethods();
  60.         System.out.println("methods2.length = " + methods2.length);
  61.         for (int i = 0; i < methods2.length; i++) {
  62.             System.out.print("method" + (i + 1) + ":/t");
  63.             System.out.println(methods2[i]);
  64.         }
  65.     
  66.         /////////////////穿插属性反射//////////////////
  67.         System.out.println("--------穿插属性反射--------");
  68.         Field[] fields = o.getClass().getDeclaredFields();
  69.         System.out.println(fields.length);
  70.         for(int i = 0; i<fields.length; i++){
  71.             System.out.print("field" + (i + 1) + ":/t");
  72.             System.out.println(fields[i]);
  73.         }
  74.         System.out.println("---------继续方法反射---------");
  75.         
  76.         /////////////////继续方法反射//////////////////
  77.         for(int i = 0; i<methods.length; i++){
  78.             System.out.print("method" + (i + 1) + ":/t");
  79.             System.out.println(methods[i]);
  80.             Method method = methods[i];
  81.             String methodName = method.getName();
  82.             String returnType = method.getReturnType().getName();
  83.             
  84.             if(methodName.startsWith("get")&&!methodName.startsWith("getClass")){
  85.                 if(returnType.equals("java.lang.String")){
  86.                     String returnval = (String)method.invoke(d,null);
  87.                     System.out.println("java.lang.String reval = "+returnval);
  88.                 }else if(returnType.equals("String")){
  89.                     String returnval = (String)method.invoke(d,null);
  90.                     System.out.println("String reval = "+returnval);
  91.                 }else if(returnType.equals("java.lang.Integer")){
  92.                     Integer returnval = (Integer)method.invoke(d,null);
  93.                     System.out.println("Integer reval = "+returnval);
  94.                 }else if(returnType.equals("int")){
  95.                     Integer returnval = (Integer)method.invoke(d,null);
  96.                     System.out.println("int reval = "+returnval);
  97.                 }
  98.             }
  99.         }
  100.     }
  101. }
  102. /*
  103.  * 几点结论:
  104.  * 对于private的方法getMethods()不反射、getDeclaredMethods()反射;
  105.  * 对于protected的方法getMethods()不反射、getDeclaredMethods()反射;
  106.  * getMethods()反射继承的和自身的public方法、getDeclaredMethods()反射自身声明的所有方法。
  107.  */

抱歉!评论已关闭.