Cognition and learning java reflection

1. Study of the Class object, Filed object (corresponding to data), Method of the image (corresponding to the function), Constructor object (corresponding to the constructor).

2.Declared be used to obtain private data and methods, but is printed using setAccessible ( to true) ; to check to ignore access modifier.

3.FS5 () function has a point to note is the same address, the reason I wrote in a comment in the.

 

 

#FS practice profile 
# 1. The full name of the class you need to create objects and methods need to be performed is defined in the configuration file 
# 2 is loaded in the program reads the configuration file 
# 3. Use reflective technology to load class files into the memory 
# 4. create Object 
# 5. execution method 
# 
#
className=bean.Admin 
methodName=eat

 

 

Filed learning objects

  

/ ** 
     * Learning java reflection mechanism 
     * Field, learning objects 
     * / 
    @Test 
    public void FS1 () throws a NoSuchFieldException, IllegalAccessException { 
        Class userClass = User.class; 

        Field, [] = userClass.getFields Fields (); // get all public member object 
        for (Field, Field: Fields) 
        { 
            System.out.println (Field); 
        } 

        Field, field1 = userClass.getField ( "FS"); // in accordance with the variable name acquisition 

        / * 
          Field, operation 
          1. set 
          sET 
          2. Get 
          GET 
         * / 
        the User new new user1 = the User (); 
        System.out.println (field1.get (user1));
        field1.set (user1, "Dada"); 
        System.out.println (field1.get (user1)); 

        // ignore modifiers can get to the private 
        Field, [] = Full userClass.getDeclaredFields (); 
        for (Field, Field, : Full) 
        { 
            System.out.println (Field,); 
        } 
        // get a single one ignores modifier 
        Field, privateField = userClass.getDeclaredField ( "the above mentioned id"); 
        // print but private, need to ignore to check access modifier 
         privateField.setAccessible (true); // reflective violent 
        System.out.println (privateField.get (user1)); 
    }

 

Constructor object of study

 

/ ** 
     * constructor reflection acquisition 
     * / 
    @Test 
    public void FS2 () throws a NoSuchMethodException, IllegalAccessException, a InvocationTargetException, an InstantiationException is { 
        Class userClass = User.class; 
        // get all 
        Constructor [] constructors = userClass.getConstructors () ; 
        for (the constructor constructor: Constructors) 
        { 
            System.out.println (constructor); 
        } 
        // get a single 
        the constructor constructor = userClass.getConstructor (Integer.class, String.class); 
        System.out.println (constructor); 

        // use get the constructor to create objects 
        Object user = constructor.newInstance (5, " dada"); 
        System.out.println (user.toString ());
 
        / * 
         empty argument constructor 
         * / 
        the Constructor constructor1 userClass.getConstructor = (); 
        System.out.println (constructor); 
        Object constructor1.newInstance user1 = (); 
        System.out.println (user1.toString ()); 
        System.out.println ( "upper empathy"); 
        // with the same token, Class this method is to facilitate an empty argument constructor method call 
        Object user2 = userClass .newInstance (); 
        System.out.println (user2.toString ()); 

        // violent reflection constructor.setAccessible (); 
    }

 

 Method of learning objects

 

/ ** 
     * Get reflection method 
     * Method, 
     * / 
    @Test 
    public void FS3 () throws a NoSuchMethodException, a InvocationTargetException, IllegalAccessException { 
        Class userClass = User.class; 

        // Get Class Name 
        String className = userClass.getName (); 
        the System.out .println (className); 
        // Get all public methods 
        method, [] = userClass.getMethods methods (); 
        for (method, method: methods) 
        { 
            // is also printed in the Obj method 
            System.out.println (method); 
            / / Get method name 
            System.out.println (method.getName ()); 
        } 
       // Get method method name return value
        Method eat = userClass.getMethod("eat");
        Method eat1= userClass.getMethod("eat",int.class);
        User user=new User();
        eat.invoke(user);//执行方法
        eat1.invoke(user,1);//执行方法



    }

 

 And reflection profiles using

 

/ ** 
     * reflection profiles and using 
     * / 
    @Test 
    public void FS4 () throws IOException, a ClassNotFoundException, IllegalAccessException, an InstantiationException is, a NoSuchMethodException, a InvocationTargetException { 
        // load configuration file. 1. 
        The Properties Pro new new = the Properties (); 
        // Get profile classes directory ClassLoader class 
        ClassLoader TestUserFS.class.getClassLoader classLoader = (); 
        // get the input stream 
        the InputStream resourceAsStream = ClassLoader.getResourceAsStream ( "FS.properties"); 
        // loading 
        pro.load (resourceAsStream); 

        // 2. Get the data defined in the configuration file 
        System.out.println (methodName); 
        String className = pro.getProperty ( "className" );
        Pro.getProperty methodName = String ( "methodName"); 
        .. 3 // loaded into memory class 
        Class = userClass the Class.forName (className); 
          .. 4 // Create Object 
        Object obj = userClass.newInstance (); 
        .. 5 // Get an object method 
         method, method = userClass.getMethod (methodName); 
         // Get function with parameters that eat 
        method, the method1 = userClass.getMethod (methodName, int.class); 
         //. 6 to perform the method. 
         Method.invoke (obj); 
        the method1. Invoke (obj,. 1); 
    }

 

Three common ways: Class.forName () function, I used before when using JDBC, the other two species rarely.

 

/ ** 
     * Notes 
     * 1.Class.forName ( "full class name") 
     * 2. Object .getClass (): Get Method 
     * 3. Class Name .class: property class obtained by class name 
     * / 
    @Test 
    public void FS5 () throws a ClassNotFoundException, IllegalAccessException, an InstantiationException is, a NoSuchMethodException, a InvocationTargetException { 

        // profile. 1 for 
        Class = objClass1 the Class.forName ( "bean.User"); 
        System.out.println (objClass1); 
        Object user1 = objClass1. the newInstance (); 
        Method, the method1 = objClass1.getMethod ( "EAT"); 
        method1.invoke (user1); 

        . // 2-byte codes used for obtaining an existing object 
        Class objClass2 = User.class; 
        System.out.println ( objClass2);

        // 3 for parameter passing 
        the User = new new USER3 the User (); 
        Class objClass3 = user3.getClass (); 
        System.out.println (objClass3); 

        / * 
         * three is the same address 
         * Why? 
         * Because the class after loading into memory only once, so you get him when only a 
         * / 
        System.out.println (objClass1.hashCode ()); 
        System.out.println (objClass2.hashCode ()); 
        System.out .println (objClass3.hashCode ()); 
    }

 

 Summary: java reflection mechanism and configuration files is really handy, as I learned before log4 configuration error in the console output helped me a lot.

The java Junit let me write the code more convenient.

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/gonT-iL-evoL-I/p/11727809.html