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.reflect.Method;
19  import java.util.Iterator;
20  
21  import jp.liq.container.util.ArrayIterator;
22  import jp.liq.container.util.IteratorWrapper;
23  
24  /**
25   * あるクラスが保持するメソッドの中から、
26   * 条件にマッチするものを順に返す Iterable です。
27   * @author nose
28   */
29  public class ClassMethodFinder<T> extends MemberFinder<MethodWrapper, ClassMethodFinder<T>> 
30      implements Iterable<MethodWrapper> {
31      
32      private final Class<T> ownerClass;
33      
34      /**
35       * このクラスの新しいインスタンスを構築します。
36       * @param ownerClass メソッドを保持するクラス。
37       */
38      public ClassMethodFinder(Class<T> ownerClass) {
39          this.ownerClass = ownerClass;
40      }
41  
42      @Override
43      protected ClassMethodFinder<T> getThis() {
44          return this;
45      }
46  
47      /**
48       * メンバを保持しているクラスを帰します。
49       */
50      public Class<T> getOwnerClass() {
51          return ownerClass;
52      }
53  
54      @Override
55      protected Iterator<MethodWrapper> getCandidates() {
56          return new MethodWrapperIterator(
57                  new ArrayIterator<Method>(ownerClass.getMethods()));
58      }
59  
60      private class MethodWrapperIterator extends IteratorWrapper<MethodWrapper, Method> {
61          public MethodWrapperIterator(Iterator<Method> iterator) {
62              super(iterator);
63          }
64  
65          public MethodWrapper next() {
66              return new MethodWrapper(iterator.next());
67          }
68          
69      }
70  }