Many friends in-depth contact with the Java language will find such two words: reflection and introspection, where to apply and how to use? Put these two together today, because they are mutually reinforcing.
Reflection
//constructs an instance of a class by its class name classclasscls_str=class.forname (\ "java.lang.string\"); //the above sentence looks familiar, Because people who have used JDBC to access the database have used J objectstr=cls_str.newinstance (); //is equivalent to stringstr=newstring (); // Call a method by the method name stringmethodname=\ "Length\"; methodm=cls_str.getmethod (Methodname,null); System.out.println (\ "Lengthis\" +m.invoke (Str,null)); //equivalent to System.out.println (Str.length ()); The above two examples are more commonly used methods. Seeing the example above, there is a question: why bother? Why is it so complicated to be done in a single statement? Yes, there is really no need for such trouble in the above example. But you imagine an application that supports dynamic feature extensions, which means that the program does not restart but can automatically load new functionality, which is represented by a specific class. First we have to define an interface class for these functions, and then we ask that all extended function classes have to implement the interface I specify, which defines the interface rules between the application and the extensible functionality, but how does it load dynamically? We have to let the application know the class name of the feature class to be extended, such as Test. FUNC1, when we tell the application of the class name (string), it can use the method of our first example to load and enable the new functionality. This is the reflection of class, do you have any other choice?
Introspection
Introspection is a default processing method of the Java language for Bean class properties and events. For example, there is a property name in Class A, so we can get its value by Getname,setname or set a new value. The Name property is accessed through Getname/setname, which is the default rule. Java provides a set of Getter/setter methods used by the API to access a property that allows you to not need to know the rules, which are stored in the package Java.beans.
/* *createdon2004-6-29 */ packagedemo; importjava.beans.beaninfo; importjava.beans.introspector; importjava.beans.propertydescriptor; publicclassintrospectordemo{ stringname; publicstaticvoidmain (String[]args) throwsexception{ introspectordemodemo= Newintrospectordemo (); demo.setname (\ "winterlau\"); //if you don't want to list the attributes of the parent class, // The second parameter of the getbeaninfo fills in the information of the parent class beaninfobi=introspector.getbeaninfo (Demo.getclass (), Object.class); Propertydescriptor[]props=bi.getpropertydescriptors (); for (inti=0; The Formbean of the development framework struts is to map the data in the form to the properties of the class through an introspective mechanism, so there is a Getter/setter method required for each property of the Formbean. But that's not always the case, what does it mean? That means that for a bean class, I can have no attributes, but as long as there is one of the getter/setter methods, then the introspection mechanism of Java will think that there is a property, such as a method setmobile in a class, then think that there is a property of mobile, This makes it easier for us to define the Bean class through an interface rather than having to care about the implementation and not care about the storage of the data in the bean. For example, we can put all the Getter/setter method into the interface definition, but the real data access is implemented in the specific class, which can improve the system extensibility.
Summarize
Applying the reflection and introspection of Java to Program design can greatly provide the intelligent and extensible of the program. There are a lot of projects that take these two technologies to achieve their core functions, but it should be said that almost all projects are more or less using both technologies. In the actual application process, the two should be combined in order to play a true intelligent and highly scalable.
Introspection and reflection in Java