1 /*
   2  * Copyright (c) 2018, 2019, 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      * @throws IllegalArgumentException if the descriptor string is not a valid
  74      * method or field descriptor
  75      * @jvms 4.4.8 The CONSTANT_MethodHandle_info Structure
  76      * @jvms 4.2.2 Unqualified Names
  77      * @jvms 4.3.2 Field Descriptors
  78      * @jvms 4.3.3 Method Descriptors
  79      */

  80     static DirectMethodHandleDesc of(DirectMethodHandleDesc.Kind kind,
  81                                      ClassDesc owner,
  82                                      String name,
  83                                      String lookupDescriptor) {
  84         switch (kind) {
  85             case GETTER:
  86             case SETTER:
  87             case STATIC_GETTER:
  88             case STATIC_SETTER:
  89                 return ofField(kind, owner, name, ClassDesc.ofDescriptor(lookupDescriptor));
  90             default:
  91                 return new DirectMethodHandleDescImpl(kind, owner, name, MethodTypeDesc.ofDescriptor(lookupDescriptor));
  92         }
  93     }
  94 
  95     /**
  96      * Creates a {@linkplain MethodHandleDesc} corresponding to an invocation of a
  97      * declared method or constructor.
  98      *
  99      * <p>The lookup descriptor string has the same format as for the lookup
 100      * methods on {@link MethodHandles.Lookup}.  If {@code kind} is
 101      * {@code CONSTRUCTOR}, the name is ignored and the return type of the lookup
 102      * type must be {@code void}.  If {@code kind} corresponds to a virtual method
 103      * invocation, the lookup type includes the method parameters but not the
 104      * receiver type.
 105      *
 106      * @param kind The kind of method handle to be described; must be one of
 107      *             {@code SPECIAL, VIRTUAL, STATIC, INTERFACE_SPECIAL,
 108      *             INTERFACE_VIRTUAL, INTERFACE_STATIC, CONSTRUCTOR}
 109      * @param owner a {@link ClassDesc} describing the class containing the
 110      *              method or constructor
 111      * @param name the unqualified name of the method (ignored if {@code kind}
 112      *             is {@code CONSTRUCTOR})
 113      * @param lookupMethodType a {@link MethodTypeDesc} describing the lookup type
 114      * @return the {@linkplain MethodHandleDesc}
 115      * @throws NullPointerException if any non-ignored arguments are null
 116      * @throws IllegalArgumentException if the {@code name} has the incorrect
 117      * format, or the kind is invalid
 118      * @jvms 4.2.2 Unqualified Names
 119      */

 120     static DirectMethodHandleDesc ofMethod(DirectMethodHandleDesc.Kind kind,
 121                                            ClassDesc owner,
 122                                            String name,
 123                                            MethodTypeDesc lookupMethodType) {
 124         switch (kind) {
 125             case GETTER:
 126             case SETTER:
 127             case STATIC_GETTER:
 128             case STATIC_SETTER:
 129                 throw new IllegalArgumentException(kind.toString());
 130             case VIRTUAL:
 131             case SPECIAL:
 132             case INTERFACE_VIRTUAL:
 133             case INTERFACE_SPECIAL:
 134             case INTERFACE_STATIC:
 135             case STATIC:
 136             case CONSTRUCTOR:
 137                 return new DirectMethodHandleDescImpl(kind, owner, name, lookupMethodType);
 138             default:
 139                 throw new IllegalArgumentException(kind.toString());
 140         }
 141     }
 142 
 143     /**
 144      * Creates a {@linkplain MethodHandleDesc} corresponding to a method handle
 145      * that accesses a field.
 146      *
 147      * @param kind the kind of the method handle to be described; must be one of {@code GETTER},
 148      *             {@code SETTER}, {@code STATIC_GETTER}, or {@code STATIC_SETTER}
 149      * @param owner a {@link ClassDesc} describing the class containing the field
 150      * @param fieldName the unqualified name of the field
 151      * @param fieldType a {@link ClassDesc} describing the type of the field
 152      * @return the {@linkplain MethodHandleDesc}
 153      * @throws NullPointerException if any of the arguments are null
 154      * @throws IllegalArgumentException if the {@code kind} is not one of the
 155      * valid values or if the field name is not valid
 156      * @jvms 4.2.2 Unqualified Names
 157      */

 158     static DirectMethodHandleDesc ofField(DirectMethodHandleDesc.Kind kind,
 159                                           ClassDesc owner,
 160                                           String fieldName,
 161                                           ClassDesc fieldType) {
 162         MethodTypeDesc mtr;
 163         switch (kind) {
 164             case GETTER: mtr = MethodTypeDesc.of(fieldType, owner); break;
 165             case SETTER: mtr = MethodTypeDesc.of(CD_void, owner, fieldType); break;
 166             case STATIC_GETTER: mtr = MethodTypeDesc.of(fieldType); break;
 167             case STATIC_SETTER: mtr = MethodTypeDesc.of(CD_void, fieldType); break;
 168             default:
 169                 throw new IllegalArgumentException(kind.toString());
 170         }
 171         return new DirectMethodHandleDescImpl(kind, owner, fieldName, mtr);
 172     }
 173 
 174     /**
 175      * Returns a {@linkplain MethodHandleDesc} corresponding to invocation of a constructor
 176      *
 177      * @param owner a {@link ClassDesc} describing the class containing the
 178      *              constructor
 179      * @param paramTypes {@link ClassDesc}s describing the parameter types of
 180      *                   the constructor
 181      * @return the {@linkplain MethodHandleDesc}
 182      * @throws NullPointerException if any argument or its contents is {@code null}
 183      */

 184     static DirectMethodHandleDesc ofConstructor(ClassDesc owner,
 185                                                 ClassDesc... paramTypes) {
 186         return MethodHandleDesc.ofMethod(CONSTRUCTOR, owner, ConstantDescs.DEFAULT_NAME,
 187                                          MethodTypeDesc.of(CD_void, paramTypes));
 188     }
 189 
 190     /**
 191      * Returns a {@linkplain MethodHandleDesc} that describes this method handle
 192      * adapted to a different type, as if by {@link MethodHandle#asType(MethodType)}.
 193      *
 194      * @param type a {@link MethodHandleDesc} describing the new method type
 195      * @return a {@linkplain MethodHandleDesc} for the adapted method handle
 196      * @throws NullPointerException if the argument is {@code null}
 197      */

 198     default MethodHandleDesc asType(MethodTypeDesc type) {
 199         return (invocationType().equals(type)) ? this : new AsTypeMethodHandleDesc(this, type);
 200     }
 201 
 202     /**
 203      * Returns a {@link MethodTypeDesc} describing the invocation type of the
 204      * method handle described by this nominal descriptor.  The invocation type
 205      * describes the full set of stack values that are consumed by the invocation
 206      * (including the receiver, if any).
 207      *
 208      * @return a {@linkplain MethodHandleDesc} describing the method handle type
 209      */

 210     MethodTypeDesc invocationType();
 211 
 212     /**
 213      * Compares the specified object with this descriptor for equality.  Returns
 214      * {@code true} if and only if the specified object is also a
 215      * {@linkplain MethodHandleDesc}, and both encode the same nominal description
 216      * of a method handle.
 217      *
 218      * @param o the other object
 219      * @return whether this descriptor is equal to the other object
 220      */
 221     boolean equals(Object o);
 222 }
--- EOF ---