1 /*
   2  * Copyright (c) 2008, 2009, 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 
  26 package sun.dyn;
  27 
  28 import java.dyn.*;
  29 import static sun.dyn.MemberName.uncaughtException;
  30 
  31 /**
  32  * Unary function composition, useful for many small plumbing jobs.
  33  * The invoke method takes a single reference argument, and returns a reference
  34  * Internally, it first calls the {@code filter} method on the argument,
  35  * Making up the difference between the raw method type and the
  36  * final method type is the responsibility of a JVM-level adapter.
  37  * @author jrose
  38  */
  39 public class FilterOneArgument extends BoundMethodHandle {
  40     protected final MethodHandle filter;  // Object -> Object
  41     protected final MethodHandle target;  // Object -> Object
  42 
  43     @Override
  44     public String toString() {
  45         return target.toString();
  46     }
  47 
  48     protected Object invoke(Object argument) throws Throwable {
  49         Object filteredArgument = filter.invokeExact(argument);
  50         return target.invokeExact(filteredArgument);
  51     }
  52 
  53     private static final MethodHandle INVOKE;
  54     static {
  55         try {
  56             INVOKE =
  57                 MethodHandleImpl.IMPL_LOOKUP.findVirtual(FilterOneArgument.class, "invoke",
  58                                                          MethodType.genericMethodType(1));
  59         } catch (NoAccessException ex) {
  60             throw uncaughtException(ex);
  61         }
  62     }
  63 
  64     protected FilterOneArgument(MethodHandle filter, MethodHandle target) {
  65         super(Access.TOKEN, INVOKE);
  66         this.filter = filter;
  67         this.target = target;
  68     }
  69 
  70     public static MethodHandle make(MethodHandle filter, MethodHandle target) {
  71         if (filter == null)  return target;
  72         if (target == null)  return filter;
  73         return new FilterOneArgument(filter, target);
  74     }
  75 
  76 //    MethodHandle make(MethodHandle filter1, MethodHandle filter2, MethodHandle target) {
  77 //        MethodHandle filter = make(filter1, filter2);
  78 //        return make(filter, target);
  79 //    }
  80 }