1 /*
   2  * Copyright (c) 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.runtime;
  26 
  27 import java.lang.invoke.MethodHandle;
  28 import java.lang.invoke.MethodHandles;
  29 import java.lang.invoke.MethodType;
  30 import java.util.Arrays;
  31 import java.util.List;
  32 
  33 /**
  34  * PatternCarriers
  35  */
  36 class PatternCarriers {
  37 
  38     private static final CarrierFactory factory = CarrierFactories.DUMB;
  39 
  40     interface CarrierFactory {
  41         MethodHandle constructor(MethodType methodType);
  42         MethodHandle component(MethodType methodType, int component);
  43     }
  44 
  45     static class DumbCarrier {
  46         private final Object[] args;
  47 
  48         DumbCarrier(Object... args) {
  49             this.args = args.clone();
  50         }
  51 
  52         Object get(int i) {
  53             return args[i];
  54         }
  55     }
  56 
  57     enum CarrierFactories implements CarrierFactory {
  58         DUMB {
  59             private final MethodHandle CARRIER_CTOR;
  60             private final MethodHandle CARRIER_GET;
  61 
  62             {
  63                 try {
  64                     CARRIER_CTOR = MethodHandles.lookup().findConstructor(DumbCarrier.class, MethodType.methodType(void.class, Object[].class));
  65                     CARRIER_GET = MethodHandles.lookup().findVirtual(DumbCarrier.class, "get", MethodType.methodType(Object.class, int.class));
  66                 }
  67                 catch (ReflectiveOperationException e) {
  68                     throw new ExceptionInInitializerError(e);
  69                 }
  70             }
  71 
  72             @Override
  73             public MethodHandle constructor(MethodType methodType) {
  74                 return CARRIER_CTOR.asType(methodType.changeReturnType(Object.class));
  75             }
  76 
  77             @Override
  78             public MethodHandle component(MethodType methodType, int component) {
  79                 return MethodHandles.insertArguments(CARRIER_GET, 1, component)
  80                                     .asType(MethodType.methodType(methodType.parameterType(component), Object.class));
  81             }
  82         },
  83         DUMB_SINGLE {
  84             // An optimization of DUMB, where we use the value itself as carrier when there is only one value
  85 
  86             @Override
  87             public MethodHandle constructor(MethodType methodType) {
  88                 return methodType.parameterCount() == 1 ? MethodHandles.identity(methodType.parameterType(0)) : DUMB.constructor(methodType);
  89             }
  90 
  91             @Override
  92             public MethodHandle component(MethodType methodType, int component) {
  93                 return methodType.parameterCount() == 1 ? MethodHandles.identity(methodType.parameterType(0)) : DUMB.component(methodType, component);
  94             }
  95         }
  96     }
  97 
  98     /**
  99      * Returns a method handle with the given method type that instantiates
 100      * a new carrier object.
 101      *
 102      * @param methodType the types of the carrier elements
 103      * @return the carrier factory
 104      */
 105     public static MethodHandle carrierFactory(MethodType methodType) {
 106         return factory.constructor(methodType);
 107     }
 108 
 109     /**
 110      * Returns a method handle that accepts a carrier and returns the i'th component
 111      *
 112      * @param methodType the type of the carrier elements
 113      * @param i the index of the component
 114      * @return the component method handle
 115      */
 116     public static MethodHandle carrierComponent(MethodType methodType, int i) {
 117         return factory.component(methodType, i);
 118     }
 119 
 120     /**
 121      * Return all the components method handles for a carrier
 122      * @param methodType the type of the carrier elements
 123      * @return the component method handles
 124      */
 125     public static List<MethodHandle> carrierComponents(MethodType methodType) {
 126         MethodHandle[] components = new MethodHandle[methodType.parameterCount()];
 127         Arrays.setAll(components, i -> factory.component(methodType, i));
 128         return List.of(components);
 129     }
 130 }