Hou Jie talks about the Java reflection mechanism

Source: Internet
Author: User

Summary

ReflectionYesJavaIt is considered a key nature of dynamic (or quasi-dynamic) languages. This mechanism allows the program to run throughReflection APIsObtain any known nameClassIncluding its internal informationModifiers(SuchPublic, staticEtc ),Superclass(Example Object)).Interfaces(For exampleCloneable), IncludingFieldsAndMethodsAnd can be changed at runtime.FieldsContent or callMethods. This article uses examples to demonstrate large areasReflection APIs.

 OffIn this article:

Reader base: possessJavaLanguage basics.

Tools used in this article:JDK1.5

 

Keywords:

Introspection(Introspection and inner view)Reflection(Reflection)

Sometimes we say that a language is very dynamic. Sometimes we will differentiate Dynamic and Static technologies and practices. We can dynamically bind (Dynamic binding), Dynamic Link (Dynamic linking), Dynamic loading (Dynamic loading. However"Dynamic"In fact, there is no strict definition that is absolutely and universally applicable. Sometimes, just as the object is oriented to the field of imported programming, one person can play a different role.

Generally, when talking about dynamic languages, the developer community roughly recognizes the following 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,RubyIs a dynamic language,C ++,Java,C #It is not a dynamic language. 

Although under such definition and classificationJavaIt is not a dynamic language, but has a very prominent Dynamic correlation mechanism:Reflection. This word means"Reflection, image, reflection", Used inJavaIt means that we can load, probe, and use it at runtime, which is completely unknown during compilation.Classes. In other words,JavaThe program can load a runtime before learning the nameClassTo learn about its complete structure (but not includingMethodsDefine), and generate its object entity, orFieldsSet value, or evoke itMethods1. This"See throughClass"Capabilities (The ability of the program to examine itself) Is calledIntrospection(Introspection, internal view, reflection).ReflectionAndIntrospectionIt is a commonly used term.

JavaHow can we make the above dynamic features? This is a profound topic. This article only briefly introduces some concepts. The whole article is mainly aboutReflection APIsThat is, to let readers know how to exploreClassStructure, how"TheClass"Generate an objectFieldsSet Value, call itsMethods. This article will talk aboutJava. lang. Class, AndJava. lang. reflectInMethod,Field,ConstructorAnd so on.Classes.

"Class"Class

As we all knowJavaThere isObject class, Is allJava classesThe root cause of inheritance, which declares thatJava classModified inMethods:HashCode (),Equals (),Clone (),ToString (),GetClass (). WhereGetClass ()ReturnsClass object.

Class classVery special. It is similar to GeneralClassesInherited fromObjectIs used to expressJavaWhen the program is runningClassesAndInterfacesIs also used to expressEnum,Array,Primitive Java types(Boolean, byte, char, short, int, long, float, double) And keywordsVoid. WhenClassLoaded, or when the loader (Class loader)DefineClass ()QuiltJVMCall,JVMAClass object. If you want"ModifyJavaStandard Library source code"To observeClass object(For exampleClassOfConstructorAddPrintln ()), Cannot! BecauseClassNoPublic constructor(SeeFigure1). At the end of this article, I will talk about it in a small space.JavaStandard Library source code change method.

ClassYesReflectionThe origin of the story. For anyClassOnly generate oneClass objectThen, the latter can evoke dozensReflection APIs. TheseAPIsThey will be unveiled at a later adventure.

 

#001 public final

#002 classClass<T>Implements java. io. Serializable,

#003 java. lang. reflect. GenericDeclaration,

#004 java. lang. reflect. Type,

#005 java. lang. reflect. AnnotatedElement {

#006PrivateClass (){}

#007Public StringToString(){

#008Return (isInterface ()? "Interface ":

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.