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.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);
  55         this.argTypes = requireNonNull(argTypes);
  56 
  57         for (ClassDesc cr : argTypes)
  58             if (cr.isPrimitive() && cr.descriptorString().equals("V"))
  59                 throw new IllegalArgumentException("Void parameters not permitted");
  60     }
  61 
  62     /**
  63      * Create a {@linkplain MethodTypeDescImpl} given a method descriptor string.
  64      *
  65      * @param descriptor the method descriptor string, as per JVMS 4.3.3
  66      * @return a {@linkplain MethodTypeDescImpl} describing the desired method type
  67      * @throws IllegalArgumentException if the descriptor string is not a valid
  68      * method descriptor
  69      * @jvms 4.3.3 Method Descriptors
  70      */
  71     static MethodTypeDescImpl ofDescriptor(String descriptor) {
  72         requireNonNull(descriptor);
  73         List<String> types = ConstantUtils.parseMethodDescriptor(descriptor);
  74         ClassDesc[] paramTypes = types.stream().skip(1).map(ClassDesc::ofDescriptor).toArray(ClassDesc[]::new);
  75         return new MethodTypeDescImpl(ClassDesc.ofDescriptor(types.get(0)), paramTypes);
  76     }
  77 
  78     @Override
  79     public ClassDesc returnType() {
  80         return returnType;
  81     }
  82 
  83     @Override
  84     public int parameterCount() {
  85         return argTypes.length;
  86     }
  87 
  88     @Override
  89     public ClassDesc parameterType(int index) {
  90         return argTypes[index];
  91     }
  92 
  93     @Override
  94     public List<ClassDesc> parameterList() {
  95         return List.of(argTypes);
  96     }
  97 
  98     @Override
  99     public ClassDesc[] parameterArray() {
 100         return argTypes.clone();
 101     }
 102 
 103     @Override
 104     public MethodTypeDesc changeReturnType(ClassDesc returnType) {
 105         return MethodTypeDesc.of(returnType, argTypes);
 106     }
 107 
 108     @Override
 109     public MethodTypeDesc changeParameterType(int index, ClassDesc paramType) {
 110         ClassDesc[] newArgs = argTypes.clone();
 111         newArgs[index] = paramType;
 112         return MethodTypeDesc.of(returnType, newArgs);
 113     }
 114 
 115     @Override
 116     public MethodTypeDesc dropParameterTypes(int start, int end) {
 117         if (start < 0 || start >= argTypes.length || end < 0 || end > argTypes.length)
 118             throw new IndexOutOfBoundsException();
 119         else if (start > end)
 120             throw new IllegalArgumentException(String.format("Range (%d, %d) not valid for size %d", start, end, argTypes.length));
 121         ClassDesc[] newArgs = new ClassDesc[argTypes.length - (end - start)];
 122         System.arraycopy(argTypes, 0, newArgs, 0, start);
 123         System.arraycopy(argTypes, end, newArgs, start, argTypes.length - end);
 124         return MethodTypeDesc.of(returnType, newArgs);
 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 
 166     @Override
 167     public String toString() {
 168         return String.format("MethodTypeDesc[%s]", displayDescriptor());
 169     }
 170 }