Java reflection mechanism Learning 1

Source: Internet
Author: User

1) concept:
In the running state, all attributes and methods of this class can be known for any class, and any method of this class can be called for any object; this kind of dynamically obtained information and the function of dynamically calling object methods is called the reflection mechanism of Java language.

2) functions:
Determine the class to which any object belongs at runtime;
Construct any class object at runtime;
Judge the member variables and methods of any class at runtime;
Call methods of any object at runtime;

Generate a dynamic proxy.

3) obtain the specific attributes of an object:
Class ownerclass = owner. getclass (): obtains the class of the object owner.
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. To access the private attribute of an object, call accesibleobject. setaccessible (field name, true) first );
To obtain the static attributes of a class, Replace the third sentence with Object Property = field. Get (Class Name) instead of the Instance name.

4) method for executing an object:
Public object invokemethod (Object owner, string methodname, object [] ARGs) throws exception {
Class ownerclass = owner. getclass ();
Class [] argsclass = new class [args. Length];
For (INT I = 0, j = args. length; I <j; I ++ ){
Argsclass = args. getclass ();
}
Method method = ownerclass. getmethod (methodname, argsclass );
Return method. Invoke (owner, argS );
}
If you are executing a static method of A Class: method. Invoke (null, argS );

5) Create an instance of an object:
Public object newinstance (string classname, object [] ARGs) throws exception {
Class newoneclass = Class. forname (classname );
Class [] argsclass = new class [args. Length];
For (INT I = 0, j = args. length; I <j; I ++ ){
Argsclass = args. getclass ();
}
Constructor cons = newoneclass. getconstructor (argsclass );
Return cons. newinstance (ARGs );
}
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.

6) determine whether it is an instance of a class:
Public Boolean isinstance (Object OBJ, class CLs ){
Return Cls. isinstance (OBJ );
}

7) reflection and Introspection
The word reflection refers to reflection, image, and reflection. in Java, it refers to classes that are completely unknown during runtime loading, exploring, and using. In other words, a Java program can load a class whose name is known only at runtime, and learn its complete structure (but does not include the definition of methods ), and generate its object entity, set its fields value, or arouse its methods1.
The ability of the program to examine itself is called introspection ). Reflection and introspection are two frequently mentioned terms.

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.