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.MethodHandles;
28 import java.lang.invoke.MethodType;
29 import java.util.Arrays;
30 import java.util.List;
31 import java.util.Optional;
32
33 import static java.lang.constant.ConstantDescs.BSM_INVOKE;
34 import static java.lang.constant.ConstantDescs.MHR_METHODTYPEDESC_FACTORY;
35 import static java.util.Objects.requireNonNull;
36
37 /**
38 * A <a href="package-summary.html#nominal">nominal descriptor</a> for a
39 * {@link MethodType}. A {@linkplain MethodTypeDescImpl} corresponds to a
40 * {@code Constant_MethodType_info} entry in the constant pool of a classfile.
41 */
42 final class MethodTypeDescImpl implements MethodTypeDesc {
43 private final ClassDesc returnType;
44 private final ClassDesc[] argTypes;
45
46 /**
47 * Construct a {@linkplain MethodTypeDesc} with the specified return type
48 * and parameter types
49 *
50 * @param returnType a {@link ClassDesc} describing the return type
51 * @param argTypes {@link ClassDesc}s describing the parameter types
52 */
53 MethodTypeDescImpl(ClassDesc returnType, ClassDesc[] argTypes) {
54 this.returnType = requireNonNull(returnType);
125 }
126
127 @Override
128 public MethodTypeDesc insertParameterTypes(int pos, ClassDesc... paramTypes) {
129 if (pos < 0 || pos > argTypes.length)
130 throw new IndexOutOfBoundsException(pos);
131 ClassDesc[] newArgs = new ClassDesc[argTypes.length + paramTypes.length];
132 System.arraycopy(argTypes, 0, newArgs, 0, pos);
133 System.arraycopy(paramTypes, 0, newArgs, pos, paramTypes.length);
134 System.arraycopy(argTypes, pos, newArgs, pos+paramTypes.length, argTypes.length - pos);
135 return MethodTypeDesc.of(returnType, newArgs);
136 }
137
138 @Override
139 public MethodType resolveConstantDesc(MethodHandles.Lookup lookup) {
140 return MethodType.fromMethodDescriptorString(descriptorString(), lookup.lookupClass().getClassLoader());
141 }
142
143 @Override
144 public Optional<? extends ConstantDesc<ConstantDesc<MethodType>>> describeConstable() {
145 return Optional.of(DynamicConstantDesc.of(BSM_INVOKE, MHR_METHODTYPEDESC_FACTORY, descriptorString()));
146 }
147
148 @Override
149 public boolean equals(Object o) {
150 if (this == o) return true;
151 if (o == null || getClass() != o.getClass()) return false;
152
153 MethodTypeDescImpl constant = (MethodTypeDescImpl) o;
154
155 return returnType.equals(constant.returnType)
156 && Arrays.equals(argTypes, constant.argTypes);
157 }
158
159 @Override
160 public int hashCode() {
161 int result = returnType.hashCode();
162 result = 31 * result + Arrays.hashCode(argTypes);
163 return result;
164 }
165
|
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.MethodHandles;
28 import java.lang.invoke.MethodType;
29 import java.util.Arrays;
30 import java.util.List;
31 import java.util.Optional;
32
33 import static java.util.Objects.requireNonNull;
34
35 /**
36 * A <a href="package-summary.html#nominal">nominal descriptor</a> for a
37 * {@link MethodType}. A {@linkplain MethodTypeDescImpl} corresponds to a
38 * {@code Constant_MethodType_info} entry in the constant pool of a classfile.
39 */
40 final class MethodTypeDescImpl implements MethodTypeDesc {
41 private final ClassDesc returnType;
42 private final ClassDesc[] argTypes;
43
44 /**
45 * Construct a {@linkplain MethodTypeDesc} with the specified return type
46 * and parameter types
47 *
48 * @param returnType a {@link ClassDesc} describing the return type
49 * @param argTypes {@link ClassDesc}s describing the parameter types
50 */
51 MethodTypeDescImpl(ClassDesc returnType, ClassDesc[] argTypes) {
52 this.returnType = requireNonNull(returnType);
123 }
124
125 @Override
126 public MethodTypeDesc insertParameterTypes(int pos, ClassDesc... paramTypes) {
127 if (pos < 0 || pos > argTypes.length)
128 throw new IndexOutOfBoundsException(pos);
129 ClassDesc[] newArgs = new ClassDesc[argTypes.length + paramTypes.length];
130 System.arraycopy(argTypes, 0, newArgs, 0, pos);
131 System.arraycopy(paramTypes, 0, newArgs, pos, paramTypes.length);
132 System.arraycopy(argTypes, pos, newArgs, pos+paramTypes.length, argTypes.length - pos);
133 return MethodTypeDesc.of(returnType, newArgs);
134 }
135
136 @Override
137 public MethodType resolveConstantDesc(MethodHandles.Lookup lookup) {
138 return MethodType.fromMethodDescriptorString(descriptorString(), lookup.lookupClass().getClassLoader());
139 }
140
141 @Override
142 public Optional<? extends ConstantDesc<ConstantDesc<MethodType>>> describeConstable() {
143 return Optional.of(DynamicConstantDesc.ofSymbolic(this));
144 }
145
146 @Override
147 public boolean equals(Object o) {
148 if (this == o) return true;
149 if (o == null || getClass() != o.getClass()) return false;
150
151 MethodTypeDescImpl constant = (MethodTypeDescImpl) o;
152
153 return returnType.equals(constant.returnType)
154 && Arrays.equals(argTypes, constant.argTypes);
155 }
156
157 @Override
158 public int hashCode() {
159 int result = returnType.hashCode();
160 result = 31 * result + Arrays.hashCode(argTypes);
161 return result;
162 }
163
|