【Java基礎】RTTI與反射之Java

leesf發表於2016-02-29

一、引言 

  很多時候我們的程式可能需要在執行時識別物件和類的資訊,比如多型就是基於執行時環境進行動態判斷實際引用的物件。在執行時識別物件和類的資訊主要有兩種方式:1.RTTI,具體是Class物件,它假定我們在編譯時已經知道了所有型別。2.反射機制,執行我們在執行時發現和使用類的資訊。

二、RTTI

  RTTI(Run-Time Type Infomation),執行時型別資訊。可以在執行時識別一個物件的型別。型別資訊在執行時通過Class物件表示,Class物件包含與類有關的資訊,可以使用Class物件來建立類的例項。

  每個類對應一個Class物件,這個Class物件放在.class檔案中,當我們的程式中首次主動使用某型別時,會把該型別所對應的Class物件載入進記憶體,在這篇文章JVM之類載入器中闡述了哪些情況符合首次主動使用。

  既然RTTI和Class物件有莫大的關係,即有了Class物件,就可以進行很多操作,那麼,我們如何獲取到Class物件呢?有三種方法1. Class.forName("全限定名");(其中,全限定名為包名+類名)。2. 類字面常量,如String.class,對應String類的Class物件。3.通過getClass()方法獲取Class物件,如String str = "abc";str.getClass();。

  通過一個類對應的Class物件後,我們可以做什麼?我們可以獲取該類的父類、介面、建立該類的物件、該類的構造器、欄位、方法等等。總之,威力相當大。

  下面我們通過一個例子來熟悉Class物件的各種用法。

package com.hust.grid.leesf.algorithms;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

interface SuperInterfaceA {
};

interface SuperInterfaceB {
};

class SuperC {
    private String name;

    public SuperC() {

    }

    public SuperC(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }
}

class Sub extends SuperC implements SuperInterfaceA, SuperInterfaceB {
    private String name;
    public Sub() {
        super();
    }

    public Sub(String name) {    
        super(name);
        this.name = name;    
    }
    
    public String getName() {
        return name;
    }
}

public class Main {
    public static Sub makeInstance(Class<?> clazz) {
        Sub sub = null;
        try {
            sub = (Sub) clazz.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }

        return sub;
    }

    public static void printBasicInfo(Class<?> clazz) {
        System.out.println("CanonicalName : " + clazz.getCanonicalName());
        System.out.println("Name : " + clazz.getName());
        System.out.println("Simple Name : " + clazz.getSimpleName());
        System.out.println("SuperClass Name : "
                + clazz.getSuperclass().getName());
        Class<?>[] interfaces = clazz.getInterfaces();
        for (Class<?> inter : interfaces) {
            System.out.println("Interface SimpleName : "
                    + inter.getSimpleName());
        }
        Constructor<?>[] constructors = clazz.getConstructors();
        for (Constructor<?> cons : constructors) {
            System.out.println("Constructor Name : " + cons.getName()
                    + " And Parameter Count : " + cons.getParameterCount());
        }
        Method[] methods = clazz.getDeclaredMethods();
        for (Method method : methods) {
            System.out.println("Method Name : " + method.getName());
        }
    }

    public static void main(String[] args) {
        //Sub sub = new Sub();
        //Class<?> clazz = sub.getClass();
        Class<?> clazz = Sub.class;
        Sub instance = makeInstance(clazz);
        if (instance != null) {
            System.out.println("make instance successful");
        } else {
            System.out.println("make instance unsuccessful");
        }
        printBasicInfo(clazz);
    }
}
View Code

  執行結果: 

make instance successful
CanonicalName : com.hust.grid.leesf.algorithms.Sub
Name : com.hust.grid.leesf.algorithms.Sub
Simple Name : Sub
SuperClass Name : com.hust.grid.leesf.algorithms.SuperC
Interface SimpleName : SuperInterfaceA
Interface SimpleName : SuperInterfaceB
Constructor Name : com.hust.grid.leesf.algorithms.Sub And Parameter Count : 0
Constructor Name : com.hust.grid.leesf.algorithms.Sub And Parameter Count : 1
Method Name : getName
View Code

  說明:使用method1、method2、method3三種方法都可以獲得Class物件,執行結果是等效的。但是三者還是有稍許的區別。區別是從類的初始化角度來看的。如Class.forName("全限定名")會導致型別的載入、連結、初始化過程,而.class則不會初始化該類。顯然,getClass肯定是會初始化該類的,因為這個方法時依託於類的物件。

  下面我們通過一個例子比較.class和forName()兩種方法的區別。

package com.hust.grid.leesf.algorithms;
import java.util.Random;
class Init1 {
    static final int staticFinal1 = 1;
    static final int staticFinal2 = Main.random.nextInt(100);
    static {
        System.out.println("init init1");
    }
}

class Init2 {
    static int staticNonFinal1 = 3;
    static {
        System.out.println("init init2");
    }
}

class Init3 {
    static int staticNonFinal1 = 5;
    static {
        System.out.println("init init3");
    }
}

public class Main {
    public static Random random = new Random(47);
    public static void main(String[] args) {
        Class<?> clazzClass = Init1.class;
        System.out.println("after init init1 ref");
        System.out.println(Init1.staticFinal1);
        System.out.println(Init1.staticFinal2);
        
        System.out.println(Init2.staticNonFinal1);
        try {
            Class<?> clazz1 = Class.forName("com.hust.grid.leesf.algorithms.Init3");
            System.out.println("after init init3 ref");
            System.out.println(Init3.staticNonFinal1);
        } catch (ClassNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
}
View Code

  執行結果:

after init init1 ref
1
init init1
58
init init2
3
init init3
after init init3 ref
5
View Code

  說明:從結果也進一步驗證了.class不會初始化類,而.forName()會初始化類。並且,對常量靜態域的使用也不會導致類的初始化。

三、反射

  與RTTI必須在編譯器就知道所有型別不同,反射不必在編譯期就知道所有的型別,它可以在執行過程中使用動態載入的類,而這個類不必在編譯期就已經知道。反射主要由java.lang.reflect類庫的Field、Method、Constructor類支援。這些類的物件都是JVM在執行時進行建立,用來表示未知的類。

  關於兩者的區別更深刻表達如下:對於RTTI而言,編譯器在編譯時開啟和檢查.class檔案;對於反射而言,.class檔案在編譯時是不可獲取的,所以在執行時開啟和檢查.class檔案。

  其實在的第一個例子中我們已經用到了Constructor、Method類,現在我們來更加具體的瞭解Constructor、Method、Field類。  

package com.hust.grid.leesf.algorithms;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;

class Human {

}

class Girl extends Human {
    private boolean beautiful;
    private int height;
    private String name;
    
    public Girl() {

    }

    public Girl(String name, int height, boolean beautiful) {
        this.name = name;
        this.height = height;
        this.beautiful = beautiful;
    }

    public boolean isBeautiful() {
        return beautiful;
    }

    public String toString() {
        return "height = " + height + " name = " + name + " beautiful = " + beautiful;
    }
    
    private void print() {
        System.out.println("i am a private method");
    }
}

class Boy extends Human {
    private boolean handsome;
    private int height;
    private String name;
    public Boy() {

    }

    public Boy(String name, int height, boolean handsome) {
        this.name = name;
        this.height = height;
        this.handsome = handsome;
    }

    public boolean isHandsome() {
        return handsome;
    }

    public String toString() {
        return "height = " + height + " name = " + name + " handsome = " + handsome;
    }

    private void print() {
        System.out.println("i am a private method");
    }
}

public class Test {
    public static void main(String[] args) throws NoSuchMethodException,
            SecurityException, InstantiationException, IllegalAccessException,
            IllegalArgumentException, InvocationTargetException,
            NoSuchFieldException {
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        Human human = null;
        String name = "leesf";
        int height = 180;
        boolean handsome = true;
        boolean flag = false;
        if ("boy".equals(input)) {
            human = new Boy(name, height, handsome);
            flag = true;
        } else {
            human = new Girl("dyd", 168, true);
        }
        scanner.close();
        Class<?> clazz = human.getClass();
        Constructor<?> constructor = clazz.getConstructor(String.class,
                int.class, boolean.class);
        Human human1 = (Human) constructor.newInstance("leesf_dyd", 175, true);
        System.out.println(human1);
        Method method = null;
        if (flag) {
            method = clazz.getMethod("isHandsome");
        } else {
            method = clazz.getMethod("isBeautiful");
        }
        System.out.println(method.invoke(human));
        Method method2 = clazz.getDeclaredMethod("print");
        method2.setAccessible(true);
        method2.invoke(human);

        Field field = clazz.getDeclaredField("height");
        System.out.println(human);
        field.setAccessible(true);
        field.set(human, 200);
        System.out.println(human);
    }
}
View Code

  輸入:boy

  執行結果: 

boy
height = 175 name = leesf_dyd handsome = true
true
i am a private method
height = 180 name = leesf handsome = true
height = 200 name = leesf handsome = true
View Code

  說明:反射可以讓我們建立一個類的例項、在類外部訪問類的私有方法、私有欄位。反射真的很強大~

四、動態代理-反射的應用

  動態建立代理並且動態處理對所代理方法的呼叫。在動態代理上所做的所有呼叫都會被重定向到單一的呼叫處理器上。

  下面是動態代理的例子

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

interface Interface {
    void doSomething();

    void doSomethingElse(String str);
}

class RealObject implements Interface {
    @Override
    public void doSomething() {
        System.out.println("doSomething");
    }

    @Override
    public void doSomethingElse(String str) {
        System.out.println("doSomething else " + str);
    }
}

class DynamicProxyHandler implements InvocationHandler {
    private Object proxied;

    public DynamicProxyHandler(Object proxied) {
        this.proxied = proxied;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {
        if (method.getName().startsWith("do")) {
            System.out.println("call do*** methods");
        }
        method.invoke(proxied, args);
        return null;
    }

}

public class DynamicProxy {
    public static void main(String[] args) {
        RealObject proxied = new RealObject();
        proxied.doSomething();
        proxied.doSomethingElse("leesf");
        Interface proxy = (Interface) Proxy.newProxyInstance(Interface.class
                .getClassLoader(), new Class[] { Interface.class },
                new DynamicProxyHandler(proxied));
        proxy.doSomething();
        proxy.doSomethingElse("leesf");
    }
}
View Code

  執行結果: 

doSomething
doSomething else leesf
call do*** methods
doSomething
call do*** methods
doSomething else leesf
View Code

  說明:可以在invoke方法中進行過濾操作。過濾出以do開頭的方法進行轉發。

五、總結

  RTTI和反射分析就到此為止,RTTI和反射確實很強大,可以幫助我們幹很多事情,用對地方絕對威力無窮,謝謝各位園友的觀看~

  

相關文章