KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > cmi > compiler > MethodProto


1 /*
2  * Copyright (C) 2002-2003, Simon Nieuviarts
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  */

19 package org.objectweb.carol.cmi.compiler;
20
21 import java.lang.reflect.Method JavaDoc;
22 import java.util.ArrayList JavaDoc;
23
24 /**
25  * @author nieuviar
26  *
27  */

28 public class MethodProto {
29     private String JavaDoc methodName;
30     private String JavaDoc returnType;
31     private String JavaDoc[] paramTypes;
32     private int hash;
33
34     public MethodProto(String JavaDoc sign) throws CompilerException {
35         int obr = sign.indexOf('(');
36         if (obr < 0)
37             badSignature(sign);
38         int mns = sign.lastIndexOf(' ', obr);
39         if (mns < 0)
40             badSignature(sign);
41         methodName = sign.substring(mns + 1, obr);
42         int mnd = methodName.lastIndexOf('.');
43         if (mnd >= 0) {
44             methodName = methodName.substring(mnd + 1);
45         }
46         while (sign.charAt(mns - 1) == ' ')
47             mns--;
48         int rts = sign.lastIndexOf(' ', mns - 1);
49         // OK even if rts is -1 : no modifier, only a return type, get it
50
returnType = sign.substring(rts + 1, mns);
51         int cbr = sign.indexOf(')');
52         String JavaDoc params = sign.substring(obr + 1, cbr);
53         ArrayList JavaDoc p = new ArrayList JavaDoc();
54         params = params.trim();
55         while (!"".equals(params)) {
56             int com = params.indexOf(',');
57             String JavaDoc param;
58             if (com < 0) {
59                 param = params;
60                 params = "";
61             } else {
62                 param = params.substring(0, com);
63                 params = params.substring(com + 1);
64             }
65             int te = param.indexOf(' ');
66             if (te > 0) {
67                 param = param.substring(0, te);
68                 p.add(param);
69             } else if (te < 0) {
70                 p.add(param);
71             }
72             params = params.trim();
73         }
74         paramTypes = new String JavaDoc[p.size()];
75         p.toArray(paramTypes);
76         doHash();
77     }
78
79     public MethodProto(Method JavaDoc m) {
80         returnType = getName(m.getReturnType());
81         methodName = m.getName();
82         Class JavaDoc[] params = m.getParameterTypes();
83         paramTypes = new String JavaDoc[params.length];
84         for (int i=0; i<params.length; i++) {
85             paramTypes[i] = getName(params[i]);
86         }
87         doHash();
88     }
89
90     public static String JavaDoc getName(Class JavaDoc c) {
91         String JavaDoc name;
92         if (c.isArray()) {
93             return getName(c.getComponentType()) + "[]";
94         } else {
95             return c.getName().replace('$', '.');
96         }
97     }
98
99     /**
100      * Generate hash value for this method prototype
101      */

102     private void doHash() {
103         hash = returnType.hashCode() + methodName.hashCode();
104         for (int i = 0; i < paramTypes.length; i++) {
105             hash += paramTypes[i].hashCode();
106         }
107     }
108
109     /**
110      * Method badSignature.
111      */

112     private void badSignature(String JavaDoc sign) throws CompilerException {
113         throw new CompilerException("Bad method signature : " + sign);
114     }
115
116     public int hashCode() {
117         return hash;
118     }
119
120     public boolean equals(Object JavaDoc obj) {
121         if (obj instanceof MethodProto) {
122             MethodProto mp = (MethodProto) obj;
123             String JavaDoc[] pt = mp.paramTypes;
124             if (methodName.equals(mp.methodName)
125                 && (paramTypes.length == pt.length)
126                 && returnType.equals(mp.returnType)) {
127                 for (int i = 0; i < paramTypes.length; i++) {
128                     if (!paramTypes[i].equals(pt[i])) {
129                         return false;
130                     }
131                 }
132                 return true;
133             }
134         }
135         return false;
136     }
137
138     public String JavaDoc toString() {
139         String JavaDoc s = returnType + " " + methodName + "(";
140         for (int i = 0; i < paramTypes.length; i++) {
141             if (i != 0)
142                 s += ",";
143             s += paramTypes[i];
144         }
145         return s + ")";
146     }
147 }
148
Popular Tags