Home » openjdk-7 » java.lang.invoke » [javadoc | source]

    1   /*
    2    * Copyright (c) 2008, 2011, 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 java.lang.invoke;
   27   
   28   import static java.lang.invoke.MethodHandleStatics.*;
   29   import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
   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   class FilterOneArgument extends BoundMethodHandle {
   40       protected final MethodHandle filter;  // Object -> Object
   41       protected final MethodHandle target;  // Object -> Object
   42   
   43       @Override
   44       String debugString() {
   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                   IMPL_LOOKUP.findVirtual(FilterOneArgument.class, "invoke",
   58                                           MethodType.genericMethodType(1));
   59           } catch (ReflectiveOperationException ex) {
   60               throw uncaughtException(ex);
   61           }
   62       }
   63   
   64       protected FilterOneArgument(MethodHandle filter, MethodHandle target) {
   65           super(INVOKE);
   66           this.filter = filter;
   67           this.target = target;
   68       }
   69   
   70       static {
   71           assert(MethodHandleNatives.workaroundWithoutRicochetFrames());  // this class is deprecated
   72       }
   73   
   74       public static MethodHandle make(MethodHandle filter, MethodHandle target) {
   75           if (filter == null)  return target;
   76           if (target == null)  return filter;
   77           return new FilterOneArgument(filter, target);
   78       }
   79   
   80   //    MethodHandle make(MethodHandle filter1, MethodHandle filter2, MethodHandle target) {
   81   //        MethodHandle filter = make(filter1, filter2);
   82   //        return make(filter, target);
   83   //    }
   84   }

Home » openjdk-7 » java.lang.invoke » [javadoc | source]