Java Reflection: Load member method

Source: Internet
Author: User
Tags reflection

Java Reflection: Load member method

Package Com.ma.reflection;import Java.lang.reflect.invocationtargetexception;import Java.lang.reflect.Method; Import Org.junit.test;import com.ma.bean.studentbean;/** * Reflection: Load Member Method * @author MA * */public class Demo3 {/** * Test no Parameter member method * @throws classnotfoundexception * @throws nosuchmethodexception * @throws SecurityException * @throws invocationtarget Exception * @throws illegalaccessexception * @throws illegalargumentexception */@Testpublic void Test1 () throws Classno Tfoundexception, SecurityException, Nosuchmethodexception, IllegalArgumentException, IllegalAccessException, invocationtargetexception{//Load class class<?> clazz = Class.forName ("Com.ma.bean.StudentBean");//Load method = Clazz.getmethod ("eat", null);//Call Method Method.invoke (new Studentbean (), null);} /** * Test the method of the participating member * @throws classnotfoundexception * @throws nosuchmethodexception * @throws SecurityException * @throws InvocationTargetException * @throws illegalaccessexception * @throws illegalargumentexception * * @Testpublic void Test2 () throws ClassNotFoundException, SecurityException, Nosuchmethodexception, IllegalArgumentException, illegalaccessexception, invocationtargetexception{//load class class<?> clazz = Class.forName ("Com.ma.bean.StudentBean");//Load Methods method = Clazz.getmethod ("Play", string.class,int.class);// Call Method Method.invoke (new Studentbean (), "Zhang San", 45);}  /** * Test Variable parameter member method * @throws classnotfoundexception * @throws nosuchmethodexception * @throws SecurityException * @throws InvocationTargetException * @throws illegalaccessexception * @throws illegalargumentexception */@Testpublic void test3 () throws ClassNotFoundException, SecurityException, Nosuchmethodexception, IllegalArgumentException, Illegalaccessexception, invocationtargetexception{//load class class<?> clazz = Class.forName (" Com.ma.bean.StudentBean ");//Load method = Clazz.getmethod (" Run ", int[].class);//Call Method Method.invoke (new Studentbean (), New int[]{1,2,3});} /** * Test Private Member Method * @throws classnotfoundexception * @throws NOsuchmethodexception * @throws SecurityException * @throws invocationtargetexception * @throws illegalaccessexception * @throws illegalargumentexception */@Testpublic void test4 () throws ClassNotFoundException, SecurityException, Nosuchmethodexception, IllegalArgumentException, illegalaccessexception, invocationtargetexception{//load class class< ?> clazz = Class.forName ("Com.ma.bean.StudentBean");//Load Methods method = Clazz.getdeclaredmethod ("jump", null);// Set Private method access rights method.setaccessible (TRUE);//Call Method Method.invoke (new Studentbean (), null);} /** * Test static Member Method * @throws classnotfoundexception * @throws nosuchmethodexception * @throws SecurityException * @throws I  Nvocationtargetexception * @throws illegalaccessexception * @throws illegalargumentexception */@Testpublic void Test5 () Throws ClassNotFoundException, SecurityException, Nosuchmethodexception, IllegalArgumentException, Illegalaccessexception, invocationtargetexception{//load class class<?> clazz = Class.forName ("Com.ma.bean.StUdentbean ");//Load Methods method = Clazz.getmethod (" Sleep ", int.class);//Call method, static method do not object can call method Method.invoke (NULL, 34) ;} /** * Main Method Test * @throws classnotfoundexception * @throws nosuchmethodexception * @throws SecurityException * @throws InvocationTargetException * @throws illegalaccessexception * @throws illegalargumentexception */@Testpublic void Test6 ( ) throws ClassNotFoundException, SecurityException, Nosuchmethodexception, IllegalArgumentException, Illegalaccessexception, InvocationTargetException {//load class class<?> Clazz = Class.forName (" Com.ma.bean.StudentBean ");//Load Methods method = Clazz.getmethod (" main ", string[].class);//Call Main method, static method do not object can call method , because the main method is to fold an array, there are two ways to call the Main method//First call//method.invoke (null, new object[]{new string[]{"AA", "BB"}});// The second invocation of Method.invoke (null, (Object) New string[]{"AA", "CC"});

Studentbean class

Package com.ma.bean;/** * Student class * @author MA * */public class Studentbean {public String name = "Liu Bei";p ublic int age;private String work = "Teacher";p rivate static String sex = "male";p ublic Studentbean () {super ();} public void Eat () {System.out.println ("Eat up ..."). ");} public void Play (String name,int age) {System.out.println (name+): Go play yo ... "+age);} public void run (int  ... intaa) {System.out.println (": This is a mutable parameter method");} private void Jump () {System.out.println ("This is Private Method");} public static void sleep (int age) {System.out.println (age+ ": This is a static method");} public static void Main (string[] args) {System.out.println ("This is the Main method!!! ");}}

  

Java Reflection: Load member method

Related Article

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.