Java reflection mechanism Learning

Source: Internet
Author: User

Nbsp; Java reflection mechanism is a key feature of quasi-Dynamic Language. The core of the Java Reflection mechanism is to allow Java Reflection APIs at runtime to obtain information about class classes with known names and dynamically generate such information, and call its method or modify its domain (or even the domain or method declared as private ).

Maybe you have been using Java for a long time, but almost no Java reflection mechanism is used. You will sneer at me and tell me that the Java reflection mechanism is useless. Java reflection mechanisms may be useless on platforms such as J2EE and J2SE (I don't know much about them and don't comment on them), but in Android Application Development, this mechanism will surprise you a lot.

If you are familiar with Android, you should know that Google often adds the @ hide annotation mark to some classes or methods in the system source code for some reasons. The function of this method or class is invisible when the SDK is generated. Therefore, you cannot see the comments. This causes some problems. Some clearly accessible things cannot be accessed during compilation! This makes some functions of your program unable to be compiled.

Of course, there is a way to remove all the "@ hide" tags in the Android source code, and then re-compile your own SDK. Another method is to use the Java reflection mechanism. Of course, you can also use reflection to access methods with access restrictions and modify their domains. However, this method is quite special. We will discuss it separately at the end of the article.

Starting from the Class
If you use Java, you should know that Java has a Class. The Class itself represents the type of a Java Object. We can use the getClass method of an Object (sub) Object to obtain the type of an Object. This function returns a Class. Of course, there are many methods to obtain Class objects, but none of them generate Class objects through the Class constructor.

Maybe you have never used a Class. maybe you thought it was useless. No matter how you think before, the Class is the source of the entire Java reflection mechanism. All stories about Java reflection begin with the Class.

Therefore, to use Java reflection, we first obtain the Class object. The following table lists several methods for getting the Class for your reference.
Exam.

Class object birth Pipeline

Example

Use getClass ()

Note: Each class has this function.

String str = "abc ";

Class c1 = str. getClass ();

Application

Class. getSuperclass ()

Button B = new Button ();

Class c1 = B. getClass ();

Class c2 = c1.getSuperclass ();

Use static method

Class. forName ()

(Most commonly used)

Class c1 = Class. forName ("java. lang. String ");

Class c2 = Class. forName ("java. awt. Button ");

Class c3 = Class. forName ("java. util. shortlist $ Entry ");

Class c4 = Class. forName ("I ");

Class c5 = Class. forName ("[I ");

Application

. Class syntax

Class c1 = String. class;

Class c2 = java. awt. Button. class;

Class c3 = Main. InnerClass. class;

Class c4 = int. class;

Class c5 = int []. class;

Application

Primitive wrapper classes

TYPE syntax

Class c1 = Boolean. TYPE;

Class c2 = Byte. TYPE;

Class c3 = Character. TYPE;

Class c4 = Short. TYPE;

Class c5 = Integer. TYPE;

Class c6 = Long. TYPE;

Class c7 = Float. TYPE;

Class c8 = Double. TYPE;

Class c9 = Void. TYPE;

Obtain basic information

After we get a Class Object of the Class, the Java reflection mechanism can be applied. First, let's get some basic information about a class.

Java class internal module

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.