comparison jvmci/jdk.vm.ci.meta/src/jdk/vm/ci/meta/ResolvedJavaMethod.java @ 23789:0cb263db490f

use MethodParameters attribute instead of depending on -g option for sanity checks (JDK-8168915)
author Doug Simon <doug.simon@oracle.com>
date Fri, 04 Nov 2016 14:22:47 +0100
parents d6bd0b9cd0b6
children 224f43824e2b
comparison
equal deleted inserted replaced
23788:e804aec381cd 23789:0cb263db490f
24 24
25 import java.lang.annotation.Annotation; 25 import java.lang.annotation.Annotation;
26 import java.lang.reflect.AnnotatedElement; 26 import java.lang.reflect.AnnotatedElement;
27 import java.lang.reflect.Array; 27 import java.lang.reflect.Array;
28 import java.lang.reflect.Method; 28 import java.lang.reflect.Method;
29 import java.lang.reflect.Modifier;
29 import java.lang.reflect.Type; 30 import java.lang.reflect.Type;
30 31
31 /** 32 /**
32 * Represents a resolved Java method. Methods, like fields and types, are resolved through 33 * Represents a resolved Java method. Methods, like fields and types, are resolved through
33 * {@link ConstantPool constant pools}. 34 * {@link ConstantPool constant pools}.
170 171
171 /** 172 /**
172 * Returns the constant pool of this method. 173 * Returns the constant pool of this method.
173 */ 174 */
174 ConstantPool getConstantPool(); 175 ConstantPool getConstantPool();
176
177 /**
178 * A {@code Parameter} provides information about method parameters.
179 */
180 class Parameter implements AnnotatedElement {
181 private final String name;
182 private final ResolvedJavaMethod method;
183 private final int modifiers;
184 private final int index;
185
186 /**
187 * Constructor for {@code Parameter}.
188 *
189 * @param name the name of the parameter
190 * @param modifiers the modifier flags for the parameter
191 * @param method the method which defines this parameter
192 * @param index the index of the parameter
193 */
194 public Parameter(String name,
195 int modifiers,
196 ResolvedJavaMethod method,
197 int index) {
198 this.name = name;
199 this.modifiers = modifiers;
200 this.method = method;
201 this.index = index;
202 }
203
204 /**
205 * Gets the name of the parameter.
206 */
207 public String getName() {
208 return name;
209 }
210
211 /**
212 * Gets the method declaring the parameter.
213 */
214 public ResolvedJavaMethod getDeclaringMethod() {
215 return method;
216 }
217
218 /**
219 * Get the modifier flags for the parameter.
220 */
221 public int getModifiers() {
222 return modifiers;
223 }
224
225 /**
226 * Gets the kind of the parameter.
227 */
228 public JavaKind getKind() {
229 return method.getSignature().getParameterKind(index);
230 }
231
232 /**
233 * Gets the formal type of the parameter.
234 */
235 public Type getParameterizedType() {
236 return method.getGenericParameterTypes()[index];
237 }
238
239 /**
240 * Gets the type of the parameter.
241 */
242 public JavaType getType() {
243 return method.getSignature().getParameterType(index, method.getDeclaringClass());
244 }
245
246 /**
247 * Determines if the parameter represents a variable argument list.
248 */
249 public boolean isVarArgs() {
250 return method.isVarArgs() && index == method.getSignature().getParameterCount(false) - 1;
251 }
252
253 public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
254 return method.getParameterAnnotations(annotationClass)[index];
255 }
256
257 public Annotation[] getAnnotations() {
258 return method.getParameterAnnotations()[index];
259 }
260
261 public Annotation[] getDeclaredAnnotations() {
262 return getAnnotations();
263 }
264
265 @Override
266 public String toString() {
267 Type type = getParameterizedType();
268 String typename = type.getTypeName();
269 if (isVarArgs()) {
270 typename = typename.replaceFirst("\\[\\]$", "...");
271 }
272
273 final StringBuilder sb = new StringBuilder(Modifier.toString(getModifiers()));
274 if (sb.length() != 0) {
275 sb.append(' ');
276 }
277 return sb.append(typename).append(' ').append(getName()).toString();
278 }
279
280 @Override
281 public boolean equals(Object obj) {
282 if (obj instanceof Parameter) {
283 Parameter other = (Parameter) obj;
284 return (other.method.equals(method) && other.index == index);
285 }
286 return false;
287 }
288
289 @Override
290 public int hashCode() {
291 return method.hashCode() ^ index;
292 }
293 }
294
295 /**
296 * Returns an array of {@code Parameter} objects that represent all the parameters to this
297 * method. Returns an array of length 0 if this method has no parameters. Returns {@code null}
298 * if the parameter information is unavailable.
299 */
300 default Parameter[] getParameters() {
301 return null;
302 }
175 303
176 /** 304 /**
177 * Returns an array of arrays that represent the annotations on the formal parameters, in 305 * Returns an array of arrays that represent the annotations on the formal parameters, in
178 * declaration order, of this method. 306 * declaration order, of this method.
179 * 307 *