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