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.MethodHandles;
  28 import java.lang.invoke.MethodType;
  29 import java.util.Arrays;
  30 import java.util.List;
  31 
  32 import static java.util.Objects.requireNonNull;
  33 
  34 /**
  35  * A <a href="package-summary.html#nominal">nominal descriptor</a> for a
  36  * {@link MethodType}.  A {@linkplain MethodTypeDescImpl} corresponds to a
  37  * {@code Constant_MethodType_info} entry in the constant pool of a classfile.
  38  */
  39 final class MethodTypeDescImpl implements MethodTypeDesc {
  40     private final ClassDesc returnType;
  41     private final ClassDesc[] argTypes;
  42 
  43     /**
  44      * Constructs a {@linkplain MethodTypeDesc} with the specified return type
  45      * and parameter types
  46      *
  47      * @param returnType a {@link ClassDesc} describing the return type
  48      * @param argTypes {@link ClassDesc}s describing the parameter types
  49      */
  50     MethodTypeDescImpl(ClassDesc returnType, ClassDesc[] argTypes) {
  51         this.returnType = requireNonNull(returnType);
  52         this.argTypes = requireNonNull(argTypes);
  53 
  54         for (ClassDesc cr : argTypes)
  55             if (cr.isPrimitive() && cr.descriptorString().equals("V"))
  56                 throw new IllegalArgumentException("Void parameters not permitted");
  57     }
  58 
  59     /**
  60      * Creates a {@linkplain MethodTypeDescImpl} given a method descriptor string.
  61      *
  62      * @param descriptor the method descriptor string
  63      * @return a {@linkplain MethodTypeDescImpl} describing the desired method type
  64      * @throws IllegalArgumentException if the descriptor string is not a valid
  65      * method descriptor
  66      * @jvms 4.3.3 Method Descriptors
  67      */
  68     static MethodTypeDescImpl ofDescriptor(String descriptor) {
  69         requireNonNull(descriptor);
  70         List<String> types = ConstantUtils.parseMethodDescriptor(descriptor);
  71         ClassDesc[] paramTypes = types.stream().skip(1).map(ClassDesc::ofDescriptor).toArray(ClassDesc[]::new);
  72         return new MethodTypeDescImpl(ClassDesc.ofDescriptor(types.get(0)), paramTypes);
  73     }
  74 
  75     @Override
  76     public ClassDesc returnType() {
  77         return returnType;
  78     }
  79 
  80     @Override
  81     public int parameterCount() {
  82         return argTypes.length;
  83     }
  84 
  85     @Override
  86     public ClassDesc parameterType(int index) {
  87         return argTypes[index];
  88     }
  89 
  90     @Override
  91     public List<ClassDesc> parameterList() {
  92         return List.of(argTypes);
  93     }
  94 
  95     @Override
  96     public ClassDesc[] parameterArray() {
  97         return argTypes.clone();
  98     }
  99 
 100     @Override
 101     public MethodTypeDesc changeReturnType(ClassDesc returnType) {
 102         return MethodTypeDesc.of(returnType, argTypes);
 103     }
 104 
 105     @Override
 106     public MethodTypeDesc changeParameterType(int index, ClassDesc paramType) {
 107         ClassDesc[] newArgs = argTypes.clone();
 108         newArgs[index] = paramType;
 109         return MethodTypeDesc.of(returnType, newArgs);
 110     }
 111 
 112     @Override
 113     public MethodTypeDesc dropParameterTypes(int start, int end) {
 114         if (start < 0 || start >= argTypes.length || end < 0 || end > argTypes.length || start > end)
 115             throw new IndexOutOfBoundsException();
 116         ClassDesc[] newArgs = new ClassDesc[argTypes.length - (end - start)];
 117         System.arraycopy(argTypes, 0, newArgs, 0, start);
 118         System.arraycopy(argTypes, end, newArgs, start, argTypes.length - end);
 119         return MethodTypeDesc.of(returnType, newArgs);
 120     }
 121 
 122     @Override
 123     public MethodTypeDesc insertParameterTypes(int pos, ClassDesc... paramTypes) {
 124         if (pos < 0 || pos > argTypes.length)
 125             throw new IndexOutOfBoundsException(pos);
 126         ClassDesc[] newArgs = new ClassDesc[argTypes.length + paramTypes.length];
 127         System.arraycopy(argTypes, 0, newArgs, 0, pos);
 128         System.arraycopy(paramTypes, 0, newArgs, pos, paramTypes.length);
 129         System.arraycopy(argTypes, pos, newArgs, pos+paramTypes.length, argTypes.length - pos);
 130         return MethodTypeDesc.of(returnType, newArgs);
 131     }
 132 
 133     @Override
 134     public MethodType resolveConstantDesc(MethodHandles.Lookup lookup) {
 135         return MethodType.fromMethodDescriptorString(descriptorString(), lookup.lookupClass().getClassLoader());
 136     }
 137 
 138     /**
 139      * Returns {@code true} if this {@linkplain MethodTypeDescImpl} is
 140      * equal to another {@linkplain MethodTypeDescImpl}.  Equality is
 141      * determined by the two descriptors having equal return types and argument
 142      * types.
 143      *
 144      * @param o the {@code MethodTypeDescImpl} to compare to this
 145      *       {@code MethodTypeDescImpl}
 146      * @return {@code true} if the specified {@code MethodTypeDescImpl} is
 147      *      equals to this {@code MethodTypeDescImpl}.
 148      */
 149     @Override
 150     public boolean equals(Object o) {
 151         if (this == o) return true;
 152         if (o == null || getClass() != o.getClass()) return false;
 153 
 154         MethodTypeDescImpl constant = (MethodTypeDescImpl) o;
 155 
 156         return returnType.equals(constant.returnType)
 157                && Arrays.equals(argTypes, constant.argTypes);
 158     }
 159 
 160     @Override
 161     public int hashCode() {
 162         int result = returnType.hashCode();
 163         result = 31 * result + Arrays.hashCode(argTypes);
 164         return result;
 165     }
 166 
 167     @Override
 168     public String toString() {
 169         return String.format("MethodTypeDesc[%s]", displayDescriptor());
 170     }
 171 }