Javaでの反射メカニズム-シンプルで一般的

25599 ワード

転載は出典を明記してください.http://blog.csdn.net/magic_jss/article/details/52187726; 
1、反射とは何ですか.
JAVA反射機構は運転状態において、いずれのクラスに対しても、このクラスのすべての属性と方法を知ることができる.いずれのオブジェクトに対しても、そのメソッドとプロパティを呼び出すことができます.このような動的に取得された情報および動的にオブジェクトを呼び出す方法の機能をjava言語の反射メカニズムと呼ぶ.
反射は実は.classバイトコードファイルを通じてJava類の操作を実現しているのではないでしょうか.
2、反射はどうやって?
1.バイトコードファイルオブジェクトの取得
バイトコードファイルオブジェクトを取得する前に、関連クラス:User.javaを参照してください.
/**
 * Created by magic on 2016 8 9 .   
 */
public class User {

    public String name;//    
    protected String sex;//     
    private int age;//    

    public User() {
        super();
    }

    protected User(int age) {
        super();
    }

    private User(String name, String sex, int age) {
        super();
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User [name=" + name + ", sex=" + sex + ", age=" + age + "]";
    }
}

バイトコードファイルオブジェクトを取得するには、次の3つの方法があります.
//   Class  
//    
Class c = Class.forName("com.magic.reflect.User");
//    
Class c1 = User.class;
//    
User user = new User();
Class c2 = user.getClass();

2.構築方法の取得
関連構造の方法:
    //    Class                 
    Constructor getConstructor(Class>... parameterTypes)
    //   Class                   
    Constructor>[] getConstructors()
    //   Class                  
    Constructor getDeclaredConstructor(Class>... parameterTypes)
    //    Class                  
    Constructor>[] getDeclaredConstructors()
        //   public       
        Constructor cs = c.getConstructor();
        //   public         
        Constructor[] cs2 = c.getConstructors();
        System.out.println(cs2.length);// 1
        //               
        Constructor cs3 = c.getDeclaredConstructor(int.class);
        Constructor cs4 = c.getDeclaredConstructor(String.class, String.class,
                int.class);
        Constructor[] cs5 = c.getDeclaredConstructors();
        System.out.println(cs5.length);// 3

        //     
        cs4.setAccessible(true);
        //   Constructor       
        User u = (User) cs4.newInstance("MAGIC", "BOY", 21);
        System.out.println(u.toString());//User [name=MAGIC, sex=BOY, age=21]

setAccessible(true)メソッドは、タイプチェックをキャンセルすることができ、例えば、プライベート変数はクラス内部でしか使用できないが、setAccessible(true)メソッドは、この制限をキャンセルしてクラス外部でもこの変数を取得することができる.上で使用したのは,Userクラスの3つのパラメータの構成方法がprivate修飾であるためである.暴力的なアクセスを使用しないと、IllegalAccessException異常が放出されます.
3.メンバー変数の取得
関連メソッド:
    //   Class                    
    Field getField(String name)
    //   Class                     
    Field[] getFields()
    //    Class                   
    Field getDeclaredField(String name)
    //   Class                   
        Constructor constructor = c.getConstructor();
        User user1 = (User) constructor.newInstance();

        //       
        Field field = c.getField("name");
        Field field2 = c.getDeclaredField("sex");
        Field field3 = c.getDeclaredField("age");
        field2.setAccessible(true);
        field3.setAccessible(true);

        //         
        field.set(user1, "MAGIC");
        field2.set(user1, "BOY");
        field3.set(user1, 21);
        System.out.println(user1.toString());// User [name=MAGIC,sex=BOY,age=21]

メンバー変数に値を設定するset(Object obj,Object...value)メソッドで、1番目のパラメータは反射されたJavaオブジェクトで、2番目のパラメータはその属性に設定された値です.上から全体的にuser 1オブジェクトとして理解されるname(field)変数は「MAGIC」に設定されます.
4.メンバーの取得方法
関連メソッド:
    //    Class                    
    Method getMethod(String name, Class>... parameterTypes)
    //    Class                     ,          
    Method[] getMethods()
    //    Class                   
    Method getDeclaredMethod(String name, Class>... parameterTypes)
    //    Class                ,    、  、  ( )       ,         
    Method[] getDeclaredMethods()
        //     Class           
        User user = (User) c.newInstance();
        Method method = c.getMethod("setName", String.class);
        Method method1 = c.getMethod("setSex", String.class);
        Method method2 = c.getMethod("setAge", int.class);

        Method method3 = c.getMethod("getName");
        Method method4 = c.getMethod("getSex");
        Method method5 = c.getMethod("getAge");

        //     
        method.invoke(user, "Magic");
        method1.invoke(user, "Boy");
        method2.invoke(user, 20);
        System.out.println(method3.invoke(user));// Magic
        System.out.println(method4.invoke(user));// Boy
        System.out.println(method5.invoke(user));// 20

メンバー変数を取得する方法とそのメソッドを呼び出す方法は比較的多く,invoke(Object obj,Object...value)メソッドでは,最初のパラメータは反射したJavaオブジェクトであり,2番目のパラメータはそのメソッドに設定された実パラメータである.
package com.cxx.study;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.TypeVariable;

public class Main {
    /**
     *      Java      ,                 !
     * @param args
     * @throws ClassNotFoundException
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws IllegalArgumentException
     * @throws NoSuchFieldException
     * @throws SecurityException
     * @throws NoSuchMethodException
     */
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, SecurityException, NoSuchFieldException, NoSuchMethodException {
        // TODO Auto-generated method stub

        //Demo1.    Java             
        Demo1();
        System.out.println("===============================================");

        //Demo2.          Class      
        Demo2();
        System.out.println("===============================================");

        //Demo3.    Java    , Class      [             ],    
        Demo3();
        System.out.println("===============================================");

        //Demo4:    Java              ,           
        Demo4();
        System.out.println("===============================================");

        //Demo5:    Java          , set   get
        Demo5();
        System.out.println("===============================================");

        //Demo6:   Java            :      ,  ,    ,    ,   
        Demo6();
        System.out.println("===============================================");

        //Demo7:   Java          
        Demo7();
        System.out.println("===============================================");

        //Demo8:   Java          
        Demo8();
        System.out.println("===============================================");

    }

    /**
     * Demo1:   Java             
     */
    public static void Demo1()
    {
        Person person = new Person();
        System.out.println("Demo1:   : " + person.getClass().getPackage().getName() + ","
                + "    : " + person.getClass().getName());
    }

    /**
     * Demo2:         Class      
     * @throws ClassNotFoundException
     */
    public static void Demo2() throws ClassNotFoundException
    {
        //          Class ,      null,           Person 
        Class> class1 = null;
        Class> class2 = null;

        //  1,      ClassNotFoundException [      ]
        class1 = Class.forName("com.cxx.study.Person");
        System.out.println("Demo2:(  1)   : " + class1.getPackage().getName() + ","
                + "    : " + class1.getName());

        //  2
        class2 = Person.class;
        System.out.println("Demo2:(  2)   : " + class2.getPackage().getName() + ","
                + "    : " + class2.getName());
    }

    /**
     * Demo3:   Java    , Class      [             ]
     * @throws ClassNotFoundException
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    public static void Demo3() throws ClassNotFoundException, InstantiationException, IllegalAccessException
    {
        Class> class1 = null;
        class1 = Class.forName("com.cxx.study.Person");
        //         ,           Person,           ~
        Person person = (Person) class1.newInstance();
        person.setAge(20);
        person.setName("LeeFeng");
        System.out.println("Demo3: " + person.getName() + " : " + person.getAge());
    }

    /**
     * Demo4:   Java              ,           
     * @throws ClassNotFoundException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @throws IllegalArgumentException
     */
    public static void Demo4() throws ClassNotFoundException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException
    {
        Class> class1 = null;
        Person person1 = null;
        Person person2 = null;

        class1 = Class.forName("com.cxx.study.Person");
        //           
        Constructor>[] constructors = class1.getConstructors();

        person1 = (Person) constructors[0].newInstance();
        person1.setAge(30);
        person1.setName("leeFeng");

        person2 = (Person) constructors[1].newInstance(20,"leeFeng");

        System.out.println("Demo4: " + person1.getName() + " : " + person1.getAge()
                + "  ,   " + person2.getName() + " : " + person2.getAge()
        );

    }

    /**
     * Demo5:   Java          , set   get
     *
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     * @throws NoSuchFieldException
     * @throws SecurityException
     * @throws InstantiationException
     * @throws ClassNotFoundException
     */
    public static void Demo5() throws IllegalArgumentException, IllegalAccessException, SecurityException, NoSuchFieldException, InstantiationException, ClassNotFoundException
    {
        Class> class1 = null;
        class1 = Class.forName("com.cxx.study.Person");
        Object obj = class1.newInstance();

        Field personNameField = class1.getDeclaredField("name");
        personNameField.setAccessible(true);
        personNameField.set(obj, "cxx");


        System.out.println("Demo5:               :" + personNameField.get(obj));

    }


    /**
     * Demo6:   Java            :      ,  ,    ,    ,   
     * @throws ClassNotFoundException
     */
    public static void Demo6() throws ClassNotFoundException
    {
        Class> class1 = null;
        class1 = Class.forName("com.cxx.study.SuperMan");

        //      
        Class>  superClass = class1.getSuperclass();
        System.out.println("Demo6:  SuperMan     : " + superClass.getName());

        System.out.println("===============================================");


        Field[] fields = class1.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            System.out.println("     : " + fields[i]);
        }
        System.out.println("===============================================");


        //     
        Method[] methods = class1.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            System.out.println("Demo6,  SuperMan    :");
            System.out.println("   :" + methods[i].getName());
            System.out.println("      :" + methods[i].getReturnType());
            System.out.println("       :" + Modifier.toString(methods[i].getModifiers()));
            System.out.println("      : " + methods[i]);
        }

        System.out.println("===============================================");

        //        ,        Class,                    
        Class> interfaces[] = class1.getInterfaces();
        for (int i = 0; i < interfaces.length; i++) {
            System.out.println("       : " + interfaces[i].getName() );
        }

    }

    /**
     * Demo7:   Java         
     * @throws ClassNotFoundException
     * @throws NoSuchMethodException
     * @throws SecurityException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     * @throws InstantiationException
     */
    public static void Demo7() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException
    {
        Class> class1 = null;
        class1 = Class.forName("com.cxx.study.SuperMan");

        System.out.println("Demo7: 
fly():"); Method method = class1.getMethod("fly"); method.invoke(class1.newInstance()); System.out.println(" walk(int m):"); method = class1.getMethod("walk",int.class); method.invoke(class1.newInstance(),100); } /** * Demo8: Java * * java 。[ ] 1)Bootstrap ClassLoader c++ , 。 2)Extension ClassLoader , jre\lib\ext 3)AppClassLoader classpath , 。 java 。 * * @throws ClassNotFoundException */ public static void Demo8() throws ClassNotFoundException { Class> class1 = null; class1 = Class.forName("com.cxx.study.SuperMan"); String nameString = class1.getClassLoader().getClass().getName(); System.out.println("Demo8: : " + nameString); } } /** * * @author xiaoyaomeng * */ class Person{ private int age; private String name; public Person(){ } public Person(int age, String name){ this.age = age; this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } } class SuperMan extends Person implements ActionInterface { private boolean BlueBriefs; public void fly() { System.out.println(" ~~"); } public boolean isBlueBriefs() { return BlueBriefs; } public void setBlueBriefs(boolean blueBriefs) { BlueBriefs = blueBriefs; } @Override public void walk(int m) { // TODO Auto-generated method stub System.out.println(" ~~ " + m + " !"); } } interface ActionInterface{ public void walk(int m); }
"C:\Program Files (x86)\Java\jdk1.8.0_151\bin\java" "-javaagent:D:\IntelliJ IDEA 2017.2.5\lib\idea_rt.jar=51338:D:\IntelliJ IDEA 2017.2.5\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files (x86)\Java\jdk1.8.0_151\jre\lib\charsets.jar;C:\Program Files (x86)\Java\jdk1.8.0_151\jre\lib\deploy.jar;C:\Program Files (x86)\Java\jdk1.8.0_151\jre\lib\ext\access-bridge-64.jar;C:\Program Files (x86)\Java\jdk1.8.0_151\jre\lib\ext\cldrdata.jar;C:\Program Files (x86)\Java\jdk1.8.0_151\jre\lib\ext\dnsns.jar;C:\Program Files (x86)\Java\jdk1.8.0_151\jre\lib\ext\jaccess.jar;C:\Program Files (x86)\Java\jdk1.8.0_151\jre\lib\ext\jfxrt.jar;C:\Program Files (x86)\Java\jdk1.8.0_151\jre\lib\ext\localedata.jar;C:\Program Files (x86)\Java\jdk1.8.0_151\jre\lib\ext
ashorn.jar;C:\Program Files (x86)\Java\jdk1.8.0_151\jre\lib\ext\sunec.jar;C:\Program Files (x86)\Java\jdk1.8.0_151\jre\lib\ext\sunjce_provider.jar;C:\Program Files (x86)\Java\jdk1.8.0_151\jre\lib\ext\sunmscapi.jar;C:\Program Files (x86)\Java\jdk1.8.0_151\jre\lib\ext\sunpkcs11.jar;C:\Program Files (x86)\Java\jdk1.8.0_151\jre\lib\ext\zipfs.jar;C:\Program Files (x86)\Java\jdk1.8.0_151\jre\lib\javaws.jar;C:\Program Files (x86)\Java\jdk1.8.0_151\jre\lib\jce.jar;C:\Program Files (x86)\Java\jdk1.8.0_151\jre\lib\jfr.jar;C:\Program Files (x86)\Java\jdk1.8.0_151\jre\lib\jfxswt.jar;C:\Program Files (x86)\Java\jdk1.8.0_151\jre\lib\jsse.jar;C:\Program Files (x86)\Java\jdk1.8.0_151\jre\lib\management-agent.jar;C:\Program Files (x86)\Java\jdk1.8.0_151\jre\lib\plugin.jar;C:\Program Files (x86)\Java\jdk1.8.0_151\jre\lib\resources.jar;C:\Program Files (x86)\Java\jdk1.8.0_151\jre\lib\rt.jar;C:\Users\cxx\Desktop\Javaweb\common\target\classes;C:\Users\cxx\.m2\repository\com\google\zxing\core\3.3.0\core-3.3.0.jar;C:\Users\cxx\.m2\repository\com\google\zxing\javase\3.3.0\javase-3.3.0.jar;C:\Users\cxx\.m2\repository\com\beust\jcommander\1.48\jcommander-1.48.jar;C:\Users\cxx\.m2\repository\com\github\jai-imageio\jai-imageio-core\1.3.1\jai-imageio-core-1.3.1.jar;C:\Users\cxx\.m2\repository\com\oracle\ojdbc6\11.2.0.3\ojdbc6-11.2.0.3.jar" com.cxx.study.Main Demo1: : com.cxx.study, : com.cxx.study.Person =============================================== Demo2:( 1) : com.cxx.study, : com.cxx.study.Person Demo2:( 2) : com.cxx.study, : com.cxx.study.Person =============================================== Demo3: LeeFeng : 20 =============================================== Demo4: leeFeng : 30 , leeFeng : 20 =============================================== Demo5: : =============================================== Demo6: SuperMan : com.cxx.study.Person =============================================== : private boolean com.cxx.study.SuperMan.BlueBriefs =============================================== Demo6, SuperMan : :fly :void :public : public void com.cxx.study.SuperMan.fly() Demo6, SuperMan : :walk :void :public : public void com.cxx.study.SuperMan.walk(int) Demo6, SuperMan : :setBlueBriefs :void :public : public void com.cxx.study.SuperMan.setBlueBriefs(boolean) Demo6, SuperMan : :isBlueBriefs :boolean :public : public boolean com.cxx.study.SuperMan.isBlueBriefs() =============================================== : com.cxx.study.ActionInterface =============================================== Demo7: fly(): ~~ walk(int m): ~~ 100 ! =============================================== Demo8: : sun.misc.Launcher$AppClassLoader =============================================== Process finished with exit code 0

個人的には、反射メカニズムを使用するいくつかの場所があります.
1.ファクトリモード:ファクトリクラスで反射すると、新しいクラスが追加されてから、ファクトリクラスFactoryを変更する必要がなくなります.2.データベースJDBCでClass.forName(Driver)を使用してデータベース接続駆動を取得します.3.クラスファイルを分析します.なにしろクラスのメソッドなどを得ることができます.4.アクセスできない変数や属性にアクセスします.他人コードを解読します.