1 /*
   2  * Copyright (c) 2008, 2017, 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.invoke;
  27 
  28 import sun.invoke.util.BytecodeDescriptor;
  29 import sun.invoke.util.VerifyAccess;
  30 
  31 import java.lang.reflect.Constructor;
  32 import java.lang.reflect.Field;
  33 import java.lang.reflect.Member;
  34 import java.lang.reflect.Method;
  35 import java.lang.reflect.Modifier;
  36 import java.util.ArrayList;
  37 import java.util.Collections;
  38 import java.util.Iterator;
  39 import java.util.List;
  40 import java.util.Objects;
  41 
  42 import static java.lang.invoke.MethodHandleNatives.Constants.*;
  43 import static java.lang.invoke.MethodHandleStatics.newIllegalArgumentException;
  44 import static java.lang.invoke.MethodHandleStatics.newInternalError;
  45 
  46 /**
  47  * A {@code MemberName} is a compact symbolic datum which fully characterizes
  48  * a method or field reference.
  49  * A member name refers to a field, method, constructor, or member type.
  50  * Every member name has a simple name (a string) and a type (either a Class or MethodType).
  51  * A member name may also have a non-null declaring class, or it may be simply
  52  * a naked name/type pair.
  53  * A member name may also have non-zero modifier flags.
  54  * Finally, a member name may be either resolved or unresolved.
  55  * If it is resolved, the existence of the named member has been determined by the JVM.
  56  * <p>
  57  * Whether resolved or not, a member name provides no access rights or
  58  * invocation capability to its possessor.  It is merely a compact
  59  * representation of all symbolic information necessary to link to
  60  * and properly use the named member.
  61  * <p>
  62  * When resolved, a member name's internal implementation may include references to JVM metadata.
  63  * This representation is stateless and only descriptive.
  64  * It provides no private information and no capability to use the member.
  65  * <p>
  66  * By contrast, a {@linkplain java.lang.reflect.Method} contains fuller information
  67  * about the internals of a method (except its bytecodes) and also
  68  * allows invocation.  A MemberName is much lighter than a Method,
  69  * since it contains about 7 fields to the 16 of Method (plus its sub-arrays),
  70  * and those seven fields omit much of the information in Method.
  71  * @author jrose
  72  */
  73 /*non-public*/ final class ResolvedMethodName {
  74     //@Injected JVM_Method* vmtarget;
  75     //@Injected Class<?>    vmholder;
  76 };
  77 
  78 /*non-public*/ final class MemberName implements Member, Cloneable {
  79     private Class<?> clazz;       // class in which the member is defined
  80     private String   name;        // may be null if not yet materialized
  81     private Object   type;        // may be null if not yet materialized
  82     private int      flags;       // modifier bits; see reflect.Modifier
  83     private ResolvedMethodName method;    // cached resolved method information
  84     //@Injected intptr_t       vmindex;   // vtable index or offset of resolved member
  85     Object   resolution;  // if null, this guy is resolved
  86 
  87     /** Return the declaring class of this member.
  88      *  In the case of a bare name and type, the declaring class will be null.
  89      */
  90     public Class<?> getDeclaringClass() {
  91         return clazz;
  92     }
  93 
  94     /** Utility method producing the class loader of the declaring class. */
  95     public ClassLoader getClassLoader() {
  96         return clazz.getClassLoader();
  97     }
  98 
  99     /** Return the simple name of this member.
 100      *  For a type, it is the same as {@link Class#getSimpleName}.
 101      *  For a method or field, it is the simple name of the member.
 102      *  For a constructor, it is always {@code "<init>"}.
 103      */
 104     public String getName() {
 105         if (name == null) {
 106             expandFromVM();
 107             if (name == null) {
 108                 return null;
 109             }
 110         }
 111         return name;
 112     }
 113 
 114     public MethodType getMethodOrFieldType() {
 115         if (isInvocable())
 116             return getMethodType();
 117         if (isGetter())
 118             return MethodType.methodType(getFieldType());
 119         if (isSetter())
 120             return MethodType.methodType(void.class, getFieldType());
 121         throw new InternalError("not a method or field: "+this);
 122     }
 123 
 124     /** Return the declared type of this member, which
 125      *  must be a method or constructor.
 126      */
 127     public MethodType getMethodType() {
 128         if (type == null) {
 129             expandFromVM();
 130             if (type == null) {
 131                 return null;
 132             }
 133         }
 134         if (!isInvocable()) {
 135             throw newIllegalArgumentException("not invocable, no method type");
 136         }
 137 
 138         {
 139             // Get a snapshot of type which doesn't get changed by racing threads.
 140             final Object type = this.type;
 141             if (type instanceof MethodType) {
 142                 return (MethodType) type;
 143             }
 144         }
 145 
 146         // type is not a MethodType yet.  Convert it thread-safely.
 147         synchronized (this) {
 148             if (type instanceof String) {
 149                 String sig = (String) type;
 150                 MethodType res = MethodType.fromDescriptor(sig, getClassLoader());
 151                 type = res;
 152             } else if (type instanceof Object[]) {
 153                 Object[] typeInfo = (Object[]) type;
 154                 Class<?>[] ptypes = (Class<?>[]) typeInfo[1];
 155                 Class<?> rtype = (Class<?>) typeInfo[0];
 156                 MethodType res = MethodType.makeImpl(rtype, ptypes, true);
 157                 type = res;
 158             }
 159             // Make sure type is a MethodType for racing threads.
 160             assert type instanceof MethodType : "bad method type " + type;
 161         }
 162         return (MethodType) type;
 163     }
 164 
 165     /** Return the descriptor of this member, which
 166      *  must be a method or constructor.
 167      */
 168     String getMethodDescriptor() {
 169         if (type == null) {
 170             expandFromVM();
 171             if (type == null) {
 172                 return null;
 173             }
 174         }
 175         if (!isInvocable()) {
 176             throw newIllegalArgumentException("not invocable, no method type");
 177         }
 178 
 179         // Get a snapshot of type which doesn't get changed by racing threads.
 180         final Object type = this.type;
 181         if (type instanceof String) {
 182             return (String) type;
 183         } else {
 184             return getMethodType().toMethodDescriptorString();
 185         }
 186     }
 187 
 188     /** Return the actual type under which this method or constructor must be invoked.
 189      *  For non-static methods or constructors, this is the type with a leading parameter,
 190      *  a reference to declaring class.  For static methods, it is the same as the declared type.
 191      */
 192     public MethodType getInvocationType() {
 193         MethodType itype = getMethodOrFieldType();
 194         Class<?> c = clazz.isValue() ? clazz.asValueType() : clazz;
 195         if (isConstructor() && getReferenceKind() == REF_newInvokeSpecial)
 196             return itype.changeReturnType(c);
 197         if (!isStatic())
 198             return itype.insertParameterTypes(0, c);
 199         return itype;
 200     }
 201 
 202     /** Utility method producing the parameter types of the method type. */
 203     public Class<?>[] getParameterTypes() {
 204         return getMethodType().parameterArray();
 205     }
 206 
 207     /** Utility method producing the return type of the method type. */
 208     public Class<?> getReturnType() {
 209         return getMethodType().returnType();
 210     }
 211 
 212     /** Return the declared type of this member, which
 213      *  must be a field or type.
 214      *  If it is a type member, that type itself is returned.
 215      */
 216     public Class<?> getFieldType() {
 217         if (type == null) {
 218             expandFromVM();
 219             if (type == null) {
 220                 return null;
 221             }
 222         }
 223         if (isInvocable()) {
 224             throw newIllegalArgumentException("not a field or nested class, no simple type");
 225         }
 226 
 227         {
 228             // Get a snapshot of type which doesn't get changed by racing threads.
 229             final Object type = this.type;
 230             if (type instanceof Class<?>) {
 231                 return (Class<?>) type;
 232             }
 233         }
 234 
 235         // type is not a Class yet.  Convert it thread-safely.
 236         synchronized (this) {
 237             if (type instanceof String) {
 238                 String sig = (String) type;
 239                 MethodType mtype = MethodType.fromDescriptor("()"+sig, getClassLoader());
 240                 Class<?> res = mtype.returnType();
 241                 type = res;
 242             }
 243             // Make sure type is a Class for racing threads.
 244             assert type instanceof Class<?> : "bad field type " + type;
 245         }
 246         return (Class<?>) type;
 247     }
 248 
 249     /** Utility method to produce either the method type or field type of this member. */
 250     public Object getType() {
 251         return (isInvocable() ? getMethodType() : getFieldType());
 252     }
 253 
 254     /** Utility method to produce the signature of this member,
 255      *  used within the class file format to describe its type.
 256      */
 257     public String getSignature() {
 258         if (type == null) {
 259             expandFromVM();
 260             if (type == null) {
 261                 return null;
 262             }
 263         }
 264         if (isInvocable())
 265             return BytecodeDescriptor.unparse(getMethodType());
 266         else
 267             return BytecodeDescriptor.unparse(getFieldType());
 268     }
 269 
 270     /** Return the modifier flags of this member.
 271      *  @see java.lang.reflect.Modifier
 272      */
 273     public int getModifiers() {
 274         return (flags & RECOGNIZED_MODIFIERS);
 275     }
 276 
 277     /** Return the reference kind of this member, or zero if none.
 278      */
 279     public byte getReferenceKind() {
 280         return (byte) ((flags >>> MN_REFERENCE_KIND_SHIFT) & MN_REFERENCE_KIND_MASK);
 281     }
 282     private boolean referenceKindIsConsistent() {
 283         byte refKind = getReferenceKind();
 284         if (refKind == REF_NONE)  return isType();
 285         if (isField()) {
 286             assert(staticIsConsistent());
 287             assert(MethodHandleNatives.refKindIsField(refKind));
 288         } else if (isConstructor()) {
 289             assert(refKind == REF_newInvokeSpecial || refKind == REF_invokeSpecial);
 290         } else if (isMethod()) {
 291             assert(staticIsConsistent());
 292             assert(MethodHandleNatives.refKindIsMethod(refKind));
 293             if (clazz.isInterface())
 294                 assert(refKind == REF_invokeInterface ||
 295                        refKind == REF_invokeStatic    ||
 296                        refKind == REF_invokeSpecial   ||
 297                        refKind == REF_invokeVirtual && isObjectPublicMethod());
 298         } else {
 299             assert(false);
 300         }
 301         return true;
 302     }
 303     private boolean isObjectPublicMethod() {
 304         if (clazz == Object.class)  return true;
 305         MethodType mtype = getMethodType();
 306         if (name.equals("toString") && mtype.returnType() == String.class && mtype.parameterCount() == 0)
 307             return true;
 308         if (name.equals("hashCode") && mtype.returnType() == int.class && mtype.parameterCount() == 0)
 309             return true;
 310         if (name.equals("equals") && mtype.returnType() == boolean.class && mtype.parameterCount() == 1 && mtype.parameterType(0) == Object.class)
 311             return true;
 312         return false;
 313     }
 314     /*non-public*/ boolean referenceKindIsConsistentWith(int originalRefKind) {
 315         int refKind = getReferenceKind();
 316         if (refKind == originalRefKind)  return true;
 317         switch (originalRefKind) {
 318         case REF_invokeInterface:
 319             // Looking up an interface method, can get (e.g.) Object.hashCode
 320             assert(refKind == REF_invokeVirtual ||
 321                    refKind == REF_invokeSpecial) : this;
 322             return true;
 323         case REF_invokeVirtual:
 324         case REF_newInvokeSpecial:
 325             // Looked up a virtual, can get (e.g.) final String.hashCode.
 326             assert(refKind == REF_invokeSpecial) : this;
 327             return true;
 328         }
 329         assert(false) : this+" != "+MethodHandleNatives.refKindName((byte)originalRefKind);
 330         return true;
 331     }
 332     private boolean staticIsConsistent() {
 333         byte refKind = getReferenceKind();
 334         return MethodHandleNatives.refKindIsStatic(refKind) == isStatic() || getModifiers() == 0;
 335     }
 336     private boolean vminfoIsConsistent() {
 337         byte refKind = getReferenceKind();
 338         assert(isResolved());  // else don't call
 339         Object vminfo = MethodHandleNatives.getMemberVMInfo(this);
 340         assert(vminfo instanceof Object[]);
 341         long vmindex = (Long) ((Object[])vminfo)[0];
 342         Object vmtarget = ((Object[])vminfo)[1];
 343         if (MethodHandleNatives.refKindIsField(refKind)) {
 344             assert(vmindex >= 0) : vmindex + ":" + this;
 345             assert(vmtarget instanceof Class);
 346         } else {
 347             if (MethodHandleNatives.refKindDoesDispatch(refKind))
 348                 assert(vmindex >= 0) : vmindex + ":" + this;
 349             else
 350                 assert(vmindex < 0) : vmindex;
 351             assert(vmtarget instanceof MemberName) : vmtarget + " in " + this;
 352         }
 353         return true;
 354     }
 355 
 356     private MemberName changeReferenceKind(byte refKind, byte oldKind) {
 357         assert(getReferenceKind() == oldKind);
 358         assert(MethodHandleNatives.refKindIsValid(refKind));
 359         flags += (((int)refKind - oldKind) << MN_REFERENCE_KIND_SHIFT);
 360         return this;
 361     }
 362 
 363     private boolean testFlags(int mask, int value) {
 364         return (flags & mask) == value;
 365     }
 366     private boolean testAllFlags(int mask) {
 367         return testFlags(mask, mask);
 368     }
 369     private boolean testAnyFlags(int mask) {
 370         return !testFlags(mask, 0);
 371     }
 372 
 373     /** Utility method to query if this member is a method handle invocation (invoke or invokeExact).
 374      */
 375     public boolean isMethodHandleInvoke() {
 376         final int bits = MH_INVOKE_MODS &~ Modifier.PUBLIC;
 377         final int negs = Modifier.STATIC;
 378         if (testFlags(bits | negs, bits) &&
 379             clazz == MethodHandle.class) {
 380             return isMethodHandleInvokeName(name);
 381         }
 382         return false;
 383     }
 384     public static boolean isMethodHandleInvokeName(String name) {
 385         switch (name) {
 386         case "invoke":
 387         case "invokeExact":
 388             return true;
 389         default:
 390             return false;
 391         }
 392     }
 393     public boolean isVarHandleMethodInvoke() {
 394         final int bits = MH_INVOKE_MODS &~ Modifier.PUBLIC;
 395         final int negs = Modifier.STATIC;
 396         if (testFlags(bits | negs, bits) &&
 397             clazz == VarHandle.class) {
 398             return isVarHandleMethodInvokeName(name);
 399         }
 400         return false;
 401     }
 402     public static boolean isVarHandleMethodInvokeName(String name) {
 403         try {
 404             VarHandle.AccessMode.valueFromMethodName(name);
 405             return true;
 406         } catch (IllegalArgumentException e) {
 407             return false;
 408         }
 409     }
 410     private static final int MH_INVOKE_MODS = Modifier.NATIVE | Modifier.FINAL | Modifier.PUBLIC;
 411 
 412     /** Utility method to query the modifier flags of this member. */
 413     public boolean isStatic() {
 414         return Modifier.isStatic(flags);
 415     }
 416     /** Utility method to query the modifier flags of this member. */
 417     public boolean isPublic() {
 418         return Modifier.isPublic(flags);
 419     }
 420     /** Utility method to query the modifier flags of this member. */
 421     public boolean isPrivate() {
 422         return Modifier.isPrivate(flags);
 423     }
 424     /** Utility method to query the modifier flags of this member. */
 425     public boolean isProtected() {
 426         return Modifier.isProtected(flags);
 427     }
 428     /** Utility method to query the modifier flags of this member. */
 429     public boolean isFinal() {
 430         // all fields declared in a value type are effectively final
 431         assert(!clazz.isValue() || !isField() || Modifier.isFinal(flags));
 432         return Modifier.isFinal(flags);
 433     }
 434     /** Utility method to query whether this member or its defining class is final. */
 435     public boolean canBeStaticallyBound() {
 436         return Modifier.isFinal(flags | clazz.getModifiers());
 437     }
 438     /** Utility method to query the modifier flags of this member. */
 439     public boolean isVolatile() {
 440         return Modifier.isVolatile(flags);
 441     }
 442     /** Utility method to query the modifier flags of this member. */
 443     public boolean isAbstract() {
 444         return Modifier.isAbstract(flags);
 445     }
 446     /** Utility method to query the modifier flags of this member. */
 447     public boolean isNative() {
 448         return Modifier.isNative(flags);
 449     }
 450     // let the rest (native, volatile, transient, etc.) be tested via Modifier.isFoo
 451 
 452     // unofficial modifier flags, used by HotSpot:
 453     static final int BRIDGE      = 0x00000040;
 454     static final int VARARGS     = 0x00000080;
 455     static final int SYNTHETIC   = 0x00001000;
 456     static final int ANNOTATION  = 0x00002000;
 457     static final int ENUM        = 0x00004000;
 458     static final int FLATTENABLE = 0x00000100;
 459     static final int FLATTENED   = 0x00008000;
 460 
 461     /** Utility method to query the modifier flags of this member; returns false if the member is not a method. */
 462     public boolean isBridge() {
 463         return testAllFlags(IS_METHOD | BRIDGE);
 464     }
 465     /** Utility method to query the modifier flags of this member; returns false if the member is not a method. */
 466     public boolean isVarargs() {
 467         return testAllFlags(VARARGS) && isInvocable();
 468     }
 469     /** Utility method to query the modifier flags of this member; returns false if the member is not a method. */
 470     public boolean isSynthetic() {
 471         return testAllFlags(SYNTHETIC);
 472     }
 473 
 474     /*
 475      * Query whether this member is a flattenable field.
 476      *
 477      * A flattenable field whose type must be of value class with ACC_FLATTENABLE flag set.
 478      * A field of value type may or may not be flattenable.
 479      */
 480     public boolean isFlattenable() { return (flags & FLATTENABLE) == FLATTENABLE; }
 481 
 482     /** Query whether this member is a flat value field */
 483     public boolean isFlatValue() { return (flags & FLATTENED) == FLATTENED; }
 484 
 485     /** Query whether this member can be assigned to null. */
 486     public boolean canBeNull()  { return !isFlattenable(); }
 487 
 488     static final String CONSTRUCTOR_NAME = "<init>";  // the ever-popular
 489 
 490     // modifiers exported by the JVM:
 491     static final int RECOGNIZED_MODIFIERS = 0xFFFF;
 492 
 493     // private flags, not part of RECOGNIZED_MODIFIERS:
 494     static final int
 495             IS_METHOD        = MN_IS_METHOD,        // method (not constructor)
 496             IS_CONSTRUCTOR   = MN_IS_CONSTRUCTOR,   // constructor
 497             IS_FIELD         = MN_IS_FIELD,         // field
 498             IS_TYPE          = MN_IS_TYPE,          // nested type
 499             CALLER_SENSITIVE = MN_CALLER_SENSITIVE; // @CallerSensitive annotation detected
 500 
 501     static final int ALL_ACCESS = Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED;
 502     static final int ALL_KINDS = IS_METHOD | IS_CONSTRUCTOR | IS_FIELD | IS_TYPE;
 503     static final int IS_INVOCABLE = IS_METHOD | IS_CONSTRUCTOR;
 504     static final int IS_FIELD_OR_METHOD = IS_METHOD | IS_FIELD;
 505     static final int SEARCH_ALL_SUPERS = MN_SEARCH_SUPERCLASSES | MN_SEARCH_INTERFACES;
 506 
 507     /** Utility method to query whether this member is a method or constructor. */
 508     public boolean isInvocable() {
 509         return testAnyFlags(IS_INVOCABLE);
 510     }
 511     /** Utility method to query whether this member is a method, constructor, or field. */
 512     public boolean isFieldOrMethod() {
 513         return testAnyFlags(IS_FIELD_OR_METHOD);
 514     }
 515     /** Query whether this member is a method. */
 516     public boolean isMethod() {
 517         return testAllFlags(IS_METHOD);
 518     }
 519     /** Query whether this member is a constructor. */
 520     public boolean isConstructor() {
 521         return testAllFlags(IS_CONSTRUCTOR);
 522     }
 523     /** Query whether this member is a field. */
 524     public boolean isField() {
 525         return testAllFlags(IS_FIELD);
 526     }
 527     /** Query whether this member is a type. */
 528     public boolean isType() {
 529         return testAllFlags(IS_TYPE);
 530     }
 531     /** Utility method to query whether this member is neither public, private, nor protected. */
 532     public boolean isPackage() {
 533         return !testAnyFlags(ALL_ACCESS);
 534     }
 535     /** Query whether this member has a CallerSensitive annotation. */
 536     public boolean isCallerSensitive() {
 537         return testAllFlags(CALLER_SENSITIVE);
 538     }
 539 
 540     /** Utility method to query whether this member is accessible from a given lookup class. */
 541     public boolean isAccessibleFrom(Class<?> lookupClass) {
 542         int mode = (ALL_ACCESS|MethodHandles.Lookup.PACKAGE|MethodHandles.Lookup.MODULE);
 543         return VerifyAccess.isMemberAccessible(this.getDeclaringClass(), this.getDeclaringClass(), flags,
 544                                                lookupClass, mode);
 545     }
 546 
 547     /**
 548      * Check if MemberName is a call to a method named {@code name} in class {@code declaredClass}.
 549      */
 550     public boolean refersTo(Class<?> declc, String n) {
 551         return clazz == declc && getName().equals(n);
 552     }
 553 
 554     /** Initialize a query.   It is not resolved. */
 555     private void init(Class<?> defClass, String name, Object type, int flags) {
 556         // defining class is allowed to be null (for a naked name/type pair)
 557         //name.toString();  // null check
 558         //type.equals(type);  // null check
 559         // fill in fields:
 560         this.clazz = defClass;
 561         this.name = name;
 562         this.type = type;
 563         this.flags = flags;
 564         assert(testAnyFlags(ALL_KINDS));
 565         assert(this.resolution == null);  // nobody should have touched this yet
 566         //assert(referenceKindIsConsistent());  // do this after resolution
 567     }
 568 
 569     /**
 570      * Calls down to the VM to fill in the fields.  This method is
 571      * synchronized to avoid racing calls.
 572      */
 573     private void expandFromVM() {
 574         if (type != null) {
 575             return;
 576         }
 577         if (!isResolved()) {
 578             return;
 579         }
 580         MethodHandleNatives.expand(this);
 581     }
 582 
 583     // Capturing information from the Core Reflection API:
 584     private static int flagsMods(int flags, int mods, byte refKind) {
 585         assert((flags & RECOGNIZED_MODIFIERS) == 0);
 586         assert((mods & ~RECOGNIZED_MODIFIERS) == 0);
 587         assert((refKind & ~MN_REFERENCE_KIND_MASK) == 0);
 588         return flags | mods | (refKind << MN_REFERENCE_KIND_SHIFT);
 589     }
 590     /** Create a name for the given reflected method.  The resulting name will be in a resolved state. */
 591     public MemberName(Method m) {
 592         this(m, false);
 593     }
 594     @SuppressWarnings("LeakingThisInConstructor")
 595     public MemberName(Method m, boolean wantSpecial) {
 596         Objects.requireNonNull(m);
 597         // fill in vmtarget, vmindex while we have m in hand:
 598         MethodHandleNatives.init(this, m);
 599         if (clazz == null) {  // MHN.init failed
 600             if (m.getDeclaringClass() == MethodHandle.class &&
 601                 isMethodHandleInvokeName(m.getName())) {
 602                 // The JVM did not reify this signature-polymorphic instance.
 603                 // Need a special case here.
 604                 // See comments on MethodHandleNatives.linkMethod.
 605                 MethodType type = MethodType.methodType(m.getReturnType(), m.getParameterTypes());
 606                 int flags = flagsMods(IS_METHOD, m.getModifiers(), REF_invokeVirtual);
 607                 init(MethodHandle.class, m.getName(), type, flags);
 608                 if (isMethodHandleInvoke())
 609                     return;
 610             }
 611             if (m.getDeclaringClass() == VarHandle.class &&
 612                 isVarHandleMethodInvokeName(m.getName())) {
 613                 // The JVM did not reify this signature-polymorphic instance.
 614                 // Need a special case here.
 615                 // See comments on MethodHandleNatives.linkMethod.
 616                 MethodType type = MethodType.methodType(m.getReturnType(), m.getParameterTypes());
 617                 int flags = flagsMods(IS_METHOD, m.getModifiers(), REF_invokeVirtual);
 618                 init(VarHandle.class, m.getName(), type, flags);
 619                 if (isVarHandleMethodInvoke())
 620                     return;
 621             }
 622             throw new LinkageError(m.toString());
 623         }
 624         assert(isResolved() && this.clazz != null);
 625         this.name = m.getName();
 626         if (this.type == null)
 627             this.type = new Object[] { m.getReturnType(), m.getParameterTypes() };
 628         if (wantSpecial) {
 629             if (isAbstract())
 630                 throw new AbstractMethodError(this.toString());
 631             if (getReferenceKind() == REF_invokeVirtual)
 632                 changeReferenceKind(REF_invokeSpecial, REF_invokeVirtual);
 633             else if (getReferenceKind() == REF_invokeInterface)
 634                 // invokeSpecial on a default method
 635                 changeReferenceKind(REF_invokeSpecial, REF_invokeInterface);
 636         }
 637     }
 638     public MemberName asSpecial() {
 639         switch (getReferenceKind()) {
 640         case REF_invokeSpecial:     return this;
 641         case REF_invokeVirtual:     return clone().changeReferenceKind(REF_invokeSpecial, REF_invokeVirtual);
 642         case REF_invokeInterface:   return clone().changeReferenceKind(REF_invokeSpecial, REF_invokeInterface);
 643         case REF_newInvokeSpecial:  return clone().changeReferenceKind(REF_invokeSpecial, REF_newInvokeSpecial);
 644         }
 645         throw new IllegalArgumentException(this.toString());
 646     }
 647     /** If this MN is not REF_newInvokeSpecial, return a clone with that ref. kind.
 648      *  In that case it must already be REF_invokeSpecial.
 649      */
 650     public MemberName asConstructor() {
 651         switch (getReferenceKind()) {
 652         case REF_invokeSpecial:     return clone().changeReferenceKind(REF_newInvokeSpecial, REF_invokeSpecial);
 653         case REF_newInvokeSpecial:  return this;
 654         }
 655         throw new IllegalArgumentException(this.toString());
 656     }
 657     /** If this MN is a REF_invokeSpecial, return a clone with the "normal" kind
 658      *  REF_invokeVirtual; also switch either to REF_invokeInterface if clazz.isInterface.
 659      *  The end result is to get a fully virtualized version of the MN.
 660      *  (Note that resolving in the JVM will sometimes devirtualize, changing
 661      *  REF_invokeVirtual of a final to REF_invokeSpecial, and REF_invokeInterface
 662      *  in some corner cases to either of the previous two; this transform
 663      *  undoes that change under the assumption that it occurred.)
 664      */
 665     public MemberName asNormalOriginal() {
 666         byte normalVirtual = clazz.isInterface() ? REF_invokeInterface : REF_invokeVirtual;
 667         byte refKind = getReferenceKind();
 668         byte newRefKind = refKind;
 669         MemberName result = this;
 670         switch (refKind) {
 671         case REF_invokeInterface:
 672         case REF_invokeVirtual:
 673         case REF_invokeSpecial:
 674             newRefKind = normalVirtual;
 675             break;
 676         }
 677         if (newRefKind == refKind)
 678             return this;
 679         result = clone().changeReferenceKind(newRefKind, refKind);
 680         assert(this.referenceKindIsConsistentWith(result.getReferenceKind()));
 681         return result;
 682     }
 683     /** Create a name for the given reflected constructor.  The resulting name will be in a resolved state. */
 684     @SuppressWarnings("LeakingThisInConstructor")
 685     public MemberName(Constructor<?> ctor) {
 686         Objects.requireNonNull(ctor);
 687         // fill in vmtarget, vmindex while we have ctor in hand:
 688         MethodHandleNatives.init(this, ctor);
 689         assert(isResolved() && this.clazz != null);
 690         this.name = CONSTRUCTOR_NAME;
 691         if (this.type == null)
 692             this.type = new Object[] { void.class, ctor.getParameterTypes() };
 693     }
 694     /** Create a name for the given reflected field.  The resulting name will be in a resolved state.
 695      */
 696     public MemberName(Field fld) {
 697         this(fld, false);
 698     }
 699     @SuppressWarnings("LeakingThisInConstructor")
 700     public MemberName(Field fld, boolean makeSetter) {
 701         Objects.requireNonNull(fld);
 702         // fill in vmtarget, vmindex while we have fld in hand:
 703         MethodHandleNatives.init(this, fld);
 704         assert(isResolved() && this.clazz != null);
 705         this.name = fld.getName();
 706         this.type = fld.getType();
 707         assert((REF_putStatic - REF_getStatic) == (REF_putField - REF_getField));
 708         byte refKind = this.getReferenceKind();
 709         assert(refKind == (isStatic() ? REF_getStatic : REF_getField));
 710         if (makeSetter) {
 711             changeReferenceKind((byte)(refKind + (REF_putStatic - REF_getStatic)), refKind);
 712         }
 713     }
 714     public boolean isGetter() {
 715         return MethodHandleNatives.refKindIsGetter(getReferenceKind());
 716     }
 717     public boolean isSetter() {
 718         return MethodHandleNatives.refKindIsSetter(getReferenceKind());
 719     }
 720     public MemberName asSetter() {
 721         byte refKind = getReferenceKind();
 722         assert(MethodHandleNatives.refKindIsGetter(refKind));
 723         assert((REF_putStatic - REF_getStatic) == (REF_putField - REF_getField));
 724         byte setterRefKind = (byte)(refKind + (REF_putField - REF_getField));
 725         return clone().changeReferenceKind(setterRefKind, refKind);
 726     }
 727     /** Create a name for the given class.  The resulting name will be in a resolved state. */
 728     public MemberName(Class<?> type) {
 729         init(type.getDeclaringClass(), type.getSimpleName(), type,
 730                 flagsMods(IS_TYPE, type.getModifiers(), REF_NONE));
 731         initResolved(true);
 732     }
 733 
 734     /**
 735      * Create a name for a signature-polymorphic invoker.
 736      * This is a placeholder for a signature-polymorphic instance
 737      * (of MH.invokeExact, etc.) that the JVM does not reify.
 738      * See comments on {@link MethodHandleNatives#linkMethod}.
 739      */
 740     static MemberName makeMethodHandleInvoke(String name, MethodType type) {
 741         return makeMethodHandleInvoke(name, type, MH_INVOKE_MODS | SYNTHETIC);
 742     }
 743     static MemberName makeMethodHandleInvoke(String name, MethodType type, int mods) {
 744         MemberName mem = new MemberName(MethodHandle.class, name, type, REF_invokeVirtual);
 745         mem.flags |= mods;  // it's not resolved, but add these modifiers anyway
 746         assert(mem.isMethodHandleInvoke()) : mem;
 747         return mem;
 748     }
 749 
 750     static MemberName makeVarHandleMethodInvoke(String name, MethodType type) {
 751         return makeVarHandleMethodInvoke(name, type, MH_INVOKE_MODS | SYNTHETIC);
 752     }
 753     static MemberName makeVarHandleMethodInvoke(String name, MethodType type, int mods) {
 754         MemberName mem = new MemberName(VarHandle.class, name, type, REF_invokeVirtual);
 755         mem.flags |= mods;  // it's not resolved, but add these modifiers anyway
 756         assert(mem.isVarHandleMethodInvoke()) : mem;
 757         return mem;
 758     }
 759 
 760     // bare-bones constructor; the JVM will fill it in
 761     MemberName() { }
 762 
 763     // locally useful cloner
 764     @Override protected MemberName clone() {
 765         try {
 766             return (MemberName) super.clone();
 767         } catch (CloneNotSupportedException ex) {
 768             throw newInternalError(ex);
 769         }
 770      }
 771 
 772     /** Get the definition of this member name.
 773      *  This may be in a super-class of the declaring class of this member.
 774      */
 775     public MemberName getDefinition() {
 776         if (!isResolved())  throw new IllegalStateException("must be resolved: "+this);
 777         if (isType())  return this;
 778         MemberName res = this.clone();
 779         res.clazz = null;
 780         res.type = null;
 781         res.name = null;
 782         res.resolution = res;
 783         res.expandFromVM();
 784         assert(res.getName().equals(this.getName()));
 785         return res;
 786     }
 787 
 788     @Override
 789     @SuppressWarnings("deprecation")
 790     public int hashCode() {
 791         // Avoid autoboxing getReferenceKind(), since this is used early and will force
 792         // early initialization of Byte$ByteCache
 793         return Objects.hash(clazz, new Byte(getReferenceKind()), name, getType());
 794     }
 795 
 796     @Override
 797     public boolean equals(Object that) {
 798         return (that instanceof MemberName && this.equals((MemberName)that));
 799     }
 800 
 801     /** Decide if two member names have exactly the same symbolic content.
 802      *  Does not take into account any actual class members, so even if
 803      *  two member names resolve to the same actual member, they may
 804      *  be distinct references.
 805      */
 806     public boolean equals(MemberName that) {
 807         if (this == that)  return true;
 808         if (that == null)  return false;
 809         return this.clazz == that.clazz
 810                 && this.getReferenceKind() == that.getReferenceKind()
 811                 && Objects.equals(this.name, that.name)
 812                 && Objects.equals(this.getType(), that.getType());
 813     }
 814 
 815     // Construction from symbolic parts, for queries:
 816     /** Create a field or type name from the given components:
 817      *  Declaring class, name, type, reference kind.
 818      *  The declaring class may be supplied as null if this is to be a bare name and type.
 819      *  The resulting name will in an unresolved state.
 820      */
 821     public MemberName(Class<?> defClass, String name, Class<?> type, byte refKind) {
 822         init(defClass, name, type, flagsMods(IS_FIELD, 0, refKind));
 823         initResolved(false);
 824     }
 825     /** Create a method or constructor name from the given components:
 826      *  Declaring class, name, type, reference kind.
 827      *  It will be a constructor if and only if the name is {@code "<init>"}.
 828      *  The declaring class may be supplied as null if this is to be a bare name and type.
 829      *  The last argument is optional, a boolean which requests REF_invokeSpecial.
 830      *  The resulting name will in an unresolved state.
 831      */
 832     public MemberName(Class<?> defClass, String name, MethodType type, byte refKind) {
 833         int initFlags = (name != null && name.equals(CONSTRUCTOR_NAME) ? IS_CONSTRUCTOR : IS_METHOD);
 834         init(defClass, name, type, flagsMods(initFlags, 0, refKind));
 835         initResolved(false);
 836     }
 837     /** Create a method, constructor, or field name from the given components:
 838      *  Reference kind, declaring class, name, type.
 839      */
 840     public MemberName(byte refKind, Class<?> defClass, String name, Object type) {
 841         int kindFlags;
 842         if (MethodHandleNatives.refKindIsField(refKind)) {
 843             kindFlags = IS_FIELD;
 844             if (!(type instanceof Class))
 845                 throw newIllegalArgumentException("not a field type");
 846         } else if (MethodHandleNatives.refKindIsMethod(refKind)) {
 847             kindFlags = IS_METHOD;
 848             if (!(type instanceof MethodType))
 849                 throw newIllegalArgumentException("not a method type");
 850         } else if (refKind == REF_newInvokeSpecial) {
 851             kindFlags = IS_CONSTRUCTOR;
 852             if (!(type instanceof MethodType) ||
 853                 !CONSTRUCTOR_NAME.equals(name))
 854                 throw newIllegalArgumentException("not a constructor type or name");
 855         } else {
 856             throw newIllegalArgumentException("bad reference kind "+refKind);
 857         }
 858         init(defClass, name, type, flagsMods(kindFlags, 0, refKind));
 859         initResolved(false);
 860     }
 861     /** Query whether this member name is resolved to a non-static, non-final method.
 862      */
 863     public boolean hasReceiverTypeDispatch() {
 864         return MethodHandleNatives.refKindDoesDispatch(getReferenceKind());
 865     }
 866 
 867     /** Query whether this member name is resolved.
 868      *  A resolved member name is one for which the JVM has found
 869      *  a method, constructor, field, or type binding corresponding exactly to the name.
 870      *  (Document?)
 871      */
 872     public boolean isResolved() {
 873         return resolution == null;
 874     }
 875 
 876     void initResolved(boolean isResolved) {
 877         assert(this.resolution == null);  // not initialized yet!
 878         if (!isResolved)
 879             this.resolution = this;
 880         assert(isResolved() == isResolved);
 881     }
 882 
 883     void checkForTypeAlias(Class<?> refc) {
 884         if (isInvocable()) {
 885             MethodType type;
 886             if (this.type instanceof MethodType)
 887                 type = (MethodType) this.type;
 888             else
 889                 this.type = type = getMethodType();
 890             if (type.erase() == type)  return;
 891             if (VerifyAccess.isTypeVisible(type, refc))  return;
 892             throw new LinkageError("bad method type alias: "+type+" not visible from "+refc);
 893         } else {
 894             Class<?> type;
 895             if (this.type instanceof Class<?>)
 896                 type = (Class<?>) this.type;
 897             else
 898                 this.type = type = getFieldType();
 899             if (VerifyAccess.isTypeVisible(type, refc))  return;
 900             throw new LinkageError("bad field type alias: "+type+" not visible from "+refc);
 901         }
 902     }
 903 
 904 
 905     /** Produce a string form of this member name.
 906      *  For types, it is simply the type's own string (as reported by {@code toString}).
 907      *  For fields, it is {@code "DeclaringClass.name/type"}.
 908      *  For methods and constructors, it is {@code "DeclaringClass.name(ptype...)rtype"}.
 909      *  If the declaring class is null, the prefix {@code "DeclaringClass."} is omitted.
 910      *  If the member is unresolved, a prefix {@code "*."} is prepended.
 911      */
 912     @SuppressWarnings("LocalVariableHidesMemberVariable")
 913     @Override
 914     public String toString() {
 915         if (isType())
 916             return type.toString();  // class java.lang.String
 917         // else it is a field, method, or constructor
 918         StringBuilder buf = new StringBuilder();
 919         if (getDeclaringClass() != null) {
 920             buf.append(getName(clazz));
 921             buf.append('.');
 922         }
 923         String name = this.name; // avoid expanding from VM
 924         buf.append(name == null ? "*" : name);
 925         Object type = this.type; // avoid expanding from VM
 926         if (!isInvocable()) {
 927             buf.append('/');
 928             buf.append(type == null ? "*" : getName(type));
 929         } else {
 930             buf.append(type == null ? "(*)*" : getName(type));
 931         }
 932         byte refKind = getReferenceKind();
 933         if (refKind != REF_NONE) {
 934             buf.append('/');
 935             buf.append(MethodHandleNatives.refKindName(refKind));
 936         }
 937         //buf.append("#").append(System.identityHashCode(this));
 938         return buf.toString();
 939     }
 940     private static String getName(Object obj) {
 941         if (obj instanceof Class<?>)
 942             return ((Class<?>)obj).getName();
 943         return String.valueOf(obj);
 944     }
 945 
 946     public IllegalAccessException makeAccessException(String message, Object from) {
 947         message = message + ": "+ toString();
 948         if (from != null)  {
 949             if (from == MethodHandles.publicLookup()) {
 950                 message += ", from public Lookup";
 951             } else {
 952                 Module m;
 953                 if (from instanceof MethodHandles.Lookup) {
 954                     MethodHandles.Lookup lookup = (MethodHandles.Lookup)from;
 955                     m = lookup.lookupClass().getModule();
 956                 } else {
 957                     m = from.getClass().getModule();
 958                 }
 959                 message += ", from " + from + " (" + m + ")";
 960             }
 961         }
 962         return new IllegalAccessException(message);
 963     }
 964     private String message() {
 965         if (isResolved())
 966             return "no access";
 967         else if (isConstructor())
 968             return "no such constructor";
 969         else if (isMethod())
 970             return "no such method";
 971         else
 972             return "no such field";
 973     }
 974     public ReflectiveOperationException makeAccessException() {
 975         String message = message() + ": "+ toString();
 976         ReflectiveOperationException ex;
 977         if (isResolved() || !(resolution instanceof NoSuchMethodError ||
 978                               resolution instanceof NoSuchFieldError))
 979             ex = new IllegalAccessException(message);
 980         else if (isConstructor())
 981             ex = new NoSuchMethodException(message);
 982         else if (isMethod())
 983             ex = new NoSuchMethodException(message);
 984         else
 985             ex = new NoSuchFieldException(message);
 986         if (resolution instanceof Throwable)
 987             ex.initCause((Throwable) resolution);
 988         return ex;
 989     }
 990 
 991     /** Actually making a query requires an access check. */
 992     /*non-public*/ static Factory getFactory() {
 993         return Factory.INSTANCE;
 994     }
 995     /** A factory type for resolving member names with the help of the VM.
 996      *  TBD: Define access-safe public constructors for this factory.
 997      */
 998     /*non-public*/ static class Factory {
 999         private Factory() { } // singleton pattern
1000         static Factory INSTANCE = new Factory();
1001 
1002         private static int ALLOWED_FLAGS = ALL_KINDS;
1003 
1004         /// Queries
1005         List<MemberName> getMembers(Class<?> defc,
1006                 String matchName, Object matchType,
1007                 int matchFlags, Class<?> lookupClass) {
1008             matchFlags &= ALLOWED_FLAGS;
1009             String matchSig = null;
1010             if (matchType != null) {
1011                 matchSig = BytecodeDescriptor.unparse(matchType);
1012                 if (matchSig.startsWith("("))
1013                     matchFlags &= ~(ALL_KINDS & ~IS_INVOCABLE);
1014                 else
1015                     matchFlags &= ~(ALL_KINDS & ~IS_FIELD);
1016             }
1017             final int BUF_MAX = 0x2000;
1018             int len1 = matchName == null ? 10 : matchType == null ? 4 : 1;
1019             MemberName[] buf = newMemberBuffer(len1);
1020             int totalCount = 0;
1021             ArrayList<MemberName[]> bufs = null;
1022             int bufCount = 0;
1023             for (;;) {
1024                 bufCount = MethodHandleNatives.getMembers(defc,
1025                         matchName, matchSig, matchFlags,
1026                         lookupClass,
1027                         totalCount, buf);
1028                 if (bufCount <= buf.length) {
1029                     if (bufCount < 0)  bufCount = 0;
1030                     totalCount += bufCount;
1031                     break;
1032                 }
1033                 // JVM returned to us with an intentional overflow!
1034                 totalCount += buf.length;
1035                 int excess = bufCount - buf.length;
1036                 if (bufs == null)  bufs = new ArrayList<>(1);
1037                 bufs.add(buf);
1038                 int len2 = buf.length;
1039                 len2 = Math.max(len2, excess);
1040                 len2 = Math.max(len2, totalCount / 4);
1041                 buf = newMemberBuffer(Math.min(BUF_MAX, len2));
1042             }
1043             ArrayList<MemberName> result = new ArrayList<>(totalCount);
1044             if (bufs != null) {
1045                 for (MemberName[] buf0 : bufs) {
1046                     Collections.addAll(result, buf0);
1047                 }
1048             }
1049             for (int i = 0; i < bufCount; i++) {
1050                 result.add(buf[i]);
1051             }
1052             // Signature matching is not the same as type matching, since
1053             // one signature might correspond to several types.
1054             // So if matchType is a Class or MethodType, refilter the results.
1055             if (matchType != null && matchType != matchSig) {
1056                 for (Iterator<MemberName> it = result.iterator(); it.hasNext();) {
1057                     MemberName m = it.next();
1058                     if (!matchType.equals(m.getType()))
1059                         it.remove();
1060                 }
1061             }
1062             return result;
1063         }
1064         /** Produce a resolved version of the given member.
1065          *  Super types are searched (for inherited members) if {@code searchSupers} is true.
1066          *  Access checking is performed on behalf of the given {@code lookupClass}.
1067          *  If lookup fails or access is not permitted, null is returned.
1068          *  Otherwise a fresh copy of the given member is returned, with modifier bits filled in.
1069          */
1070         private MemberName resolve(byte refKind, MemberName ref, Class<?> lookupClass,
1071                                    boolean speculativeResolve) {
1072             MemberName m = ref.clone();  // JVM will side-effect the ref
1073             assert(refKind == m.getReferenceKind());
1074             try {
1075                 // There are 4 entities in play here:
1076                 //   * LC: lookupClass
1077                 //   * REFC: symbolic reference class (MN.clazz before resolution);
1078                 //   * DEFC: resolved method holder (MN.clazz after resolution);
1079                 //   * PTYPES: parameter types (MN.type)
1080                 //
1081                 // What we care about when resolving a MemberName is consistency between DEFC and PTYPES.
1082                 // We do type alias (TA) checks on DEFC to ensure that. DEFC is not known until the JVM
1083                 // finishes the resolution, so do TA checks right after MHN.resolve() is over.
1084                 //
1085                 // All parameters passed by a caller are checked against MH type (PTYPES) on every invocation,
1086                 // so it is safe to call a MH from any context.
1087                 //
1088                 // REFC view on PTYPES doesn't matter, since it is used only as a starting point for resolution and doesn't
1089                 // participate in method selection.
1090                 m = MethodHandleNatives.resolve(m, lookupClass, speculativeResolve);
1091                 if (m == null && speculativeResolve) {
1092                     return null;
1093                 }
1094                 m.checkForTypeAlias(m.getDeclaringClass());
1095                 m.resolution = null;
1096             } catch (ClassNotFoundException | LinkageError ex) {
1097                 // JVM reports that the "bytecode behavior" would get an error
1098                 assert(!m.isResolved());
1099                 m.resolution = ex;
1100                 return m;
1101             }
1102             assert(m.referenceKindIsConsistent());
1103             m.initResolved(true);
1104             assert(m.vminfoIsConsistent());
1105             return m;
1106         }
1107         /** Produce a resolved version of the given member.
1108          *  Super types are searched (for inherited members) if {@code searchSupers} is true.
1109          *  Access checking is performed on behalf of the given {@code lookupClass}.
1110          *  If lookup fails or access is not permitted, a {@linkplain ReflectiveOperationException} is thrown.
1111          *  Otherwise a fresh copy of the given member is returned, with modifier bits filled in.
1112          */
1113         public
1114         <NoSuchMemberException extends ReflectiveOperationException>
1115         MemberName resolveOrFail(byte refKind, MemberName m, Class<?> lookupClass,
1116                                  Class<NoSuchMemberException> nsmClass)
1117                 throws IllegalAccessException, NoSuchMemberException {
1118             MemberName result = resolve(refKind, m, lookupClass, false);
1119             if (result.isResolved())
1120                 return result;
1121             ReflectiveOperationException ex = result.makeAccessException();
1122             if (ex instanceof IllegalAccessException)  throw (IllegalAccessException) ex;
1123             throw nsmClass.cast(ex);
1124         }
1125         /** Produce a resolved version of the given member.
1126          *  Super types are searched (for inherited members) if {@code searchSupers} is true.
1127          *  Access checking is performed on behalf of the given {@code lookupClass}.
1128          *  If lookup fails or access is not permitted, return null.
1129          *  Otherwise a fresh copy of the given member is returned, with modifier bits filled in.
1130          */
1131         public
1132         MemberName resolveOrNull(byte refKind, MemberName m, Class<?> lookupClass) {
1133             MemberName result = resolve(refKind, m, lookupClass, true);
1134             if (result != null && result.isResolved())
1135                 return result;
1136             return null;
1137         }
1138         /** Return a list of all methods defined by the given class.
1139          *  Super types are searched (for inherited members) if {@code searchSupers} is true.
1140          *  Access checking is performed on behalf of the given {@code lookupClass}.
1141          *  Inaccessible members are not added to the last.
1142          */
1143         public List<MemberName> getMethods(Class<?> defc, boolean searchSupers,
1144                 Class<?> lookupClass) {
1145             return getMethods(defc, searchSupers, null, null, lookupClass);
1146         }
1147         /** Return a list of matching methods defined by the given class.
1148          *  Super types are searched (for inherited members) if {@code searchSupers} is true.
1149          *  Returned methods will match the name (if not null) and the type (if not null).
1150          *  Access checking is performed on behalf of the given {@code lookupClass}.
1151          *  Inaccessible members are not added to the last.
1152          */
1153         public List<MemberName> getMethods(Class<?> defc, boolean searchSupers,
1154                 String name, MethodType type, Class<?> lookupClass) {
1155             int matchFlags = IS_METHOD | (searchSupers ? SEARCH_ALL_SUPERS : 0);
1156             return getMembers(defc, name, type, matchFlags, lookupClass);
1157         }
1158         /** Return a list of all constructors defined by the given class.
1159          *  Access checking is performed on behalf of the given {@code lookupClass}.
1160          *  Inaccessible members are not added to the last.
1161          */
1162         public List<MemberName> getConstructors(Class<?> defc, Class<?> lookupClass) {
1163             return getMembers(defc, null, null, IS_CONSTRUCTOR, lookupClass);
1164         }
1165         /** Return a list of all fields defined by the given class.
1166          *  Super types are searched (for inherited members) if {@code searchSupers} is true.
1167          *  Access checking is performed on behalf of the given {@code lookupClass}.
1168          *  Inaccessible members are not added to the last.
1169          */
1170         public List<MemberName> getFields(Class<?> defc, boolean searchSupers,
1171                 Class<?> lookupClass) {
1172             return getFields(defc, searchSupers, null, null, lookupClass);
1173         }
1174         /** Return a list of all fields defined by the given class.
1175          *  Super types are searched (for inherited members) if {@code searchSupers} is true.
1176          *  Returned fields will match the name (if not null) and the type (if not null).
1177          *  Access checking is performed on behalf of the given {@code lookupClass}.
1178          *  Inaccessible members are not added to the last.
1179          */
1180         public List<MemberName> getFields(Class<?> defc, boolean searchSupers,
1181                 String name, Class<?> type, Class<?> lookupClass) {
1182             int matchFlags = IS_FIELD | (searchSupers ? SEARCH_ALL_SUPERS : 0);
1183             return getMembers(defc, name, type, matchFlags, lookupClass);
1184         }
1185         /** Return a list of all nested types defined by the given class.
1186          *  Super types are searched (for inherited members) if {@code searchSupers} is true.
1187          *  Access checking is performed on behalf of the given {@code lookupClass}.
1188          *  Inaccessible members are not added to the last.
1189          */
1190         public List<MemberName> getNestedTypes(Class<?> defc, boolean searchSupers,
1191                 Class<?> lookupClass) {
1192             int matchFlags = IS_TYPE | (searchSupers ? SEARCH_ALL_SUPERS : 0);
1193             return getMembers(defc, null, null, matchFlags, lookupClass);
1194         }
1195         private static MemberName[] newMemberBuffer(int length) {
1196             MemberName[] buf = new MemberName[length];
1197             // fill the buffer with dummy structs for the JVM to fill in
1198             for (int i = 0; i < length; i++)
1199                 buf[i] = new MemberName();
1200             return buf;
1201         }
1202     }
1203 }