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.MethodHandles;
  28 import java.lang.invoke.MethodType;
  29 
  30 public class MHSamTF extends MHBasicUnaryTF {
  31 
  32     public static interface SAM {
  33         public Object run(Object[] o) throws Throwable;
  34     }
  35 
  36     public static class SAMCaller {
  37         private final SAM sam;
  38 
  39         public SAMCaller(SAM sam) {
  40             this.sam = sam;
  41         }
  42 
  43         public Object callSAM(Object[] o) throws Throwable {
  44             return sam.run(o);
  45         }
  46     }
  47 
  48     public MHSamTF(MHCall target) {
  49         super(target);
  50     }
  51 
  52     @Override
  53     protected MethodHandle computeInboundMH(MethodHandle targetMH) throws NoSuchMethodException, IllegalAccessException {
  54         throw new RuntimeException("Internal error: Functionality is disabled in JDK7");
  55         /*
  56         MethodHandle mh = targetMH.asSpreader(Object[].class, targetMH.type().parameterCount());
  57 
  58         SAM sam = MethodHandles.asInstance(mh, SAM.class);
  59 
  60         // The checks below aimed to increase coverage
  61         MethodHandle mhCopy = MethodHandles.wrapperInstanceTarget(sam);
  62         if ( ! mh.equals(mhCopy) ) {
  63             throw new IllegalArgumentException("wrapperInstanceTarget returned a different MH: [" + mhCopy + "]; original was [" + mh + "]");
  64         }
  65 
  66         Class<?> samClass = MethodHandles.wrapperInstanceType(sam);
  67         if ( ! SAM.class.equals(samClass) ) {
  68             throw new IllegalArgumentException("wrapperInstanceType returned a different class: [" + samClass + "]; original was [" + SAM.class + "]");
  69         }
  70 
  71         if ( ! MethodHandles.isWrapperInstance(sam) ) {
  72             throw new IllegalArgumentException("isWrapperInstance returned false for SAM object: [" + sam + "]");
  73         }
  74 
  75         return MethodHandles.convertArguments(
  76                    MethodHandles.lookup().findVirtual(SAMCaller.class, "callSAM", MethodType.methodType(Object.class, Object[].class))
  77                        .bindTo(new SAMCaller(sam))
  78                        .asCollector(Object[].class, targetMH.type().parameterCount()),
  79                    targetMH.type());
  80 
  81         */
  82     }
  83 
  84     @Override
  85     protected String getName() {
  86         return "SAM";
  87     }
  88 
  89     @Override
  90     protected String getDescription() {
  91         return "";
  92     }
  93 }