< prev index next >

src/java.base/share/classes/java/lang/constant/DirectMethodHandleDesc.java

Print this page




  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 DirectMethodHandleDesc} 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      * @since 12
  61      */
  62     enum Kind {
  63         /** A method handle for a method invoked as with {@code invokestatic} */
  64         STATIC(REF_invokeStatic),
  65         /** A method handle for a method invoked as with {@code invokestatic} */
  66         INTERFACE_STATIC(REF_invokeStatic, true),
  67         /** A method handle for a method invoked as with {@code invokevirtual} */
  68         VIRTUAL(REF_invokeVirtual),
  69         /** A method handle for a method invoked as with {@code invokeinterface} */
  70         INTERFACE_VIRTUAL(REF_invokeInterface, true),
  71         /** A method handle for a method invoked as with {@code invokespecial} */
  72         SPECIAL(REF_invokeSpecial),
  73         /** A method handle for an interface method invoked as with {@code invokespecial} */
  74         INTERFACE_SPECIAL(REF_invokeSpecial, true),
  75         /** A method handle for a constructor */
  76         CONSTRUCTOR(REF_newInvokeSpecial),
  77         /** A method handle for a read accessor for an instance field  */
  78         GETTER(REF_getField),
  79         /** A method handle for a write accessor for an instance field  */
  80         SETTER(REF_putField),
  81         /** A method handle for a read accessor for a static field  */
  82         STATIC_GETTER(REF_getStatic),
  83         /** A method handle for a write accessor for a static field  */
  84         STATIC_SETTER(REF_putStatic);
  85 
  86         /** The corresponding {@code refKind} value for this kind of method handle,
  87          * as defined by {@link MethodHandleInfo}
  88          */
  89         public final int refKind;
  90 
  91         /** Is this an interface
  92          */
  93         public final boolean isInterface;
  94         Kind(int refKind) {
  95             this(refKind, false);
  96         }
  97 
  98         Kind(int refKind, boolean isInterface) { this.refKind = refKind; this.isInterface = isInterface; }
  99 
 100         /**
 101          * Returns the enumeration member with the given {@code refKind} field.
 102          * Behaves as if {@code valueOf(refKind, false)}.  As a special case,
 103          * if {@code refKind} is {@code REF_invokeInterface} (9) then the
 104          * {@code isInterface} field will be true.


 198          */
 199         boolean isVirtualMethod() {
 200             switch (this) {
 201                 case VIRTUAL:
 202                 case SPECIAL:
 203                 case INTERFACE_VIRTUAL:
 204                 case INTERFACE_SPECIAL:
 205                     return true;
 206                 default:
 207                     return false;
 208             }
 209         }
 210     }
 211 
 212     /**
 213      * Returns the {@code kind} of the method handle described by this nominal
 214      * descriptor.
 215      *
 216      * @return the {@link Kind}
 217      */

 218     Kind kind();
 219 
 220     /**
 221      * Returns the {@code refKind} of the method handle described by this nominal
 222      * reference, as defined by {@link MethodHandleInfo}.
 223      *
 224      * @return the reference kind
 225      */

 226     int refKind();
 227 
 228     /**
 229      * Indicates if the method is declared by an interface
 230      *
 231      * @return true if the method is declared by an interface
 232      */
 233     boolean isOwnerInterface();
 234 
 235     /**
 236      * Returns a {@link ClassDesc} describing the class declaring the
 237      * method or field described by this nominal descriptor.
 238      *
 239      * @return the class declaring the method or field
 240      */

 241     ClassDesc owner();
 242 
 243     /**
 244      * Returns the name of the method or field described by this nominal descriptor.
 245      * For constructors, returns the reserved name {@code "<init>"}.
 246      *
 247      * @return the name of the method or field
 248      */

 249     String methodName();
 250 
 251     /**
 252      * Returns the lookup descriptor of the method handle described by this descriptor,
 253      * after adjusting for the invocation mode.  This will correspond to either
 254      * a method type descriptor string (for methods and constructors), or a field
 255      * descriptor string (for field access method handles).  The lookup descriptor
 256      * string is in the same format as accepted by {@link MethodHandleDesc#of(Kind, ClassDesc, String, String)}.
 257      *
 258      * @return the lookup descriptor string
 259      */

 260     String lookupDescriptor();
 261 }


  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 jdk.internal.lang.annotation.Foldable;
  35 
  36 import static java.lang.invoke.MethodHandleInfo.REF_getField;
  37 import static java.lang.invoke.MethodHandleInfo.REF_getStatic;
  38 import static java.lang.invoke.MethodHandleInfo.REF_invokeInterface;
  39 import static java.lang.invoke.MethodHandleInfo.REF_invokeSpecial;
  40 import static java.lang.invoke.MethodHandleInfo.REF_invokeStatic;
  41 import static java.lang.invoke.MethodHandleInfo.REF_invokeVirtual;
  42 import static java.lang.invoke.MethodHandleInfo.REF_newInvokeSpecial;
  43 import static java.lang.invoke.MethodHandleInfo.REF_putField;
  44 import static java.lang.invoke.MethodHandleInfo.REF_putStatic;
  45 
  46 /**
  47  * A <a href="package-summary.html#nominal">nominal descriptor</a> for a direct
  48  * {@link MethodHandle}.  A {@linkplain DirectMethodHandleDesc} corresponds to
  49  * a {@code Constant_MethodHandle_info} entry in the constant pool of a classfile.
  50  *
  51  * @apiNote In the future, if the Java language permits, {@linkplain DirectMethodHandleDesc}
  52  * may become a {@code sealed} interface, which would prohibit subclassing except
  53  * by explicitly permitted types.  Non-platform classes should not implement
  54  * {@linkplain DirectMethodHandleDesc} directly.
  55  *
  56  * @since 12
  57  */
  58 public interface DirectMethodHandleDesc extends MethodHandleDesc {
  59     /**
  60      * Kinds of method handles that can be described with {@linkplain DirectMethodHandleDesc}.
  61      *
  62      * @since 12
  63      */
  64     enum Kind {
  65         /** A method handle for a method invoked as with {@code invokestatic} */
  66         @Foldable STATIC(REF_invokeStatic),
  67         /** A method handle for a method invoked as with {@code invokestatic} */
  68         @Foldable INTERFACE_STATIC(REF_invokeStatic, true),
  69         /** A method handle for a method invoked as with {@code invokevirtual} */
  70         @Foldable VIRTUAL(REF_invokeVirtual),
  71         /** A method handle for a method invoked as with {@code invokeinterface} */
  72         @Foldable INTERFACE_VIRTUAL(REF_invokeInterface, true),
  73         /** A method handle for a method invoked as with {@code invokespecial} */
  74         @Foldable SPECIAL(REF_invokeSpecial),
  75         /** A method handle for an interface method invoked as with {@code invokespecial} */
  76         @Foldable INTERFACE_SPECIAL(REF_invokeSpecial, true),
  77         /** A method handle for a constructor */
  78         @Foldable CONSTRUCTOR(REF_newInvokeSpecial),
  79         /** A method handle for a read accessor for an instance field  */
  80         @Foldable GETTER(REF_getField),
  81         /** A method handle for a write accessor for an instance field  */
  82         @Foldable SETTER(REF_putField),
  83         /** A method handle for a read accessor for a static field  */
  84         @Foldable STATIC_GETTER(REF_getStatic),
  85         /** A method handle for a write accessor for a static field  */
  86         @Foldable STATIC_SETTER(REF_putStatic);
  87 
  88         /** The corresponding {@code refKind} value for this kind of method handle,
  89          * as defined by {@link MethodHandleInfo}
  90          */
  91         public final int refKind;
  92 
  93         /** Is this an interface
  94          */
  95         public final boolean isInterface;
  96         Kind(int refKind) {
  97             this(refKind, false);
  98         }
  99 
 100         Kind(int refKind, boolean isInterface) { this.refKind = refKind; this.isInterface = isInterface; }
 101 
 102         /**
 103          * Returns the enumeration member with the given {@code refKind} field.
 104          * Behaves as if {@code valueOf(refKind, false)}.  As a special case,
 105          * if {@code refKind} is {@code REF_invokeInterface} (9) then the
 106          * {@code isInterface} field will be true.


 200          */
 201         boolean isVirtualMethod() {
 202             switch (this) {
 203                 case VIRTUAL:
 204                 case SPECIAL:
 205                 case INTERFACE_VIRTUAL:
 206                 case INTERFACE_SPECIAL:
 207                     return true;
 208                 default:
 209                     return false;
 210             }
 211         }
 212     }
 213 
 214     /**
 215      * Returns the {@code kind} of the method handle described by this nominal
 216      * descriptor.
 217      *
 218      * @return the {@link Kind}
 219      */
 220     @Foldable
 221     Kind kind();
 222 
 223     /**
 224      * Returns the {@code refKind} of the method handle described by this nominal
 225      * reference, as defined by {@link MethodHandleInfo}.
 226      *
 227      * @return the reference kind
 228      */
 229     @Foldable
 230     int refKind();
 231 
 232     /**
 233      * Indicates if the method is declared by an interface
 234      *
 235      * @return true if the method is declared by an interface
 236      */
 237     boolean isOwnerInterface();
 238 
 239     /**
 240      * Returns a {@link ClassDesc} describing the class declaring the
 241      * method or field described by this nominal descriptor.
 242      *
 243      * @return the class declaring the method or field
 244      */
 245     @Foldable
 246     ClassDesc owner();
 247 
 248     /**
 249      * Returns the name of the method or field described by this nominal descriptor.
 250      * For constructors, returns the reserved name {@code "<init>"}.
 251      *
 252      * @return the name of the method or field
 253      */
 254     @Foldable
 255     String methodName();
 256 
 257     /**
 258      * Returns the lookup descriptor of the method handle described by this descriptor,
 259      * after adjusting for the invocation mode.  This will correspond to either
 260      * a method type descriptor string (for methods and constructors), or a field
 261      * descriptor string (for field access method handles).  The lookup descriptor
 262      * string is in the same format as accepted by {@link MethodHandleDesc#of(Kind, ClassDesc, String, String)}.
 263      *
 264      * @return the lookup descriptor string
 265      */
 266     @Foldable
 267     String lookupDescriptor();
 268 }
< prev index next >