OSDN Git Service

e3ce681a5fe13f820d7935921e0ae7362e109ef0
[mikutoga/TogaGem.git] / src / main / java / jp / sfjp / mikutoga / corelib / EmptyProxyFactory.java
1 /*
2  * nothing proxy factory
3  *
4  * License : The MIT License
5  * Copyright(c) 2013 MikuToga Partners
6  */
7
8 package jp.sfjp.mikutoga.corelib;
9
10 import java.lang.reflect.InvocationHandler;
11 import java.lang.reflect.Method;
12 import java.lang.reflect.Proxy;
13
14 /**
15  * 何もしないインタフェース実装を生成する。
16  */
17 public final class EmptyProxyFactory {
18
19     /** 何もせず何も返さないInvoker。 */
20     public static final InvocationHandler NOTHING_INVOKER = new Nothing();
21
22
23     /**
24      * ダミーコンストラクタ。
25      */
26     private EmptyProxyFactory(){
27         assert false;
28         throw new AssertionError();
29     }
30
31
32     /**
33      * 何もしないインタフェース実装のインスタンスを生成する。
34      *
35      * <p>インタフェースの各メソッド戻り値はvoidでなければならない。
36      *
37      * @param types インタフェース群
38      * @return インタフェースを実装したインスタンス。
39      */
40     public static Object buildEmptyProxy(Class<?>... types){
41         ClassLoader loader = types[0].getClassLoader();
42
43         Object proxy =
44                 Proxy.newProxyInstance(loader, types, NOTHING_INVOKER);
45
46         return proxy;
47     }
48
49
50     /**
51      * 何もしないInvoker実装。
52      */
53     private static class Nothing implements InvocationHandler{
54
55         /**
56          * コンストラクタ。
57          */
58         Nothing(){
59             super();
60             return;
61         }
62
63         /**
64          * {@inheritDoc}
65          * NOTHING...
66          * @param proxy {@inheritDoc}
67          * @param method {@inheritDoc}
68          * @param args {@inheritDoc}
69          * @return {@inheritDoc}
70          */
71         @Override
72         public Object invoke(Object proxy, Method method, Object[] args){
73             return null;
74         }
75
76     }
77
78 }