Popular Posts
javax.net.ssl.SSLHandshakeException: Connection closed by peer in Android 5.0 Lollipop Recently, there is a error occurs when access website via ssl connection like below although it worked fine several days ago. // Enable SSL... Close window without confirm (I.E only) window.opener=null; window.open('','_self'); window.close(); android.intent.action.SCREEN_ON & android.intent.action.SCREEN_OFF First, I've tried create a receiver to receive screen on/off and register receiver on AndroidManifest.xml like below, but unfortunately ...
Blog Archive
Stats
Dynamic Proxy
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Date;

public class MathObj implements IMath {

    @Override
    public double sum(Number... value) {
        double sum = 0;
        for (int i = 0; i < value.length; i++) {
            System.out.print(value[i]);
            if (i < value.length - 1)
                System.out.print(" + ");
            sum += value[i].doubleValue();
        }
        System.out.printf(" = %f%n", sum);
        return sum;
    }

    public static void main(String[] args) throws ClassNotFoundException {
        // 建立代理物件
        Object deleagate = Proxy.newProxyInstance(
                ObjectHandler.class.getClassLoader(),
                new Class[] { IMath.class },
                new ObjectHandler(new MathObj())
        );

        // 利用代理執行
        double sum = ((IMath) deleagate).sum(1, 2, 3, 4, 5, 6, 7);

        System.out.printf("Invoke result: %f%n", sum);
    }

}

// 定義代理程式
class ObjectHandler implements InvocationHandler {

    // 原始物件
    Object o;

    public ObjectHandler(Object o) {
        this.o = o;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // 委派代理呼叫方法時, 要處理的動作
        System.out.println("Start at " + new Date());
        Object result = method.invoke(o, args);
        System.out.println("End at " + new Date());
        return result;
    }

}

// 定義介面
interface IMath {
    double sum(Number... value);
}
感想: 不好用xd