Beginner in java: Java reflection mechanism-general Linux technology-Linux programming and kernel information. For details, refer to the following section. Summary
Reflection is a key feature of Java as a dynamic (or quasi-dynamic) language. This mechanism allows the program to obtain the internal information of any class with a known name through Reflection APIs at runtime, including its modifiers (such as public and static), superclass (such as Object), implementation of interfaces (such as Cloneable), also includes all information of fields and methods, and can change fields content or arouse methods at runtime. This article uses examples to demonstrate Reflection APIs in a large area.
About this article:
Reader base: Has the Java language base.
Tool used in this article: JDK1.5
Keywords:
Introspection)
Reflection (Reflection)
Sometimes we say that a language is very dynamic. Sometimes we will differentiate Dynamic and Static technologies and practices. We can use the dynamic binding, dynamic linking, and dynamic loading interfaces on Lang. However, the word "dynamic" does not have a strict definition that is absolutely and universally applicable. Sometimes, just like the object orientation was imported into the programming field at the beginning, one person gets a different number and gets different calls.
Generally speaking, when talking about dynamic languages, the developer community roughly agrees with the definition: "When a program is running, the program structure or variable type can be changed. This language is called dynamic language ". From this point of view, Perl, Python, and Ruby are dynamic languages, and C ++, Java, and C # Are not dynamic languages.
Although Java is not a dynamic language under such definition and classification, it has a very prominent Dynamic correlation mechanism: Reflection. This word refers to "Reflection, image, reflection". In Java, it refers to classes that are completely unknown during runtime loading, exploring, and using compilation. 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.
How can Java make the above dynamic features? This is a profound topic. This article only briefly introduces some concepts. Reflection APIs will be introduced in the whole article, that is to say, let the reader know how to explore the structure of the class, how to generate an entity for a class with the name only known during runtime, set a value for its fields, and call its methods. This article will talk about java. lang. Class, methods, fields, Constructor, and so on in java. lang. reflect.
"Class" class
As we all know, Java has an Object class, which is the root inheritance of all Java classes. It declares several methods that should be rewritten in all Java classes: hashCode (), equals (), clone (), toString (), getClass (), etc. GetClass () returns a Class object.
Class class is very special. It inherits from objects like general classes. In fact, it is used to express the classes and interfaces when Java is running. It is also used to express 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 called by JVM, JVM automatically generates a Class object. If you want to observe the actual generation time of the Class object by modifying the Java standard library source code (for example, adding a println () to the constructor of the Class), you cannot! Because the Class does not have public constructor (SEE ). At the end of this article, I will allocate a small part of space to talk about how to change the source code of the Java standard library.
Class is the origin of the Reflection story. For any class you want to explore, you can only generate a Class object for it first, then the latter can evoke dozens of Reflection APIs. These APIs will be unveiled in a later adventure.
#001 public final
#002 class Class 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}
...
: 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 JVM.
"Class" object acquisition path
Java allows us to generate a corresponding class object for a Class from multiple pipelines. Is a summary.
Example of creating a Class object Pipeline
Use getClass ()
Note: Each class has this function String str = "abc ";
Class c1 = str. getClass ();
Application
Class. getSuperclass () 2 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;
: Java allows multiple pipelines to generate Class objects.
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.