Java Reflection Mechanism 3__java

Source: Internet
Author: User
Tags reflection

Summary

Reflection is a key feature of Java as a dynamic (or quasi dynamic) language. This mechanism allows the program to obtain internal information, including its modifiers (such as public, static, and so on), superclass (for example, object) of any class that has a known name through the reflection APIs at run time, Implementation interfaces (such as cloneable), also includes all information about fields and methods, and can change fields content or evoke methods at run time. This article by example, a large area demonstration reflection APIs.

About this article:

Reader base: Java language Basics.

This article applies the tool: JDK1.5

Keywords:

Introspection (introspection, inner view)

Reflection (Reflection)

Sometimes we say that a language has a strong dynamic, and sometimes we distinguish between dynamic and static technologies and practices. We're catchy. Dynamic binding (dynamically binding), dynamic linking, dynamic loading, and so on. However, the term "dynamic" does not have the absolute and universally applicable strict definition, sometimes even like object-oriented was originally imported into the programming domain, one person a number, each blow each tune.

Generally speaking, the developer community speaks of dynamic languages, with a broadly agreed definition of: "When a program runs, it allows you to change the program structure or variable type, which is called a dynamic language." From this point of view, Perl,python,ruby is a dynamic language, c++,java,c# is not a dynamic language.

Although Java is not a dynamic language in this definition and classification, it has a very prominent dynamic correlation mechanism: Reflection. The meaning of this word is "reflection, image, reflection," which is used in Java to refer to the classes that we can load, detect, and use during compilation. In other words, a Java program can load a class that knows its name at run time, learns its full structure (but does not include methods definitions), generates its object entity, or sets a value on its fields, or evokes its methods1. This ability to "See Through Class" (the ability of the program to examine itself) is called introspection (introspective, internal, introspective). Reflection and introspection are two terms that are often mentioned.

How can Java make these dynamic features? This is a far-reaching topic, this article simply introduces some concepts. The main thing is to introduce reflection APIs, that is, to let readers know how to explore the structure of class, how to generate an entity, set a value for its fields, and call its methods for a class whose name is known to the runtime. This article will talk about Java.lang.Class, and Java.lang.reflect method, Field, constructor and so on classes.

"Class" Class

It is well known that Java has an object class that is the source of all Java classes, which declares several methods:hashcode (), Equals (), clones () that should be overwritten in all Java classes, ToString (), GetClass (), and so on. where GetClass () returns a class object.

Class class is very special. It inherits from the object like the general classes, which is used to express the classes and interfaces of the Java program runtime, and to express the enum, array, primitive Java types (Boolean, Byte, char, short, int, long, float, double, and keyword void. When a class is loaded, or when the defineclass () of the Loader (class loader) is invoked by the JVM, the JVM automatically generates a Class object. If you want to use the "Modify Java Standard library source" to observe the actual generation time of class object (for example, to add a println () to the constructor of class), not enough. Because class does not have a public constructor (see Figure 1). At the end of this article I will make a small piece of space to talk about Java standard library source code changes.

Class is the origin of the reflection story. For any class you want to explore, you have to create a class object for it first, and then you can invoke more than 10 reflection APIs through the latter. These APIs will be unveiled at a later stage of the expedition in one by one.

#001 Public Final

#002 class Class<t> implements Java.io.Serializable,

#003 Java.lang.reflect.GenericDeclaration,

#004 Java.lang.reflect.Type,

#005 Java.lang.reflect.AnnotatedElement {

#006 Private Class () {}

#007 public String toString () {

#008 return (isinterface)? "Interface":

#009 (Isprimitive ()? "": Class))

#010 + getName ();

#011}

...

Figure 1:class Class fragment. Note that its private empty ctor means that no one is allowed to generate class object programmatically. Yes, its object can only be generated by the JVM.

Approach to the acquisition of "Class" object

Java allows us to generate corresponding class object for a class from a variety of pipelines. Figure 2 is a collation.

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.