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 jdk.internal.lang.annotation.Foldable;
  34 import static java.lang.constant.ConstantDescs.BSM_INVOKE;
  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      * Constructs 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      * Creates a {@linkplain MethodTypeDescImpl} given a method descriptor string.
  64      *
  65      * @param descriptor the method descriptor string
  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     @Foldable
  72     static MethodTypeDescImpl ofDescriptor(String descriptor) {
  73         requireNonNull(descriptor);
  74         List<String> types = ConstantUtils.parseMethodDescriptor(descriptor);
  75         ClassDesc[] paramTypes = types.stream().skip(1).map(ClassDesc::ofDescriptor).toArray(ClassDesc[]::new);
  76         return new MethodTypeDescImpl(ClassDesc.ofDescriptor(types.get(0)), paramTypes);
  77     }
  78 
  79     @Foldable
  80     @Override
  81     public ClassDesc returnType() {
  82         return returnType;
  83     }
  84 
  85     @Foldable
  86     @Override
  87     public int parameterCount() {
  88         return argTypes.length;
  89     }
  90 
  91     @Foldable
  92     @Override
  93     public ClassDesc parameterType(int index) {
  94         return argTypes[index];
  95     }
  96 
  97     @Override
  98     public List<ClassDesc> parameterList() {
  99         return List.of(argTypes);
 100     }
 101 
 102     @Override
 103     public ClassDesc[] parameterArray() {
 104         return argTypes.clone();
 105     }
 106 
 107     @Foldable
 108     @Override
 109     public MethodTypeDesc changeReturnType(ClassDesc returnType) {
 110         return MethodTypeDesc.of(returnType, argTypes);
 111     }
 112 
 113     @Foldable
 114     @Override
 115     public MethodTypeDesc changeParameterType(int index, ClassDesc paramType) {
 116         ClassDesc[] newArgs = argTypes.clone();
 117         newArgs[index] = paramType;
 118         return MethodTypeDesc.of(returnType, newArgs);
 119     }
 120 
 121     @Foldable
 122     @Override
 123     public MethodTypeDesc dropParameterTypes(int start, int end) {
 124         if (start < 0 || start >= argTypes.length || end < 0 || end > argTypes.length)
 125             throw new IndexOutOfBoundsException();
 126         else if (start > end)
 127             throw new IllegalArgumentException(String.format("Range (%d, %d) not valid for size %d", start, end, argTypes.length));
 128         ClassDesc[] newArgs = new ClassDesc[argTypes.length - (end - start)];
 129         System.arraycopy(argTypes, 0, newArgs, 0, start);
 130         System.arraycopy(argTypes, end, newArgs, start, argTypes.length - end);
 131         return MethodTypeDesc.of(returnType, newArgs);
 132     }
 133 
 134     @Foldable
 135     @Override
 136     public MethodTypeDesc insertParameterTypes(int pos, ClassDesc... paramTypes) {
 137         if (pos < 0 || pos > argTypes.length)
 138             throw new IndexOutOfBoundsException(pos);
 139         ClassDesc[] newArgs = new ClassDesc[argTypes.length + paramTypes.length];
 140         System.arraycopy(argTypes, 0, newArgs, 0, pos);
 141         System.arraycopy(paramTypes, 0, newArgs, pos, paramTypes.length);
 142         System.arraycopy(argTypes, pos, newArgs, pos+paramTypes.length, argTypes.length - pos);
 143         return MethodTypeDesc.of(returnType, newArgs);
 144     }
 145 
 146     @Override
 147     public MethodType resolveConstantDesc(MethodHandles.Lookup lookup) {
 148         return MethodType.fromMethodDescriptorString(descriptorString(), lookup.lookupClass().getClassLoader());
 149     }
 150 
 151     /**
 152      * Returns {@code true} if this {@linkplain MethodTypeDescImpl} is
 153      * equal to another {@linkplain MethodTypeDescImpl}.  Equality is
 154      * determined by the two descriptors having equal return types and argument
 155      * types.
 156      *
 157      * @param o the {@code MethodTypeDescImpl} to compare to this
 158      *       {@code MethodTypeDescImpl}
 159      * @return {@code true} if the specified {@code MethodTypeDescImpl} is
 160      *      equals to this {@code MethodTypeDescImpl}.
 161      */
 162     @Override
 163     public boolean equals(Object o) {
 164         if (this == o) return true;
 165         if (o == null || getClass() != o.getClass()) return false;
 166 
 167         MethodTypeDescImpl constant = (MethodTypeDescImpl) o;
 168 
 169         return returnType.equals(constant.returnType)
 170                && Arrays.equals(argTypes, constant.argTypes);
 171     }
 172 
 173     @Override
 174     public int hashCode() {
 175         int result = returnType.hashCode();
 176         result = 31 * result + Arrays.hashCode(argTypes);
 177         return result;
 178     }
 179 
 180     @Override
 181     public String toString() {
 182         return String.format("MethodTypeDesc[%s]", displayDescriptor());
 183     }
 184 
 185     @Override
 186     public Optional<? extends ConstantDesc> describeConstable() {
 187         return Optional.empty();
 188     }
 189 }