KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > service > MethodIterator


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.service;
16
17 import java.lang.reflect.Method JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.NoSuchElementException JavaDoc;
23
24 import org.apache.hivemind.util.Defense;
25
26 /**
27  * Utility used to iterate over the visible methods of a class.
28  *
29  * @author Howard Lewis Ship
30  */

31 public class MethodIterator
32 {
33     private boolean _toString;
34
35     private int _index = 0;
36
37     /** @since 1.1 */
38     private int _count;
39
40     /** @since 1.1 */
41     private List JavaDoc _signatures;
42
43     public MethodIterator(Class JavaDoc subjectClass)
44     {
45         Defense.notNull(subjectClass, "subjectClass");
46
47         Method JavaDoc[] methods = subjectClass.getMethods();
48
49         Map JavaDoc map = new HashMap JavaDoc();
50
51         for (int i = 0; i < methods.length; i++)
52             processMethod(methods[i], map);
53
54         _signatures = new ArrayList JavaDoc(map.values());
55         _count = _signatures.size();
56     }
57
58     /** @since 1.1 */
59     private void processMethod(Method JavaDoc m, Map JavaDoc map)
60     {
61         _toString |= ClassFabUtils.isToString(m);
62
63         MethodSignature sig = new MethodSignature(m);
64         String JavaDoc uid = sig.getUniqueId();
65
66         MethodSignature existing = (MethodSignature) map.get(uid);
67
68         if (existing == null || sig.isOverridingSignatureOf(existing))
69             map.put(uid, sig);
70     }
71
72     public boolean hasNext()
73     {
74         return _index < _count;
75     }
76
77     /**
78      * Returns the next method (as a {@link MethodSignature}, returning null when all are
79      * exhausted. Each method signature is returned exactly once (even if the same method signature
80      * is defined in multiple inherited classes or interfaces). The order in which method signatures
81      * are returned is not specified.
82      *
83      * @throws NoSuchElementException
84      * if there are no more signatures
85      */

86     public MethodSignature next()
87     {
88         if (_index >= _count)
89             throw new NoSuchElementException JavaDoc();
90
91         return (MethodSignature) _signatures.get(_index++);
92     }
93
94     /**
95      * Returns true if the method <code>public String toString()</code> is part of the interface.
96      */

97     public boolean getToString()
98     {
99         return _toString;
100     }
101 }
Popular Tags