Java methods for performing objects using reflection

Source: Internet
Author: User
Import java.lang.reflect.InvocationTargetException;

Import Java.lang.reflect.Method; public class Classutil {/** * methods of performing srcclass by reflection * @param object---The object underlying method is I nvoked from * @param srcclass---class @param paramclasses---parameter class array * @param methodname---Side Name * @param args---The arguments used for the "method" Call * @return * @throws SecurityException *  @throws nosuchmethodexception * @throws illegalaccessexception * @throws illegalargumentexception * @throws InvocationTargetException */public static object Executeobjectmethod (object, class<?> Srcclass, Cl Ass<?>[] paramclasses, String methodname, Object ... args) throws SecurityException, Nosuchmethodexception, Illegalaccessexception, IllegalArgumentException, InvocationTargetException {Method method = Srcclass.getmethod (M
        Ethodname, paramclasses); Return Method.invoke (object, args); The public static void main (string[] args) throws Exception {//tohexstring static method that performs an integer by reflection.
        Out.println (integer.tohexstring (233));
        SYSTEM.OUT.PRINTLN (Classutil.executeobjectmethod (null, Integer.class, New Class[]{int.class}, "Tohexstring", 233));
        String value = new String ("test");
    Executes the length () method of the String Object System.out.println (Executeobjectmethod (Value, Value.getclass (), NULL, "Length");
 }

}

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.