1193N/A/*
3530N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
1193N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1193N/A *
1193N/A * This code is free software; you can redistribute it and/or modify it
1193N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
1193N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1193N/A *
1193N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1193N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1193N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1193N/A * version 2 for more details (a copy is included in the LICENSE file that
1193N/A * accompanied this code).
1193N/A *
1193N/A * You should have received a copy of the GNU General Public License version
1193N/A * 2 along with this work; if not, write to the Free Software Foundation,
1193N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1193N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1193N/A */
1193N/A
3793N/Apackage java.lang.invoke;
1193N/A
3793N/Aimport sun.invoke.util.BytecodeDescriptor;
5459N/Aimport sun.invoke.util.VerifyAccess;
5459N/A
1193N/Aimport java.lang.reflect.Constructor;
1193N/Aimport java.lang.reflect.Field;
1193N/Aimport java.lang.reflect.Method;
1193N/Aimport java.lang.reflect.Member;
1193N/Aimport java.lang.reflect.Modifier;
1193N/Aimport java.util.ArrayList;
2040N/Aimport java.util.Arrays;
1193N/Aimport java.util.Collections;
1193N/Aimport java.util.Iterator;
1193N/Aimport java.util.List;
3793N/Aimport static java.lang.invoke.MethodHandleNatives.Constants.*;
3793N/Aimport static java.lang.invoke.MethodHandleStatics.*;
5459N/Aimport java.util.Objects;
1193N/A
1193N/A/**
2431N/A * A {@code MemberName} is a compact symbolic datum which fully characterizes
2431N/A * a method or field reference.
2431N/A * A member name refers to a field, method, constructor, or member type.
2431N/A * Every member name has a simple name (a string) and a type (either a Class or MethodType).
2431N/A * A member name may also have a non-null declaring class, or it may be simply
2431N/A * a naked name/type pair.
2431N/A * A member name may also have non-zero modifier flags.
2431N/A * Finally, a member name may be either resolved or unresolved.
2431N/A * If it is resolved, the existence of the named
2431N/A * <p>
2431N/A * Whether resolved or not, a member name provides no access rights or
2431N/A * invocation capability to its possessor. It is merely a compact
2431N/A * representation of all symbolic information necessary to link to
2431N/A * and properly use the named member.
2431N/A * <p>
2431N/A * When resolved, a member name's internal implementation may include references to JVM metadata.
1193N/A * This representation is stateless and only decriptive.
1193N/A * It provides no private information and no capability to use the member.
1193N/A * <p>
2431N/A * By contrast, a {@linkplain java.lang.reflect.Method} contains fuller information
1193N/A * about the internals of a method (except its bytecodes) and also
2431N/A * allows invocation. A MemberName is much lighter than a Method,
2431N/A * since it contains about 7 fields to the 16 of Method (plus its sub-arrays),
1193N/A * and those seven fields omit much of the information in Method.
1193N/A * @author jrose
1193N/A */
3792N/A/*non-public*/ final class MemberName implements Member, Cloneable {
1193N/A private Class<?> clazz; // class in which the method is defined
1193N/A private String name; // may be null if not yet materialized
1193N/A private Object type; // may be null if not yet materialized
1193N/A private int flags; // modifier bits; see reflect.Modifier
5459N/A //@Injected JVM_Method* vmtarget;
5459N/A //@Injected int vmindex;
5459N/A private Object resolution; // if null, this guy is resolved
1193N/A
2431N/A /** Return the declaring class of this member.
2431N/A * In the case of a bare name and type, the declaring class will be null.
2431N/A */
1193N/A public Class<?> getDeclaringClass() {
1193N/A return clazz;
1193N/A }
1193N/A
2431N/A /** Utility method producing the class loader of the declaring class. */
1193N/A public ClassLoader getClassLoader() {
1193N/A return clazz.getClassLoader();
1193N/A }
1193N/A
2431N/A /** Return the simple name of this member.
2431N/A * For a type, it is the same as {@link Class#getSimpleName}.
2431N/A * For a method or field, it is the simple name of the member.
2431N/A * For a constructor, it is always {@code "&lt;init&gt;"}.
2431N/A */
1193N/A public String getName() {
1193N/A if (name == null) {
1193N/A expandFromVM();
1193N/A if (name == null) return null;
1193N/A }
1193N/A return name;
1193N/A }
1193N/A
5459N/A public MethodType getMethodOrFieldType() {
5459N/A if (isInvocable())
5459N/A return getMethodType();
5459N/A if (isGetter())
5459N/A return MethodType.methodType(getFieldType());
5459N/A if (isSetter())
5459N/A return MethodType.methodType(void.class, getFieldType());
5459N/A throw new InternalError("not a method or field: "+this);
5459N/A }
5459N/A
2431N/A /** Return the declared type of this member, which
2431N/A * must be a method or constructor.
2431N/A */
1193N/A public MethodType getMethodType() {
1193N/A if (type == null) {
1193N/A expandFromVM();
1193N/A if (type == null) return null;
1193N/A }
1193N/A if (!isInvocable())
1193N/A throw newIllegalArgumentException("not invocable, no method type");
1193N/A if (type instanceof MethodType) {
1193N/A return (MethodType) type;
1193N/A }
1193N/A if (type instanceof String) {
1193N/A String sig = (String) type;
2040N/A MethodType res = MethodType.fromMethodDescriptorString(sig, getClassLoader());
1193N/A this.type = res;
1193N/A return res;
1193N/A }
1193N/A if (type instanceof Object[]) {
1193N/A Object[] typeInfo = (Object[]) type;
1193N/A Class<?>[] ptypes = (Class<?>[]) typeInfo[1];
1193N/A Class<?> rtype = (Class<?>) typeInfo[0];
2040N/A MethodType res = MethodType.methodType(rtype, ptypes);
1193N/A this.type = res;
1193N/A return res;
1193N/A }
1193N/A throw new InternalError("bad method type "+type);
1193N/A }
1193N/A
2431N/A /** Return the actual type under which this method or constructor must be invoked.
2431N/A * For non-static methods or constructors, this is the type with a leading parameter,
2431N/A * a reference to declaring class. For static methods, it is the same as the declared type.
2431N/A */
1193N/A public MethodType getInvocationType() {
5459N/A MethodType itype = getMethodOrFieldType();
5459N/A if (isConstructor() && getReferenceKind() == REF_newInvokeSpecial)
5459N/A return itype.changeReturnType(clazz);
1193N/A if (!isStatic())
5459N/A return itype.insertParameterTypes(0, clazz);
1193N/A return itype;
1193N/A }
1193N/A
2431N/A /** Utility method producing the parameter types of the method type. */
1193N/A public Class<?>[] getParameterTypes() {
1193N/A return getMethodType().parameterArray();
1193N/A }
1193N/A
2431N/A /** Utility method producing the return type of the method type. */
1193N/A public Class<?> getReturnType() {
1193N/A return getMethodType().returnType();
1193N/A }
1193N/A
2431N/A /** Return the declared type of this member, which
2431N/A * must be a field or type.
2431N/A * If it is a type member, that type itself is returned.
2431N/A */
1193N/A public Class<?> getFieldType() {
1193N/A if (type == null) {
1193N/A expandFromVM();
1193N/A if (type == null) return null;
1193N/A }
1193N/A if (isInvocable())
1193N/A throw newIllegalArgumentException("not a field or nested class, no simple type");
1193N/A if (type instanceof Class<?>) {
1193N/A return (Class<?>) type;
1193N/A }
1193N/A if (type instanceof String) {
1193N/A String sig = (String) type;
2040N/A MethodType mtype = MethodType.fromMethodDescriptorString("()"+sig, getClassLoader());
1193N/A Class<?> res = mtype.returnType();
1193N/A this.type = res;
1193N/A return res;
1193N/A }
1193N/A throw new InternalError("bad field type "+type);
1193N/A }
1193N/A
2431N/A /** Utility method to produce either the method type or field type of this member. */
1193N/A public Object getType() {
1193N/A return (isInvocable() ? getMethodType() : getFieldType());
1193N/A }
1193N/A
2431N/A /** Utility method to produce the signature of this member,
2431N/A * used within the class file format to describe its type.
2431N/A */
1193N/A public String getSignature() {
1193N/A if (type == null) {
1193N/A expandFromVM();
1193N/A if (type == null) return null;
1193N/A }
1193N/A if (type instanceof String)
1193N/A return (String) type;
1193N/A if (isInvocable())
2040N/A return BytecodeDescriptor.unparse(getMethodType());
1193N/A else
2040N/A return BytecodeDescriptor.unparse(getFieldType());
1193N/A }
1193N/A
2431N/A /** Return the modifier flags of this member.
2431N/A * @see java.lang.reflect.Modifier
2431N/A */
1193N/A public int getModifiers() {
1193N/A return (flags & RECOGNIZED_MODIFIERS);
1193N/A }
1193N/A
5459N/A /** Return the reference kind of this member, or zero if none.
5459N/A */
5459N/A public byte getReferenceKind() {
5459N/A return (byte) ((flags >>> MN_REFERENCE_KIND_SHIFT) & MN_REFERENCE_KIND_MASK);
5459N/A }
5459N/A private boolean referenceKindIsConsistent() {
5459N/A byte refKind = getReferenceKind();
5459N/A if (refKind == REF_NONE) return isType();
5459N/A if (isField()) {
5459N/A assert(staticIsConsistent());
5459N/A assert(MethodHandleNatives.refKindIsField(refKind));
5459N/A } else if (isConstructor()) {
5459N/A assert(refKind == REF_newInvokeSpecial || refKind == REF_invokeSpecial);
5459N/A } else if (isMethod()) {
5459N/A assert(staticIsConsistent());
5459N/A assert(MethodHandleNatives.refKindIsMethod(refKind));
5459N/A if (clazz.isInterface())
5459N/A assert(refKind == REF_invokeInterface ||
5459N/A refKind == REF_invokeVirtual && isObjectPublicMethod());
5459N/A } else {
5459N/A assert(false);
5459N/A }
5459N/A return true;
5459N/A }
5459N/A private boolean isObjectPublicMethod() {
5459N/A if (clazz == Object.class) return true;
5459N/A MethodType mtype = getMethodType();
5459N/A if (name.equals("toString") && mtype.returnType() == String.class && mtype.parameterCount() == 0)
5459N/A return true;
5459N/A if (name.equals("hashCode") && mtype.returnType() == int.class && mtype.parameterCount() == 0)
5459N/A return true;
5459N/A if (name.equals("equals") && mtype.returnType() == boolean.class && mtype.parameterCount() == 1 && mtype.parameterType(0) == Object.class)
5459N/A return true;
5459N/A return false;
5459N/A }
5459N/A /*non-public*/ boolean referenceKindIsConsistentWith(int originalRefKind) {
5459N/A int refKind = getReferenceKind();
5459N/A if (refKind == originalRefKind) return true;
5459N/A switch (originalRefKind) {
5459N/A case REF_invokeInterface:
5459N/A // Looking up an interface method, can get (e.g.) Object.hashCode
5459N/A assert(refKind == REF_invokeVirtual ||
5459N/A refKind == REF_invokeSpecial) : this;
5459N/A return true;
5459N/A case REF_invokeVirtual:
5459N/A case REF_newInvokeSpecial:
5459N/A // Looked up a virtual, can get (e.g.) final String.hashCode.
5459N/A assert(refKind == REF_invokeSpecial) : this;
5459N/A return true;
5459N/A }
5459N/A assert(false) : this;
5459N/A return true;
5459N/A }
5459N/A private boolean staticIsConsistent() {
5459N/A byte refKind = getReferenceKind();
5459N/A return MethodHandleNatives.refKindIsStatic(refKind) == isStatic() || getModifiers() == 0;
5459N/A }
5459N/A private boolean vminfoIsConsistent() {
5459N/A byte refKind = getReferenceKind();
5459N/A assert(isResolved()); // else don't call
5459N/A Object vminfo = MethodHandleNatives.getMemberVMInfo(this);
5459N/A assert(vminfo instanceof Object[]);
5459N/A long vmindex = (Long) ((Object[])vminfo)[0];
5459N/A Object vmtarget = ((Object[])vminfo)[1];
5459N/A if (MethodHandleNatives.refKindIsField(refKind)) {
5459N/A assert(vmindex >= 0) : vmindex + ":" + this;
5459N/A assert(vmtarget instanceof Class);
5459N/A } else {
5459N/A if (MethodHandleNatives.refKindDoesDispatch(refKind))
5459N/A assert(vmindex >= 0) : vmindex + ":" + this;
5459N/A else
5459N/A assert(vmindex < 0) : vmindex;
5459N/A assert(vmtarget instanceof MemberName) : vmtarget + " in " + this;
5459N/A }
5459N/A return true;
5459N/A }
5459N/A
5459N/A private MemberName changeReferenceKind(byte refKind, byte oldKind) {
5459N/A assert(getReferenceKind() == oldKind);
5459N/A assert(MethodHandleNatives.refKindIsValid(refKind));
5459N/A flags += (((int)refKind - oldKind) << MN_REFERENCE_KIND_SHIFT);
5459N/A// if (isConstructor() && refKind != REF_newInvokeSpecial)
5459N/A// flags += (IS_METHOD - IS_CONSTRUCTOR);
5459N/A// else if (refKind == REF_newInvokeSpecial && isMethod())
5459N/A// flags += (IS_CONSTRUCTOR - IS_METHOD);
5459N/A return this;
1193N/A }
1193N/A
1193N/A private boolean testFlags(int mask, int value) {
1193N/A return (flags & mask) == value;
1193N/A }
1193N/A private boolean testAllFlags(int mask) {
1193N/A return testFlags(mask, mask);
1193N/A }
1193N/A private boolean testAnyFlags(int mask) {
1193N/A return !testFlags(mask, 0);
1193N/A }
1193N/A
5459N/A /** Utility method to query if this member is a method handle invocation (invoke or invokeExact). */
5459N/A public boolean isMethodHandleInvoke() {
5459N/A final int bits = Modifier.NATIVE | Modifier.FINAL;
5459N/A final int negs = Modifier.STATIC;
5459N/A if (testFlags(bits | negs, bits) &&
5459N/A clazz == MethodHandle.class) {
5459N/A return name.equals("invoke") || name.equals("invokeExact");
5459N/A }
5459N/A return false;
5459N/A }
5459N/A
2431N/A /** Utility method to query the modifier flags of this member. */
1193N/A public boolean isStatic() {
1193N/A return Modifier.isStatic(flags);
1193N/A }
2431N/A /** Utility method to query the modifier flags of this member. */
1193N/A public boolean isPublic() {
1193N/A return Modifier.isPublic(flags);
1193N/A }
2431N/A /** Utility method to query the modifier flags of this member. */
1193N/A public boolean isPrivate() {
1193N/A return Modifier.isPrivate(flags);
1193N/A }
2431N/A /** Utility method to query the modifier flags of this member. */
1193N/A public boolean isProtected() {
1193N/A return Modifier.isProtected(flags);
1193N/A }
2431N/A /** Utility method to query the modifier flags of this member. */
1193N/A public boolean isFinal() {
1193N/A return Modifier.isFinal(flags);
1193N/A }
5459N/A /** Utility method to query whether this member or its defining class is final. */
5459N/A public boolean canBeStaticallyBound() {
5459N/A return Modifier.isFinal(flags | clazz.getModifiers());
5459N/A }
5459N/A /** Utility method to query the modifier flags of this member. */
5459N/A public boolean isVolatile() {
5459N/A return Modifier.isVolatile(flags);
5459N/A }
2431N/A /** Utility method to query the modifier flags of this member. */
1193N/A public boolean isAbstract() {
1193N/A return Modifier.isAbstract(flags);
1193N/A }
5459N/A /** Utility method to query the modifier flags of this member. */
5459N/A public boolean isNative() {
5459N/A return Modifier.isNative(flags);
5459N/A }
1193N/A // let the rest (native, volatile, transient, etc.) be tested via Modifier.isFoo
1193N/A
1193N/A // unofficial modifier flags, used by HotSpot:
1193N/A static final int BRIDGE = 0x00000040;
1193N/A static final int VARARGS = 0x00000080;
1193N/A static final int SYNTHETIC = 0x00001000;
1193N/A static final int ANNOTATION= 0x00002000;
1193N/A static final int ENUM = 0x00004000;
2431N/A /** Utility method to query the modifier flags of this member; returns false if the member is not a method. */
1193N/A public boolean isBridge() {
1193N/A return testAllFlags(IS_METHOD | BRIDGE);
1193N/A }
2431N/A /** Utility method to query the modifier flags of this member; returns false if the member is not a method. */
1193N/A public boolean isVarargs() {
1193N/A return testAllFlags(VARARGS) && isInvocable();
1193N/A }
2431N/A /** Utility method to query the modifier flags of this member; returns false if the member is not a method. */
1193N/A public boolean isSynthetic() {
1193N/A return testAllFlags(SYNTHETIC);
1193N/A }
1193N/A
1193N/A static final String CONSTRUCTOR_NAME = "<init>"; // the ever-popular
1193N/A
1193N/A // modifiers exported by the JVM:
1193N/A static final int RECOGNIZED_MODIFIERS = 0xFFFF;
1193N/A
1193N/A // private flags, not part of RECOGNIZED_MODIFIERS:
1193N/A static final int
6338N/A IS_METHOD = MN_IS_METHOD, // method (not constructor)
6338N/A IS_CONSTRUCTOR = MN_IS_CONSTRUCTOR, // constructor
6338N/A IS_FIELD = MN_IS_FIELD, // field
6338N/A IS_TYPE = MN_IS_TYPE, // nested type
6338N/A IS_CALLER_SENSITIVE = MN_CALLER_SENSITIVE; // @CallerSensitive annotation
1193N/A
1193N/A static final int ALL_ACCESS = Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED;
1193N/A static final int ALL_KINDS = IS_METHOD | IS_CONSTRUCTOR | IS_FIELD | IS_TYPE;
1193N/A static final int IS_INVOCABLE = IS_METHOD | IS_CONSTRUCTOR;
1193N/A static final int IS_FIELD_OR_METHOD = IS_METHOD | IS_FIELD;
5459N/A static final int SEARCH_ALL_SUPERS = MN_SEARCH_SUPERCLASSES | MN_SEARCH_INTERFACES;
1193N/A
2431N/A /** Utility method to query whether this member is a method or constructor. */
1193N/A public boolean isInvocable() {
1193N/A return testAnyFlags(IS_INVOCABLE);
1193N/A }
2431N/A /** Utility method to query whether this member is a method, constructor, or field. */
1193N/A public boolean isFieldOrMethod() {
1193N/A return testAnyFlags(IS_FIELD_OR_METHOD);
1193N/A }
2431N/A /** Query whether this member is a method. */
1193N/A public boolean isMethod() {
1193N/A return testAllFlags(IS_METHOD);
1193N/A }
2431N/A /** Query whether this member is a constructor. */
1193N/A public boolean isConstructor() {
1193N/A return testAllFlags(IS_CONSTRUCTOR);
1193N/A }
2431N/A /** Query whether this member is a field. */
1193N/A public boolean isField() {
1193N/A return testAllFlags(IS_FIELD);
1193N/A }
2431N/A /** Query whether this member is a type. */
1193N/A public boolean isType() {
1193N/A return testAllFlags(IS_TYPE);
1193N/A }
2431N/A /** Utility method to query whether this member is neither public, private, nor protected. */
1193N/A public boolean isPackage() {
1193N/A return !testAnyFlags(ALL_ACCESS);
1193N/A }
6338N/A /** Utility method to query whether this member is annotated with @CallerSensitive. */
6338N/A public boolean isCallerSensitive() {
6338N/A return testAllFlags(IS_CALLER_SENSITIVE);
6338N/A }
1193N/A
5459N/A /** Utility method to query whether this member is accessible from a given lookup class. */
5459N/A public boolean isAccessibleFrom(Class<?> lookupClass) {
5459N/A return VerifyAccess.isMemberAccessible(this.getDeclaringClass(), this.getDeclaringClass(), flags,
5459N/A lookupClass, ALL_ACCESS|MethodHandles.Lookup.PACKAGE);
5459N/A }
5459N/A
1193N/A /** Initialize a query. It is not resolved. */
1193N/A private void init(Class<?> defClass, String name, Object type, int flags) {
1193N/A // defining class is allowed to be null (for a naked name/type pair)
2431N/A //name.toString(); // null check
2431N/A //type.equals(type); // null check
1193N/A // fill in fields:
1193N/A this.clazz = defClass;
1193N/A this.name = name;
1193N/A this.type = type;
5463N/A this.flags = flags;
5463N/A assert(testAnyFlags(ALL_KINDS));
5459N/A assert(this.resolution == null); // nobody should have touched this yet
5463N/A //assert(referenceKindIsConsistent()); // do this after resolution
1193N/A }
1193N/A
1193N/A private void expandFromVM() {
1193N/A if (!isResolved()) return;
1193N/A if (type instanceof Object[])
1193N/A type = null; // don't saddle JVM w/ typeInfo
1193N/A MethodHandleNatives.expand(this);
1193N/A }
1193N/A
1193N/A // Capturing information from the Core Reflection API:
5459N/A private static int flagsMods(int flags, int mods, byte refKind) {
1193N/A assert((flags & RECOGNIZED_MODIFIERS) == 0);
1193N/A assert((mods & ~RECOGNIZED_MODIFIERS) == 0);
5459N/A assert((refKind & ~MN_REFERENCE_KIND_MASK) == 0);
5459N/A return flags | mods | (refKind << MN_REFERENCE_KIND_SHIFT);
1193N/A }
2431N/A /** Create a name for the given reflected method. The resulting name will be in a resolved state. */
1193N/A public MemberName(Method m) {
5459N/A this(m, false);
5459N/A }
5459N/A @SuppressWarnings("LeakingThisInConstructor")
5459N/A public MemberName(Method m, boolean wantSpecial) {
5460N/A m.getClass(); // NPE check
1193N/A // fill in vmtarget, vmindex while we have m in hand:
1193N/A MethodHandleNatives.init(this, m);
5459N/A assert(isResolved() && this.clazz != null);
5459N/A this.name = m.getName();
5459N/A if (this.type == null)
5459N/A this.type = new Object[] { m.getReturnType(), m.getParameterTypes() };
5459N/A if (wantSpecial) {
5459N/A if (getReferenceKind() == REF_invokeVirtual)
5459N/A changeReferenceKind(REF_invokeSpecial, REF_invokeVirtual);
5459N/A }
5459N/A }
5459N/A public MemberName asSpecial() {
5459N/A switch (getReferenceKind()) {
5459N/A case REF_invokeSpecial: return this;
5459N/A case REF_invokeVirtual: return clone().changeReferenceKind(REF_invokeSpecial, REF_invokeVirtual);
5459N/A case REF_newInvokeSpecial: return clone().changeReferenceKind(REF_invokeSpecial, REF_newInvokeSpecial);
5459N/A }
5459N/A throw new IllegalArgumentException(this.toString());
5459N/A }
5459N/A public MemberName asConstructor() {
5459N/A switch (getReferenceKind()) {
5459N/A case REF_invokeSpecial: return clone().changeReferenceKind(REF_newInvokeSpecial, REF_invokeSpecial);
5459N/A case REF_newInvokeSpecial: return this;
5459N/A }
5459N/A throw new IllegalArgumentException(this.toString());
1193N/A }
2431N/A /** Create a name for the given reflected constructor. The resulting name will be in a resolved state. */
5459N/A @SuppressWarnings("LeakingThisInConstructor")
5455N/A public MemberName(Constructor<?> ctor) {
5460N/A ctor.getClass(); // NPE check
1193N/A // fill in vmtarget, vmindex while we have ctor in hand:
1193N/A MethodHandleNatives.init(this, ctor);
5459N/A assert(isResolved() && this.clazz != null);
5459N/A this.name = CONSTRUCTOR_NAME;
5459N/A if (this.type == null)
5459N/A this.type = new Object[] { void.class, ctor.getParameterTypes() };
1193N/A }
5459N/A /** Create a name for the given reflected field. The resulting name will be in a resolved state.
5459N/A */
1193N/A public MemberName(Field fld) {
5459N/A this(fld, false);
5459N/A }
5459N/A @SuppressWarnings("LeakingThisInConstructor")
5459N/A public MemberName(Field fld, boolean makeSetter) {
5460N/A fld.getClass(); // NPE check
1193N/A // fill in vmtarget, vmindex while we have fld in hand:
1193N/A MethodHandleNatives.init(this, fld);
5459N/A assert(isResolved() && this.clazz != null);
5459N/A this.name = fld.getName();
5459N/A this.type = fld.getType();
5459N/A assert((REF_putStatic - REF_getStatic) == (REF_putField - REF_getField));
5459N/A byte refKind = this.getReferenceKind();
5459N/A assert(refKind == (isStatic() ? REF_getStatic : REF_getField));
5459N/A if (makeSetter) {
5459N/A changeReferenceKind((byte)(refKind + (REF_putStatic - REF_getStatic)), refKind);
5459N/A }
5459N/A }
5459N/A public boolean isGetter() {
5459N/A return MethodHandleNatives.refKindIsGetter(getReferenceKind());
5459N/A }
5459N/A public boolean isSetter() {
5459N/A return MethodHandleNatives.refKindIsSetter(getReferenceKind());
5459N/A }
5459N/A public MemberName asSetter() {
5459N/A byte refKind = getReferenceKind();
5459N/A assert(MethodHandleNatives.refKindIsGetter(refKind));
5459N/A assert((REF_putStatic - REF_getStatic) == (REF_putField - REF_getField));
5459N/A byte setterRefKind = (byte)(refKind + (REF_putField - REF_getField));
5459N/A return clone().changeReferenceKind(setterRefKind, refKind);
1193N/A }
2431N/A /** Create a name for the given class. The resulting name will be in a resolved state. */
1193N/A public MemberName(Class<?> type) {
5459N/A init(type.getDeclaringClass(), type.getSimpleName(), type,
5459N/A flagsMods(IS_TYPE, type.getModifiers(), REF_NONE));
5459N/A initResolved(true);
1193N/A }
1193N/A
1193N/A // bare-bones constructor; the JVM will fill it in
1193N/A MemberName() { }
1193N/A
1193N/A // locally useful cloner
1193N/A @Override protected MemberName clone() {
1193N/A try {
1193N/A return (MemberName) super.clone();
1193N/A } catch (CloneNotSupportedException ex) {
5465N/A throw newInternalError(ex);
1193N/A }
1193N/A }
1193N/A
5459N/A /** Get the definition of this member name.
5459N/A * This may be in a super-class of the declaring class of this member.
5459N/A */
5459N/A public MemberName getDefinition() {
5459N/A if (!isResolved()) throw new IllegalStateException("must be resolved: "+this);
5459N/A if (isType()) return this;
5459N/A MemberName res = this.clone();
5459N/A res.clazz = null;
5459N/A res.type = null;
5459N/A res.name = null;
5459N/A res.resolution = res;
5459N/A res.expandFromVM();
5459N/A assert(res.getName().equals(this.getName()));
5459N/A return res;
5459N/A }
5459N/A
5459N/A @Override
5459N/A public int hashCode() {
5459N/A return Objects.hash(clazz, flags, name, getType());
5459N/A }
5459N/A @Override
5459N/A public boolean equals(Object that) {
5459N/A return (that instanceof MemberName && this.equals((MemberName)that));
5459N/A }
5459N/A
5459N/A /** Decide if two member names have exactly the same symbolic content.
5459N/A * Does not take into account any actual class members, so even if
5459N/A * two member names resolve to the same actual member, they may
5459N/A * be distinct references.
5459N/A */
5459N/A public boolean equals(MemberName that) {
5459N/A if (this == that) return true;
5459N/A if (that == null) return false;
5459N/A return this.clazz == that.clazz
5459N/A && this.flags == that.flags
5459N/A && Objects.equals(this.name, that.name)
5459N/A && Objects.equals(this.getType(), that.getType());
5459N/A }
1193N/A
1193N/A // Construction from symbolic parts, for queries:
5459N/A /** Create a field or type name from the given components: Declaring class, name, type, reference kind.
2431N/A * The declaring class may be supplied as null if this is to be a bare name and type.
2431N/A * The resulting name will in an unresolved state.
2431N/A */
5459N/A public MemberName(Class<?> defClass, String name, Class<?> type, byte refKind) {
5459N/A init(defClass, name, type, flagsMods(IS_FIELD, 0, refKind));
5459N/A initResolved(false);
1193N/A }
2431N/A /** Create a field or type name from the given components: Declaring class, name, type.
2431N/A * The declaring class may be supplied as null if this is to be a bare name and type.
2431N/A * The modifier flags default to zero.
2431N/A * The resulting name will in an unresolved state.
2431N/A */
5459N/A public MemberName(Class<?> defClass, String name, Class<?> type, Void unused) {
5459N/A this(defClass, name, type, REF_NONE);
5459N/A initResolved(false);
1193N/A }
2431N/A /** Create a method or constructor name from the given components: Declaring class, name, type, modifiers.
2431N/A * It will be a constructor if and only if the name is {@code "&lt;init&gt;"}.
2431N/A * The declaring class may be supplied as null if this is to be a bare name and type.
5459N/A * The last argument is optional, a boolean which requests REF_invokeSpecial.
2431N/A * The resulting name will in an unresolved state.
2431N/A */
5459N/A public MemberName(Class<?> defClass, String name, MethodType type, byte refKind) {
5459N/A @SuppressWarnings("LocalVariableHidesMemberVariable")
5459N/A int flags = (name != null && name.equals(CONSTRUCTOR_NAME) ? IS_CONSTRUCTOR : IS_METHOD);
5459N/A init(defClass, name, type, flagsMods(flags, 0, refKind));
5459N/A initResolved(false);
1193N/A }
5459N/A// /** Create a method or constructor name from the given components: Declaring class, name, type, modifiers.
5459N/A// * It will be a constructor if and only if the name is {@code "&lt;init&gt;"}.
5459N/A// * The declaring class may be supplied as null if this is to be a bare name and type.
5459N/A// * The modifier flags default to zero.
5459N/A// * The resulting name will in an unresolved state.
5459N/A// */
5459N/A// public MemberName(Class<?> defClass, String name, MethodType type, Void unused) {
5459N/A// this(defClass, name, type, REF_NONE);
5459N/A// }
5459N/A
5459N/A /** Query whether this member name is resolved to a non-static, non-final method.
2431N/A */
5459N/A public boolean hasReceiverTypeDispatch() {
5459N/A return MethodHandleNatives.refKindDoesDispatch(getReferenceKind());
1193N/A }
1193N/A
2431N/A /** Query whether this member name is resolved.
2431N/A * A resolved member name is one for which the JVM has found
2431N/A * a method, constructor, field, or type binding corresponding exactly to the name.
2431N/A * (Document?)
2431N/A */
2431N/A public boolean isResolved() {
5459N/A return resolution == null;
5459N/A }
5459N/A
5459N/A private void initResolved(boolean isResolved) {
5459N/A assert(this.resolution == null); // not initialized yet!
5459N/A if (!isResolved)
5459N/A this.resolution = this;
5459N/A assert(isResolved() == isResolved);
1193N/A }
1193N/A
5459N/A void checkForTypeAlias() {
5459N/A if (isInvocable()) {
5459N/A MethodType type;
5459N/A if (this.type instanceof MethodType)
5459N/A type = (MethodType) this.type;
5459N/A else
5459N/A this.type = type = getMethodType();
5459N/A if (type.erase() == type) return;
5459N/A if (VerifyAccess.isTypeVisible(type, clazz)) return;
5459N/A throw new LinkageError("bad method type alias: "+type+" not visible from "+clazz);
5459N/A } else {
5459N/A Class<?> type;
5459N/A if (this.type instanceof Class<?>)
5459N/A type = (Class<?>) this.type;
5459N/A else
5459N/A this.type = type = getFieldType();
5459N/A if (VerifyAccess.isTypeVisible(type, clazz)) return;
5459N/A throw new LinkageError("bad field type alias: "+type+" not visible from "+clazz);
5459N/A }
1193N/A }
1193N/A
5459N/A
2431N/A /** Produce a string form of this member name.
2431N/A * For types, it is simply the type's own string (as reported by {@code toString}).
2431N/A * For fields, it is {@code "DeclaringClass.name/type"}.
2431N/A * For methods and constructors, it is {@code "DeclaringClass.name(ptype...)rtype"}.
2431N/A * If the declaring class is null, the prefix {@code "DeclaringClass."} is omitted.
2431N/A * If the member is unresolved, a prefix {@code "*."} is prepended.
2431N/A */
5459N/A @SuppressWarnings("LocalVariableHidesMemberVariable")
1193N/A @Override
1193N/A public String toString() {
1193N/A if (isType())
1193N/A return type.toString(); // class java.lang.String
1193N/A // else it is a field, method, or constructor
1193N/A StringBuilder buf = new StringBuilder();
1193N/A if (getDeclaringClass() != null) {
1193N/A buf.append(getName(clazz));
1193N/A buf.append('.');
1193N/A }
2431N/A String name = getName();
2431N/A buf.append(name == null ? "*" : name);
2431N/A Object type = getType();
2431N/A if (!isInvocable()) {
2431N/A buf.append('/');
2431N/A buf.append(type == null ? "*" : getName(type));
2431N/A } else {
2431N/A buf.append(type == null ? "(*)*" : getName(type));
2431N/A }
5459N/A byte refKind = getReferenceKind();
5459N/A if (refKind != REF_NONE) {
5459N/A buf.append('/');
5459N/A buf.append(MethodHandleNatives.refKindName(refKind));
1193N/A }
5459N/A //buf.append("#").append(System.identityHashCode(this));
1193N/A return buf.toString();
1193N/A }
1193N/A private static String getName(Object obj) {
1193N/A if (obj instanceof Class<?>)
1193N/A return ((Class<?>)obj).getName();
2040N/A return String.valueOf(obj);
1193N/A }
1193N/A
3792N/A public IllegalAccessException makeAccessException(String message, Object from) {
3792N/A message = message + ": "+ toString();
3530N/A if (from != null) message += ", from " + from;
3530N/A return new IllegalAccessException(message);
1193N/A }
4250N/A private String message() {
4250N/A if (isResolved())
4250N/A return "no access";
4250N/A else if (isConstructor())
4250N/A return "no such constructor";
4250N/A else if (isMethod())
4250N/A return "no such method";
4250N/A else
4250N/A return "no such field";
4250N/A }
4250N/A public ReflectiveOperationException makeAccessException() {
4250N/A String message = message() + ": "+ toString();
5459N/A ReflectiveOperationException ex;
5459N/A if (isResolved() || !(resolution instanceof NoSuchMethodError ||
5459N/A resolution instanceof NoSuchFieldError))
5459N/A ex = new IllegalAccessException(message);
3792N/A else if (isConstructor())
5459N/A ex = new NoSuchMethodException(message);
3792N/A else if (isMethod())
5459N/A ex = new NoSuchMethodException(message);
3530N/A else
5459N/A ex = new NoSuchFieldException(message);
5459N/A if (resolution instanceof Throwable)
5459N/A ex.initCause((Throwable) resolution);
5459N/A return ex;
3011N/A }
1193N/A
1193N/A /** Actually making a query requires an access check. */
3792N/A /*non-public*/ static Factory getFactory() {
1193N/A return Factory.INSTANCE;
1193N/A }
2431N/A /** A factory type for resolving member names with the help of the VM.
2431N/A * TBD: Define access-safe public constructors for this factory.
2431N/A */
4183N/A /*non-public*/ static class Factory {
1193N/A private Factory() { } // singleton pattern
1193N/A static Factory INSTANCE = new Factory();
1193N/A
5459N/A private static int ALLOWED_FLAGS = ALL_KINDS;
1193N/A
1193N/A /// Queries
1193N/A List<MemberName> getMembers(Class<?> defc,
1193N/A String matchName, Object matchType,
1193N/A int matchFlags, Class<?> lookupClass) {
1193N/A matchFlags &= ALLOWED_FLAGS;
1193N/A String matchSig = null;
1193N/A if (matchType != null) {
2040N/A matchSig = BytecodeDescriptor.unparse(matchType);
1193N/A if (matchSig.startsWith("("))
1193N/A matchFlags &= ~(ALL_KINDS & ~IS_INVOCABLE);
1193N/A else
1193N/A matchFlags &= ~(ALL_KINDS & ~IS_FIELD);
1193N/A }
1193N/A final int BUF_MAX = 0x2000;
1193N/A int len1 = matchName == null ? 10 : matchType == null ? 4 : 1;
1193N/A MemberName[] buf = newMemberBuffer(len1);
1193N/A int totalCount = 0;
1193N/A ArrayList<MemberName[]> bufs = null;
2040N/A int bufCount = 0;
1193N/A for (;;) {
2040N/A bufCount = MethodHandleNatives.getMembers(defc,
1193N/A matchName, matchSig, matchFlags,
1202N/A lookupClass,
1193N/A totalCount, buf);
1193N/A if (bufCount <= buf.length) {
2040N/A if (bufCount < 0) bufCount = 0;
2040N/A totalCount += bufCount;
1193N/A break;
1193N/A }
2040N/A // JVM returned to us with an intentional overflow!
1193N/A totalCount += buf.length;
1193N/A int excess = bufCount - buf.length;
5459N/A if (bufs == null) bufs = new ArrayList<>(1);
1193N/A bufs.add(buf);
1193N/A int len2 = buf.length;
1193N/A len2 = Math.max(len2, excess);
1193N/A len2 = Math.max(len2, totalCount / 4);
1193N/A buf = newMemberBuffer(Math.min(BUF_MAX, len2));
1193N/A }
5459N/A ArrayList<MemberName> result = new ArrayList<>(totalCount);
1193N/A if (bufs != null) {
1193N/A for (MemberName[] buf0 : bufs) {
1193N/A Collections.addAll(result, buf0);
1193N/A }
1193N/A }
2040N/A result.addAll(Arrays.asList(buf).subList(0, bufCount));
1193N/A // Signature matching is not the same as type matching, since
1193N/A // one signature might correspond to several types.
1193N/A // So if matchType is a Class or MethodType, refilter the results.
1193N/A if (matchType != null && matchType != matchSig) {
1193N/A for (Iterator<MemberName> it = result.iterator(); it.hasNext();) {
1193N/A MemberName m = it.next();
1193N/A if (!matchType.equals(m.getType()))
1193N/A it.remove();
1193N/A }
1193N/A }
1193N/A return result;
1193N/A }
2431N/A /** Produce a resolved version of the given member.
2431N/A * Super types are searched (for inherited members) if {@code searchSupers} is true.
2431N/A * Access checking is performed on behalf of the given {@code lookupClass}.
2431N/A * If lookup fails or access is not permitted, null is returned.
2431N/A * Otherwise a fresh copy of the given member is returned, with modifier bits filled in.
2431N/A */
5459N/A private MemberName resolve(byte refKind, MemberName ref, Class<?> lookupClass) {
5459N/A MemberName m = ref.clone(); // JVM will side-effect the ref
5459N/A assert(refKind == m.getReferenceKind());
5459N/A try {
5459N/A m = MethodHandleNatives.resolve(m, lookupClass);
5459N/A m.checkForTypeAlias();
5459N/A m.resolution = null;
5459N/A } catch (LinkageError ex) {
5459N/A // JVM reports that the "bytecode behavior" would get an error
5459N/A assert(!m.isResolved());
5459N/A m.resolution = ex;
5459N/A return m;
5459N/A }
5459N/A assert(m.referenceKindIsConsistent());
5459N/A m.initResolved(true);
5459N/A assert(m.vminfoIsConsistent());
5459N/A return m;
1193N/A }
2431N/A /** Produce a resolved version of the given member.
2431N/A * Super types are searched (for inherited members) if {@code searchSupers} is true.
2431N/A * Access checking is performed on behalf of the given {@code lookupClass}.
3530N/A * If lookup fails or access is not permitted, a {@linkplain ReflectiveOperationException} is thrown.
2431N/A * Otherwise a fresh copy of the given member is returned, with modifier bits filled in.
2431N/A */
3530N/A public
3530N/A <NoSuchMemberException extends ReflectiveOperationException>
5459N/A MemberName resolveOrFail(byte refKind, MemberName m, Class<?> lookupClass,
3530N/A Class<NoSuchMemberException> nsmClass)
3530N/A throws IllegalAccessException, NoSuchMemberException {
5459N/A MemberName result = resolve(refKind, m, lookupClass);
5459N/A if (result.isResolved())
1193N/A return result;
5459N/A ReflectiveOperationException ex = result.makeAccessException();
3530N/A if (ex instanceof IllegalAccessException) throw (IllegalAccessException) ex;
3530N/A throw nsmClass.cast(ex);
1193N/A }
5459N/A /** Produce a resolved version of the given member.
5459N/A * Super types are searched (for inherited members) if {@code searchSupers} is true.
5459N/A * Access checking is performed on behalf of the given {@code lookupClass}.
5459N/A * If lookup fails or access is not permitted, return null.
5459N/A * Otherwise a fresh copy of the given member is returned, with modifier bits filled in.
5459N/A */
5459N/A public
5459N/A MemberName resolveOrNull(byte refKind, MemberName m, Class<?> lookupClass) {
5459N/A MemberName result = resolve(refKind, m, lookupClass);
5459N/A if (result.isResolved())
5459N/A return result;
5459N/A return null;
5459N/A }
2431N/A /** Return a list of all methods defined by the given class.
2431N/A * Super types are searched (for inherited members) if {@code searchSupers} is true.
2431N/A * Access checking is performed on behalf of the given {@code lookupClass}.
2431N/A * Inaccessible members are not added to the last.
2431N/A */
1193N/A public List<MemberName> getMethods(Class<?> defc, boolean searchSupers,
1193N/A Class<?> lookupClass) {
1193N/A return getMethods(defc, searchSupers, null, null, lookupClass);
1193N/A }
2431N/A /** Return a list of matching methods defined by the given class.
2431N/A * Super types are searched (for inherited members) if {@code searchSupers} is true.
2431N/A * Returned methods will match the name (if not null) and the type (if not null).
2431N/A * Access checking is performed on behalf of the given {@code lookupClass}.
2431N/A * Inaccessible members are not added to the last.
2431N/A */
1193N/A public List<MemberName> getMethods(Class<?> defc, boolean searchSupers,
1193N/A String name, MethodType type, Class<?> lookupClass) {
1193N/A int matchFlags = IS_METHOD | (searchSupers ? SEARCH_ALL_SUPERS : 0);
1193N/A return getMembers(defc, name, type, matchFlags, lookupClass);
1193N/A }
2431N/A /** Return a list of all constructors defined by the given class.
2431N/A * Access checking is performed on behalf of the given {@code lookupClass}.
2431N/A * Inaccessible members are not added to the last.
2431N/A */
1193N/A public List<MemberName> getConstructors(Class<?> defc, Class<?> lookupClass) {
1193N/A return getMembers(defc, null, null, IS_CONSTRUCTOR, lookupClass);
1193N/A }
2431N/A /** Return a list of all fields defined by the given class.
2431N/A * Super types are searched (for inherited members) if {@code searchSupers} is true.
2431N/A * Access checking is performed on behalf of the given {@code lookupClass}.
2431N/A * Inaccessible members are not added to the last.
2431N/A */
1193N/A public List<MemberName> getFields(Class<?> defc, boolean searchSupers,
1193N/A Class<?> lookupClass) {
1193N/A return getFields(defc, searchSupers, null, null, lookupClass);
1193N/A }
2431N/A /** Return a list of all fields defined by the given class.
2431N/A * Super types are searched (for inherited members) if {@code searchSupers} is true.
2431N/A * Returned fields will match the name (if not null) and the type (if not null).
2431N/A * Access checking is performed on behalf of the given {@code lookupClass}.
2431N/A * Inaccessible members are not added to the last.
2431N/A */
1193N/A public List<MemberName> getFields(Class<?> defc, boolean searchSupers,
1193N/A String name, Class<?> type, Class<?> lookupClass) {
1193N/A int matchFlags = IS_FIELD | (searchSupers ? SEARCH_ALL_SUPERS : 0);
1193N/A return getMembers(defc, name, type, matchFlags, lookupClass);
1193N/A }
2431N/A /** Return a list of all nested types defined by the given class.
2431N/A * Super types are searched (for inherited members) if {@code searchSupers} is true.
2431N/A * Access checking is performed on behalf of the given {@code lookupClass}.
2431N/A * Inaccessible members are not added to the last.
2431N/A */
1193N/A public List<MemberName> getNestedTypes(Class<?> defc, boolean searchSupers,
1193N/A Class<?> lookupClass) {
1193N/A int matchFlags = IS_TYPE | (searchSupers ? SEARCH_ALL_SUPERS : 0);
1193N/A return getMembers(defc, null, null, matchFlags, lookupClass);
1193N/A }
1193N/A private static MemberName[] newMemberBuffer(int length) {
1193N/A MemberName[] buf = new MemberName[length];
1193N/A // fill the buffer with dummy structs for the JVM to fill in
1193N/A for (int i = 0; i < length; i++)
1193N/A buf[i] = new MemberName();
1193N/A return buf;
1193N/A }
1193N/A }
1193N/A
1193N/A// static {
1193N/A// System.out.println("Hello world! My methods are:");
1193N/A// System.out.println(Factory.INSTANCE.getMethods(MemberName.class, true, null));
1193N/A// }
1193N/A}