1 package org.djutils.reflection;
2
3 import java.io.Serializable;
4 import java.lang.reflect.Constructor;
5 import java.lang.reflect.Method;
6 import java.util.ArrayList;
7 import java.util.List;
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 public class MethodSignature implements Serializable
23 {
24
25 private static final long serialVersionUID = 1L;
26
27
28 private String value = null;
29
30
31
32
33
34 public MethodSignature(final String value)
35 {
36 super();
37 this.value = value;
38 }
39
40
41
42
43
44 public MethodSignature(final Method method)
45 {
46 super();
47 Class<?>[] parameterTypes = new Class<?>[0];
48 if (method.getParameterTypes() != null)
49 {
50 parameterTypes = method.getParameterTypes();
51 }
52 this.value = "(";
53 for (int i = 0; i < parameterTypes.length; i++)
54 {
55 this.value = this.value + FieldSignature.toDescriptor(parameterTypes[i]);
56 }
57 this.value = this.value + ")" + FieldSignature.toDescriptor(method.getReturnType());
58 }
59
60
61
62
63
64 public MethodSignature(final Constructor<?> constructor)
65 {
66 super();
67 Class<?>[] parameterTypes = new Class<?>[0];
68 if (constructor.getParameterTypes() != null)
69 {
70 parameterTypes = constructor.getParameterTypes();
71 }
72
73 this.value = "(";
74 for (int i = 0; i < parameterTypes.length; i++)
75 {
76 this.value = this.value + FieldSignature.toDescriptor(parameterTypes[i]);
77 }
78 this.value = this.value + ")" + FieldSignature.toDescriptor(constructor.getDeclaringClass());
79 }
80
81
82
83
84 public String getParameterDescriptor()
85 {
86 return MethodSignature.getParameterDescriptor(this.value);
87 }
88
89
90
91
92
93
94 public Class<?>[] getParameterTypes() throws ClassNotFoundException
95 {
96 return MethodSignature.getParameterTypes(this.value);
97 }
98
99
100
101
102 public String getReturnDescriptor()
103 {
104 return MethodSignature.getReturnDescriptor(this.value);
105 }
106
107
108
109
110
111
112 public Class<?> getReturnType() throws ClassNotFoundException
113 {
114 return MethodSignature.getReturnType(this.value);
115 }
116
117
118 @Override
119 public String toString()
120 {
121 return this.value;
122 }
123
124
125
126
127
128 public static String getParameterDescriptor(final String methodDescriptor)
129 {
130 return methodDescriptor.substring(1, methodDescriptor.indexOf(')'));
131 }
132
133
134
135
136
137
138
139 public static Class<?>[] getParameterTypes(final String methodDescriptor) throws ClassNotFoundException
140 {
141 String parameterDescriptor = MethodSignature.getParameterDescriptor(methodDescriptor);
142 List<Class<?>> result = new ArrayList<Class<?>>();
143 int length = 0;
144 while (length < parameterDescriptor.length())
145 {
146 String array = "";
147 while (parameterDescriptor.charAt(length) == '[')
148 {
149 array = array + "[";
150 length++;
151 }
152 if (parameterDescriptor.charAt(length) == 'L')
153 {
154 String argument = parameterDescriptor.substring(length);
155 argument = array + argument.substring(0, argument.indexOf(';') + 1);
156 result.add(FieldSignature.toClass(argument));
157 length = length + argument.length() - array.length();
158 }
159 else
160 {
161 result.add(FieldSignature.toClass(array + parameterDescriptor.charAt(length)));
162 length++;
163 }
164 }
165 return result.toArray(new Class<?>[result.size()]);
166 }
167
168
169
170
171
172 public static String getReturnDescriptor(final String methodDescriptor)
173 {
174 return methodDescriptor.substring(methodDescriptor.indexOf(')') + 1);
175 }
176
177
178
179
180
181
182
183 public static Class<?> getReturnType(final String methodDescriptor) throws ClassNotFoundException
184 {
185 return FieldSignature.toClass(MethodSignature.getReturnDescriptor(methodDescriptor));
186 }
187 }