Java Reflectメカニズムの詳細

16591 ワード

Java Reflectメカニズムの詳細
反射メカニズムは、実行状態において、いずれのクラスに対しても、このクラスのすべての属性と方法を知ることができる.いずれのオブジェクトに対しても、そのメソッドとプロパティを呼び出すことができます.このような動的に取得された情報および動的にオブジェクトを呼び出す方法の機能をjava言語の反射メカニズムと呼ぶ.
反射メカニズムは主に以下の機能を提供します.-実行時に任意のオブジェクトが属するクラスを判断します.-実行時に任意のクラスのオブジェクトを構築します.-実行時に任意のクラスが持つメンバー変数とメソッドを判断します.-実行時に任意のオブジェクトを呼び出す方法.-動的エージェントを生成します.
反射メカニズムの関連APIは、1つのオブジェクトから完全なパケット名とクラス名を取得します.
/**
 *           
 * @author mazaiting
 */
public class Reflect1 {
    public static void main(String[] args) {
        Reflect1 reflect1 = new Reflect1();
        System.out.println("  : " + reflect1.getClass().getPackage());
        System.out.println("  : " + reflect1.getClass().getName());
        System.out.println("   : " + reflect1.getClass().getSimpleName());
        System.out.println("   : " + reflect1.getClass().getCanonicalName());
        System.out.println("   : " + reflect1.getClass().getTypeName());
        /**
         *   : package com.mazaiting
         *   : com.mazaiting.Reflect1
         *    : Reflect1
         *    : com.mazaiting.Reflect1
         *    : com.mazaiting.Reflect1
         */
    }
}

クラスオブジェクトのインスタンス化
/**
 *    Class   
 * @author mazaiting
 */
public class Reflect2 {
    public static void main(String[] args) throws ClassNotFoundException {
        Class> class1 = null;
        Class> class2 = null;
        Class> class3 = null;
        
        class1 = Class.forName("com.mazaiting.Reflect2");
        class2 = new Reflect2().getClass();
        class3 = Reflect2.class;
        
        System.out.println("  : " + class1.getName());
        System.out.println("  : " + class2.getName());
        System.out.println("  : " + class3.getName());
        /**
         *   : com.mazaiting.Reflect2
         *   : com.mazaiting.Reflect2
         *   : com.mazaiting.Reflect2
         */     
    }
}

オブジェクトの親とインプリメンテーションのインタフェースを取得する
/**
 *                
 * @author mazaiting
 */
public class Reflect3 implements Serializable{
    public static void main(String[] args) throws ClassNotFoundException {
        Class> clazz = Class.forName("com.mazaiting.Reflect3");
        Class> superclass = clazz.getSuperclass();
        System.out.println("clazz   : " + superclass.getName());
        
        Class>[] interfaces = clazz.getInterfaces();
        for (int i = 0; i < interfaces.length; i++){
            System.out.println("  " + i + ": " + interfaces[i].getName());
        }
        /**
         * clazz   : java.lang.Object
         *   0: java.io.Serializable
         */     
    }
}

クラス内のすべてのコンストラクション関数を取得
/**
 *              
 *                
 * @author mazaiting
 */
public class Reflect4 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
        Class> clazz = Class.forName("com.mazaiting.User");
        
        Constructor>[] constructors = clazz.getConstructors();
        System.out.println("      " + constructors.length + " ");
        for (int i=0;i[] parameterTypes = constructors[i].getParameterTypes();
            System.out.print("cons[" + i + "] (");
            for (int j=0; j
public class User {
    private String name;
    private int age;
    
    public User() {
        super();
    }
    public User(String name) {
        super();
        this.name = name;
    }
    public User(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "User [name=" + name + ", age=" + age + "]";
    }   
}

クラスのすべてのプロパティを取得
/**
 *           
 * @author mazaiting
 */
public class Reflect5 implements Serializable{
    private static final long serialVersionUID = -2862585049955236662L;
    
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
        Class> clazz = Class.forName("com.mazaiting.Reflect5");
        System.out.println("===============    ===============");
        //          
        Field[] fields = clazz.getDeclaredFields();
        for(int i=0;i type = fields[i].getType();
            System.out.println(mo + " " + type.getName() + " " + fields[i].getName() + ";");
        }
        
        System.out.println("==========            ==========");
        
        //               
        Field[] fields2 = clazz.getFields();
        for(int i=0;i type = fields2[i].getType();
            System.out.println(mo + " " + type.getName() + " " + fields2[i].getName() + ";");
        }
        
        /**
         * ===============    ===============
         * private static final long serialVersionUID;
         * ==========            ==========
         * 
         */
    }
}

クラスのすべてのメソッドを取得
/**
 *           
 * @author mazaiting
 */
public class Reflect6 implements Serializable{
    private static final long serialVersionUID = -2862585049955236662L;
    
    public static void main(String[] args) throws ClassNotFoundException {
        Class> clazz = Class.forName("com.mazaiting.User");
        //           ,               
        //    clazz.getDeclaredMethods()  
        Method[] methods = clazz.getMethods();
    
        for(int i=0;i returnType = methods[i].getReturnType();
            int modifiers = methods[i].getModifiers();
            //      
            String modifier = Modifier.toString(modifiers);
            Class>[] parameterTypes = methods[i].getParameterTypes();
            Class>[] exceptionTypes = methods[i].getExceptionTypes();
            System.out.println("   : " + methods[i].getName());
            System.out.println("    : " + modifier);
            System.out.println("   : " + returnType.getName());
            System.out.println("    : " + parameterTypes.length);
            System.out.println("    : " + exceptionTypes.length);
            System.out.println("---------------------------------------");
        }
        /**
         *    : toString
         *     : public
         *    : java.lang.String
         *     : 0
         *     : 0
         * ---------------------------------------
         *    : getName
         *     : public
         *    : java.lang.String
         *     : 0
         *     : 0
         * ---------------------------------------
         *    : setName
         *     : public
         *    : void
         *     : 1
         *     : 0
         * ---------------------------------------
         *    : getAge
         *     : public
         *    : int
         *     : 0
         *     : 0
         * ---------------------------------------
         *    : setAge
         *     : public
         *    : void
         *     : 1
         *     : 0
         * ---------------------------------------
         *    : wait
         *     : public final
         *    : void
         *     : 0
         *     : 1
         * ---------------------------------------
         *    : wait
         *     : public final
         *    : void
         *     : 2
         *     : 1
         * ---------------------------------------
         *    : wait
         *     : public final native
         *    : void
         *     : 1
         *     : 1
         * ---------------------------------------
         *    : equals
         *     : public
         *    : boolean
         *     : 1
         *     : 0
         * ---------------------------------------
         *    : hashCode
         *     : public native
         *    : int
         *     : 0
         *     : 0
         * ---------------------------------------
         *    : getClass
         *     : public final native
         *    : java.lang.Class
         *     : 0
         *     : 0
         * ---------------------------------------
         *    : notify
         *     : public final native
         *    : void
         *     : 0
         *     : 0
         * ---------------------------------------
         *    : notifyAll
         *     : public final native
         *    : void
         *     : 0
         *     : 0
         * ---------------------------------------
         */
    }
}

反射メカニズムによってクラスを呼び出す方法
/**
 *             
 * @author mazaiting
 */
public class Reflect7 implements Serializable{
    private static final long serialVersionUID = -2862585049955236662L;
    
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
        Class> clazz = Class.forName("com.mazaiting.Reflect7");
        
        //       reflect1  
        Method reflect1 = clazz.getDeclaredMethod("reflect1");
        //     
        reflect1.invoke(clazz.newInstance());
        
        Method reflect2 = clazz.getDeclaredMethod("reflect2", int.class, String.class);
        reflect2.invoke(clazz.newInstance(), 20, "mazaiting");  
        
        /**
         * Java      -         1.
         * Java      -         2.
         * age -> 20. name -> mazaiting
         */
    }
    
    public void reflect1() {
        System.out.println("Java      -         1.");
    }
    public void reflect2(int age, String name) {
        System.out.println("Java      -         2.");
        System.out.println("age -> " + age + ". name -> " + name);
    }
}

クラスのプロパティを反射メカニズムで操作する
/**
 *          
 * @author mazaiting
 */
public class Reflect8 implements Serializable{
    private static final long serialVersionUID = -2862585049955236662L;
    private String proprety = null;
    public static void main(String[] args) throws Exception{
        //    
        Class> clazz = Class.forName("com.mazaiting.Reflect8");
        //         
        Reflect8 instance = (Reflect8) clazz.newInstance();
        //         
        Field proprety = clazz.getDeclaredField("proprety");
        //      
        proprety.setAccessible(true);
        //     
        proprety.set(instance, "  ");
        System.out.println(proprety.get(instance));     
        /**
         *   
         */
    }
}

反射メカニズムのダイナミックエージェント

/**
 *          
 *  java         。
 * 1) Bootstrap ClassLoader       c++  ,        。
 * 2) Extension ClassLoader           ,      jrelibext     
 * 3) AppClassLoader   classpath    ,        。    java       。
 *           ,        InvocationHandler     ,          。
 * @author mazaiting
 */
public class Reflect9 {
    
    public static void main(String[] args) throws Exception{
        MyInvocationHandler invocationHandler = new MyInvocationHandler();
        Subject sub = (Subject) invocationHandler.bind(new RealSubject());
        String info = sub.say("mazaiting", 23);
        System.out.println(info);
        /**
         * mazaiting, 23
         */
    }
}

class MyInvocationHandler implements InvocationHandler{
    private Object obj = null;
    public Object bind(Object obj) {
        this.obj = obj;
        return Proxy.newProxyInstance(obj.getClass().getClassLoader(), 
                obj.getClass().getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object temp = method.invoke(obj, args);
        return temp;
    }
    
}

class RealSubject implements Subject{

    @Override
    public String say(String name, int age) {
        return name + ", " + age;
    }
    
}

interface Subject{
    String say(String name, int age);
}

インスタンスは、汎用IntegerのArrayListにStringタイプのオブジェクトを格納します.
/**
 *     Integer ArrayList     String     。
 * @author mazaiting
 */
public class Reflect10 {
    
    public static void main(String[] args) throws Exception{
        
        ArrayList list = new ArrayList<>();
        
        Method method = list.getClass().getDeclaredMethod("add", Object.class);
        
        method.invoke(list, "Java      ");
        
        System.out.println(list.get(0));
        /**
         * Java      
         */
    }
}

反射による配列情報の取得と修正
/**
 *             
 * @author mazaiting
 */
public class Reflect11 {
    
    public static void main(String[] args) throws Exception{
        int[] temp = {1, 2, 3, 4, 5};
        Class> componentType = temp.getClass().getComponentType();
        System.out.println("    : " + componentType.getName());
        System.out.println("    : " + Array.getLength(temp));
        System.out.println("        : " + Array.get(temp, 0));
        //           100
        Array.set(temp, 0, 100);
        System.out.println("            : " + Array.get(temp, 0));
    }
}

反射メカニズムによる配列のサイズの変更
/**
 *            
 * @author mazaiting
 */
public class Reflect12 {
    
    public static void main(String[] args) throws Exception {
        int[] temp = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        int[] newTemp = (int[]) arrayInc(temp, 15);
        print(newTemp);
        String[] atr = { "a", "b", "c" };
        String[] str1 = (String[]) arrayInc(atr, 8);
        print(str1);
        /**
         *      : 15
         * 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 
         *      : 8
         * a b c null null null null null 
         */
    }
    //       
    public static Object arrayInc(Object obj, int len) {
        Class> arr = obj.getClass().getComponentType();
        Object newArr = Array.newInstance(arr, len);
        int co = Array.getLength(obj);
        System.arraycopy(obj, 0, newArr, 0, co);
        return newArr;
    }
    //   
    public static void print(Object obj) {
        Class> c = obj.getClass();
        if (!c.isArray()) {
            return;
        }
        System.out.println("     : " + Array.getLength(obj));
        for (int i = 0; i < Array.getLength(obj); i++) {
            System.out.print(Array.get(obj, i) + " ");
        }
        System.out.println();
    }
}

反射メカニズムをファクトリモードに適用する
/**
 *            
 * @author mazaiting
 */
public class Reflect13 {
    
    public static void main(String[] args) throws Exception {
//      Fruit f = Factory.getInstance("com.mazaiting.Orange");
        Fruit f = Factory.getInstance("com.mazaiting.Apple");
        if (f != null) {
            f.eat();
        }
        /**
         * Apple
         */
    }
    
}

interface Fruit {
    public abstract void eat();
}
class Apple implements Fruit {
    public void eat() {
        System.out.println("Apple");
    }
}
class Orange implements Fruit {
    public void eat() {
        System.out.println("Orange");
    }
}
class Factory {
    public static Fruit getInstance(String ClassName) {
        Fruit f = null;
        try {
            f = (Fruit) Class.forName(ClassName).newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return f;
    }
}