1 /* 2 * Copyright (c) 2011, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package vm.mlvm.meth.share.transform.v2; 25 26 import java.lang.invoke.MethodHandle; 27 import java.lang.invoke.MethodType; 28 29 import vm.mlvm.meth.share.Argument; 30 import vm.mlvm.meth.share.Arguments; 31 import vm.mlvm.meth.share.TestTypes; 32 33 public abstract class MHCastTF extends MHBasicUnaryTF { 34 35 protected final Argument[] newArgs; 36 protected final Argument newRetVal; 37 protected final MethodType newMT; 38 39 protected MHCastTF(MHCall target, Class<?> newRetType, Class<?>[] newArgTypes) { 40 super(target); 41 42 Argument[] targetArgs = target.getArgs(); 43 44 if ( newArgTypes.length != targetArgs.length ) 45 throw new IllegalArgumentException("newArgTypes length (" + newArgTypes.length + ") should be equal to argument count (" + targetArgs.length + ")"); 46 47 this.newArgs = new Argument[newArgTypes.length]; 48 for ( int i = 0; i < this.newArgs.length; i++ ) { 49 if ( ! TestTypes.canConvertType(targetArgs[i].getType(), this.newArgs[i].getType(), false) ) 50 throw new IllegalArgumentException("Can't convert argument #" + i + " from [" + targetArgs[i].getType() + " to [" + this.newArgs[i].getType()); 51 52 this.newArgs[i] = convert(targetArgs[i], newArgTypes[i], false); 53 } 54 55 this.newRetVal = convert(target.getRetVal(), newRetType, true); 56 57 this.newMT = MethodType.methodType(this.newRetVal.getType(), Arguments.typesArray(this.newArgs)); 58 } 59 60 protected abstract Argument convert(Argument argument, Class<?> newClass, boolean isRetType); 61 62 @Override 63 protected abstract MethodHandle computeInboundMH(MethodHandle targetMH) throws NoSuchMethodException, IllegalAccessException; 64 65 @Override 66 protected Argument[] computeInboundArgs(Argument[] targetArgs) { 67 return this.newArgs; 68 } 69 70 @Override 71 protected Argument computeRetVal(Argument targetRetVal) { 72 return this.newRetVal; 73 } 74 75 @Override 76 protected String getDescription() { 77 return "newRetVal=[" + this.newRetVal + "]; newArgs=[" + this.newArgs + "]"; 78 } 79 80 }