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.vocabulary;
17  
18  import static jp.liq.container.vocabulary.VocabularyException.Reason.FAILED_TO_FIND_METHOD;
19  import jp.liq.container.reflect.ClassMethodFinder;
20  import jp.liq.container.reflect.MethodWrapper;
21  import jp.liq.container.reflect.ObjectMethodFinder;
22  
23  /**
24   * リフレクション API の Wrapper を生成する static メソッド群です。
25   * このクラスで定義されたメソッドは、static インポートして使用すると
26   * 具合が良いように命名されています。
27   * @author nose
28   */
29  public class Methods {
30  
31      /**
32       * MethodFinder を生成します。
33       * 検索の対象となるメソッドは、引数に指定されたjava.lang.Classの、
34       * getMethods メソッドを使用して取得されます。
35       * @param type メソッドを持つクラス 
36       * @return 生成されたMethodFinder
37       */
38      public final <T> ClassMethodFinder<T> of(Class<T> type) {
39          ClassMethodFinder<T> rv = new ClassMethodFinder<T>(type);
40          return rv;
41      }
42  
43      /**
44       * ObjectMethodFinder を生成します。
45       * 検索の対象となるメソッドは、引数に指定されたjava.lang.Classの、
46       * getMethods メソッドを使用して取得されます。
47       * @param target メソッドを持つクラス 
48       * @return 生成されたObjectMethodFinder
49       */
50      public final ObjectMethodFinder of(Object target) {
51          ObjectMethodFinder rv = new ObjectMethodFinder(target);
52          return rv;
53      }
54  
55      /**
56       * MethodWrapperを生成します。
57       * @param type メソッドを持つ型 
58       * @param name メソッド名
59       * @param params メソッドの引数の型
60       * @return 生成された MethodWrapper
61       */
62      public final MethodWrapper of(Class<?> type, String name, Class<?>... params) {
63          try {
64              return new MethodWrapper(type.getMethod(name, params));
65          } catch (Exception e) {
66              throw new VocabularyException(e, type, FAILED_TO_FIND_METHOD);
67          }
68      }
69  
70  }