jdk/src/share/classes/java/dyn/MethodHandleStatics.java
changeset 8821 2836ee97ee27
equal deleted inserted replaced
8693:2173b8120b13 8821:2836ee97ee27
       
     1 /*
       
     2  * Copyright (c) 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.dyn;
       
    27 
       
    28 /**
       
    29  * This class consists exclusively of static names internal to the
       
    30  * method handle implementation.
       
    31  * Usage:  {@code import static java.dyn.MethodHandleStatics.*}
       
    32  * @author John Rose, JSR 292 EG
       
    33  */
       
    34 /*non-public*/ class MethodHandleStatics {
       
    35 
       
    36     private MethodHandleStatics() { }  // do not instantiate
       
    37 
       
    38     /*non-public*/ static String getNameString(MethodHandle target, MethodType type) {
       
    39         if (type == null)
       
    40             type = target.type();
       
    41         MemberName name = null;
       
    42         if (target != null)
       
    43             name = MethodHandleNatives.getMethodName(target);
       
    44         if (name == null)
       
    45             return "invoke" + type;
       
    46         return name.getName() + type;
       
    47     }
       
    48 
       
    49     /*non-public*/ static String getNameString(MethodHandle target, MethodHandle typeHolder) {
       
    50         return getNameString(target, typeHolder == null ? (MethodType) null : typeHolder.type());
       
    51     }
       
    52 
       
    53     /*non-public*/ static String getNameString(MethodHandle target) {
       
    54         return getNameString(target, (MethodType) null);
       
    55     }
       
    56 
       
    57     /*non-public*/ static String addTypeString(Object obj, MethodHandle target) {
       
    58         String str = String.valueOf(obj);
       
    59         if (target == null)  return str;
       
    60         int paren = str.indexOf('(');
       
    61         if (paren >= 0) str = str.substring(0, paren);
       
    62         return str + target.type();
       
    63     }
       
    64 
       
    65     static void checkSpreadArgument(Object av, int n) {
       
    66         if (av == null ? n != 0 : ((Object[])av).length != n)
       
    67             throw newIllegalArgumentException("Array is not of length "+n);
       
    68     }
       
    69 
       
    70     // handy shared exception makers (they simplify the common case code)
       
    71     /*non-public*/ static RuntimeException newIllegalStateException(String message) {
       
    72         return new IllegalStateException(message);
       
    73     }
       
    74     /*non-public*/ static RuntimeException newIllegalStateException(String message, Object obj) {
       
    75         return new IllegalStateException(message(message, obj));
       
    76     }
       
    77     /*non-public*/ static RuntimeException newIllegalArgumentException(String message) {
       
    78         return new IllegalArgumentException(message);
       
    79     }
       
    80     /*non-public*/ static RuntimeException newIllegalArgumentException(String message, Object obj) {
       
    81         return new IllegalArgumentException(message(message, obj));
       
    82     }
       
    83     /*non-public*/ static Error uncaughtException(Exception ex) {
       
    84         Error err = new InternalError("uncaught exception");
       
    85         err.initCause(ex);
       
    86         return err;
       
    87     }
       
    88     private static String message(String message, Object obj) {
       
    89         if (obj != null)  message = message + ": " + obj;
       
    90         return message;
       
    91     }
       
    92 }