< prev index next >

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

Print this page
rev 13458 : 8147078: MethodHandles.catchException does not enforce Throwable subtype
   1 /*
   2  * Copyright (c) 2008, 2014, 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


3103      * To create such a throwing handler, compose the handler creation logic
3104      * with {@link #throwException throwException},
3105      * in order to create a method handle of the correct return type.
3106      * @param target method handle to call
3107      * @param exType the type of exception which the handler will catch
3108      * @param handler method handle to call if a matching exception is thrown
3109      * @return method handle which incorporates the specified try/catch logic
3110      * @throws NullPointerException if any argument is null
3111      * @throws IllegalArgumentException if {@code handler} does not accept
3112      *          the given exception type, or if the method handle types do
3113      *          not match in their return types and their
3114      *          corresponding parameters
3115      * @see MethodHandles#tryFinally(MethodHandle, MethodHandle)
3116      */
3117     public static
3118     MethodHandle catchException(MethodHandle target,
3119                                 Class<? extends Throwable> exType,
3120                                 MethodHandle handler) {
3121         MethodType ttype = target.type();
3122         MethodType htype = handler.type();


3123         if (htype.parameterCount() < 1 ||
3124             !htype.parameterType(0).isAssignableFrom(exType))
3125             throw newIllegalArgumentException("handler does not accept exception type "+exType);
3126         if (htype.returnType() != ttype.returnType())
3127             throw misMatchedTypes("target and handler return types", ttype, htype);
3128         List<Class<?>> targs = ttype.parameterList();
3129         List<Class<?>> hargs = htype.parameterList();
3130         hargs = hargs.subList(1, hargs.size());  // omit leading parameter from handler
3131         if (!targs.equals(hargs)) {
3132             int hpc = hargs.size(), tpc = targs.size();
3133             if (hpc >= tpc || !targs.subList(0, hpc).equals(hargs))
3134                 throw misMatchedTypes("target and handler types", ttype, htype);
3135             handler = dropArguments(handler, 1+hpc, targs.subList(hpc, tpc));
3136             htype = handler.type();
3137         }
3138         return MethodHandleImpl.makeGuardWithCatch(target, exType, handler);
3139     }
3140 
3141     /**
3142      * Produces a method handle which will throw exceptions of the given {@code exType}.


   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


3103      * To create such a throwing handler, compose the handler creation logic
3104      * with {@link #throwException throwException},
3105      * in order to create a method handle of the correct return type.
3106      * @param target method handle to call
3107      * @param exType the type of exception which the handler will catch
3108      * @param handler method handle to call if a matching exception is thrown
3109      * @return method handle which incorporates the specified try/catch logic
3110      * @throws NullPointerException if any argument is null
3111      * @throws IllegalArgumentException if {@code handler} does not accept
3112      *          the given exception type, or if the method handle types do
3113      *          not match in their return types and their
3114      *          corresponding parameters
3115      * @see MethodHandles#tryFinally(MethodHandle, MethodHandle)
3116      */
3117     public static
3118     MethodHandle catchException(MethodHandle target,
3119                                 Class<? extends Throwable> exType,
3120                                 MethodHandle handler) {
3121         MethodType ttype = target.type();
3122         MethodType htype = handler.type();
3123         if (!Throwable.class.isAssignableFrom(exType))
3124             throw new ClassCastException(exType.getName());
3125         if (htype.parameterCount() < 1 ||
3126             !htype.parameterType(0).isAssignableFrom(exType))
3127             throw newIllegalArgumentException("handler does not accept exception type "+exType);
3128         if (htype.returnType() != ttype.returnType())
3129             throw misMatchedTypes("target and handler return types", ttype, htype);
3130         List<Class<?>> targs = ttype.parameterList();
3131         List<Class<?>> hargs = htype.parameterList();
3132         hargs = hargs.subList(1, hargs.size());  // omit leading parameter from handler
3133         if (!targs.equals(hargs)) {
3134             int hpc = hargs.size(), tpc = targs.size();
3135             if (hpc >= tpc || !targs.subList(0, hpc).equals(hargs))
3136                 throw misMatchedTypes("target and handler types", ttype, htype);
3137             handler = dropArguments(handler, 1+hpc, targs.subList(hpc, tpc));
3138             htype = handler.type();
3139         }
3140         return MethodHandleImpl.makeGuardWithCatch(target, exType, handler);
3141     }
3142 
3143     /**
3144      * Produces a method handle which will throw exceptions of the given {@code exType}.


< prev index next >