Private methods to invoke classes and parent classes with Java reflection

Source: Internet
Author: User
Today and a friend talked about the invocation of the parent-private method, which was supposed to be easy to do with reflection, because it seemed to make no difference whether it was private or not, but it was not possible to invoke the private method of the call itself, but not the private method of invoking the parent class. The reason for this is probably because the Getdeclaredmethod method and the GetMethod method do not look for a private method of the parent class, so they have to write their own recursion. Put the code out handy for more people. This code can solve a lot of practical problems, but it's not very good to do with reflection. [Java]  View plain copy package com.syj.util.reflect;      import  java.lang.reflect.method;     /**   * <p>   * title:   Private Method Invocation Tool class    * </p>   *    * <p>   *  Description: Private method to invoke a class using Java reflection    * </p>   *    * <p>    * Copyright: Copyright  (c)  2007   * </p>   *     *  @author   Sun Yujia    *  @main  sunyujia@yahoo.cn   * @ date jun 1, 2008 10:18:58 pm   */   public class  privateutil {       /**       *  Use recursion to find the specified method of a class, if not found, go to the father to find up to the top object.        *        *  @param  clazz&nBsp      *             Target Class        *  @param  methodName       *              Method Name        *   @param  classes       *              method parameter type array        *  @return   Method Object         *  @throws  Exception       */        public static method getmethod (class clazz, string methodname,                final class[] classes)  throws Exception {           Method  Method = null;           try {                method = clazz.getdeclaredmethod ( methodname, classes);           } catch  ( Nosuchmethodexception e)  {                try {                    method = clazz.getmethod (methodname, classes);                } catch  (Nosuchmethodexception ex)  {                    if  (Clazz.getsuperclass ()  == null)  {                        return method;                    } else {                        method = getmethod (Clazz.getsuperclass (), methodname,                                 classes);                    }                }           }            return method;       }      &Nbsp;   /**       *        *   @param  obj       *              the object of the adjustment method        *  @param  methodName        *             Method Name        *  @param  classes 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.