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