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