Java reflection method using

    java is not available through the new keyword objects and member variables and methods of using the class object, the first time we think can use reflection to realize these functions, reflecting very strong, we learned a lot of things can be used in conjunction with, let's together to learn to use reflection it!

   We use it to achieve a Book class reflection

    code show as below:

   

 

  Three ways to get the class

    The first: use Class.forName (String classPath) // ClassPath: class name reflected the need to write, generally based on the package name class name.

    Note: This will generate a ClassNotFoundException anomaly, we need to throw an exception handling or

    Return value: Class Object

      

  try {
   Class clz = Class.forName("com.entity.Book");
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  }

    The second: direct use of Class clz = class name .class

    This situation is typical in this class we know when to use

    

  Class clz = Book.class;

    Third: Class clz = Object getClass ();.

    Provided that the object has been instantiated out

   Book book = new Book();
   Class clz = book.getClass();    

    

    Summary: These three methods have their own advantages, we generally use the first more, according to their actual needs to use is the best

 

  Gets constructor

   getDeclaredConstructors (); Get all constructors

   getDeclaredConstructor (parameter type); all acquired a constructor

   getConstructors (); Get all constructors disclosed

   getConstructor (parameter type); obtaining a single disclosed constructor

 

   Instructions:

// Get all constructors 
        the Constructor [] = Constructors clz.getDeclaredConstructors ();
         // get a single all constructors 
        the try { 
            the Constructor constructor =                
            clz.getDeclaredConstructor (String. Class ); 
        } the catch (a NoSuchMethodException E) { 
            e.printStackTrace (); 
        } 

        // get all public constructors 
        the constructor [] = constructors1 clz.getConstructors ();
         // get a single constructor discloses 
        the try { 
            the constructor constructor = clz.getConstructor (String. class);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }        

  Gets modifier

   getModifiers (); // get all modifiers

   Return Type: integer types, if there are two modifiers, the sum of the two return modifiers, e.g. public static void getAll () {}

   The return value is the sum of public and static

   Integer Definition:

   0--默认不写 1--public 2--private 4--protected
   8--static 16--final 32--synchronized
   64--volatile 128--transient 256--native
   512--interface 1024--abstract

   

   for (Constructor constructor : constructors) {
        System.out.println(constructor.getModifiers());
   }

 

   Modifier.isStatic (clz.getModifiers) // determines whether the static type

   Modifier There are some other determination methods, all start with is

 

  Get name

   Return Type: String, it may reflect the class names, method names, function names, and so constructed

   getName (); // get the full name for example: com.bean.Book

   getSimpleName () // get class names such as: Book

    

   Class clz = Book.class;
   String name1 = clz.getName();
   String name2 = clz.getSimpleName();    

 

  Gets package

   Return Type: package

   getPackage();

   

    Package aPackage = clz.getPackage();

 

  Get Interface

   Return Type: Class [] interfaces

   getInterfaces ()

   

    Class[] interfaces = clz.getInterfaces();

 

 

  Get the parent class / superclass

   Return Type: Class superclass

   getSuperclass()

    

    Class superclass = clz.getSuperclass();

 

  Acquisition method

   getMethods () // get all the disclosed methods

   Note: The system comes with it will also approach

   CLZ the Class.forName = class ( "Book" ); 
   Method, [] Methods = clz.getMethods ();     // get all of the disclosed methods 
   for (Method, Method: Methods) { 
       System.out.println (method.getName ()) ; 
   }    

   getMethod (String name) // get a single disclosed method, parameters can be specified method name

    Method = clz.getMethod Method, ( "printAll");         // get a single disclosed method 
    Method.invoke (clz.newInstance (), null );

   getDeclaredMethods () // get all the way

   Note: It does not get system comes methods

    Method[] methods1 = clz.getDeclaredMethods();    //获取所有方法
    for (Method method1 : methods1) {
         System.out.println(method1.getName());
    }    

   getDeclaredMethod (String name) // get all the individual method parameters are specified method name

   

    The method1 = clz.getDeclaredMethod Method, ( "printOne");     // get a single All methods 
    System.out.println (method1.getName ());

  Gets the field

   Public (Public)

   getFields () // get all the public fields

   getField (String name) // parameter field specifies a field to get a single public

   all

   getDeclaredFields () // Get all fields

   getDeclaredField (String name) // Get a single field parameter specified field

     Book CLZ = class. Class ;
      // Public 
     Field, [] = clz.getFields Fields ();    // All publications fields 
     Field, ID = clz.getField ( "Age"); // Age field
      // All 
     Field [] declaredFields = clz.getDeclaredFields ();    // all fields 
     clz.getDeclaredField ( "name");    // name field

 

  Instantiate an object

   newInstance(Object initargs)

   The first way

  Book CLZ = Class. Class ; 
  Object obj = clz.newInstance ();         // create a reference book No Object

   The second way

  Book CLZ = class. Class ; 
  the Constructor constructor = clz.getDeclaredConstructor ();    // get constructor with no arguments 
  Object obj = Constructor.newInstance ();     // instantiate objects book

 

  Setting Access Properties

   clz.setAccessible (true) // accessible

   clz.setAccessible (false) // inaccessible

    // default is false 
   Field, the above mentioned id = clz.getField ( "Age"); // Age field   id.setAccessible ( to true ); // set Accessible id.setAccessible ( false ); // set inaccessible

 

  Instructions

   method.invoke(Object obj,Object... args)

   obj: If is an instance method, the method is put to its class object

   obj: static method, write null

   args: parameter value method did not write null, or not write all right

   

    Method = clz.getMethod Method, ( "printAll");         // get a single disclosed method 
    Method.invoke (clz.newInstance (), null ); // Use

   Of course, these are just common, reflecting not just only those

 

Guess you like

Origin www.cnblogs.com/liweixml/p/11462813.html