Java reflection mechanism analysis (I)-Definition and API

Source: Internet
Author: User

1. What is the Java reflection mechanism?

The reflection mechanism of Java allows you to fully understand any class and other attributes and methods while running the program, and call the attributes and methods of an object at will. This dynamic acquisition during runtime is the reflection mechanism of Java. In fact, this is also a symbol of dynamic language Java.


To sum up reflection in one sentence is to load a class that is only known at runtime and its complete internal structure.


2. Why is there a Java reflection mechanism?

Why should we use the Java reflection mechanism?

I think there are two types:

First, reflection aims to expand unknown applications. For example, if you write a program that defines some interfaces, the DLL that implements these interfaces can be inserted into the program as a plug-in. How can this problem be achieved? It can be implemented through reflection. Is to load the DLL into the memory, and then call the method in the DLL through reflection.


Second, if you do not know the class name during the encoding stage, you must read the class name from the configuration file at runtime. At this time, there is no way to hard encode new classname (), reflection is required to create this object.


An example we often see in our life helps us to understand why we should use the reflection mechanism of Java: you enter a restaurant and you don't know how much the food they all have. So the first thing you do is "The waiter takes a menu" and points to the menu and says "I want this, I want it ".


3. Let's take a look at reflectionapi.

In our life, when we use an unknown thing, we always use help to solve our usage problems. TV sets are helpful and computers are helpful, almost all things carry its help, and Java's reflection mechanism is no exception.


With the help of the reflection API in JDK, it mainly describes what is a Java reflection mechanism, what attributes and methods are provided by this reflection mechanism, and further we can know what kind of work can be done through it.


Let's take a look at this part of the API. These interfaces and classes are all in the lang package.


 

Interface:


Class:



This section briefly introduces the usage of some classes and interfaces.

1) A Member member is an interface that reflects the identity information of a single member (field or method) or constructor.


2) invocationhandler is the interface implemented by the calling handler of the proxy instance (the specific usage of this interface will be highlighted in Java reflection mechanism analysis 4)


3) method provides information about the methods of a class and interfaces of the methods of the category class.


Example:

 Import Java. lang. reflect. method; </P> <p> public class testmethod {</P> <p>/** <br/> * @ Param ARGs <br/> * @ throws exception <br/> */<br/> Public static void main (string [] ARGs) throws exception {<br/> // todo auto-generated method stub <br/> class classtype = Class. forname (ARGs [0]); <br/> method methods [] = classtype. getdeclaredmethods (); <br/> for (INT I = 0; I <methods. length; I ++) {<br/> system. out. println (methods [I]. tostring (); <br/>}</P> <p>}


4) filed provides information about the domain of a class and the interface of the domain of the category class.


Example:

 

Import Java. lang. reflect. field; </P> <p> public class testfield {</P> <p>/** <br/> * @ Param ARGs <br/> * @ throws exception <br/> */<br/> Public static void main (string [] ARGs) throws exception {<br/> // todo auto-generated method stub <br/> class classtype = Class. forname (ARGs [0]); <br/> Field [] fields = classtype. getfields (); <br/> for (INT I = 0; I <fields. length; I ++) {<br/> system. out. println (fields [I]. tostring (); <br/>}</P> <p >}< br/>

5) The array class provides methods for dynamically creating and accessing Java arrays.


Example:

 Import Java. lang. reflect. array; </P> <p> public class testarray {</P> <p> Public testarray () {</P> <p >}< br/>/** <br/> * @ Param ARGs <br/> * @ throws exception <br/> */<br /> Public static void main (string [] ARGs) throws exception {</P> <p> class <?> Classtype = Class. forname ("Java. lang. string "); </P> <p> Object array = array. newinstance (classtype, 10); </P> <p> array. set (array, 5, "hello"); </P> <p> string S = (string) array. get (array, 5); <br/> system. out. println (s); </P> <p >}</P> <p>}

6) proxy provides static methods for dynamically generating proxy classes and class instances (this method is described in Java radiology mechanism analysis 4 ).


For the usage of other classes and interfaces, see API

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.