< prev index next >

src/java.base/share/classes/java/lang/invoke/MethodHandleProxies.java

Print this page


   1 /*
   2  * Copyright (c) 2008, 2013, 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


 199             // sun.invoke.WrapperInstance is a restricted interface not accessible
 200             // by any non-null class loader.
 201             final ClassLoader loader = proxyLoader;
 202             proxy = AccessController.doPrivileged(new PrivilegedAction<>() {
 203                 public Object run() {
 204                     return Proxy.newProxyInstance(
 205                             loader,
 206                             new Class<?>[]{ intfc, WrapperInstance.class },
 207                             ih);
 208                 }
 209             });
 210         } else {
 211             proxy = Proxy.newProxyInstance(proxyLoader,
 212                                            new Class<?>[]{ intfc, WrapperInstance.class },
 213                                            ih);
 214         }
 215         return intfc.cast(proxy);
 216     }
 217 
 218     private static MethodHandle bindCaller(MethodHandle target, Class<?> hostClass) {
 219         MethodHandle cbmh = MethodHandleImpl.bindCaller(target, hostClass);
 220         if (target.isVarargsCollector()) {
 221             MethodType type = cbmh.type();
 222             int arity = type.parameterCount();
 223             return cbmh.asVarargsCollector(type.parameterType(arity-1));
 224         }
 225         return cbmh;
 226     }
 227 
 228     /**
 229      * Determines if the given object was produced by a call to {@link #asInterfaceInstance asInterfaceInstance}.
 230      * @param x any reference
 231      * @return true if the reference is not null and points to an object produced by {@code asInterfaceInstance}
 232      */
 233     public static
 234     boolean isWrapperInstance(Object x) {
 235         return x instanceof WrapperInstance;
 236     }
 237 
 238     private static WrapperInstance asWrapperInstance(Object x) {
 239         try {
 240             if (x != null)
 241                 return (WrapperInstance) x;
 242         } catch (ClassCastException ex) {
 243         }
 244         throw newIllegalArgumentException("not a wrapper instance");
 245     }


   1 /*
   2  * Copyright (c) 2008, 2016, 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


 199             // sun.invoke.WrapperInstance is a restricted interface not accessible
 200             // by any non-null class loader.
 201             final ClassLoader loader = proxyLoader;
 202             proxy = AccessController.doPrivileged(new PrivilegedAction<>() {
 203                 public Object run() {
 204                     return Proxy.newProxyInstance(
 205                             loader,
 206                             new Class<?>[]{ intfc, WrapperInstance.class },
 207                             ih);
 208                 }
 209             });
 210         } else {
 211             proxy = Proxy.newProxyInstance(proxyLoader,
 212                                            new Class<?>[]{ intfc, WrapperInstance.class },
 213                                            ih);
 214         }
 215         return intfc.cast(proxy);
 216     }
 217 
 218     private static MethodHandle bindCaller(MethodHandle target, Class<?> hostClass) {
 219         return MethodHandleImpl.bindCaller(target, hostClass).withVarargs(target.isVarargsCollector());






 220     }
 221 
 222     /**
 223      * Determines if the given object was produced by a call to {@link #asInterfaceInstance asInterfaceInstance}.
 224      * @param x any reference
 225      * @return true if the reference is not null and points to an object produced by {@code asInterfaceInstance}
 226      */
 227     public static
 228     boolean isWrapperInstance(Object x) {
 229         return x instanceof WrapperInstance;
 230     }
 231 
 232     private static WrapperInstance asWrapperInstance(Object x) {
 233         try {
 234             if (x != null)
 235                 return (WrapperInstance) x;
 236         } catch (ClassCastException ex) {
 237         }
 238         throw newIllegalArgumentException("not a wrapper instance");
 239     }


< prev index next >