KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > myoodb > core > MethodHelper


1 ///////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
4
//
5
// All Rights Reserved
6
//
7
// This program is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License and GNU Library
9
// General Public License as published by the Free Software Foundation;
10
// either version 2, or (at your option) any later version.
11
//
12
// This program is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License and GNU Library General Public License
16
// for more details.
17
//
18
// You should have received a copy of the GNU General Public License
19
// and GNU Library General Public License along with this program; if
20
// not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
21
// MA 02139, USA.
22
//
23
///////////////////////////////////////////////////////////////////////////////
24
package org.myoodb.core;
25
26 import java.util.*;
27 import java.lang.reflect.Method JavaDoc;
28
29 public final class MethodHelper
30 {
31     public final static String JavaDoc SIGNATURE_DELIMITER =",";
32
33     public static String JavaDoc getSignature(Class JavaDoc[] args)
34     {
35         StringBuilder JavaDoc result = new StringBuilder JavaDoc();
36         result.append("\"");
37         for (int i = 0; i < args.length; i++)
38         {
39             if (i> 0)
40             {
41                 result.append(SIGNATURE_DELIMITER);
42             }
43             result.append(args[i].getName());
44         }
45         result.append("\"");
46         return result.toString();
47     }
48
49     public static String JavaDoc getSignature(String JavaDoc[] args)
50     {
51         StringBuilder JavaDoc result = new StringBuilder JavaDoc();
52         result.append("\"");
53         for (int i = 0; i < args.length; i++)
54         {
55             if (i> 0)
56             {
57                 result.append(SIGNATURE_DELIMITER);
58             }
59             result.append(args[i]);
60         }
61         result.append("\"");
62         return result.toString();
63     }
64
65     public static int getMethodIndex(Method JavaDoc[] methods, Method JavaDoc method)
66     {
67         for (int i = 0; i < methods.length; i++)
68         {
69             if (method.equals(methods[i]) == true)
70             {
71                 return i;
72             }
73         }
74
75         throw new org.myoodb.exception.InternalException("MethodHelper.getMethodIndex (unable to find method: " + method + " )");
76     }
77
78     public static Method JavaDoc[] getMethods(Class JavaDoc classType, Class JavaDoc interfaceType)
79     {
80         Method JavaDoc[] implMethods = classType.getMethods();
81         Method JavaDoc[] interfaceMethods = interfaceType.getMethods();
82
83         TreeSet initSet = new TreeSet();
84         for (int i = 0; i < implMethods.length; i++)
85         {
86             String JavaDoc implName = implMethods[i].getName();
87             String JavaDoc implSig = MethodHelper.getSignature(implMethods[i].getParameterTypes());
88             MethodSignature implMethodSig = new MethodSignature(interfaceType.getName(), implName, implSig, implMethods[i]);
89             initSet.add(implMethodSig);
90         }
91
92         TreeMap indexMap = new TreeMap();
93         for (int j = 0; j < interfaceMethods.length; j++)
94         {
95             String JavaDoc interfaceName = interfaceMethods[j].getName();
96             String JavaDoc interfaceSig = MethodHelper.getSignature(interfaceMethods[j].getParameterTypes());
97             MethodSignature interfaceMethodSig = new MethodSignature(interfaceType.getName(), interfaceName, interfaceSig, null);
98
99             org.myoodb.MyOodbIndex index = null;
100             MethodSignature foundMethodSig = null;
101
102             for (int k = 0; k < implMethods.length; k++)
103             {
104                 String JavaDoc implName = implMethods[k].getName();
105                 String JavaDoc implSig = MethodHelper.getSignature(implMethods[k].getParameterTypes());
106                 MethodSignature implMethodSig = new MethodSignature(interfaceType.getName(), implName, implSig, implMethods[k]);
107
108                 if (interfaceMethodSig.equals(implMethodSig) == true)
109                 {
110                     foundMethodSig = implMethodSig;
111                     index = interfaceMethods[j].getAnnotation(org.myoodb.MyOodbIndex.class);
112                     break;
113                 }
114             }
115
116             if (index != null)
117             {
118                 indexMap.put(new Integer JavaDoc(index.value()), foundMethodSig);
119                 initSet.remove(foundMethodSig);
120             }
121         }
122
123         int index = 0;
124         Iterator iter = indexMap.values().iterator();
125         while (iter.hasNext())
126         {
127             MethodSignature methodSig = (MethodSignature) iter.next();
128             implMethods[index++] = methodSig.getMethod();
129         }
130
131         iter = initSet.iterator();
132         while (iter.hasNext())
133         {
134             MethodSignature methodSig = (MethodSignature) iter.next();
135             implMethods[index++] = methodSig.getMethod();
136         }
137
138         return implMethods;
139     }
140 }
141
Popular Tags