Invoke method for Java reflection

Source: Internet
Author: User
Tags reflection wrapper

Let's start with a brief introduction to two meta-annotations:
@retention is () The parameter action time in parentheses compile, run, source code
@target () parentheses within the parameter scope variable, method ....

Get Class object
Class clazz = Class.forName ("Cn.ph.test.UserDao");
Get Object methods
Method[] mds = Clazz.getmethods ();
Traverse
for (Method Md:mds) {
/**
* @MyTest. Class Note decoding objects
*invake (Object Obj,object...args)
*/
if (Md.isannotationpresent (Mytest.class)) {
If there is a custom mytest annotation on the current method, execute, otherwise ignore
Md.invoke (New Userdao ());//class corresponding to the method
}

/*
GetMethod The first parameter is the method name, the second parameter is the parameter type of the method (multiple, separated),
method = Clazz.getmethod ("Test", String.class);
The Invoke (Object Obj,object args[]) method of the methods class must be an object for the parameter to be received.
If the parameter is a base type of data, it must be converted to an object of the appropriate wrapper type. The return value of the Invoke () method is always the object,
If the return type of the method that is actually called is the base type data, then the Invoke () method converts it to an object of the corresponding wrapper type.
and return it to
String s = "AA";
Method.invoke (M, (Object) s);//error time, parameter remember strong turn Object
*/

}

Invoke method for Java reflection

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.