rt/emul/compact/src/main/java/java/lang/invoke/MethodHandleStatics.java
branchjdk8-b132
changeset 1646 c880a8a8803b
child 1651 5c990ed353e9
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/main/java/java/lang/invoke/MethodHandleStatics.java	Sat Aug 09 11:11:13 2014 +0200
     1.3 @@ -0,0 +1,131 @@
     1.4 +/*
     1.5 + * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package java.lang.invoke;
    1.30 +
    1.31 +import java.security.AccessController;
    1.32 +import java.security.PrivilegedAction;
    1.33 +import sun.misc.Unsafe;
    1.34 +
    1.35 +/**
    1.36 + * This class consists exclusively of static names internal to the
    1.37 + * method handle implementation.
    1.38 + * Usage:  {@code import static java.lang.invoke.MethodHandleStatics.*}
    1.39 + * @author John Rose, JSR 292 EG
    1.40 + */
    1.41 +/*non-public*/ class MethodHandleStatics {
    1.42 +
    1.43 +    private MethodHandleStatics() { }  // do not instantiate
    1.44 +
    1.45 +    static final Unsafe UNSAFE = Unsafe.getUnsafe();
    1.46 +
    1.47 +    static final boolean DEBUG_METHOD_HANDLE_NAMES;
    1.48 +    static final boolean DUMP_CLASS_FILES;
    1.49 +    static final boolean TRACE_INTERPRETER;
    1.50 +    static final boolean TRACE_METHOD_LINKAGE;
    1.51 +    static final Integer COMPILE_THRESHOLD;
    1.52 +    static {
    1.53 +        final Object[] values = { false, false, false, false, null };
    1.54 +        AccessController.doPrivileged(new PrivilegedAction<Void>() {
    1.55 +                public Void run() {
    1.56 +                    values[0] = Boolean.getBoolean("java.lang.invoke.MethodHandle.DEBUG_NAMES");
    1.57 +                    values[1] = Boolean.getBoolean("java.lang.invoke.MethodHandle.DUMP_CLASS_FILES");
    1.58 +                    values[2] = Boolean.getBoolean("java.lang.invoke.MethodHandle.TRACE_INTERPRETER");
    1.59 +                    values[3] = Boolean.getBoolean("java.lang.invoke.MethodHandle.TRACE_METHOD_LINKAGE");
    1.60 +                    values[4] = Integer.getInteger("java.lang.invoke.MethodHandle.COMPILE_THRESHOLD");
    1.61 +                    return null;
    1.62 +                }
    1.63 +            });
    1.64 +        DEBUG_METHOD_HANDLE_NAMES = (Boolean) values[0];
    1.65 +        DUMP_CLASS_FILES          = (Boolean) values[1];
    1.66 +        TRACE_INTERPRETER         = (Boolean) values[2];
    1.67 +        TRACE_METHOD_LINKAGE      = (Boolean) values[3];
    1.68 +        COMPILE_THRESHOLD         = (Integer) values[4];
    1.69 +    }
    1.70 +
    1.71 +    /*non-public*/ static String getNameString(MethodHandle target, MethodType type) {
    1.72 +        if (type == null)
    1.73 +            type = target.type();
    1.74 +        MemberName name = null;
    1.75 +        if (target != null)
    1.76 +            name = target.internalMemberName();
    1.77 +        if (name == null)
    1.78 +            return "invoke" + type;
    1.79 +        return name.getName() + type;
    1.80 +    }
    1.81 +
    1.82 +    /*non-public*/ static String getNameString(MethodHandle target, MethodHandle typeHolder) {
    1.83 +        return getNameString(target, typeHolder == null ? (MethodType) null : typeHolder.type());
    1.84 +    }
    1.85 +
    1.86 +    /*non-public*/ static String getNameString(MethodHandle target) {
    1.87 +        return getNameString(target, (MethodType) null);
    1.88 +    }
    1.89 +
    1.90 +    /*non-public*/ static String addTypeString(Object obj, MethodHandle target) {
    1.91 +        String str = String.valueOf(obj);
    1.92 +        if (target == null)  return str;
    1.93 +        int paren = str.indexOf('(');
    1.94 +        if (paren >= 0) str = str.substring(0, paren);
    1.95 +        return str + target.type();
    1.96 +    }
    1.97 +
    1.98 +    // handy shared exception makers (they simplify the common case code)
    1.99 +    /*non-public*/ static InternalError newInternalError(String message, Throwable cause) {
   1.100 +        return new InternalError(message, cause);
   1.101 +    }
   1.102 +    /*non-public*/ static InternalError newInternalError(Throwable cause) {
   1.103 +        return new InternalError(cause);
   1.104 +    }
   1.105 +    /*non-public*/ static RuntimeException newIllegalStateException(String message) {
   1.106 +        return new IllegalStateException(message);
   1.107 +    }
   1.108 +    /*non-public*/ static RuntimeException newIllegalStateException(String message, Object obj) {
   1.109 +        return new IllegalStateException(message(message, obj));
   1.110 +    }
   1.111 +    /*non-public*/ static RuntimeException newIllegalArgumentException(String message) {
   1.112 +        return new IllegalArgumentException(message);
   1.113 +    }
   1.114 +    /*non-public*/ static RuntimeException newIllegalArgumentException(String message, Object obj) {
   1.115 +        return new IllegalArgumentException(message(message, obj));
   1.116 +    }
   1.117 +    /*non-public*/ static RuntimeException newIllegalArgumentException(String message, Object obj, Object obj2) {
   1.118 +        return new IllegalArgumentException(message(message, obj, obj2));
   1.119 +    }
   1.120 +    /*non-public*/ static Error uncaughtException(Throwable ex) {
   1.121 +        throw newInternalError("uncaught exception", ex);
   1.122 +    }
   1.123 +    static Error NYI() {
   1.124 +        throw new AssertionError("NYI");
   1.125 +    }
   1.126 +    private static String message(String message, Object obj) {
   1.127 +        if (obj != null)  message = message + ": " + obj;
   1.128 +        return message;
   1.129 +    }
   1.130 +    private static String message(String message, Object obj, Object obj2) {
   1.131 +        if (obj != null || obj2 != null)  message = message + ": " + obj + ", " + obj2;
   1.132 +        return message;
   1.133 +    }
   1.134 +}