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.MethodHandles;
  29 import java.lang.invoke.MethodType;
  30 
  31 import static java.lang.constant.ConstantDescs.CD_void;
  32 import static java.lang.constant.DirectMethodHandleDesc.Kind.CONSTRUCTOR;
  33 
  34 /**
  35  * A <a href="package-summary.html#nominal">nominal descriptor</a> for a
  36  * {@link MethodHandle} constant.
  37  *
  38  * @apiNote In the future, if the Java language permits, {@linkplain MethodHandleDesc}
  39  * may become a {@code sealed} interface, which would prohibit subclassing except
  40  * by explicitly permitted types.  Non-platform classes should not implement
  41  * {@linkplain MethodHandleDesc} directly.
  42  *
  43  * @since 12
  44  */
  45 public interface MethodHandleDesc
  46         extends ConstantDesc {
  47 
  48     /**
  49      * Creates a {@linkplain MethodHandleDesc} corresponding to an invocation of a
  50      * declared method, invocation of a constructor, or access to a field.
  51      *
  52      * <p>The lookup descriptor string has the same format as for the various
  53      * variants of {@code CONSTANT_MethodHandle_info} and for the lookup
  54      * methods on {@link MethodHandles.Lookup}.  For a method or constructor
  55      * invocation, it is interpreted as a method type descriptor; for field
  56      * access, it is interpreted as a field descriptor.  If {@code kind} is
  57      * {@code CONSTRUCTOR}, the {@code name} parameter is ignored and the return
  58      * type of the lookup descriptor must be {@code void}.  If {@code kind}
  59      * corresponds to a virtual method invocation, the lookup type includes the
  60      * method parameters but not the receiver type.
  61      *
  62      * @param kind The kind of method handle to be described
  63      * @param owner a {@link ClassDesc} describing the class containing the
  64      *              method, constructor, or field
  65      * @param name the unqualified name of the method or field (ignored if
  66      *             {@code kind} is {@code CONSTRUCTOR})
  67      * @param lookupDescriptor a method descriptor string the lookup type,
  68      *                         if the request is for a method invocation, or
  69      *                         describing the invocation type, if the request is
  70      *                         for a field or constructor
  71      * @return the {@linkplain MethodHandleDesc}
  72      * @throws NullPointerException if any of the non-ignored arguments are null
  73      * @jvms 4.4.8 The CONSTANT_MethodHandle_info Structure
  74      * @jvms 4.2.2 Unqualified Names
  75      * @jvms 4.3.2 Field Descriptors
  76      * @jvms 4.3.3 Method Descriptors
  77      */
  78     static DirectMethodHandleDesc of(DirectMethodHandleDesc.Kind kind,
  79                                      ClassDesc owner,
  80                                      String name,
  81                                      String lookupDescriptor) {
  82         switch (kind) {
  83             case GETTER:
  84             case SETTER:
  85             case STATIC_GETTER:
  86             case STATIC_SETTER:
  87                 return ofField(kind, owner, name, ClassDesc.ofDescriptor(lookupDescriptor));
  88             default:
  89                 return new DirectMethodHandleDescImpl(kind, owner, name, MethodTypeDesc.ofDescriptor(lookupDescriptor));
  90         }
  91     }
  92 
  93     /**
  94      * Creates a {@linkplain MethodHandleDesc} corresponding to an invocation of a
  95      * declared method or constructor.
  96      *
  97      * <p>The lookup descriptor string has the same format as for the lookup
  98      * methods on {@link MethodHandles.Lookup}.  If {@code kind} is
  99      * {@code CONSTRUCTOR}, the name is ignored and the return type of the lookup
 100      * type must be {@code void}.  If {@code kind} corresponds to a virtual method
 101      * invocation, the lookup type includes the method parameters but not the
 102      * receiver type.
 103      *
 104      * @param kind The kind of method handle to be described; must be one of
 105      *             {@code SPECIAL, VIRTUAL, STATIC, INTERFACE_SPECIAL,
 106      *             INTERFACE_VIRTUAL, INTERFACE_STATIC, CONSTRUCTOR}
 107      * @param owner a {@link ClassDesc} describing the class containing the
 108      *              method or constructor
 109      * @param name the unqualified name of the method (ignored if {@code kind}
 110      *             is {@code CONSTRUCTOR})
 111      * @param lookupMethodType a {@link MethodTypeDesc} describing the lookup type
 112      * @return the {@linkplain MethodHandleDesc}
 113      * @throws NullPointerException if any non-ignored arguments are null
 114      * @throws IllegalArgumentException if the {@code name} has the incorrect
 115      * format, or the kind is invalid
 116      * @jvms 4.2.2 Unqualified Names
 117      */
 118     static DirectMethodHandleDesc ofMethod(DirectMethodHandleDesc.Kind kind,
 119                                            ClassDesc owner,
 120                                            String name,
 121                                            MethodTypeDesc lookupMethodType) {
 122         switch (kind) {
 123             case GETTER:
 124             case SETTER:
 125             case STATIC_GETTER:
 126             case STATIC_SETTER:
 127                 throw new IllegalArgumentException(kind.toString());
 128             case VIRTUAL:
 129             case SPECIAL:
 130             case INTERFACE_VIRTUAL:
 131             case INTERFACE_SPECIAL:
 132             case INTERFACE_STATIC:
 133             case STATIC:
 134             case CONSTRUCTOR:
 135                 return new DirectMethodHandleDescImpl(kind, owner, name, lookupMethodType);
 136             default:
 137                 throw new IllegalArgumentException(kind.toString());
 138         }
 139     }
 140 
 141     /**
 142      * Creates a {@linkplain MethodHandleDesc} corresponding to a method handle
 143      * that accesses a field.
 144      *
 145      * @param kind the kind of the method handle to be described; must be one of {@code GETTER},
 146      *             {@code SETTER}, {@code STATIC_GETTER}, or {@code STATIC_SETTER}
 147      * @param owner a {@link ClassDesc} describing the class containing the field
 148      * @param fieldName the unqualified name of the field
 149      * @param fieldType a {@link ClassDesc} describing the type of the field
 150      * @return the {@linkplain MethodHandleDesc}
 151      * @throws NullPointerException if any of the arguments are null
 152      * @throws IllegalArgumentException if the {@code kind} is not one of the
 153      * valid values or if the field name is not valid
 154      * @jvms 4.2.2 Unqualified Names
 155      */
 156     static DirectMethodHandleDesc ofField(DirectMethodHandleDesc.Kind kind,
 157                                           ClassDesc owner,
 158                                           String fieldName,
 159                                           ClassDesc fieldType) {
 160         MethodTypeDesc mtr;
 161         switch (kind) {
 162             case GETTER: mtr = MethodTypeDesc.of(fieldType, owner); break;
 163             case SETTER: mtr = MethodTypeDesc.of(CD_void, owner, fieldType); break;
 164             case STATIC_GETTER: mtr = MethodTypeDesc.of(fieldType); break;
 165             case STATIC_SETTER: mtr = MethodTypeDesc.of(CD_void, fieldType); break;
 166             default:
 167                 throw new IllegalArgumentException(kind.toString());
 168         }
 169         return new DirectMethodHandleDescImpl(kind, owner, fieldName, mtr);
 170     }
 171 
 172     /**
 173      * Returns a {@linkplain MethodHandleDesc} corresponding to invocation of a constructor
 174      *
 175      * @param owner a {@link ClassDesc} describing the class containing the
 176      *              constructor
 177      * @param paramTypes {@link ClassDesc}s describing the parameter types of
 178      *                   the constructor
 179      * @return the {@linkplain MethodHandleDesc}
 180      * @throws NullPointerException if any of the arguments are null
 181      */
 182     static DirectMethodHandleDesc ofConstructor(ClassDesc owner,
 183                                                 ClassDesc... paramTypes) {
 184         return MethodHandleDesc.ofMethod(CONSTRUCTOR, owner, ConstantDescs.DEFAULT_NAME,
 185                                          MethodTypeDesc.of(CD_void, paramTypes));
 186     }
 187 
 188     /**
 189      * Returns a {@linkplain MethodHandleDesc} that describes this method handle
 190      * adapted to a different type, as if by {@link MethodHandle#asType(MethodType)}.
 191      *
 192      * @param type a {@link MethodHandleDesc} describing the new method type
 193      * @return a {@linkplain MethodHandleDesc} for the adapted method handle
 194      */
 195     default MethodHandleDesc asType(MethodTypeDesc type) {
 196         return (invocationType().equals(type)) ? this : new AsTypeMethodHandleDesc(this, type);
 197     }
 198 
 199     /**
 200      * Returns a {@link MethodTypeDesc} describing the invocation type of the
 201      * method handle described by this nominal descriptor.  The invocation type
 202      * describes the full set of stack values that are consumed by the invocation
 203      * (including the receiver, if any).
 204      *
 205      * @return a {@linkplain MethodHandleDesc} describing the method handle type
 206      */
 207     MethodTypeDesc invocationType();
 208 
 209     /**
 210      * Compares the specified object with this descriptor for equality.  Returns
 211      * {@code true} if and only if the specified object is also a
 212      * {@linkplain MethodHandleDesc}, and both encode the same nominal description
 213      * of a method handle.
 214      *
 215      * @param o the other object
 216      * @return whether this descriptor is equal to the other object
 217      */
 218     boolean equals(Object o);
 219 }