OSDN Git Service

use context classloader for java.lang.reflect.Proxy
[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 = Thread.currentThread().getContextClassLoader();
42         Object proxy = buildEmptyProxy(loader, types);
43         return proxy;
44     }
45
46     /**
47      * 何もしないインタフェース実装のインスタンスを生成する。
48      *
49      * <p>インタフェースの各メソッド戻り値はvoidでなければならない。
50      *
51      * @param loader class-loader
52      * @param types インタフェース群
53      * @return インタフェースを実装したインスタンス。
54      */
55     public static Object buildEmptyProxy(
56             ClassLoader loader, Class<?>... types){
57         Object proxy =
58                 Proxy.newProxyInstance(loader, types, NOTHING_INVOKER);
59         return proxy;
60     }
61
62
63     /**
64      * 何もしないInvoker実装。
65      */
66     private static class Nothing implements InvocationHandler{
67
68         /**
69          * コンストラクタ。
70          */
71         Nothing(){
72             super();
73             return;
74         }
75
76         /**
77          * {@inheritDoc}
78          * NOTHING...
79          * @param proxy {@inheritDoc}
80          * @param method {@inheritDoc}
81          * @param args {@inheritDoc}
82          * @return {@inheritDoc}
83          */
84         @Override
85         public Object invoke(Object proxy, Method method, Object[] args){
86             return null;
87         }
88
89     }
90
91 }