Java reflection mechanism and significance of

Meaning reflected :
JAVA reflection mechanism is in the operating state, for any class, are able to know all the properties and methods of this class; For any object, you can call any of its methods and properties; this dynamic access to information and dynamic object method call feature called reflection java language. (The point is to say that the code is simple and straightforward, improve code reuse rate, convenient external calls, source code, decompile can see.)

Package avicit.mms.common; 

interface Animal { // animal Interface 
    public  void say (); // call 
} 

class Dog the implements Animal { // definition of dog 
    public  void say () { 
        System.out.println ( "Wang, Wang Wang !!! " ); 
    } 
} 

class Cat the implements Animal {
     public  void say () { 
        System.out.println ( " meow, Mew, Mew !!! " ); 
    } 
} 

class Factory's {
     public  staticThe getInstance Animal (String className) { 
        Animal Animal = null ;
         the try { 
            Animal = (Animal) the Class.forName (className) .newInstance (); 
        } the catch (Exception E) { 
            e.printStackTrace (); 
        } 
        return Animal; 
    } 
} 

public  class the Test {
     public  static  void main (String args []) {
         // acquired via an interface instance factory class, the class name passed in the full package. 
        Animal Animal Factory.getInstance = ( "avicit.mms.common.Dog" );
         IF (animal! =null ) { // determines whether acquisition interface instance 
            animal.say (); 
        } 
    } 
}

Output:
Woof, woof, woof! ! !

  If no reflection, so if we add a tiger class, you have to determine where in the Factory, each adding a class to be modified once Factory, but with the class name reflected only in the call incoming full time can be completed. Results: reflection, a modified codes; without reflection, two modified code.

Reflection is generally used:

  1. In JDBC, dynamically loaded by reflection database driver.
  2. Web servers use reflection to invoke a method Sevlet service.
  3. Eclispe development tools analysis using the reflection plane moving object types and structures, properties, and method for dynamically prompt objects.
  4. Many have used reflection frame, injecting property, method calls, such as Spring.

Reflection of the advantages and disadvantages:

  1. Advantages: can be performed dynamically during operation within the business dynamic execution method, access the properties, to maximize the flexibility of java.
  2. Disadvantages: performance impact, such operations are always slower than direct execution of java code.

The role of reflection:

  1. Analyzing an object belongs to any class at runtime
  2. At runtime class of an object in any configuration
  3. Determining at runtime any class has member variables and methods
  4. Call any of the object's method at run time

Reflection of Use:

  1. Create an object by a fully qualified class name
    1. Class.forName ( "fully-qualified class name"); for example: com.mysql.jdbc.Driver Driver class has been loaded into the jvm, and completed the initialization class on the line
    2. Class name .class; acquiring Class <? > Clz objects
    3. Object .getClass ();
  2. Get constructor object, the new object constructor
    1. Clazz.getConstructor([String.class]);
    2. Con.newInstance ([parameters]);
  3. Create an instance of the object (and quite new class name () constructor with no arguments) by a class object
    1. Cls.newInstance();
  4. Get a property of an object by the object class
    1. Field c = cls.getFields (): get all the public (public) field of a class, including the fields in the parent class.
    2. Field c = cls.getDeclaredFields (): get all the fields declared in a class, which includes public, private and proteced, but does not include a statement of the parent class field
  5. It is obtained by a method of an object class object
    1. Cls.getMethod ( "method name", class ...... parameaType); (can only get public)
    2. Cls.getDeclareMethod ( "method name"); (get any modification of the method can not be executed private)
    3. M.setAccessible (true); (make private methods can be performed)
  6. Let execution method
    1. Method.invoke (obj instance object, obj variable parameter); ----- (there is a return value)
Package avicit.mms.common; 

Import the java.lang.reflect *. ; 

public  class the Main {
     public  static  void main (String [] args) throws Exception {
         // Returns A constructor of 
        the Constructor A. C = class .getConstructor () ;
         // return all class a Constructors public declarations of 
        the constructor [] = A. cons class .getConstructors ();
         // return all class a construction method, comprising Private 
        the constructor [] = A. cons2 class .getDeclaredConstructors ( );
         // returns first class public method a 
        method m = A.class .getMethod ( "say" );
         // perform 
        m.invoke (A. class .newInstance (), null );
         // return all public methods A class 
        Method, [] = A. MS class .getMethods ();
         / / return all category a method, comprising Private 
        method, [] = A. allMs class .getDeclaredMethods ();
         // returns the public fields of the class a 
        field, A. field = class .getField ( "I" ); 
        System.out.println (Field.get (a. class .newInstance ()));
         // returns a static class field 
        System.out.println (field.get (null )); 
    } 
} 

class A {
     public  int I =. 1 ;
     public  static  int B = 2 ;
     public A () { 
        System.out.println ( "constructor with no arguments" ); 
    } 
    Private A (String S) { 
        the System. Out.println ( "configured with a parameter" + S); 
    } 
    
    public  void say () { 
        System.out.println ( "say" ); 
    } 
}

Guess you like

Origin www.cnblogs.com/1012hq/p/11310683.html