1 /*
   2  * Copyright (c) 2018, 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 package java.lang.constant;
  26 
  27 import java.lang.invoke.MethodHandle;
  28 import java.lang.invoke.MethodHandleInfo;
  29 import java.util.OptionalInt;
  30 import java.util.stream.Stream;
  31 
  32 import jdk.internal.vm.annotation.Stable;
  33 
  34 import static java.lang.invoke.MethodHandleInfo.REF_getField;
  35 import static java.lang.invoke.MethodHandleInfo.REF_getStatic;
  36 import static java.lang.invoke.MethodHandleInfo.REF_invokeInterface;
  37 import static java.lang.invoke.MethodHandleInfo.REF_invokeSpecial;
  38 import static java.lang.invoke.MethodHandleInfo.REF_invokeStatic;
  39 import static java.lang.invoke.MethodHandleInfo.REF_invokeVirtual;
  40 import static java.lang.invoke.MethodHandleInfo.REF_newInvokeSpecial;
  41 import static java.lang.invoke.MethodHandleInfo.REF_putField;
  42 import static java.lang.invoke.MethodHandleInfo.REF_putStatic;
  43 
  44 /**
  45  * A <a href="package-summary.html#nominal">nominal descriptor</a> for a direct
  46  * {@link MethodHandle}.  A {@linkplain DirectMethodHandleDescImpl} corresponds to
  47  * a {@code Constant_MethodHandle_info} entry in the constant pool of a classfile.
  48  *
  49  * @apiNote In the future, if the Java language permits, {@linkplain DirectMethodHandleDesc}
  50  * may become a {@code sealed} interface, which would prohibit subclassing except
  51  * by explicitly permitted types.  Non-platform classes should not implement
  52  * {@linkplain DirectMethodHandleDesc} directly.
  53  *
  54  * @since 12
  55  */
  56 public interface DirectMethodHandleDesc extends MethodHandleDesc {
  57     /**
  58      * Kinds of method handles that can be described with {@linkplain DirectMethodHandleDesc}.
  59      */
  60     enum Kind {
  61         /** A method handle for a method invoked as with {@code invokestatic} */
  62         STATIC(REF_invokeStatic),
  63         /** A method handle for a method invoked as with {@code invokestatic} */
  64         INTERFACE_STATIC(REF_invokeStatic, true),
  65         /** A method handle for a method invoked as with {@code invokevirtual} */
  66         VIRTUAL(REF_invokeVirtual),
  67         /** A method handle for a method invoked as with {@code invokeinterface} */
  68         INTERFACE_VIRTUAL(REF_invokeInterface, true),
  69         /** A method handle for a method invoked as with {@code invokespecial} */
  70         SPECIAL(REF_invokeSpecial),
  71         /** A method handle for an interface method invoked as with {@code invokespecial} */
  72         INTERFACE_SPECIAL(REF_invokeSpecial, true),
  73         /** A method handle for a constructor */
  74         CONSTRUCTOR(REF_newInvokeSpecial),
  75         /** A method handle for a read accessor for an instance field  */
  76         GETTER(REF_getField),
  77         /** A method handle for a write accessor for an instance field  */
  78         SETTER(REF_putField),
  79         /** A method handle for a read accessor for a static field  */
  80         STATIC_GETTER(REF_getStatic),
  81         /** A method handle for a write accessor for a static field  */
  82         STATIC_SETTER(REF_putStatic);
  83 
  84         /** The corresponding {@code refKind} value for this kind of method handle,
  85          * as defined by {@link MethodHandleInfo}
  86          */
  87         public final int refKind;
  88 
  89         /** Is this an interface
  90          */
  91         public final boolean isInterface;
  92         Kind(int refKind) {
  93             this(refKind, false);
  94         }
  95 
  96         Kind(int refKind, boolean isInterface) { this.refKind = refKind; this.isInterface = isInterface; }
  97 
  98         /**
  99          * Find the enumeration member with the given {@code refKind} field.
 100          * Behaves as if {@code valueOf(refKind, false)}.  As a special case,
 101          * if {@code refKind} is {@code REF_invokeInterface} (9) then the
 102          * {@code isInterface} field will be true.
 103          *
 104          * @param refKind refKind of desired member
 105          * @return the matching enumeration member
 106          * @throws IllegalArgumentException if there is no such member
 107          */
 108         public static Kind valueOf(int refKind) {
 109             return valueOf(refKind, false);
 110         }
 111 
 112         /**
 113          * Find the enumeration member with the given the {@code refKind} and
 114          * {@code isInterface} arguments.
 115          * For most values of {@code refKind} there is an exact match regardless of the value of {@code isInterface}.
 116          * These are:
 117          * <UL>
 118          *     <LI>{@code REF_invokeVirtual} which matches to {@code VIRTUAL}
 119          *     <LI>{@code REF_invokeInterface} which matches to {@code INTERFACE_VIRTUAL}
 120          *     <LI>{@code REF_newInvokeSpecial} which matches to {@code CONSTRUCTOR}
 121          *     <LI>{@code REF_getField} which matches to {@code GETTER}
 122          *     <LI>{@code REF_putField} which matches to {@code SETTER}
 123          *     <LI>{@code REF_getStatic} which matches to {@code STATIC_GETTER}
 124          *     <LI>{@code REF_putStatic} which matches to {@code STATIC_SETTER}
 125          * </UL>
 126          * As for the rest, the returned kind will depend on the value (false or true accordingly) of {@code isInterface}:
 127          * <UL>
 128          *     <LI>{@code REF_invokeStatic} which matches to {@code STATIC} or {@code INTERFACE_STATIC}
 129          *     <LI>{@code REF_invokeSpecial} which matches to {@code SPECIAL} or {@code INTERFACE_SPECIAL}
 130          * </UL>
 131          * @param refKind refKind of desired member
 132          * @param isInterface whether desired member is for interface methods
 133          * @return the matching enumeration member
 134          * @throws IllegalArgumentException if there is no such member
 135          */
 136         public static Kind valueOf(int refKind, boolean isInterface) {
 137             int i = tableIndex(refKind, isInterface);
 138             if (i >= 0 && i < TABLE.length) {
 139                 Kind kind = TABLE[i];
 140                 if (kind == null) {
 141                     throw new IllegalArgumentException(String.format("refKind=%d", refKind));
 142                 }
 143                 if (kind.refKind == refKind && kind.isInterface == isInterface) {
 144                     return kind;
 145                 }
 146             }
 147             throw new IllegalArgumentException(String.format("refKind=%d", refKind));
 148         }
 149 
 150         private static int tableIndex(int refKind, boolean isInterface) {
 151             if (refKind < 0)  return refKind;
 152             return (refKind * 2) + (isInterface ? 1 : 0);
 153         }
 154 
 155         private static final @Stable Kind[] TABLE;
 156 
 157         static {
 158             // Pack the static table.
 159             int max = 0;
 160             for (Kind k : values())
 161                 max = Math.max(max, tableIndex(k.refKind, true));
 162 
 163             TABLE = new Kind[max+1];
 164             for (Kind kind : values()) {
 165                 int i = tableIndex(kind.refKind, kind.isInterface);
 166                 if (i >= TABLE.length || TABLE[i] != null)
 167                     throw new AssertionError("TABLE entry for " + kind);
 168                 TABLE[i] = kind;
 169             }
 170 
 171             // Pack in some aliases also.
 172             int ii = tableIndex(REF_invokeInterface, false);
 173             if (TABLE[ii] != null)
 174                 throw new AssertionError("TABLE entry for (invokeInterface, false) used by " + TABLE[ii]);
 175             TABLE[ii] = INTERFACE_VIRTUAL;
 176 
 177             for (Kind kind : values()) {
 178                 if (!kind.isInterface) {
 179                     // Add extra cache entry to alias the isInterface case.
 180                     // For example, (REF_getStatic, X) will produce STATIC_GETTER
 181                     // for either truth value of X.
 182                     int i = tableIndex(kind.refKind, true);
 183                     if (TABLE[i] == null) {
 184                         // There is not a specific Kind for interfaces
 185                         if (kind == VIRTUAL)  kind = INTERFACE_VIRTUAL;
 186                         if (TABLE[i] == null)  TABLE[i] = kind;
 187                     }
 188                 }
 189             }
 190         }
 191 
 192         /**
 193          * Does this {@code Kind} correspond to a virtual method invocation?
 194          *
 195          * @return if this {@code Kind} corresponds to a virtual method invocation
 196          */
 197         boolean isVirtualMethod() {
 198             switch (this) {
 199                 case VIRTUAL:
 200                 case SPECIAL:
 201                 case INTERFACE_VIRTUAL:
 202                 case INTERFACE_SPECIAL:
 203                     return true;
 204                 default:
 205                     return false;
 206             }
 207         }
 208     }
 209 
 210     /**
 211      * Return the {@code kind} of the method handle described by this nominal
 212      * descriptor.
 213      *
 214      * @return the {@link Kind}
 215      */
 216     Kind kind();
 217 
 218     /**
 219      * Return the {@code refKind} of the method handle described by this nominal
 220      * reference, as defined by {@link MethodHandleInfo}.
 221      *
 222      * @return the reference kind
 223      */
 224     int refKind();
 225 
 226     /**
 227      * Indicates if the method is declared by an interface
 228      *
 229      * @return true if the method is declared by an interface
 230      */
 231     boolean isOwnerInterface();
 232 
 233     /**
 234      * Return a {@link ClassDesc} describing the class declaring the
 235      * method or field described by this nominal descriptor.
 236      *
 237      * @return the class declaring the method or field
 238      */
 239     ClassDesc owner();
 240 
 241     /**
 242      * Return the name of the method or field described by this nominal descriptor.
 243      *
 244      * @return the name of the method or field
 245      */
 246     String methodName();
 247 
 248     /**
 249      * Return the lookup descriptor of the method handle described by this descriptor,
 250      * after adjusting for the invocation mode.  This will correspond to either
 251      * a method type descriptor string (for methods and constructors), or a field
 252      * descriptor string (for field access method handles).  The lookup descriptor
 253      * string is in the same format as accepted by {@link MethodHandleDesc#of(Kind, ClassDesc, String, String)}.
 254      *
 255      * @return the lookup descriptor string
 256      */
 257     String lookupDescriptor();
 258 }