1
2
3
4
5
6
7 package nl.tudelft.simulation.language.reflection;
8
9 import java.io.Serializable;
10 import java.lang.reflect.Constructor;
11 import java.lang.reflect.Method;
12 import java.util.ArrayList;
13 import java.util.List;
14
15 /***
16 * A method descriptor represents the parameters that the method takes and the
17 * value that it returns. It is a series of characters generated by the grammar
18 * described at <a href =
19 * "http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#1169">
20 * The Java Virtual Machine Specification </a>.
21 * <p>
22 * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft
23 * University of Technology </a>, the Netherlands.
24 * <p>
25 * See for project information <a
26 * href="http://www.simulation.tudelft.nl/dsol/language">www.simulation.tudelft.nl/language
27 * </a> <br>
28 * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
29 * General Public License (LGPL) </a>, no warranty
30 *
31 * @author <a href="http://www.peter-jacobs.com/index.htm">Peter Jacobs </a>, <a
32 * href="mailto:nlang@fbk.eur.nl">Niels Lang </a><a
33 * href="mailto:a.verbraeck@tbm.tudelft.nl">Alexander
34 * Verbraeck </a>
35 * @version $Revision: 1.8 $ $Date: 2005/08/04 12:08:54 $
36 * @since 1.5
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 @Override
143 public String toString()
144 {
145 return this.value;
146 }
147
148 /***
149 * @return Returns the parameterDescriptor
150 * @param methodDescriptor the methodDescriptor
151 */
152 public static String getParameterDescriptor(final String methodDescriptor)
153 {
154 return methodDescriptor.substring(1, methodDescriptor.indexOf(')'));
155 }
156
157 /***
158 * returns the parameterTypes
159 *
160 * @param methodDescriptor the string
161 * @return ClassDescriptor[] the result
162 * @throws ClassNotFoundException on incomplete classPath
163 */
164 public static Class[] getParameterTypes(final String methodDescriptor)
165 throws ClassNotFoundException
166 {
167 String parameterDescriptor = MethodSignature
168 .getParameterDescriptor(methodDescriptor);
169 List<Class> result = new ArrayList<Class>();
170 int length = 0;
171 while (length < parameterDescriptor.length())
172 {
173 String array = "";
174 while (parameterDescriptor.charAt(length) == '[')
175 {
176 array = array + "[";
177 length++;
178 }
179 if (parameterDescriptor.charAt(length) == 'L')
180 {
181 String argument = parameterDescriptor.substring(length);
182 argument = array
183 + argument.substring(0, argument.indexOf(';') + 1);
184 result.add(FieldSignature.toClass(argument));
185 length = length + argument.length() - array.length();
186 } else
187 {
188 result.add(FieldSignature.toClass(array
189 + parameterDescriptor.charAt(length)));
190 length++;
191 }
192 }
193 return result.toArray(new Class[result.size()]);
194 }
195
196 /***
197 * @return Returns the returnDescriptor
198 * @param methodDescriptor the methodDescriptor
199 */
200 public static String getReturnDescriptor(final String methodDescriptor)
201 {
202 return methodDescriptor.substring(methodDescriptor.indexOf(')') + 1);
203 }
204
205 /***
206 * returns the returnType of this methodDescriptor
207 *
208 * @param methodDescriptor the returnDescriptor
209 * @return Returns the returnType
210 * @throws ClassNotFoundException on incomplete classPath
211 */
212 public static Class getReturnType(final String methodDescriptor)
213 throws ClassNotFoundException
214 {
215 return FieldSignature.toClass(MethodSignature
216 .getReturnDescriptor(methodDescriptor));
217 }
218 }