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