1 /* 2 * Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.lang.reflect; 27 28 import java.lang.annotation.Annotation; 29 import java.util.Arrays; 30 import java.util.Map; 31 import java.util.Set; 32 import java.util.Objects; 33 import java.util.StringJoiner; 34 import java.util.stream.Collectors; 35 36 import jdk.internal.access.SharedSecrets; 37 import jdk.internal.vm.annotation.Stable; 38 import sun.reflect.annotation.AnnotationParser; 39 import sun.reflect.annotation.AnnotationSupport; 40 import sun.reflect.annotation.TypeAnnotationParser; 41 import sun.reflect.annotation.TypeAnnotation; 42 import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl; 43 import sun.reflect.generics.repository.ConstructorRepository; 44 45 /** 46 * A shared superclass for the common functionality of {@link Method} 47 * and {@link Constructor}. 48 * 49 * @sealedGraph 50 * @since 1.8 51 */ 52 public abstract sealed class Executable extends AccessibleObject 53 implements Member, GenericDeclaration permits Constructor, Method { 54 /* 55 * Only grant package-visibility to the constructor. 56 */ 57 @SuppressWarnings("deprecation") 58 Executable() {} 59 60 /** 61 * Accessor method to allow code sharing 62 */ 63 abstract byte[] getAnnotationBytes(); 64 65 /** 66 * Does the Executable have generic information. 67 */ 68 abstract boolean hasGenericInformation(); 69 70 abstract ConstructorRepository getGenericInfo(); 71 72 boolean equalParamTypes(Class<?>[] params1, Class<?>[] params2) { 73 /* Avoid unnecessary cloning */ 74 if (params1.length == params2.length) { 75 for (int i = 0; i < params1.length; i++) { 76 if (params1[i] != params2[i]) 77 return false; 78 } 79 return true; 80 } 81 return false; 82 } 83 84 Annotation[][] parseParameterAnnotations(byte[] parameterAnnotations) { 85 return AnnotationParser.parseParameterAnnotations( 86 parameterAnnotations, 87 SharedSecrets.getJavaLangAccess(). 88 getConstantPool(getDeclaringClass()), 89 getDeclaringClass()); 90 } 91 92 void printModifiersIfNonzero(StringBuilder sb, int mask, boolean isDefault) { 93 int mod = getModifiers() & mask; 94 95 if (mod != 0 && !isDefault) { 96 sb.append(Modifier.toString(mod)).append(' '); 97 } else { 98 int access_mod = mod & Modifier.ACCESS_MODIFIERS; 99 if (access_mod != 0) 100 sb.append(Modifier.toString(access_mod)).append(' '); 101 if (isDefault) 102 sb.append("default "); 103 mod = (mod & ~Modifier.ACCESS_MODIFIERS); 104 if (mod != 0) 105 sb.append(Modifier.toString(mod)).append(' '); 106 } 107 } 108 109 String sharedToString(int modifierMask, 110 boolean isDefault, 111 Class<?>[] parameterTypes, 112 Class<?>[] exceptionTypes) { 113 try { 114 StringBuilder sb = new StringBuilder(); 115 116 printModifiersIfNonzero(sb, modifierMask, isDefault); 117 specificToStringHeader(sb); 118 sb.append(Arrays.stream(parameterTypes) 119 .map(Type::getTypeName) 120 .collect(Collectors.joining(",", "(", ")"))); 121 if (exceptionTypes.length > 0) { 122 sb.append(Arrays.stream(exceptionTypes) 123 .map(Type::getTypeName) 124 .collect(Collectors.joining(",", " throws ", ""))); 125 } 126 return sb.toString(); 127 } catch (Exception e) { 128 return "<" + e + ">"; 129 } 130 } 131 132 /** 133 * Generate toString header information specific to a method or 134 * constructor. 135 */ 136 abstract void specificToStringHeader(StringBuilder sb); 137 138 static String typeVarBounds(TypeVariable<?> typeVar) { 139 Type[] bounds = typeVar.getBounds(); 140 if (bounds.length == 1 && bounds[0].equals(Object.class)) { 141 return typeVar.getName(); 142 } else { 143 return typeVar.getName() + " extends " + 144 Arrays.stream(bounds) 145 .map(Type::getTypeName) 146 .collect(Collectors.joining(" & ")); 147 } 148 } 149 150 String sharedToGenericString(int modifierMask, boolean isDefault) { 151 try { 152 StringBuilder sb = new StringBuilder(); 153 154 printModifiersIfNonzero(sb, modifierMask, isDefault); 155 156 TypeVariable<?>[] typeparms = getTypeParameters(); 157 if (typeparms.length > 0) { 158 sb.append(Arrays.stream(typeparms) 159 .map(Executable::typeVarBounds) 160 .collect(Collectors.joining(",", "<", "> "))); 161 } 162 163 specificToGenericStringHeader(sb); 164 165 sb.append('('); 166 StringJoiner sj = new StringJoiner(","); 167 Type[] params = getGenericParameterTypes(); 168 for (int j = 0; j < params.length; j++) { 169 String param = params[j].getTypeName(); 170 if (isVarArgs() && (j == params.length - 1)) // replace T[] with T... 171 param = param.replaceFirst("\\[\\]$", "..."); 172 sj.add(param); 173 } 174 sb.append(sj.toString()); 175 sb.append(')'); 176 177 Type[] exceptionTypes = getGenericExceptionTypes(); 178 if (exceptionTypes.length > 0) { 179 sb.append(Arrays.stream(exceptionTypes) 180 .map(Type::getTypeName) 181 .collect(Collectors.joining(",", " throws ", ""))); 182 } 183 return sb.toString(); 184 } catch (Exception e) { 185 return "<" + e + ">"; 186 } 187 } 188 189 /** 190 * Generate toGenericString header information specific to a 191 * method or constructor. 192 */ 193 abstract void specificToGenericStringHeader(StringBuilder sb); 194 195 /** 196 * Returns the {@code Class} object representing the class or interface 197 * that declares the executable represented by this object. 198 */ 199 public abstract Class<?> getDeclaringClass(); 200 201 /** 202 * Returns the name of the executable represented by this object. 203 */ 204 public abstract String getName(); 205 206 /** 207 * {@return the Java language {@linkplain Modifier modifiers} for 208 * the executable represented by this object} 209 * @see #accessFlags 210 */ 211 public abstract int getModifiers(); 212 213 /** 214 * {@return an unmodifiable set of the {@linkplain AccessFlag 215 * access flags} for the executable represented by this object, 216 * possibly empty} 217 * 218 * @see #getModifiers() 219 * @jvms 4.6 Methods 220 * @since 20 221 */ 222 @Override 223 public Set<AccessFlag> accessFlags() { 224 return AccessFlag.maskToAccessFlags(getModifiers(), 225 AccessFlag.Location.METHOD); 226 } 227 228 /** 229 * Returns an array of {@code TypeVariable} objects that represent the 230 * type variables declared by the generic declaration represented by this 231 * {@code GenericDeclaration} object, in declaration order. Returns an 232 * array of length 0 if the underlying generic declaration declares no type 233 * variables. 234 * 235 * @return an array of {@code TypeVariable} objects that represent 236 * the type variables declared by this generic declaration 237 * @throws GenericSignatureFormatError if the generic 238 * signature of this generic declaration does not conform to 239 * the format specified in 240 * <cite>The Java Virtual Machine Specification</cite> 241 */ 242 public abstract TypeVariable<?>[] getTypeParameters(); 243 244 // returns shared array of parameter types - must never give it out 245 // to the untrusted code... 246 abstract Class<?>[] getSharedParameterTypes(); 247 248 // returns shared array of exception types - must never give it out 249 // to the untrusted code... 250 abstract Class<?>[] getSharedExceptionTypes(); 251 252 /** 253 * Returns an array of {@code Class} objects that represent the formal 254 * parameter types, in declaration order, of the executable 255 * represented by this object. Returns an array of length 256 * 0 if the underlying executable takes no parameters. 257 * Note that the constructors of some inner classes 258 * may have an implicitly declared parameter in addition to 259 * explicitly declared ones. 260 * 261 * @return the parameter types for the executable this object 262 * represents 263 */ 264 public abstract Class<?>[] getParameterTypes(); 265 266 /** 267 * Returns the number of formal parameters (whether explicitly 268 * declared or implicitly declared or neither) for the executable 269 * represented by this object. 270 * 271 * @return The number of formal parameters for the executable this 272 * object represents 273 */ 274 public abstract int getParameterCount(); 275 276 /** 277 * Returns an array of {@code Type} objects that represent the 278 * formal parameter types, in declaration order, of the executable 279 * represented by this object. An array of length 0 is returned if the 280 * underlying executable takes no parameters. Note that the 281 * constructors of some inner classes may have an implicitly 282 * declared parameter in addition to explicitly declared ones. 283 * Also note that as a <a 284 * href="{@docRoot}/java.base/java/lang/reflect/package-summary.html#LanguageJvmModel">modeling 285 * artifact</a>, the number of returned parameters can differ 286 * depending on whether or not generic information is present. If 287 * generic information is present, only parameters explicitly 288 * present in the source will be returned; if generic information 289 * is not present, implicit and synthetic parameters may be 290 * returned as well. 291 * 292 * <p>If a formal parameter type is a parameterized type, 293 * the {@code Type} object returned for it must accurately reflect 294 * the actual type arguments used in the source code. 295 * 296 * <p>If a formal parameter type is a type variable or a parameterized 297 * type, it is created. Otherwise, it is resolved. 298 * 299 * @return an array of {@code Type}s that represent the formal 300 * parameter types of the underlying executable, in declaration order 301 * @throws GenericSignatureFormatError 302 * if the generic method signature does not conform to the format 303 * specified in 304 * <cite>The Java Virtual Machine Specification</cite> 305 * @throws TypeNotPresentException if any of the parameter 306 * types of the underlying executable refers to a non-existent type 307 * declaration 308 * @throws MalformedParameterizedTypeException if any of 309 * the underlying executable's parameter types refer to a parameterized 310 * type that cannot be instantiated for any reason 311 */ 312 public Type[] getGenericParameterTypes() { 313 if (hasGenericInformation()) 314 return getGenericInfo().getParameterTypes(); 315 else 316 return getParameterTypes(); 317 } 318 319 /** 320 * Behaves like {@code getGenericParameterTypes}, but returns type 321 * information for all parameters, including synthetic parameters. 322 */ 323 Type[] getAllGenericParameterTypes() { 324 final boolean genericInfo = hasGenericInformation(); 325 326 // Easy case: we don't have generic parameter information. In 327 // this case, we just return the result of 328 // getParameterTypes(). 329 if (!genericInfo) { 330 return getParameterTypes(); 331 } else { 332 final boolean realParamData = hasRealParameterData(); 333 final Type[] genericParamTypes = getGenericParameterTypes(); 334 final Type[] nonGenericParamTypes = getSharedParameterTypes(); 335 // If we have real parameter data, then we use the 336 // synthetic and mandate flags to our advantage. 337 if (realParamData) { 338 final Type[] out = new Type[nonGenericParamTypes.length]; 339 final Parameter[] params = getParameters(); 340 int fromidx = 0; 341 for (int i = 0; i < out.length; i++) { 342 final Parameter param = params[i]; 343 if (param.isSynthetic() || param.isImplicit()) { 344 // If we hit a synthetic or mandated parameter, 345 // use the non generic parameter info. 346 out[i] = nonGenericParamTypes[i]; 347 } else { 348 // Otherwise, use the generic parameter info. 349 out[i] = genericParamTypes[fromidx]; 350 fromidx++; 351 } 352 } 353 return out; 354 } else { 355 // Otherwise, use the non-generic parameter data. 356 // Without method parameter reflection data, we have 357 // no way to figure out which parameters are 358 // synthetic/mandated, thus, no way to match up the 359 // indexes. 360 return genericParamTypes.length == nonGenericParamTypes.length ? 361 genericParamTypes : getParameterTypes(); 362 } 363 } 364 } 365 366 /** 367 * {@return an array of {@code Parameter} objects representing 368 * all the parameters to the underlying executable represented by 369 * this object} An array of length 0 is returned if the executable 370 * has no parameters. 371 * 372 * <p>The parameters of the underlying executable do not necessarily 373 * have unique names, or names that are legal identifiers in the 374 * Java programming language (JLS {@jls 3.8}). 375 * 376 * @throws MalformedParametersException if the class file contains 377 * a MethodParameters attribute that is improperly formatted. 378 */ 379 public Parameter[] getParameters() { 380 // TODO: This may eventually need to be guarded by security 381 // mechanisms similar to those in Field, Method, etc. 382 // 383 // Need to copy the cached array to prevent users from messing 384 // with it. Since parameters are immutable, we can 385 // shallow-copy. 386 return parameterData().parameters.clone(); 387 } 388 389 private Parameter[] synthesizeAllParams() { 390 final int realparams = getParameterCount(); 391 final Parameter[] out = new Parameter[realparams]; 392 for (int i = 0; i < realparams; i++) 393 // TODO: is there a way to synthetically derive the 394 // modifiers? Probably not in the general case, since 395 // we'd have no way of knowing about them, but there 396 // may be specific cases. 397 out[i] = new Parameter("arg" + i, 0, this, i); 398 return out; 399 } 400 401 private void verifyParameters(final Parameter[] parameters) { 402 final int mask = Modifier.FINAL | Modifier.SYNTHETIC | Modifier.MANDATED; 403 404 if (getParameterCount() != parameters.length) 405 throw new MalformedParametersException("Wrong number of parameters in MethodParameters attribute"); 406 407 for (Parameter parameter : parameters) { 408 final String name = parameter.getRealName(); 409 final int mods = parameter.getModifiers(); 410 411 if (name != null) { 412 if (name.isEmpty() || name.indexOf('.') != -1 || 413 name.indexOf(';') != -1 || name.indexOf('[') != -1 || 414 name.indexOf('/') != -1) { 415 throw new MalformedParametersException("Invalid parameter name \"" + name + "\""); 416 } 417 } 418 419 if (mods != (mods & mask)) { 420 throw new MalformedParametersException("Invalid parameter modifiers"); 421 } 422 } 423 } 424 425 426 boolean hasRealParameterData() { 427 return parameterData().isReal; 428 } 429 430 private ParameterData parameterData() { 431 ParameterData parameterData = this.parameterData; 432 if (parameterData != null) { 433 return parameterData; 434 } 435 436 Parameter[] tmp; 437 // Go to the JVM to get them 438 try { 439 tmp = getParameters0(); 440 } catch (IllegalArgumentException e) { 441 // Rethrow ClassFormatErrors 442 throw new MalformedParametersException("Invalid constant pool index"); 443 } 444 445 // If we get back nothing, then synthesize parameters 446 if (tmp == null) { 447 tmp = synthesizeAllParams(); 448 parameterData = new ParameterData(tmp, false); 449 } else { 450 verifyParameters(tmp); 451 parameterData = new ParameterData(tmp, true); 452 } 453 return this.parameterData = parameterData; 454 } 455 456 private transient @Stable ParameterData parameterData; 457 458 record ParameterData(@Stable Parameter[] parameters, boolean isReal) {} 459 460 private native Parameter[] getParameters0(); 461 native byte[] getTypeAnnotationBytes0(); 462 463 // Needed by reflectaccess 464 byte[] getTypeAnnotationBytes() { 465 return getTypeAnnotationBytes0(); 466 } 467 468 /** 469 * Returns an array of {@code Class} objects that represent the 470 * types of exceptions declared to be thrown by the underlying 471 * executable represented by this object. Returns an array of 472 * length 0 if the executable declares no exceptions in its {@code 473 * throws} clause. 474 * 475 * @return the exception types declared as being thrown by the 476 * executable this object represents 477 */ 478 public abstract Class<?>[] getExceptionTypes(); 479 480 /** 481 * Returns an array of {@code Type} objects that represent the 482 * exceptions declared to be thrown by this executable object. 483 * Returns an array of length 0 if the underlying executable declares 484 * no exceptions in its {@code throws} clause. 485 * 486 * <p>If an exception type is a type variable or a parameterized 487 * type, it is created. Otherwise, it is resolved. 488 * 489 * @return an array of Types that represent the exception types 490 * thrown by the underlying executable 491 * @throws GenericSignatureFormatError 492 * if the generic method signature does not conform to the format 493 * specified in 494 * <cite>The Java Virtual Machine Specification</cite> 495 * @throws TypeNotPresentException if the underlying executable's 496 * {@code throws} clause refers to a non-existent type declaration 497 * @throws MalformedParameterizedTypeException if 498 * the underlying executable's {@code throws} clause refers to a 499 * parameterized type that cannot be instantiated for any reason 500 */ 501 public Type[] getGenericExceptionTypes() { 502 Type[] result; 503 if (hasGenericInformation() && 504 ((result = getGenericInfo().getExceptionTypes()).length > 0)) 505 return result; 506 else 507 return getExceptionTypes(); 508 } 509 510 /** 511 * {@return a string describing this {@code Executable}, including 512 * any type parameters} 513 */ 514 public abstract String toGenericString(); 515 516 /** 517 * {@return {@code true} if this executable was declared to take a 518 * variable number of arguments; returns {@code false} otherwise} 519 */ 520 public boolean isVarArgs() { 521 return (getModifiers() & Modifier.VARARGS) != 0; 522 } 523 524 /** 525 * Returns {@code true} if this executable is a synthetic 526 * construct; returns {@code false} otherwise. 527 * 528 * @return true if and only if this executable is a synthetic 529 * construct as defined by 530 * <cite>The Java Language Specification</cite>. 531 * @jls 13.1 The Form of a Binary 532 * @jvms 4.6 Methods 533 */ 534 public boolean isSynthetic() { 535 return Modifier.isSynthetic(getModifiers()); 536 } 537 538 /** 539 * Returns an array of arrays of {@code Annotation}s that 540 * represent the annotations on the formal parameters, in 541 * declaration order, of the {@code Executable} represented by 542 * this object. Synthetic and mandated parameters (see 543 * explanation below), such as the outer "this" parameter to an 544 * inner class constructor will be represented in the returned 545 * array. If the executable has no parameters (meaning no formal, 546 * no synthetic, and no mandated parameters), a zero-length array 547 * will be returned. If the {@code Executable} has one or more 548 * parameters, a nested array of length zero is returned for each 549 * parameter with no annotations. The annotation objects contained 550 * in the returned arrays are serializable. The caller of this 551 * method is free to modify the returned arrays; it will have no 552 * effect on the arrays returned to other callers. 553 * 554 * A compiler may add extra parameters that are implicitly 555 * declared in source ("mandated"), as well as parameters that 556 * are neither implicitly nor explicitly declared in source 557 * ("synthetic") to the parameter list for a method. See {@link 558 * java.lang.reflect.Parameter} for more information. 559 * 560 * <p>Note that any annotations returned by this method are 561 * declaration annotations. 562 * 563 * @see java.lang.reflect.Parameter 564 * @see java.lang.reflect.Parameter#getAnnotations 565 * @return an array of arrays that represent the annotations on 566 * the formal and implicit parameters, in declaration order, of 567 * the executable represented by this object 568 */ 569 public abstract Annotation[][] getParameterAnnotations(); 570 571 Annotation[][] sharedGetParameterAnnotations(Class<?>[] parameterTypes, 572 byte[] parameterAnnotations) { 573 int numParameters = parameterTypes.length; 574 if (parameterAnnotations == null) 575 return new Annotation[numParameters][0]; 576 577 Annotation[][] result = parseParameterAnnotations(parameterAnnotations); 578 579 if (result.length != numParameters && 580 handleParameterNumberMismatch(result.length, parameterTypes)) { 581 Annotation[][] tmp = new Annotation[numParameters][]; 582 // Shift annotations down to account for any implicit leading parameters 583 System.arraycopy(result, 0, tmp, numParameters - result.length, result.length); 584 for (int i = 0; i < numParameters - result.length; i++) { 585 tmp[i] = new Annotation[0]; 586 } 587 result = tmp; 588 } 589 return result; 590 } 591 592 abstract boolean handleParameterNumberMismatch(int resultLength, Class<?>[] parameterTypes); 593 594 /** 595 * {@inheritDoc} 596 * @throws NullPointerException {@inheritDoc} 597 */ 598 @Override 599 public <T extends Annotation> T getAnnotation(Class<T> annotationClass) { 600 Objects.requireNonNull(annotationClass); 601 return annotationClass.cast(declaredAnnotations().get(annotationClass)); 602 } 603 604 /** 605 * {@inheritDoc} 606 * 607 * @throws NullPointerException {@inheritDoc} 608 */ 609 @Override 610 public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) { 611 Objects.requireNonNull(annotationClass); 612 613 return AnnotationSupport.getDirectlyAndIndirectlyPresent(declaredAnnotations(), annotationClass); 614 } 615 616 /** 617 * {@inheritDoc} 618 */ 619 @Override 620 public Annotation[] getDeclaredAnnotations() { 621 return AnnotationParser.toArray(declaredAnnotations()); 622 } 623 624 private transient volatile Map<Class<? extends Annotation>, Annotation> declaredAnnotations; 625 626 private Map<Class<? extends Annotation>, Annotation> declaredAnnotations() { 627 Map<Class<? extends Annotation>, Annotation> declAnnos; 628 if ((declAnnos = declaredAnnotations) == null) { 629 synchronized (this) { 630 if ((declAnnos = declaredAnnotations) == null) { 631 Executable root = (Executable)getRoot(); 632 if (root != null) { 633 declAnnos = root.declaredAnnotations(); 634 } else { 635 declAnnos = AnnotationParser.parseAnnotations( 636 getAnnotationBytes(), 637 SharedSecrets.getJavaLangAccess(). 638 getConstantPool(getDeclaringClass()), 639 getDeclaringClass() 640 ); 641 } 642 declaredAnnotations = declAnnos; 643 } 644 } 645 } 646 return declAnnos; 647 } 648 649 /** 650 * Returns an {@code AnnotatedType} object that represents the use of a type to 651 * specify the return type of the method/constructor represented by this 652 * Executable. 653 * 654 * If this {@code Executable} object represents a constructor, the {@code 655 * AnnotatedType} object represents the type of the constructed object. 656 * 657 * If this {@code Executable} object represents a method, the {@code 658 * AnnotatedType} object represents the use of a type to specify the return 659 * type of the method. 660 * 661 * @return an object representing the return type of the method 662 * or constructor represented by this {@code Executable} 663 */ 664 public abstract AnnotatedType getAnnotatedReturnType(); 665 666 /* Helper for subclasses of Executable. 667 * 668 * Returns an AnnotatedType object that represents the use of a type to 669 * specify the return type of the method/constructor represented by this 670 * Executable. 671 */ 672 AnnotatedType getAnnotatedReturnType0(Type returnType) { 673 return TypeAnnotationParser.buildAnnotatedType(getTypeAnnotationBytes0(), 674 SharedSecrets.getJavaLangAccess(). 675 getConstantPool(getDeclaringClass()), 676 this, 677 getDeclaringClass(), 678 returnType, 679 TypeAnnotation.TypeAnnotationTarget.METHOD_RETURN); 680 } 681 682 /** 683 * Returns an {@code AnnotatedType} object that represents the use of a 684 * type to specify the receiver type of the method/constructor represented 685 * by this {@code Executable} object. 686 * 687 * The receiver type of a method/constructor is available only if the 688 * method/constructor has a receiver parameter (JLS {@jls 8.4.1}). If this {@code 689 * Executable} object <em>represents an instance method or represents a 690 * constructor of an inner member class</em>, and the 691 * method/constructor <em>either</em> has no receiver parameter or has a 692 * receiver parameter with no annotations on its type, then the return 693 * value is an {@code AnnotatedType} object representing an element with no 694 * annotations. 695 * 696 * If this {@code Executable} object represents a static method or 697 * represents a constructor of a top level, static member, local, or 698 * anonymous class, then the return value is null. 699 * 700 * @return an object representing the receiver type of the method or 701 * constructor represented by this {@code Executable} or {@code null} if 702 * this {@code Executable} can not have a receiver parameter 703 * 704 * @jls 8.4 Method Declarations 705 * @jls 8.4.1 Formal Parameters 706 * @jls 8.8 Constructor Declarations 707 */ 708 public AnnotatedType getAnnotatedReceiverType() { 709 if (Modifier.isStatic(this.getModifiers())) 710 return null; 711 return TypeAnnotationParser.buildAnnotatedType(getTypeAnnotationBytes0(), 712 SharedSecrets.getJavaLangAccess(). 713 getConstantPool(getDeclaringClass()), 714 this, 715 getDeclaringClass(), 716 parameterize(getDeclaringClass()), 717 TypeAnnotation.TypeAnnotationTarget.METHOD_RECEIVER); 718 } 719 720 Type parameterize(Class<?> c) { 721 Class<?> ownerClass = c.getDeclaringClass(); 722 TypeVariable<?>[] typeVars = c.getTypeParameters(); 723 724 // base case, static nested classes, according to JLS 8.1.3, has no 725 // enclosing instance, therefore its owner is not generified. 726 if (ownerClass == null || Modifier.isStatic(c.getModifiers())) { 727 if (typeVars.length == 0) 728 return c; 729 else 730 return ParameterizedTypeImpl.make(c, typeVars, null); 731 } 732 733 // Resolve owner 734 Type ownerType = parameterize(ownerClass); 735 if (ownerType instanceof Class<?> && typeVars.length == 0) // We have yet to encounter type parameters 736 return c; 737 else 738 return ParameterizedTypeImpl.make(c, typeVars, ownerType); 739 } 740 741 /** 742 * Returns an array of {@code AnnotatedType} objects that represent the use 743 * of types to specify formal parameter types of the method/constructor 744 * represented by this Executable. The order of the objects in the array 745 * corresponds to the order of the formal parameter types in the 746 * declaration of the method/constructor. 747 * 748 * Returns an array of length 0 if the method/constructor declares no 749 * parameters. 750 * Note that the constructors of some inner classes 751 * may have an implicitly declared parameter in addition to 752 * explicitly declared ones. 753 * 754 * @return an array of objects representing the types of the 755 * formal parameters of the method or constructor represented by this 756 * {@code Executable} 757 */ 758 public AnnotatedType[] getAnnotatedParameterTypes() { 759 return TypeAnnotationParser.buildAnnotatedTypes(getTypeAnnotationBytes0(), 760 SharedSecrets.getJavaLangAccess(). 761 getConstantPool(getDeclaringClass()), 762 this, 763 getDeclaringClass(), 764 getAllGenericParameterTypes(), 765 TypeAnnotation.TypeAnnotationTarget.METHOD_FORMAL_PARAMETER); 766 } 767 768 /** 769 * Returns an array of {@code AnnotatedType} objects that represent the use 770 * of types to specify the declared exceptions of the method/constructor 771 * represented by this Executable. The order of the objects in the array 772 * corresponds to the order of the exception types in the declaration of 773 * the method/constructor. 774 * 775 * Returns an array of length 0 if the method/constructor declares no 776 * exceptions. 777 * 778 * @return an array of objects representing the declared 779 * exceptions of the method or constructor represented by this {@code 780 * Executable} 781 */ 782 public AnnotatedType[] getAnnotatedExceptionTypes() { 783 return TypeAnnotationParser.buildAnnotatedTypes(getTypeAnnotationBytes0(), 784 SharedSecrets.getJavaLangAccess(). 785 getConstantPool(getDeclaringClass()), 786 this, 787 getDeclaringClass(), 788 getGenericExceptionTypes(), 789 TypeAnnotation.TypeAnnotationTarget.THROWS); 790 } 791 }