OSDN Git Service

冗長な宣言を抑止
[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 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      * <p>インタフェースの各メソッド戻り値はvoidでなければならない。
35      * @param types インタフェース群
36      * @return インタフェースを実装したインスタンス。
37      */
38     public static Object buildEmptyProxy(Class<?>... types){
39         ClassLoader loader = types[0].getClassLoader();
40
41         Object proxy =
42                 Proxy.newProxyInstance(loader, types, NOTHING_INVOKER);
43
44         return proxy;
45     }
46
47
48     /**
49      * 何もしないInvoker実装。
50      */
51     private static class Nothing implements InvocationHandler{
52
53         /**
54          * コンストラクタ。
55          */
56         Nothing(){
57             super();
58             return;
59         }
60
61         /**
62          * {@inheritDoc}
63          * NOTHING...
64          * @param proxy {@inheritDoc}
65          * @param method {@inheritDoc}
66          * @param args {@inheritDoc}
67          * @return {@inheritDoc}
68          */
69         @Override
70         public Object invoke(Object proxy, Method method, Object[] args){
71             return null;
72         }
73
74     }
75
76 }