利用java反射调用类的的私有方法

  1. import java.lang.reflect.Method;      
  2.   
  3. public class PrivateUtil {      
  4.     /**    
  5.      * 利用递归找一个类的指定方法,如果找不到,去父亲里面找直到最上层Object对象为止。    
  6.      *     
  7.      * @param clazz    
  8.      *            目标类    
  9.      * @param methodName    
  10.      *            方法名    
  11.      * @param classes    
  12.      *            方法参数类型数组    
  13.      * @return 方法对象    
  14.      * @throws Exception    
  15.      */     
  16.     public static Method getMethod(Class clazz, String methodName,      
  17.             final Class[] classes) throws Exception {      
  18.         Method method = null;      
  19.         try {      
  20.             method = clazz.getDeclaredMethod(methodName, classes);      
  21.         } catch (NoSuchMethodException e) {      
  22.             try {      
  23.                 method = clazz.getMethod(methodName, classes);      
  24.             } catch (NoSuchMethodException ex) {      
  25.                 if (clazz.getSuperclass() == null) {      
  26.                     return method;      
  27.                 } else {      
  28.                     method = getMethod(clazz.getSuperclass(), methodName,      
  29.                             classes);      
  30.                 }      
  31.             }      
  32.         }      
  33.         return method;      
  34.     }      
  35.      
  36.     /**    
  37.      *     
  38.      * @param obj    
  39.      *            调整方法的对象    
  40.      * @param methodName    
  41.      *            方法名    
  42.      * @param classes    
  43.      *            参数类型数组    
  44.      * @param objects    
  45.      *            参数数组    
  46.      * @return 方法的返回值    
  47.      */     
  48.     public static Object invoke(final Object obj, final String methodName,      
  49.             final Class[] classes, final Object[] objects) {      
  50.         try {      
  51.             Method method = getMethod(obj.getClass(), methodName, classes);      
  52.             method.setAccessible(true);// 调用private方法的关键一句话      
  53.             return method.invoke(obj, objects);      
  54.         } catch (Exception e) {      
  55.             throw new RuntimeException(e);      
  56.         }      
  57.     }      
  58.      
  59.     public static Object invoke(final Object obj, final String methodName,      
  60.             final Class[] classes) {      
  61.         return invoke(obj, methodName, classes, new Object[] {});      
  62.     }      
  63.      
  64.     public static Object invoke(final Object obj, final String methodName) {      
  65.         return invoke(obj, methodName, new Class[] {}, new Object[] {});      
  66.     }      
  67.      
  68.     /**    
  69.      * 测试反射调用    
  70.      *     
  71.      * @param args    
  72.      */     
  73.     public static void main(String[] args) {      
  74.         PrivateUtil.invoke(new B(), "printlnA"new Class[] { String.class },      
  75.                 new Object[] { "test" });      
  76.         PrivateUtil.invoke(new B(), "printlnB");      
  77.     }      
  78. }      
  79.      
  80. class A {      
  81.     private void printlnA(String s) {      
  82.         System.out.println(s);      
  83.     }      
  84. }      
  85.      
  86. class B extends A {      
  87.     private void printlnB() {      
  88.         System.out.println("b");      
  89.     }      
  90. }     
  91.   
  92. import java.lang.reflect.Method;   
  93.   
  94. public class PrivateUtil {   
  95.     /**  
  96.      * 利用递归找一个类的指定方法,如果找不到,去父亲里面找直到最上层Object对象为止。  
  97.      *   
  98.      * @param clazz  
  99.      *            目标类  
  100.      * @param methodName  
  101.      *            方法名  
  102.      * @param classes  
  103.      *            方法参数类型数组  
  104.      * @return 方法对象  
  105.      * @throws Exception  
  106.      */  
  107.     public static Method getMethod(Class clazz, String methodName,   
  108.             final Class[] classes) throws Exception {   
  109.         Method method = null;   
  110.         try {   
  111.             method = clazz.getDeclaredMethod(methodName, classes);   
  112.         } catch (NoSuchMethodException e) {   
  113.             try {   
  114.                 method = clazz.getMethod(methodName, classes);   
  115.             } catch (NoSuchMethodException ex) {   
  116.                 if (clazz.getSuperclass() == null) {   
  117.                     return method;   
  118.                 } else {   
  119.                     method = getMethod(clazz.getSuperclass(), methodName,   
  120.                             classes);   
  121.                 }   
  122.             }   
  123.         }   
  124.         return method;   
  125.     }   
  126.   
  127.     /**  
  128.      *   
  129.      * @param obj  
  130.      *            调整方法的对象  
  131.      * @param methodName  
  132.      *            方法名  
  133.      * @param classes  
  134.      *            参数类型数组  
  135.      * @param objects  
  136.      *            参数数组  
  137.      * @return 方法的返回值  
  138.      */  
  139.     public static Object invoke(final Object obj, final String methodName,   
  140.             final Class[] classes, final Object[] objects) {   
  141.         try {   
  142.             Method method = getMethod(obj.getClass(), methodName, classes);   
  143.             method.setAccessible(true);// 调用private方法的关键一句话   
  144.             return method.invoke(obj, objects);   
  145.         } catch (Exception e) {   
  146.             throw new RuntimeException(e);   
  147.         }   
  148.     }   
  149.   
  150.     public static Object invoke(final Object obj, final String methodName,   
  151.             final Class[] classes) {   
  152.         return invoke(obj, methodName, classes, new Object[] {});   
  153.     }   
  154.   
  155.     public static Object invoke(final Object obj, final String methodName) {   
  156.         return invoke(obj, methodName, new Class[] {}, new Object[] {});   
  157.     }   
  158.   
  159.     /**  
  160.      * 测试反射调用  
  161.      *   
  162.      * @param args  
  163.      */  
  164.     public static void main(String[] args) {   
  165.         PrivateUtil.invoke(new B(), "printlnA"new Class[] { String.class },   
  166.                 new Object[] { "test" });   
  167.         PrivateUtil.invoke(new B(), "printlnB");   
  168.     }   
  169. }   
  170.   
  171. class A {   
  172.     private void printlnA(String s) {   
  173.         System.out.println(s);   
  174.     }   
  175. }   
  176.   
  177. class B extends A {   
  178.     private void printlnB() {   
  179.         System.out.println("b");   
  180.     }   
  181. }  
  1. da shang
    donate-alipay
               donate-weixin weixinpay

发表评论↓↓