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;
  25 
  26 import java.lang.invoke.MethodType;
  27 import java.util.ArrayList;
  28 import java.util.Arrays;
  29 import java.util.List;
  30 
  31 public final class Arguments {
  32 
  33     public static List<Argument> listFromArray(Object[][] a) {
  34         ArrayList<Argument> result = new ArrayList<Argument>(a.length);
  35 
  36         for (Object[] elem : a) {
  37             result.add(Argument.fromArray(elem));
  38         }
  39 
  40         return result;
  41     }
  42 
  43     public static Argument[] fromArray(Object[][] a) {
  44         return listFromArray(a).toArray(new Argument[a.length]);
  45     }
  46 
  47     public static Class<?>[] typesArray(List<Argument> vts) {
  48         return typesArray(vts.toArray(new Argument[vts.size()]));
  49     }
  50 
  51     public static Class<?>[] typesArray(Argument[] vts) {
  52         Class<?>[] result = new Class<?>[vts.length];
  53         for (int i = 0; i < vts.length; i++)
  54             result[i] = vts[i].getType();
  55         return result;
  56     }
  57 
  58     public static Object[] valuesArray(List<Argument> vts) {
  59         return valuesArray(vts.toArray(new Argument[vts.size()]));
  60     }
  61 
  62     public static Object[] valuesArray(Argument[] vts) {
  63         Object[] result = new Object[vts.length];
  64         for (int i = 0; i < vts.length; i++)
  65             result[i] = vts[i].getValue();
  66         return result;
  67     }
  68 
  69     public static Argument[] fromMethodType(boolean isVirtual, MethodType t, MethodParameterValueProvider vp) {
  70         int virtualOffset = isVirtual ? 1 : 0;
  71         Class<?>[] paramTypes = t.parameterArray();
  72         Argument[] result = new Argument[paramTypes.length - virtualOffset];
  73         for (int i = virtualOffset; i < paramTypes.length; i++) {
  74             result[i - virtualOffset] = new Argument(paramTypes[i], vp.getValue(t, i));
  75         }
  76         return result;
  77     }
  78 
  79     public static int[] findTag(List<Argument> args, String tag) {
  80         return findTag(args.toArray(new Argument[args.size()]), tag);
  81     }
  82 
  83     public static int[] findTag(Argument[] args, String tag) {
  84         int[] result = new int[args.length];
  85         int resCount = 0;
  86         for ( int i = 0; i < args.length; i++ ) {
  87             if ( args[i].getTag().equals(tag) )
  88                 result[resCount++] = i;
  89         }
  90 
  91         return Arrays.copyOf(result, resCount);
  92     }
  93 
  94 }