Turn Android reflection

Source: Internet
Author: User
From: http://hi.baidu.com/imaboy/item/1aa30b12b888e5eb9913d66c
Android reflection

Java reflection mechanism
The Java reflection mechanism is in the running state. For any class, all attributes and methods of this class can be known; for any object, any method of this class can be called; this kind of dynamically obtained information and the function of dynamically calling object methods is called the reflection mechanism of Java language.
The Java reflection mechanism mainly provides the following functions: to judge the class to which any object belongs at runtime; to construct the object of any class at runtime; judge the member variables and methods of any class at runtime; call the methods of any object at runtime; generate a dynamic proxy.
1. Get the attributes of an object

1 public object getproperty (Object owner, string fieldname) throws exception {
2 class ownerclass = owner. getclass ();
3
4 field = ownerclass. getfield (fieldname );
5
6 object property = field. Get (owner );
7
8 return property;
9}
Class ownerclass = owner. getclass (): obtains the class of the object.

Field field = ownerclass. getfield (fieldname): obtains the attribute declared by the class through the class.

Object Property = field. Get (owner): obtains the instance of this attribute through the object. If this attribute is not public, illegalaccessexception is reported here.

2. Obtain the static attributes of a class.

1 public object getstaticproperty (string classname, string fieldname)
2 throws exception {
3 class ownerclass = Class. forname (classname );
4
5 Field field = ownerclass. getfield (fieldname );
6
7 object property = field. Get (ownerclass );
8
9 return property;
10}

Class ownerclass = Class. forname (classname): first obtain the class of this class.

Field field = ownerclass. getfield (fieldname): Same as above, get the attribute declared by class through class.

Object Property = field. Get (ownerclass): This is somewhat different from the above. Because this property is static, it is directly retrieved from the class of the class.

3. Execute the method of an object

1 public object invokemethod (Object owner, string methodname, object [] ARGs) throws exception {
2
3 class ownerclass = owner. getclass ();
4
5 class [] argsclass = new class [args. Length];
6
7 For (INT I = 0, j = args. length; I <j; I ++ ){
8 argsclass [I] = ARGs [I]. getclass ();
9}
10
11 method = ownerclass. getmethod (methodname, argsclass );
12
13 return method. Invoke (owner, argS );
14}
Class owner_class = owner. getclass (): first, you must obtain the class of this object.

5 ~ Row 9: configure the class array of the parameter as the condition for finding the method.

Method method = ownerclass. getmethod (methodname, argsclass): Obtain the method to be executed through the method name and the class array of the parameter.

Method. Invoke (owner, argS): executes this method. The parameters of the invoke method are the objects that execute this method, and the parameter arrays. The returned value is an object and the return value of this method.

4. Execute the static method of a class

1 public object invokestaticmethod (string classname, string methodname,
2 object [] ARGs) throws exception {
3 class ownerclass = Class. forname (classname );
4
5 class [] argsclass = new class [args. Length];
6
7 For (INT I = 0, j = args. length; I <j; I ++ ){
8 argsclass [I] = ARGs [I]. getclass ();
9}
10
11 method = ownerclass. getmethod (methodname, argsclass );
12
13 return method. Invoke (null, argS );
14}

The basic principle is the same as that of instance 3. The difference is that the last line, one of the parameters of invoke is null, because this is a static method and does not need to be run by the instance.

5. Create an instance
1
2 public object newinstance (string classname, object [] ARGs) throws exception {
3 class newoneclass = Class. forname (classname );
4
5 class [] argsclass = new class [args. Length];
6
7 For (INT I = 0, j = args. length; I <j; I ++ ){
8 argsclass [I] = ARGs [I]. getclass ();
9}
10
11 constructor cons = newoneclass. getconstructor (argsclass );
12
13 return cons. newinstance (ARGs );
14
15}

The method described here is to execute a constructor with parameters to create an instance. If no parameter is required, you can use newoneclass. newinstance () directly.

Class newoneclass = Class. forname (classname): the first step is to obtain the class of the instance to be constructed.

5th ~ Row 9th: obtains the class array of the parameter.

Constructor cons = newoneclass. getconstructor (argsclass): Obtain the constructor.

Cons. newinstance (ARGs): creates an instance.

6. Determine whether it is an instance of a class

1 Public Boolean isinstance (Object OBJ, class CLs ){
2 Return Cls. isinstance (OBJ );
3}

7. Obtain an element in the array.
1 public object getbyarray (Object array, int index ){
2 Return array. Get (array, index );
3}

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.