jaroslav@1692: /*
jaroslav@1692: * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
jaroslav@1692: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@1692: *
jaroslav@1692: * This code is free software; you can redistribute it and/or modify it
jaroslav@1692: * under the terms of the GNU General Public License version 2 only, as
jaroslav@1692: * published by the Free Software Foundation. Oracle designates this
jaroslav@1692: * particular file as subject to the "Classpath" exception as provided
jaroslav@1692: * by Oracle in the LICENSE file that accompanied this code.
jaroslav@1692: *
jaroslav@1692: * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@1692: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@1692: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jaroslav@1692: * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@1692: * accompanied this code).
jaroslav@1692: *
jaroslav@1692: * You should have received a copy of the GNU General Public License version
jaroslav@1692: * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@1692: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@1692: *
jaroslav@1692: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@1692: * or visit www.oracle.com if you need additional information or have any
jaroslav@1692: * questions.
jaroslav@1692: */
jaroslav@1692:
jaroslav@1692: package java.lang.invoke;
jaroslav@1692:
jaroslav@1692: import java.lang.ref.WeakReference;
jaroslav@1692: import java.lang.ref.Reference;
jaroslav@1692: import java.lang.ref.ReferenceQueue;
jaroslav@1692: import java.util.Arrays;
jaroslav@1692: import java.util.Collections;
jaroslav@1692: import java.util.List;
jaroslav@1692: import java.util.Objects;
jaroslav@1692: import java.util.concurrent.ConcurrentMap;
jaroslav@1692: import java.util.concurrent.ConcurrentHashMap;
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * A method type represents the arguments and return type accepted and
jaroslav@1692: * returned by a method handle, or the arguments and return type passed
jaroslav@1692: * and expected by a method handle caller. Method types must be properly
jaroslav@1692: * matched between a method handle and all its callers,
jaroslav@1692: * and the JVM's operations enforce this matching at, specifically
jaroslav@1692: * during calls to {@link MethodHandle#invokeExact MethodHandle.invokeExact}
jaroslav@1692: * and {@link MethodHandle#invoke MethodHandle.invoke}, and during execution
jaroslav@1692: * of {@code invokedynamic} instructions.
jaroslav@1692: *
jaroslav@1692: * The structure is a return type accompanied by any number of parameter types.
jaroslav@1692: * The types (primitive, {@code void}, and reference) are represented by {@link Class} objects.
jaroslav@1692: * (For ease of exposition, we treat {@code void} as if it were a type.
jaroslav@1692: * In fact, it denotes the absence of a return type.)
jaroslav@1692: *
jaroslav@1692: * All instances of {@code MethodType} are immutable.
jaroslav@1692: * Two instances are completely interchangeable if they compare equal.
jaroslav@1692: * Equality depends on pairwise correspondence of the return and parameter types and on nothing else.
jaroslav@1692: *
jaroslav@1692: * This type can be created only by factory methods.
jaroslav@1692: * All factory methods may cache values, though caching is not guaranteed.
jaroslav@1692: * Some factory methods are static, while others are virtual methods which
jaroslav@1692: * modify precursor method types, e.g., by changing a selected parameter.
jaroslav@1692: *
jaroslav@1692: * Factory methods which operate on groups of parameter types
jaroslav@1692: * are systematically presented in two versions, so that both Java arrays and
jaroslav@1692: * Java lists can be used to work with groups of parameter types.
jaroslav@1692: * The query methods {@code parameterArray} and {@code parameterList}
jaroslav@1692: * also provide a choice between arrays and lists.
jaroslav@1692: *
jaroslav@1692: * {@code MethodType} objects are sometimes derived from bytecode instructions
jaroslav@1692: * such as {@code invokedynamic}, specifically from the type descriptor strings associated
jaroslav@1692: * with the instructions in a class file's constant pool.
jaroslav@1692: *
jaroslav@1692: * Like classes and strings, method types can also be represented directly
jaroslav@1692: * in a class file's constant pool as constants.
jaroslav@1692: * A method type may be loaded by an {@code ldc} instruction which refers
jaroslav@1692: * to a suitable {@code CONSTANT_MethodType} constant pool entry.
jaroslav@1692: * The entry refers to a {@code CONSTANT_Utf8} spelling for the descriptor string.
jaroslav@1692: * (For full details on method type constants,
jaroslav@1692: * see sections 4.4.8 and 5.4.3.5 of the Java Virtual Machine Specification.)
jaroslav@1692: *
jaroslav@1692: * When the JVM materializes a {@code MethodType} from a descriptor string,
jaroslav@1692: * all classes named in the descriptor must be accessible, and will be loaded.
jaroslav@1692: * (But the classes need not be initialized, as is the case with a {@code CONSTANT_Class}.)
jaroslav@1692: * This loading may occur at any time before the {@code MethodType} object is first derived.
jaroslav@1692: * @author John Rose, JSR 292 EG
jaroslav@1692: */
jaroslav@1692: public final
jaroslav@1692: class MethodType implements java.io.Serializable {
jaroslav@1692: private static final long serialVersionUID = 292L; // {rtype, {ptype...}}
jaroslav@1692:
jaroslav@1692: // The rtype and ptypes fields define the structural identity of the method type:
jaroslav@1692: private final Class> rtype;
jaroslav@1692: private final Class>[] ptypes;
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Check the given parameters for validity and store them into the final fields.
jaroslav@1692: */
jaroslav@1692: private MethodType(Class> rtype, Class>[] ptypes, boolean trusted) {
jaroslav@1692: this.rtype = rtype;
jaroslav@1692: // defensively copy the array passed in by the user
jaroslav@1692: this.ptypes = trusted ? ptypes : Arrays.copyOf(ptypes, ptypes.length);
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Construct a temporary unchecked instance of MethodType for use only as a key to the intern table.
jaroslav@1692: * Does not check the given parameters for validity, and must be discarded after it is used as a searching key.
jaroslav@1692: * The parameters are reversed for this constructor, so that is is not accidentally used.
jaroslav@1692: */
jaroslav@1692: private MethodType(Class>[] ptypes, Class> rtype) {
jaroslav@1692: this.rtype = rtype;
jaroslav@1692: this.ptypes = ptypes;
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /** This number, mandated by the JVM spec as 255,
jaroslav@1692: * is the maximum number of slots
jaroslav@1692: * that any Java method can receive in its argument list.
jaroslav@1692: * It limits both JVM signatures and method type objects.
jaroslav@1692: * The longest possible invocation will look like
jaroslav@1692: * {@code staticMethod(arg1, arg2, ..., arg255)} or
jaroslav@1692: * {@code x.virtualMethod(arg1, arg2, ..., arg254)}.
jaroslav@1692: */
jaroslav@1692: /*non-public*/ static final int MAX_JVM_ARITY = 255; // this is mandated by the JVM spec.
jaroslav@1692:
jaroslav@1692: /** This number is the maximum arity of a method handle, 254.
jaroslav@1692: * It is derived from the absolute JVM-imposed arity by subtracting one,
jaroslav@1692: * which is the slot occupied by the method handle itself at the
jaroslav@1692: * beginning of the argument list used to invoke the method handle.
jaroslav@1692: * The longest possible invocation will look like
jaroslav@1692: * {@code mh.invoke(arg1, arg2, ..., arg254)}.
jaroslav@1692: */
jaroslav@1692: // Issue: Should we allow MH.invokeWithArguments to go to the full 255?
jaroslav@1692: /*non-public*/ static final int MAX_MH_ARITY = MAX_JVM_ARITY-1; // deduct one for mh receiver
jaroslav@1692:
jaroslav@1692: /** This number is the maximum arity of a method handle invoker, 253.
jaroslav@1692: * It is derived from the absolute JVM-imposed arity by subtracting two,
jaroslav@1692: * which are the slots occupied by invoke method handle, and the
jaroslav@1692: * target method handle, which are both at the beginning of the argument
jaroslav@1692: * list used to invoke the target method handle.
jaroslav@1692: * The longest possible invocation will look like
jaroslav@1692: * {@code invokermh.invoke(targetmh, arg1, arg2, ..., arg253)}.
jaroslav@1692: */
jaroslav@1692: /*non-public*/ static final int MAX_MH_INVOKER_ARITY = MAX_MH_ARITY-1; // deduct one more for invoker
jaroslav@1692:
jaroslav@1692: private static void checkRtype(Class> rtype) {
jaroslav@1692: Objects.requireNonNull(rtype);
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: static final ConcurrentWeakInternSet internTable = new ConcurrentWeakInternSet<>();
jaroslav@1692:
jaroslav@1692: static final Class>[] NO_PTYPES = {};
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Finds or creates an instance of the given method type.
jaroslav@1692: * @param rtype the return type
jaroslav@1692: * @param ptypes the parameter types
jaroslav@1692: * @return a method type with the given components
jaroslav@1692: * @throws NullPointerException if {@code rtype} or {@code ptypes} or any element of {@code ptypes} is null
jaroslav@1692: * @throws IllegalArgumentException if any element of {@code ptypes} is {@code void.class}
jaroslav@1692: */
jaroslav@1692: public static
jaroslav@1692: MethodType methodType(Class> rtype, Class>[] ptypes) {
jaroslav@1692: return makeImpl(rtype, ptypes, false);
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Finds or creates a method type with the given components.
jaroslav@1692: * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692: * @param rtype the return type
jaroslav@1692: * @param ptypes the parameter types
jaroslav@1692: * @return a method type with the given components
jaroslav@1692: * @throws NullPointerException if {@code rtype} or {@code ptypes} or any element of {@code ptypes} is null
jaroslav@1692: * @throws IllegalArgumentException if any element of {@code ptypes} is {@code void.class}
jaroslav@1692: */
jaroslav@1692: public static
jaroslav@1692: MethodType methodType(Class> rtype, List> ptypes) {
jaroslav@1692: boolean notrust = false; // random List impl. could return evil ptypes array
jaroslav@1692: return makeImpl(rtype, listToArray(ptypes), notrust);
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: private static Class>[] listToArray(List> ptypes) {
jaroslav@1692: return ptypes.toArray(NO_PTYPES);
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Finds or creates a method type with the given components.
jaroslav@1692: * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692: * The leading parameter type is prepended to the remaining array.
jaroslav@1692: * @param rtype the return type
jaroslav@1692: * @param ptype0 the first parameter type
jaroslav@1692: * @param ptypes the remaining parameter types
jaroslav@1692: * @return a method type with the given components
jaroslav@1692: * @throws NullPointerException if {@code rtype} or {@code ptype0} or {@code ptypes} or any element of {@code ptypes} is null
jaroslav@1692: * @throws IllegalArgumentException if {@code ptype0} or {@code ptypes} or any element of {@code ptypes} is {@code void.class}
jaroslav@1692: */
jaroslav@1692: public static
jaroslav@1692: MethodType methodType(Class> rtype, Class> ptype0, Class>... ptypes) {
jaroslav@1692: Class>[] ptypes1 = new Class>[1+ptypes.length];
jaroslav@1692: ptypes1[0] = ptype0;
jaroslav@1692: System.arraycopy(ptypes, 0, ptypes1, 1, ptypes.length);
jaroslav@1692: return makeImpl(rtype, ptypes1, true);
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Finds or creates a method type with the given components.
jaroslav@1692: * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692: * The resulting method has no parameter types.
jaroslav@1692: * @param rtype the return type
jaroslav@1692: * @return a method type with the given return value
jaroslav@1692: * @throws NullPointerException if {@code rtype} is null
jaroslav@1692: */
jaroslav@1692: public static
jaroslav@1692: MethodType methodType(Class> rtype) {
jaroslav@1692: return makeImpl(rtype, NO_PTYPES, true);
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Finds or creates a method type with the given components.
jaroslav@1692: * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692: * The resulting method has the single given parameter type.
jaroslav@1692: * @param rtype the return type
jaroslav@1692: * @param ptype0 the parameter type
jaroslav@1692: * @return a method type with the given return value and parameter type
jaroslav@1692: * @throws NullPointerException if {@code rtype} or {@code ptype0} is null
jaroslav@1692: * @throws IllegalArgumentException if {@code ptype0} is {@code void.class}
jaroslav@1692: */
jaroslav@1692: public static
jaroslav@1692: MethodType methodType(Class> rtype, Class> ptype0) {
jaroslav@1692: return makeImpl(rtype, new Class>[]{ ptype0 }, true);
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Finds or creates a method type with the given components.
jaroslav@1692: * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692: * The resulting method has the same parameter types as {@code ptypes},
jaroslav@1692: * and the specified return type.
jaroslav@1692: * @param rtype the return type
jaroslav@1692: * @param ptypes the method type which supplies the parameter types
jaroslav@1692: * @return a method type with the given components
jaroslav@1692: * @throws NullPointerException if {@code rtype} or {@code ptypes} is null
jaroslav@1692: */
jaroslav@1692: public static
jaroslav@1692: MethodType methodType(Class> rtype, MethodType ptypes) {
jaroslav@1692: return makeImpl(rtype, ptypes.ptypes, true);
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Sole factory method to find or create an interned method type.
jaroslav@1692: * @param rtype desired return type
jaroslav@1692: * @param ptypes desired parameter types
jaroslav@1692: * @param trusted whether the ptypes can be used without cloning
jaroslav@1692: * @return the unique method type of the desired structure
jaroslav@1692: */
jaroslav@1692: /*trusted*/ static
jaroslav@1692: MethodType makeImpl(Class> rtype, Class>[] ptypes, boolean trusted) {
jaroslav@1692: throw new IllegalStateException();
jaroslav@1692: }
jaroslav@1692: private static final MethodType[] objectOnlyTypes = new MethodType[20];
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Finds or creates a method type whose components are {@code Object} with an optional trailing {@code Object[]} array.
jaroslav@1692: * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692: * All parameters and the return type will be {@code Object},
jaroslav@1692: * except the final array parameter if any, which will be {@code Object[]}.
jaroslav@1692: * @param objectArgCount number of parameters (excluding the final array parameter if any)
jaroslav@1692: * @param finalArray whether there will be a trailing array parameter, of type {@code Object[]}
jaroslav@1692: * @return a generally applicable method type, for all calls of the given fixed argument count and a collected array of further arguments
jaroslav@1692: * @throws IllegalArgumentException if {@code objectArgCount} is negative or greater than 255 (or 254, if {@code finalArray} is true)
jaroslav@1692: * @see #genericMethodType(int)
jaroslav@1692: */
jaroslav@1692: public static
jaroslav@1692: MethodType genericMethodType(int objectArgCount, boolean finalArray) {
jaroslav@1692: throw new IllegalStateException();
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Finds or creates a method type whose components are all {@code Object}.
jaroslav@1692: * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692: * All parameters and the return type will be Object.
jaroslav@1692: * @param objectArgCount number of parameters
jaroslav@1692: * @return a generally applicable method type, for all calls of the given argument count
jaroslav@1692: * @throws IllegalArgumentException if {@code objectArgCount} is negative or greater than 255
jaroslav@1692: * @see #genericMethodType(int, boolean)
jaroslav@1692: */
jaroslav@1692: public static
jaroslav@1692: MethodType genericMethodType(int objectArgCount) {
jaroslav@1692: return genericMethodType(objectArgCount, false);
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Finds or creates a method type with a single different parameter type.
jaroslav@1692: * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692: * @param num the index (zero-based) of the parameter type to change
jaroslav@1692: * @param nptype a new parameter type to replace the old one with
jaroslav@1692: * @return the same type, except with the selected parameter changed
jaroslav@1692: * @throws IndexOutOfBoundsException if {@code num} is not a valid index into {@code parameterArray()}
jaroslav@1692: * @throws IllegalArgumentException if {@code nptype} is {@code void.class}
jaroslav@1692: * @throws NullPointerException if {@code nptype} is null
jaroslav@1692: */
jaroslav@1692: public MethodType changeParameterType(int num, Class> nptype) {
jaroslav@1692: throw new IllegalStateException();
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Finds or creates a method type with additional parameter types.
jaroslav@1692: * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692: * @param num the position (zero-based) of the inserted parameter type(s)
jaroslav@1692: * @param ptypesToInsert zero or more new parameter types to insert into the parameter list
jaroslav@1692: * @return the same type, except with the selected parameter(s) inserted
jaroslav@1692: * @throws IndexOutOfBoundsException if {@code num} is negative or greater than {@code parameterCount()}
jaroslav@1692: * @throws IllegalArgumentException if any element of {@code ptypesToInsert} is {@code void.class}
jaroslav@1692: * or if the resulting method type would have more than 255 parameter slots
jaroslav@1692: * @throws NullPointerException if {@code ptypesToInsert} or any of its elements is null
jaroslav@1692: */
jaroslav@1692: public MethodType insertParameterTypes(int num, Class>... ptypesToInsert) {
jaroslav@1692: throw new IllegalStateException();
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Finds or creates a method type with additional parameter types.
jaroslav@1692: * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692: * @param ptypesToInsert zero or more new parameter types to insert after the end of the parameter list
jaroslav@1692: * @return the same type, except with the selected parameter(s) appended
jaroslav@1692: * @throws IllegalArgumentException if any element of {@code ptypesToInsert} is {@code void.class}
jaroslav@1692: * or if the resulting method type would have more than 255 parameter slots
jaroslav@1692: * @throws NullPointerException if {@code ptypesToInsert} or any of its elements is null
jaroslav@1692: */
jaroslav@1692: public MethodType appendParameterTypes(Class>... ptypesToInsert) {
jaroslav@1692: return insertParameterTypes(parameterCount(), ptypesToInsert);
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Finds or creates a method type with additional parameter types.
jaroslav@1692: * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692: * @param num the position (zero-based) of the inserted parameter type(s)
jaroslav@1692: * @param ptypesToInsert zero or more new parameter types to insert into the parameter list
jaroslav@1692: * @return the same type, except with the selected parameter(s) inserted
jaroslav@1692: * @throws IndexOutOfBoundsException if {@code num} is negative or greater than {@code parameterCount()}
jaroslav@1692: * @throws IllegalArgumentException if any element of {@code ptypesToInsert} is {@code void.class}
jaroslav@1692: * or if the resulting method type would have more than 255 parameter slots
jaroslav@1692: * @throws NullPointerException if {@code ptypesToInsert} or any of its elements is null
jaroslav@1692: */
jaroslav@1692: public MethodType insertParameterTypes(int num, List> ptypesToInsert) {
jaroslav@1692: return insertParameterTypes(num, listToArray(ptypesToInsert));
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Finds or creates a method type with additional parameter types.
jaroslav@1692: * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692: * @param ptypesToInsert zero or more new parameter types to insert after the end of the parameter list
jaroslav@1692: * @return the same type, except with the selected parameter(s) appended
jaroslav@1692: * @throws IllegalArgumentException if any element of {@code ptypesToInsert} is {@code void.class}
jaroslav@1692: * or if the resulting method type would have more than 255 parameter slots
jaroslav@1692: * @throws NullPointerException if {@code ptypesToInsert} or any of its elements is null
jaroslav@1692: */
jaroslav@1692: public MethodType appendParameterTypes(List> ptypesToInsert) {
jaroslav@1692: return insertParameterTypes(parameterCount(), ptypesToInsert);
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Finds or creates a method type with modified parameter types.
jaroslav@1692: * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692: * @param start the position (zero-based) of the first replaced parameter type(s)
jaroslav@1692: * @param end the position (zero-based) after the last replaced parameter type(s)
jaroslav@1692: * @param ptypesToInsert zero or more new parameter types to insert into the parameter list
jaroslav@1692: * @return the same type, except with the selected parameter(s) replaced
jaroslav@1692: * @throws IndexOutOfBoundsException if {@code start} is negative or greater than {@code parameterCount()}
jaroslav@1692: * or if {@code end} is negative or greater than {@code parameterCount()}
jaroslav@1692: * or if {@code start} is greater than {@code end}
jaroslav@1692: * @throws IllegalArgumentException if any element of {@code ptypesToInsert} is {@code void.class}
jaroslav@1692: * or if the resulting method type would have more than 255 parameter slots
jaroslav@1692: * @throws NullPointerException if {@code ptypesToInsert} or any of its elements is null
jaroslav@1692: */
jaroslav@1692: /*non-public*/ MethodType replaceParameterTypes(int start, int end, Class>... ptypesToInsert) {
jaroslav@1692: throw new IllegalStateException();
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Finds or creates a method type with some parameter types omitted.
jaroslav@1692: * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692: * @param start the index (zero-based) of the first parameter type to remove
jaroslav@1692: * @param end the index (greater than {@code start}) of the first parameter type after not to remove
jaroslav@1692: * @return the same type, except with the selected parameter(s) removed
jaroslav@1692: * @throws IndexOutOfBoundsException if {@code start} is negative or greater than {@code parameterCount()}
jaroslav@1692: * or if {@code end} is negative or greater than {@code parameterCount()}
jaroslav@1692: * or if {@code start} is greater than {@code end}
jaroslav@1692: */
jaroslav@1692: public MethodType dropParameterTypes(int start, int end) {
jaroslav@1692: throw new IllegalStateException();
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Finds or creates a method type with a different return type.
jaroslav@1692: * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692: * @param nrtype a return parameter type to replace the old one with
jaroslav@1692: * @return the same type, except with the return type change
jaroslav@1692: * @throws NullPointerException if {@code nrtype} is null
jaroslav@1692: */
jaroslav@1692: public MethodType changeReturnType(Class> nrtype) {
jaroslav@1692: throw new IllegalStateException();
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Reports if this type contains a primitive argument or return value.
jaroslav@1692: * The return type {@code void} counts as a primitive.
jaroslav@1692: * @return true if any of the types are primitives
jaroslav@1692: */
jaroslav@1692: public boolean hasPrimitives() {
jaroslav@1692: throw new IllegalStateException();
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Reports if this type contains a wrapper argument or return value.
jaroslav@1692: * Wrappers are types which box primitive values, such as {@link Integer}.
jaroslav@1692: * The reference type {@code java.lang.Void} counts as a wrapper,
jaroslav@1692: * if it occurs as a return type.
jaroslav@1692: * @return true if any of the types are wrappers
jaroslav@1692: */
jaroslav@1692: public boolean hasWrappers() {
jaroslav@1692: return unwrap() != this;
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Erases all reference types to {@code Object}.
jaroslav@1692: * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692: * All primitive types (including {@code void}) will remain unchanged.
jaroslav@1692: * @return a version of the original type with all reference types replaced
jaroslav@1692: */
jaroslav@1692: public MethodType erase() {
jaroslav@1692: throw new IllegalStateException();
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Erases all reference types to {@code Object}, and all subword types to {@code int}.
jaroslav@1692: * This is the reduced type polymorphism used by private methods
jaroslav@1692: * such as {@link MethodHandle#invokeBasic invokeBasic}.
jaroslav@1692: * @return a version of the original type with all reference and subword types replaced
jaroslav@1692: */
jaroslav@1692: /*non-public*/ MethodType basicType() {
jaroslav@1692: throw new IllegalStateException();
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * @return a version of the original type with MethodHandle prepended as the first argument
jaroslav@1692: */
jaroslav@1692: /*non-public*/ MethodType invokerType() {
jaroslav@1692: throw new IllegalStateException();
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Converts all types, both reference and primitive, to {@code Object}.
jaroslav@1692: * Convenience method for {@link #genericMethodType(int) genericMethodType}.
jaroslav@1692: * The expression {@code type.wrap().erase()} produces the same value
jaroslav@1692: * as {@code type.generic()}.
jaroslav@1692: * @return a version of the original type with all types replaced
jaroslav@1692: */
jaroslav@1692: public MethodType generic() {
jaroslav@1692: return genericMethodType(parameterCount());
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Converts all primitive types to their corresponding wrapper types.
jaroslav@1692: * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692: * All reference types (including wrapper types) will remain unchanged.
jaroslav@1692: * A {@code void} return type is changed to the type {@code java.lang.Void}.
jaroslav@1692: * The expression {@code type.wrap().erase()} produces the same value
jaroslav@1692: * as {@code type.generic()}.
jaroslav@1692: * @return a version of the original type with all primitive types replaced
jaroslav@1692: */
jaroslav@1692: public MethodType wrap() {
jaroslav@1692: return hasPrimitives() ? wrapWithPrims(this) : this;
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Converts all wrapper types to their corresponding primitive types.
jaroslav@1692: * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692: * All primitive types (including {@code void}) will remain unchanged.
jaroslav@1692: * A return type of {@code java.lang.Void} is changed to {@code void}.
jaroslav@1692: * @return a version of the original type with all wrapper types replaced
jaroslav@1692: */
jaroslav@1692: public MethodType unwrap() {
jaroslav@1692: MethodType noprims = !hasPrimitives() ? this : wrapWithPrims(this);
jaroslav@1692: return unwrapWithNoPrims(noprims);
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: private static MethodType wrapWithPrims(MethodType pt) {
jaroslav@1692: throw new IllegalStateException();
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: private static MethodType unwrapWithNoPrims(MethodType wt) {
jaroslav@1692: throw new IllegalStateException();
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Returns the parameter type at the specified index, within this method type.
jaroslav@1692: * @param num the index (zero-based) of the desired parameter type
jaroslav@1692: * @return the selected parameter type
jaroslav@1692: * @throws IndexOutOfBoundsException if {@code num} is not a valid index into {@code parameterArray()}
jaroslav@1692: */
jaroslav@1692: public Class> parameterType(int num) {
jaroslav@1692: return ptypes[num];
jaroslav@1692: }
jaroslav@1692: /**
jaroslav@1692: * Returns the number of parameter types in this method type.
jaroslav@1692: * @return the number of parameter types
jaroslav@1692: */
jaroslav@1692: public int parameterCount() {
jaroslav@1692: return ptypes.length;
jaroslav@1692: }
jaroslav@1692: /**
jaroslav@1692: * Returns the return type of this method type.
jaroslav@1692: * @return the return type
jaroslav@1692: */
jaroslav@1692: public Class> returnType() {
jaroslav@1692: return rtype;
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Presents the parameter types as a list (a convenience method).
jaroslav@1692: * The list will be immutable.
jaroslav@1692: * @return the parameter types (as an immutable list)
jaroslav@1692: */
jaroslav@1692: public List> parameterList() {
jaroslav@1692: return Collections.unmodifiableList(Arrays.asList(ptypes));
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /*non-public*/ Class> lastParameterType() {
jaroslav@1692: int len = ptypes.length;
jaroslav@1692: return len == 0 ? void.class : ptypes[len-1];
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Presents the parameter types as an array (a convenience method).
jaroslav@1692: * Changes to the array will not result in changes to the type.
jaroslav@1692: * @return the parameter types (as a fresh copy if necessary)
jaroslav@1692: */
jaroslav@1692: public Class>[] parameterArray() {
jaroslav@1692: return ptypes.clone();
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Compares the specified object with this type for equality.
jaroslav@1692: * That is, it returns true if and only if the specified object
jaroslav@1692: * is also a method type with exactly the same parameters and return type.
jaroslav@1692: * @param x object to compare
jaroslav@1692: * @see Object#equals(Object)
jaroslav@1692: */
jaroslav@1692: @Override
jaroslav@1692: public boolean equals(Object x) {
jaroslav@1692: return this == x || x instanceof MethodType && equals((MethodType)x);
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: private boolean equals(MethodType that) {
jaroslav@1692: return this.rtype == that.rtype
jaroslav@1692: && Arrays.equals(this.ptypes, that.ptypes);
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Returns the hash code value for this method type.
jaroslav@1692: * It is defined to be the same as the hashcode of a List
jaroslav@1692: * whose elements are the return type followed by the
jaroslav@1692: * parameter types.
jaroslav@1692: * @return the hash code value for this method type
jaroslav@1692: * @see Object#hashCode()
jaroslav@1692: * @see #equals(Object)
jaroslav@1692: * @see List#hashCode()
jaroslav@1692: */
jaroslav@1692: @Override
jaroslav@1692: public int hashCode() {
jaroslav@1692: int hashCode = 31 + rtype.hashCode();
jaroslav@1692: for (Class> ptype : ptypes)
jaroslav@1692: hashCode = 31*hashCode + ptype.hashCode();
jaroslav@1692: return hashCode;
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Returns a string representation of the method type,
jaroslav@1692: * of the form {@code "(PT0,PT1...)RT"}.
jaroslav@1692: * The string representation of a method type is a
jaroslav@1692: * parenthesis enclosed, comma separated list of type names,
jaroslav@1692: * followed immediately by the return type.
jaroslav@1692: *
jaroslav@1692: * Each type is represented by its
jaroslav@1692: * {@link java.lang.Class#getSimpleName simple name}.
jaroslav@1692: */
jaroslav@1692: @Override
jaroslav@1692: public String toString() {
jaroslav@1692: StringBuilder sb = new StringBuilder();
jaroslav@1692: sb.append("(");
jaroslav@1692: for (int i = 0; i < ptypes.length; i++) {
jaroslav@1692: if (i > 0) sb.append(",");
jaroslav@1692: sb.append(ptypes[i].getSimpleName());
jaroslav@1692: }
jaroslav@1692: sb.append(")");
jaroslav@1692: sb.append(rtype.getSimpleName());
jaroslav@1692: return sb.toString();
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Finds or creates an instance of a method type, given the spelling of its bytecode descriptor.
jaroslav@1692: * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692: * Any class or interface name embedded in the descriptor string
jaroslav@1692: * will be resolved by calling {@link ClassLoader#loadClass(java.lang.String)}
jaroslav@1692: * on the given loader (or if it is null, on the system class loader).
jaroslav@1692: *
jaroslav@1692: * Note that it is possible to encounter method types which cannot be
jaroslav@1692: * constructed by this method, because their component types are
jaroslav@1692: * not all reachable from a common class loader.
jaroslav@1692: *
jaroslav@1692: * This method is included for the benefit of applications that must
jaroslav@1692: * generate bytecodes that process method handles and {@code invokedynamic}.
jaroslav@1692: * @param descriptor a bytecode-level type descriptor string "(T...)T"
jaroslav@1692: * @param loader the class loader in which to look up the types
jaroslav@1692: * @return a method type matching the bytecode-level type descriptor
jaroslav@1692: * @throws NullPointerException if the string is null
jaroslav@1692: * @throws IllegalArgumentException if the string is not well-formed
jaroslav@1692: * @throws TypeNotPresentException if a named type cannot be found
jaroslav@1692: */
jaroslav@1692: public static MethodType fromMethodDescriptorString(String descriptor, ClassLoader loader)
jaroslav@1692: throws IllegalArgumentException
jaroslav@1692: {
jaroslav@1692: throw new IllegalStateException();
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Produces a bytecode descriptor representation of the method type.
jaroslav@1692: *
jaroslav@1692: * Note that this is not a strict inverse of {@link #fromMethodDescriptorString fromMethodDescriptorString}.
jaroslav@1692: * Two distinct classes which share a common name but have different class loaders
jaroslav@1692: * will appear identical when viewed within descriptor strings.
jaroslav@1692: *
jaroslav@1692: * This method is included for the benefit of applications that must
jaroslav@1692: * generate bytecodes that process method handles and {@code invokedynamic}.
jaroslav@1692: * {@link #fromMethodDescriptorString(java.lang.String, java.lang.ClassLoader) fromMethodDescriptorString},
jaroslav@1692: * because the latter requires a suitable class loader argument.
jaroslav@1692: * @return the bytecode type descriptor representation
jaroslav@1692: */
jaroslav@1692: public String toMethodDescriptorString() {
jaroslav@1692: throw new IllegalStateException();
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /// Serialization.
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * There are no serializable fields for {@code MethodType}.
jaroslav@1692: */
jaroslav@1692: private static final java.io.ObjectStreamField[] serialPersistentFields = { };
jaroslav@1692:
jaroslav@1692: // /**
jaroslav@1692: // * Save the {@code MethodType} instance to a stream.
jaroslav@1692: // *
jaroslav@1692: // * @serialData
jaroslav@1692: // * For portability, the serialized format does not refer to named fields.
jaroslav@1692: // * Instead, the return type and parameter type arrays are written directly
jaroslav@1692: // * from the {@code writeObject} method, using two calls to {@code s.writeObject}
jaroslav@1692: // * as follows:
jaroslav@1692: // *
{@code
jaroslav@1692: //s.writeObject(this.returnType());
jaroslav@1692: //s.writeObject(this.parameterArray());
jaroslav@1692: // * }
jaroslav@1692: // *
jaroslav@1692: // * The deserialized field values are checked as if they were
jaroslav@1692: // * provided to the factory method {@link #methodType(Class,Class[]) methodType}.
jaroslav@1692: // * For example, null values, or {@code void} parameter types,
jaroslav@1692: // * will lead to exceptions during deserialization.
jaroslav@1692: // * @param s the stream to write the object to
jaroslav@1692: // * @throws java.io.IOException if there is a problem writing the object
jaroslav@1692: // */
jaroslav@1692: // private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException {
jaroslav@1692: // s.defaultWriteObject(); // requires serialPersistentFields to be an empty array
jaroslav@1692: // s.writeObject(returnType());
jaroslav@1692: // s.writeObject(parameterArray());
jaroslav@1692: // }
jaroslav@1692: //
jaroslav@1692: // /**
jaroslav@1692: // * Reconstitute the {@code MethodType} instance from a stream (that is,
jaroslav@1692: // * deserialize it).
jaroslav@1692: // * This instance is a scratch object with bogus final fields.
jaroslav@1692: // * It provides the parameters to the factory method called by
jaroslav@1692: // * {@link #readResolve readResolve}.
jaroslav@1692: // * After that call it is discarded.
jaroslav@1692: // * @param s the stream to read the object from
jaroslav@1692: // * @throws java.io.IOException if there is a problem reading the object
jaroslav@1692: // * @throws ClassNotFoundException if one of the component classes cannot be resolved
jaroslav@1692: // * @see #MethodType()
jaroslav@1692: // * @see #readResolve
jaroslav@1692: // * @see #writeObject
jaroslav@1692: // */
jaroslav@1692: // private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException {
jaroslav@1692: // s.defaultReadObject(); // requires serialPersistentFields to be an empty array
jaroslav@1692: //
jaroslav@1692: // Class> returnType = (Class>) s.readObject();
jaroslav@1692: // Class>[] parameterArray = (Class>[]) s.readObject();
jaroslav@1692: //
jaroslav@1692: // // Probably this object will never escape, but let's check
jaroslav@1692: // // the field values now, just to be sure.
jaroslav@1692: // checkRtype(returnType);
jaroslav@1692: // checkPtypes(parameterArray);
jaroslav@1692: //
jaroslav@1692: // parameterArray = parameterArray.clone(); // make sure it is unshared
jaroslav@1692: // MethodType_init(returnType, parameterArray);
jaroslav@1692: // }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * For serialization only.
jaroslav@1692: * Sets the final fields to null, pending {@code Unsafe.putObject}.
jaroslav@1692: */
jaroslav@1692: private MethodType() {
jaroslav@1692: this.rtype = null;
jaroslav@1692: this.ptypes = null;
jaroslav@1692: }
jaroslav@1692: // private void MethodType_init(Class> rtype, Class>[] ptypes) {
jaroslav@1692: // // In order to communicate these values to readResolve, we must
jaroslav@1692: // // store them into the implementation-specific final fields.
jaroslav@1692: // checkRtype(rtype);
jaroslav@1692: // checkPtypes(ptypes);
jaroslav@1692: // UNSAFE.putObject(this, rtypeOffset, rtype);
jaroslav@1692: // UNSAFE.putObject(this, ptypesOffset, ptypes);
jaroslav@1692: // }
jaroslav@1692:
jaroslav@1692: // Support for resetting final fields while deserializing
jaroslav@1692: // private static final long rtypeOffset, ptypesOffset;
jaroslav@1692: // static {
jaroslav@1692: // try {
jaroslav@1692: // rtypeOffset = UNSAFE.objectFieldOffset
jaroslav@1692: // (MethodType.class.getDeclaredField("rtype"));
jaroslav@1692: // ptypesOffset = UNSAFE.objectFieldOffset
jaroslav@1692: // (MethodType.class.getDeclaredField("ptypes"));
jaroslav@1692: // } catch (Exception ex) {
jaroslav@1692: // throw new Error(ex);
jaroslav@1692: // }
jaroslav@1692: // }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Resolves and initializes a {@code MethodType} object
jaroslav@1692: * after serialization.
jaroslav@1692: * @return the fully initialized {@code MethodType} object
jaroslav@1692: */
jaroslav@1692: private Object readResolve() {
jaroslav@1692: // Do not use a trusted path for deserialization:
jaroslav@1692: //return makeImpl(rtype, ptypes, true);
jaroslav@1692: // Verify all operands, and make sure ptypes is unshared:
jaroslav@1692: return methodType(rtype, ptypes);
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Simple implementation of weak concurrent intern set.
jaroslav@1692: *
jaroslav@1692: * @param interned type
jaroslav@1692: */
jaroslav@1692: private static class ConcurrentWeakInternSet {
jaroslav@1692:
jaroslav@1692: private final ConcurrentMap, WeakEntry> map;
jaroslav@1692: private final ReferenceQueue stale;
jaroslav@1692:
jaroslav@1692: public ConcurrentWeakInternSet() {
jaroslav@1692: this.map = new ConcurrentHashMap<>();
jaroslav@1692: this.stale = new ReferenceQueue<>();
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Get the existing interned element.
jaroslav@1692: * This method returns null if no element is interned.
jaroslav@1692: *
jaroslav@1692: * @param elem element to look up
jaroslav@1692: * @return the interned element
jaroslav@1692: */
jaroslav@1692: public T get(T elem) {
jaroslav@1692: if (elem == null) throw new NullPointerException();
jaroslav@1692: expungeStaleElements();
jaroslav@1692:
jaroslav@1692: WeakEntry value = map.get(new WeakEntry<>(elem));
jaroslav@1692: if (value != null) {
jaroslav@1692: T res = value.get();
jaroslav@1692: if (res != null) {
jaroslav@1692: return res;
jaroslav@1692: }
jaroslav@1692: }
jaroslav@1692: return null;
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: /**
jaroslav@1692: * Interns the element.
jaroslav@1692: * Always returns non-null element, matching the one in the intern set.
jaroslav@1692: * Under the race against another add(), it can return different
jaroslav@1692: * element, if another thread beats us to interning it.
jaroslav@1692: *
jaroslav@1692: * @param elem element to add
jaroslav@1692: * @return element that was actually added
jaroslav@1692: */
jaroslav@1692: public T add(T elem) {
jaroslav@1692: if (elem == null) throw new NullPointerException();
jaroslav@1692:
jaroslav@1692: // Playing double race here, and so spinloop is required.
jaroslav@1692: // First race is with two concurrent updaters.
jaroslav@1692: // Second race is with GC purging weak ref under our feet.
jaroslav@1692: // Hopefully, we almost always end up with a single pass.
jaroslav@1692: T interned;
jaroslav@1692: WeakEntry e = new WeakEntry<>(elem, stale);
jaroslav@1692: do {
jaroslav@1692: expungeStaleElements();
jaroslav@1692: WeakEntry exist = map.putIfAbsent(e, e);
jaroslav@1692: interned = (exist == null) ? elem : exist.get();
jaroslav@1692: } while (interned == null);
jaroslav@1692: return interned;
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: private void expungeStaleElements() {
jaroslav@1692: Reference extends T> reference;
jaroslav@1692: while ((reference = stale.poll()) != null) {
jaroslav@1692: map.remove(reference);
jaroslav@1692: }
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: private static class WeakEntry extends WeakReference {
jaroslav@1692:
jaroslav@1692: public final int hashcode;
jaroslav@1692:
jaroslav@1692: public WeakEntry(T key, ReferenceQueue queue) {
jaroslav@1692: super(key, queue);
jaroslav@1692: hashcode = key.hashCode();
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: public WeakEntry(T key) {
jaroslav@1692: super(key);
jaroslav@1692: hashcode = key.hashCode();
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: @Override
jaroslav@1692: public boolean equals(Object obj) {
jaroslav@1692: if (obj instanceof WeakEntry) {
jaroslav@1692: Object that = ((WeakEntry) obj).get();
jaroslav@1692: Object mine = get();
jaroslav@1692: return (that == null || mine == null) ? (this == obj) : mine.equals(that);
jaroslav@1692: }
jaroslav@1692: return false;
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: @Override
jaroslav@1692: public int hashCode() {
jaroslav@1692: return hashcode;
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: }
jaroslav@1692: }
jaroslav@1692:
jaroslav@1692: }