View Javadoc

1   /*
2    * Copyright 2007-2008 Naoki NOSE.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package jp.liq.container.reflect;
17  
18  import java.lang.annotation.Annotation;
19  import java.lang.reflect.Method;
20  
21  /**
22   * リフレクション API の Method をラップします。
23   * 
24   * @author nose
25   */
26  public class MethodWrapper extends ParameterizedMember {
27      private final Method method;
28  
29      public MethodWrapper(Method method) {
30          this.method = method;
31      }
32  
33      /**
34       * @see jp.liq.container.reflect.Member#name()
35       */
36      public String name() {
37          return method.getName();
38      }
39      /**
40       * ラップされた Method を返します。
41       */
42      public java.lang.reflect.Method getMethod() {
43          return method;
44      }
45  
46      /**
47       * このメソッドを呼び出し、戻り値を返す ObjectFactory を生成します。
48       * @param target メソッド呼び出しの対象となるオブジェクト
49       * @param type 戻り値の型
50       * @return 生成された ObjectFactory
51       */
52      public <T> ObjectFactory<T> createObjectFactory(final Object target, final Class<T> type) {
53          final MethodWrapper m = this;
54  
55          //TODO check type mismatch
56          return new ObjectFactory<T>() {
57  
58              public T createObject(Object[] args) throws ReflectException {
59                  return type.cast(m.invoke(target, args));
60              }
61          };
62      }
63      
64      /**
65       * @see jp.liq.container.reflect.ObjectFactory#createObject(Object[])
66       */
67      public Object invoke(Object target, Object[] args) throws ReflectException {
68          try {
69              return method.invoke(target, args);
70          } catch (Exception e) {
71              throw new ReflectException(e, this,
72                      ReflectException.Reason.FAILED_TO_INVOKE_METHOD);
73          }
74      }
75  
76      /**
77       * @see jp.liq.container.reflect.Member#isAnnotationPresent(java.lang.Class)
78       */
79      public boolean isAnnotationPresent(Class<? extends Annotation> ann) {
80          return method.getAnnotation(ann) != null;
81      }
82      /**
83       * @see java.lang.Object#equals(java.lang.Object)
84       */
85      public boolean equals(Object o) {
86          return method.equals(o);
87      }
88      /**
89       * @see java.lang.Object#hashCode()
90       */
91      public int hashCode() {
92          return method.hashCode();
93      }
94      /**
95       * このメソッドの文字列表現を返します。
96       * ラップされている Method の toString を呼んでるだけですが。
97       */
98      public String toString() {
99          return method.toString();
100     }
101 
102     /**
103      * @see jp.liq.container.reflect.ParameterizedMember#getParameterAnnotations()
104      */
105     public Annotation[][] getParameterAnnotations() {
106         return method.getParameterAnnotations();
107     }
108 
109     /**
110      * @see jp.liq.container.reflect.ParameterizedMember#getParameterTypes()
111      */
112     public Class<?>[] getParameterTypes() {
113         return method.getParameterTypes();
114     }
115 
116     /**
117      * @see jp.liq.container.reflect.Member#getModifiers()
118      */
119     @Override
120     public int getModifiers() {
121         return method.getModifiers();
122     }
123 }