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.invoke;
  26 
  27 import java.util.List;
  28 
  29 /**
  30  * An entity that has a field or method type descriptor
  31  *
  32  * @jvms 4.3.2 Field Descriptors
  33  * @jvms 4.3.3 Method Descriptors
  34  *
  35  * @since 12
  36  */
  37 public interface TypeDescriptor {
  38     /**
  39      * Return the type descriptor string for this instance, which must be either
  40      * a field type descriptor (JVMS 4.3.2) or method type descriptor (JVMS 4.3.3).
  41      *
  42      * @return the type descriptor
  43      * @jvms 4.3.2 Field Descriptors
  44      * @jvms 4.3.3 Method Descriptors
  45      */
  46     String descriptorString();
  47 
  48 
  49     /**
  50      * An entity that has a field type descriptor
  51      *
  52      * @param <F> the class implementing {@linkplain TypeDescriptor.OfField}
  53      * @jvms 4.3.2 Field Descriptors
  54      * @since 12
  55      */
  56     interface OfField<F extends TypeDescriptor.OfField<F>> extends TypeDescriptor {
  57         /**
  58          * Does this field descriptor describe an array type?
  59          * @return whether this field descriptor describes an array type
  60          */
  61         boolean isArray();
  62 
  63         /**
  64          * Does this field descriptor describe a primitive type?
  65          * @return whether this field descriptor describes a primitive type
  66          */
  67         boolean isPrimitive();
  68 
  69         /**
  70          * If this field descriptor describes an array type, return
  71          * a descriptor for its component type, otherwise return {@code null}.
  72          * @return the component type, or {@code null} if this field descriptor does
  73          * not describe an array type
  74          */
  75         F componentType();
  76 
  77         /**
  78          * Return a descriptor for the array type whose component type is described by this
  79          * descriptor
  80          * @return the descriptor for the array type
  81          */
  82         F arrayType();
  83     }
  84 
  85 
  86     /**
  87      * An entity that has a method type descriptor
  88      *
  89      * @param <F> the type representing field type descriptors
  90      * @param <M> the class implementing {@linkplain TypeDescriptor.OfMethod}
  91      * @jvms 4.3.2 Field Descriptors
  92      * @jvms 4.3.3 Method Descriptors
  93      * @since 12
  94      */
  95     interface OfMethod<F extends TypeDescriptor.OfField<F>, M extends TypeDescriptor.OfMethod<F, M>>
  96             extends TypeDescriptor {
  97 
  98         /**
  99          * Return the number of parameters in the method type
 100          * @return the number of parameters
 101          */
 102         int parameterCount();
 103 
 104         /**
 105          * Return a field descriptor describing the requested parameter of the method type
 106          * described by this descriptor
 107          * @param i the index of the parameter
 108          * @return a field descriptor for the requested parameter type
 109          * @throws IndexOutOfBoundsException if the index is outside the half-open
 110          * range {[0, parameterCount)}
 111          */
 112         F parameterType(int i);
 113 
 114         /**
 115          * Return a field descriptor describing the return type of the method type described
 116          * by this descriptor
 117          * @return a field descriptor for the return type
 118          */
 119         F returnType();
 120 
 121         /**
 122          * Return an array of field descriptors for the parameter types of the method type
 123          * described by this descriptor
 124          * @return field descriptors for the parameter types
 125          */
 126         F[] parameterArray();
 127 
 128         /**
 129          * Return an immutable list of field descriptors for the parameter types of the method type
 130          * described by this descriptor
 131          * @return field descriptors for the parameter types
 132          */
 133         List<F> parameterList();
 134 
 135         /**
 136          * Return a method descriptor that is identical to this one, except that the return
 137          * type has been changed to the specified type
 138          *
 139          * @param newReturn a field descriptor for the new return type
 140          * @throws NullPointerException if any argument is {@code null}
 141          * @return the new method descriptor
 142          */
 143         M changeReturnType(F newReturn);
 144 
 145         /**
 146          * Return a method descriptor that is identical to this one,
 147          * except that a single parameter type has been changed to the specified type.
 148          *
 149          * @param index the index of the parameter to change
 150          * @param paramType a field descriptor describing the new parameter type
 151          * @return the new method descriptor
 152          * @throws NullPointerException if any argument is {@code null}
 153          * @throws IndexOutOfBoundsException if the index is outside the half-open
 154          * range {[0, parameterCount)}
 155          */
 156         M changeParameterType(int index, F paramType);
 157 
 158         /**
 159          * Return a method descriptor that is identical to this one,
 160          * except that a range of parameter types have been removed.
 161          *
 162          * @param start the index of the first parameter to remove
 163          * @param end the index after the last parameter to remove
 164          * @return the new method descriptor
 165          *
 166          * @throws IndexOutOfBoundsException if {@code start} is outside the half-open
 167          * range {@code [0, parameterCount)}, or {@code end} is outside the closed range
 168          * {@code [0, parameterCount]}, or if {@code start > end}
 169          */
 170         M dropParameterTypes(int start, int end);
 171 
 172         /**
 173          * Return a method descriptor that is identical to this one,
 174          * except that a range of additional parameter types have been inserted.
 175          *
 176          * @param pos the index at which to insert the first inserted parameter
 177          * @param paramTypes field descriptors describing the new parameter types
 178          *                   to insert
 179          * @return the new method descriptor
 180          * @throws NullPointerException if any argument is {@code null}
 181          * @throws IndexOutOfBoundsException if {@code pos} is outside the closed
 182          * range {[0, parameterCount]}
 183          */
 184         @SuppressWarnings("unchecked")
 185         M insertParameterTypes(int pos, F... paramTypes);
 186     }
 187 }