jdk/src/share/classes/java/lang/invoke/MethodHandleStatics.java
author jrose
Thu, 12 May 2011 19:27:49 -0700
changeset 9646 5ebbe5ab084f
parent 8822 8145ab9f5f86
child 9731 d0f7a3e441c4
permissions -rw-r--r--
6939861: JVM should handle more conversion operations Summary: Integrate JDK code with JVM-supplied ricochet frames. Reviewed-by: never, twisti
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
     1
/*
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
     4
 *
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    10
 *
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    15
 * accompanied this code).
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    16
 *
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    20
 *
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    23
 * questions.
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    24
 */
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    25
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    26
package java.lang.invoke;
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    27
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    28
/**
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    29
 * This class consists exclusively of static names internal to the
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    30
 * method handle implementation.
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    31
 * Usage:  {@code import static java.lang.invoke.MethodHandleStatics.*}
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    32
 * @author John Rose, JSR 292 EG
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    33
 */
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    34
/*non-public*/ class MethodHandleStatics {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    35
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    36
    private MethodHandleStatics() { }  // do not instantiate
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    37
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    38
    /*non-public*/ static String getNameString(MethodHandle target, MethodType type) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    39
        if (type == null)
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    40
            type = target.type();
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    41
        MemberName name = null;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    42
        if (target != null)
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    43
            name = MethodHandleNatives.getMethodName(target);
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    44
        if (name == null)
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    45
            return "invoke" + type;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    46
        return name.getName() + type;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    47
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    48
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    49
    /*non-public*/ static String getNameString(MethodHandle target, MethodHandle typeHolder) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    50
        return getNameString(target, typeHolder == null ? (MethodType) null : typeHolder.type());
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    51
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    52
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    53
    /*non-public*/ static String getNameString(MethodHandle target) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    54
        return getNameString(target, (MethodType) null);
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    55
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    56
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    57
    /*non-public*/ static String addTypeString(Object obj, MethodHandle target) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    58
        String str = String.valueOf(obj);
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    59
        if (target == null)  return str;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    60
        int paren = str.indexOf('(');
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    61
        if (paren >= 0) str = str.substring(0, paren);
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    62
        return str + target.type();
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    63
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    64
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    65
    static void checkSpreadArgument(Object av, int n) {
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    66
        if (av == null) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    67
            if (n == 0)  return;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    68
        } else if (av instanceof Object[]) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    69
            int len = ((Object[])av).length;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    70
            if (len == n)  return;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    71
        } else {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    72
            int len = java.lang.reflect.Array.getLength(av);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    73
            if (len == n)  return;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    74
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    75
        // fall through to error:
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    76
        throw newIllegalArgumentException("Array is not of length "+n);
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    77
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    78
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    79
    // handy shared exception makers (they simplify the common case code)
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    80
    /*non-public*/ static RuntimeException newIllegalStateException(String message) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    81
        return new IllegalStateException(message);
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    82
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    83
    /*non-public*/ static RuntimeException newIllegalStateException(String message, Object obj) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    84
        return new IllegalStateException(message(message, obj));
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    85
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    86
    /*non-public*/ static RuntimeException newIllegalArgumentException(String message) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    87
        return new IllegalArgumentException(message);
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    88
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    89
    /*non-public*/ static RuntimeException newIllegalArgumentException(String message, Object obj) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    90
        return new IllegalArgumentException(message(message, obj));
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    91
    }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    92
    /*non-public*/ static RuntimeException newIllegalArgumentException(String message, Object obj, Object obj2) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    93
        return new IllegalArgumentException(message(message, obj, obj2));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    94
    }
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    95
    /*non-public*/ static Error uncaughtException(Exception ex) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    96
        Error err = new InternalError("uncaught exception");
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    97
        err.initCause(ex);
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    98
        return err;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
    99
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
   100
    private static String message(String message, Object obj) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
   101
        if (obj != null)  message = message + ": " + obj;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
   102
        return message;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
   103
    }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   104
    private static String message(String message, Object obj, Object obj2) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   105
        if (obj != null || obj2 != null)  message = message + ": " + obj + ", " + obj2;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   106
        return message;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   107
    }
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
diff changeset
   108
}