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