About Java Reflection

Source: Internet
Author: User

It's written in great detail, collection first.

Original address: http://dev.111cn.net/article/49/49876.shtm


Reflection is one of the characteristics of Java Program Development language, it allows the running Java program to check itself, or "self audit", and can directly manipulate the internal properties of the program. For example, you can use it to get the names of the members in the Java class and display them. JavaBean is one of the practical applications of reflection, which allows some tools to visualize the operation of software components. These tools dynamically load and get the properties of the Java component (Class) by reflection.

1. A simple example
Consider the following simple example, let's see how reflection works.
Import java.lang.reflect.*;
public class Dumpmethods {
public static void Main (String args[]) {
try {
Class C = class.forname (Args[0]);
Method m[] = C.getdeclaredmethods ();
for (int i = 0; i < m.length; i++)
System.out.println (M[i].tostring ());
catch (Throwable e) {
System.err.println (e);
}
}
}
Follow the following statement:
Java dumpmethods Java.util.Stack
Its result output is:
Public Java.lang.Object Java.util.Stack.push (java.lang.Object)
Public synchronized Java.lang.Object Java.util.Stack.pop ()
Public synchronized Java.lang.Object Java.util.Stack.peek ()
public boolean java.util.Stack.empty ()
public synchronized int Java.util.Stack.search (java.lang.Object)
This lists the method names of the Java.util.Stack class and their qualifiers and return types.
This program uses Class.forName to load the specified class and then calls Getdeclaredmethods to get a list of the methods defined in the class. Java.lang.reflect.Methods is a class used to describe a single method in a class.
2. Start using Reflection
Classes used for reflection, such as method, can be found in the Java.lang.relfect package. There are three steps you must follow to use these classes: The first step is to get the Java.lang.Class object of the class you want to manipulate. In a running Java program, use the Java.lang.Class class to describe classes and interfaces.
Here's one way to get a Class object:
Class C = class.forname ("java.lang.String");
This statement gets the class object of a String class. There is another way, such as the following statement:
Class C = int.class;
Or
Class C = integer.type;
They can obtain class information of the basic type. The latter method accesses the predefined type fields in the encapsulated class (such as Integer) of the base types.
The second step is to call a method such as Getdeclaredmethods to get a list of all the methods defined in the class.

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.