1 /*
   2  * Copyright (c) 2012, 2019, 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             final Type[] out = new Type[nonGenericParamTypes.length];
 311             final Parameter[] params = getParameters();
 312             int fromidx = 0;
 313             // If we have real parameter data, then we use the
 314             // synthetic and mandate flags to our advantage.
 315             if (realParamData) {
 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             } else {
 329                 // Otherwise, use the non-generic parameter data.
 330                 // Without method parameter reflection data, we have
 331                 // no way to figure out which parameters are
 332                 // synthetic/mandated, thus, no way to match up the
 333                 // indexes.
 334                 return genericParamTypes.length == nonGenericParamTypes.length ?
 335                     genericParamTypes : nonGenericParamTypes;
 336             }
 337             return out;
 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 (getParameterTypes().length != 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      * @see java.lang.reflect.Parameter
 548      * @see java.lang.reflect.Parameter#getAnnotations
 549      * @return an array of arrays that represent the annotations on
 550      *    the formal and implicit parameters, in declaration order, of
 551      *    the executable represented by this object
 552      */
 553     public abstract Annotation[][] getParameterAnnotations();
 554 
 555     Annotation[][] sharedGetParameterAnnotations(Class<?>[] parameterTypes,
 556                                                  byte[] parameterAnnotations) {
 557         int numParameters = parameterTypes.length;
 558         if (parameterAnnotations == null)
 559             return new Annotation[numParameters][0];
 560 
 561         Annotation[][] result = parseParameterAnnotations(parameterAnnotations);
 562 
 563         if (result.length != numParameters &&
 564             handleParameterNumberMismatch(result.length, numParameters)) {
 565             Annotation[][] tmp = new Annotation[result.length+1][];
 566             // Shift annotations down one to account for an implicit leading parameter
 567             System.arraycopy(result, 0, tmp, 1, result.length);
 568             tmp[0] = new Annotation[0];
 569             result = tmp;
 570         }
 571         return result;
 572     }
 573 
 574     abstract boolean handleParameterNumberMismatch(int resultLength, int numParameters);
 575 
 576     /**
 577      * {@inheritDoc}
 578      * @throws NullPointerException  {@inheritDoc}
 579      */
 580     public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
 581         Objects.requireNonNull(annotationClass);
 582         return annotationClass.cast(declaredAnnotations().get(annotationClass));
 583     }
 584 
 585     /**
 586      * {@inheritDoc}
 587      * @throws NullPointerException {@inheritDoc}
 588      */
 589     @Override
 590     public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
 591         Objects.requireNonNull(annotationClass);
 592 
 593         return AnnotationSupport.getDirectlyAndIndirectlyPresent(declaredAnnotations(), annotationClass);
 594     }
 595 
 596     /**
 597      * {@inheritDoc}
 598      */
 599     public Annotation[] getDeclaredAnnotations()  {
 600         return AnnotationParser.toArray(declaredAnnotations());
 601     }
 602 
 603     private transient volatile Map<Class<? extends Annotation>, Annotation> declaredAnnotations;
 604 
 605     private Map<Class<? extends Annotation>, Annotation> declaredAnnotations() {
 606         Map<Class<? extends Annotation>, Annotation> declAnnos;
 607         if ((declAnnos = declaredAnnotations) == null) {
 608             synchronized (this) {
 609                 if ((declAnnos = declaredAnnotations) == null) {
 610                     Executable root = (Executable)getRoot();
 611                     if (root != null) {
 612                         declAnnos = root.declaredAnnotations();
 613                     } else {
 614                         declAnnos = AnnotationParser.parseAnnotations(
 615                                 getAnnotationBytes(),
 616                                 SharedSecrets.getJavaLangAccess().
 617                                         getConstantPool(getDeclaringClass()),
 618                                 getDeclaringClass()
 619                         );
 620                     }
 621                     declaredAnnotations = declAnnos;
 622                 }
 623             }
 624         }
 625         return declAnnos;
 626     }
 627 
 628     /**
 629      * Returns an {@code AnnotatedType} object that represents the use of a type to
 630      * specify the return type of the method/constructor represented by this
 631      * Executable.
 632      *
 633      * If this {@code Executable} object represents a constructor, the {@code
 634      * AnnotatedType} object represents the type of the constructed object.
 635      *
 636      * If this {@code Executable} object represents a method, the {@code
 637      * AnnotatedType} object represents the use of a type to specify the return
 638      * type of the method.
 639      *
 640      * @return an object representing the return type of the method
 641      * or constructor represented by this {@code Executable}
 642      */
 643     public abstract AnnotatedType getAnnotatedReturnType();
 644 
 645     /* Helper for subclasses of Executable.
 646      *
 647      * Returns an AnnotatedType object that represents the use of a type to
 648      * specify the return type of the method/constructor represented by this
 649      * Executable.
 650      */
 651     AnnotatedType getAnnotatedReturnType0(Type returnType) {
 652         return TypeAnnotationParser.buildAnnotatedType(getTypeAnnotationBytes0(),
 653                 SharedSecrets.getJavaLangAccess().
 654                         getConstantPool(getDeclaringClass()),
 655                 this,
 656                 getDeclaringClass(),
 657                 returnType,
 658                 TypeAnnotation.TypeAnnotationTarget.METHOD_RETURN);
 659     }
 660 
 661     /**
 662      * Returns an {@code AnnotatedType} object that represents the use of a
 663      * type to specify the receiver type of the method/constructor represented
 664      * by this {@code Executable} object.
 665      *
 666      * The receiver type of a method/constructor is available only if the
 667      * method/constructor has a receiver parameter (JLS 8.4.1). If this {@code
 668      * Executable} object <em>represents an instance method or represents a
 669      * constructor of an inner member class</em>, and the
 670      * method/constructor <em>either</em> has no receiver parameter or has a
 671      * receiver parameter with no annotations on its type, then the return
 672      * value is an {@code AnnotatedType} object representing an element with no
 673      * annotations.
 674      *
 675      * If this {@code Executable} object represents a static method or
 676      * represents a constructor of a top level, static member, local, or
 677      * anonymous class, then the return value is null.
 678      *
 679      * @return an object representing the receiver type of the method or
 680      * constructor represented by this {@code Executable} or {@code null} if
 681      * this {@code Executable} can not have a receiver parameter
 682      *
 683      * @jls 8.4 Method Declarations
 684      * @jls 8.4.1 Formal Parameters
 685      * @jls 8.8 Constructor Declarations
 686      */
 687     public AnnotatedType getAnnotatedReceiverType() {
 688         if (Modifier.isStatic(this.getModifiers()))
 689             return null;
 690         return TypeAnnotationParser.buildAnnotatedType(getTypeAnnotationBytes0(),
 691                 SharedSecrets.getJavaLangAccess().
 692                         getConstantPool(getDeclaringClass()),
 693                 this,
 694                 getDeclaringClass(),
 695                 getDeclaringClass(),
 696                 TypeAnnotation.TypeAnnotationTarget.METHOD_RECEIVER);
 697     }
 698 
 699     /**
 700      * Returns an array of {@code AnnotatedType} objects that represent the use
 701      * of types to specify formal parameter types of the method/constructor
 702      * represented by this Executable. The order of the objects in the array
 703      * corresponds to the order of the formal parameter types in the
 704      * declaration of the method/constructor.
 705      *
 706      * Returns an array of length 0 if the method/constructor declares no
 707      * parameters.
 708      * Note that the constructors of some inner classes
 709      * may have an implicitly declared parameter in addition to
 710      * explicitly declared ones.
 711      *
 712      * @return an array of objects representing the types of the
 713      * formal parameters of the method or constructor represented by this
 714      * {@code Executable}
 715      */
 716     public AnnotatedType[] getAnnotatedParameterTypes() {
 717         return TypeAnnotationParser.buildAnnotatedTypes(getTypeAnnotationBytes0(),
 718                 SharedSecrets.getJavaLangAccess().
 719                         getConstantPool(getDeclaringClass()),
 720                 this,
 721                 getDeclaringClass(),
 722                 getAllGenericParameterTypes(),
 723                 TypeAnnotation.TypeAnnotationTarget.METHOD_FORMAL_PARAMETER);
 724     }
 725 
 726     /**
 727      * Returns an array of {@code AnnotatedType} objects that represent the use
 728      * of types to specify the declared exceptions of the method/constructor
 729      * represented by this Executable. The order of the objects in the array
 730      * corresponds to the order of the exception types in the declaration of
 731      * the method/constructor.
 732      *
 733      * Returns an array of length 0 if the method/constructor declares no
 734      * exceptions.
 735      *
 736      * @return an array of objects representing the declared
 737      * exceptions of the method or constructor represented by this {@code
 738      * Executable}
 739      */
 740     public AnnotatedType[] getAnnotatedExceptionTypes() {
 741         return TypeAnnotationParser.buildAnnotatedTypes(getTypeAnnotationBytes0(),
 742                 SharedSecrets.getJavaLangAccess().
 743                         getConstantPool(getDeclaringClass()),
 744                 this,
 745                 getDeclaringClass(),
 746                 getGenericExceptionTypes(),
 747                 TypeAnnotation.TypeAnnotationTarget.THROWS);
 748     }
 749 
 750 }