1
2
3
4
5
6
7
8
9
10 package nl.tudelft.simulation.language.reflection;
11
12 import java.io.Serializable;
13 import java.lang.reflect.Constructor;
14 import java.lang.reflect.Method;
15 import java.util.ArrayList;
16 import java.util.List;
17
18 /***
19 * A method descriptor represents the parameters that the method takes and the
20 * value that it returns. It is a series of characters generated by the grammar
21 * described at <a href =
22 * "http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#1169">
23 * The Java Virtual Machine Specification </a> <br>
24 * (c) copyright 2004 <a href="http://www.simulation.tudelft.nl">Delft
25 * University of Technology </a>, the Netherlands. <br>
26 * See for project information <a
27 * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
28 * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
29 * License (GPL) </a>, no warranty <br>
30 *
31 * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter
32 * Jacobs </a>, <a href="mailto:nlang@fbk.eur.nl">Niels Lang </a><a
33 * href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">Alexander
34 * Verbraeck </a>
35 * @version 1.3 March 24, 2004
36 * @since 1.3
37 */
38 public class MethodSignature implements Serializable
39 {
40 /*** the value of the methodDescriptor */
41 private String value = null;
42
43 /***
44 * constructs a new MethodSignature
45 *
46 * @param value the descriptor
47 */
48 public MethodSignature(final String value)
49 {
50 super();
51 this.value = value;
52 }
53
54 /***
55 * constructs a new MethodSignature
56 *
57 * @param method the method
58 */
59 public MethodSignature(final Method method)
60 {
61 super();
62 Class[] parameterTypes = new Class[0];
63 if (method.getParameterTypes() != null)
64 {
65 parameterTypes = method.getParameterTypes();
66 }
67 this.value = "(";
68 for (int i = 0; i < parameterTypes.length; i++)
69 {
70 this.value = this.value
71 + FieldSignature.toDescriptor(parameterTypes[i]);
72 }
73 this.value = this.value + ")"
74 + FieldSignature.toDescriptor(method.getReturnType());
75 }
76
77 /***
78 * constructs a new MethodSignature
79 *
80 * @param constructor the constructor
81 */
82 public MethodSignature(final Constructor constructor)
83 {
84 super();
85 Class[] parameterTypes = new Class[0];
86 if (constructor.getParameterTypes() != null)
87 {
88 parameterTypes = constructor.getParameterTypes();
89 }
90
91 this.value = "(";
92 for (int i = 0; i < parameterTypes.length; i++)
93 {
94 this.value = this.value
95 + FieldSignature.toDescriptor(parameterTypes[i]);
96 }
97 this.value = this.value + ")"
98 + FieldSignature.toDescriptor(constructor.getDeclaringClass());
99 }
100
101 /***
102 * @return Returns the parameterDescriptor
103 */
104 public String getParameterDescriptor()
105 {
106 return MethodSignature.getParameterDescriptor(this.value);
107 }
108
109 /***
110 * returns the parameterTypes
111 *
112 * @return ClassDescriptor[] the result
113 * @throws ClassNotFoundException on incomplete classPath
114 */
115 public Class[] getParameterTypes() throws ClassNotFoundException
116 {
117 return MethodSignature.getParameterTypes(this.value);
118 }
119
120 /***
121 * @return Returns the returnDescriptor
122 */
123 public String getReturnDescriptor()
124 {
125 return MethodSignature.getReturnDescriptor(this.value);
126 }
127
128 /***
129 * returns the returnType of this methodDescriptor
130 *
131 * @return Returns the returnType
132 * @throws ClassNotFoundException on incomplete classPath
133 */
134 public Class getReturnType() throws ClassNotFoundException
135 {
136 return MethodSignature.getReturnType(this.value);
137 }
138
139 /***
140 * @see java.lang.Object#toString()
141 */
142 public String toString()
143 {
144 return this.value;
145 }
146
147 /***
148 * @return Returns the parameterDescriptor
149 * @param methodDescriptor the methodDescriptor
150 */
151 public static String getParameterDescriptor(final String methodDescriptor)
152 {
153 return methodDescriptor.substring(1, methodDescriptor.indexOf(')'));
154 }
155
156 /***
157 * returns the parameterTypes
158 *
159 * @param methodDescriptor the string
160 * @return ClassDescriptor[] the result
161 * @throws ClassNotFoundException on incomplete classPath
162 */
163 public static Class[] getParameterTypes(final String methodDescriptor)
164 throws ClassNotFoundException
165 {
166 String parameterDescriptor = MethodSignature
167 .getParameterDescriptor(methodDescriptor);
168 List result = new ArrayList();
169 int length = 0;
170 while (length < parameterDescriptor.length())
171 {
172 String array = "";
173 while (parameterDescriptor.charAt(length) == '[')
174 {
175 array = array + "[";
176 length++;
177 }
178 if (parameterDescriptor.charAt(length) == 'L')
179 {
180 String argument = parameterDescriptor.substring(length);
181 argument = array
182 + argument.substring(0, argument.indexOf(';') + 1);
183 result.add(FieldSignature.toClass(argument));
184 length = length + argument.length() - array.length();
185 } else
186 {
187 result.add(FieldSignature.toClass(array
188 + parameterDescriptor.charAt(length)));
189 length++;
190 }
191 }
192 return (Class[]) result.toArray(new Class[result.size()]);
193 }
194
195 /***
196 * @return Returns the returnDescriptor
197 * @param methodDescriptor the methodDescriptor
198 */
199 public static String getReturnDescriptor(final String methodDescriptor)
200 {
201 return methodDescriptor.substring(methodDescriptor.indexOf(')') + 1);
202 }
203
204 /***
205 * returns the returnType of this methodDescriptor
206 *
207 * @param methodDescriptor the returnDescriptor
208 * @return Returns the returnType
209 * @throws ClassNotFoundException on incomplete classPath
210 */
211 public static Class getReturnType(final String methodDescriptor)
212 throws ClassNotFoundException
213 {
214 return FieldSignature.toClass(MethodSignature
215 .getReturnDescriptor(methodDescriptor));
216 }
217 }