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