Home » openjdk-7 » java.lang.invoke » [javadoc | source]

    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.lang.invoke;
   27   
   28   import java.security.AccessController;
   29   import java.security.PrivilegedAction;
   30   
   31   /**
   32    * This class consists exclusively of static names internal to the
   33    * method handle implementation.
   34    * Usage:  {@code import static java.lang.invoke.MethodHandleStatics.*}
   35    * @author John Rose, JSR 292 EG
   36    */
   37   /*non-public*/ class MethodHandleStatics {
   38   
   39       private MethodHandleStatics() { }  // do not instantiate
   40   
   41       static final boolean DEBUG_METHOD_HANDLE_NAMES;
   42       static {
   43           final Object[] values = { false };
   44           AccessController.doPrivileged(new PrivilegedAction<Void>() {
   45                   public Void run() {
   46                       values[0] = Boolean.getBoolean("java.lang.invoke.MethodHandle.DEBUG_NAMES");
   47                       return null;
   48                   }
   49               });
   50           DEBUG_METHOD_HANDLE_NAMES = (Boolean) values[0];
   51       }
   52   
   53       /*non-public*/ static String getNameString(MethodHandle target, MethodType type) {
   54           if (type == null)
   55               type = target.type();
   56           MemberName name = null;
   57           if (target != null)
   58               name = MethodHandleNatives.getMethodName(target);
   59           if (name == null)
   60               return "invoke" + type;
   61           return name.getName() + type;
   62       }
   63   
   64       /*non-public*/ static String getNameString(MethodHandle target, MethodHandle typeHolder) {
   65           return getNameString(target, typeHolder == null ? (MethodType) null : typeHolder.type());
   66       }
   67   
   68       /*non-public*/ static String getNameString(MethodHandle target) {
   69           return getNameString(target, (MethodType) null);
   70       }
   71   
   72       /*non-public*/ static String addTypeString(Object obj, MethodHandle target) {
   73           String str = String.valueOf(obj);
   74           if (target == null)  return str;
   75           int paren = str.indexOf('(');
   76           if (paren >= 0) str = str.substring(0, paren);
   77           return str + target.type();
   78       }
   79   
   80       static void checkSpreadArgument(Object av, int n) {
   81           if (av == null) {
   82               if (n == 0)  return;
   83           } else if (av instanceof Object[]) {
   84               int len = ((Object[])av).length;
   85               if (len == n)  return;
   86           } else {
   87               int len = java.lang.reflect.Array.getLength(av);
   88               if (len == n)  return;
   89           }
   90           // fall through to error:
   91           throw newIllegalArgumentException("Array is not of length "+n);
   92       }
   93   
   94       // handy shared exception makers (they simplify the common case code)
   95       /*non-public*/ static RuntimeException newIllegalStateException(String message) {
   96           return new IllegalStateException(message);
   97       }
   98       /*non-public*/ static RuntimeException newIllegalStateException(String message, Object obj) {
   99           return new IllegalStateException(message(message, obj));
  100       }
  101       /*non-public*/ static RuntimeException newIllegalArgumentException(String message) {
  102           return new IllegalArgumentException(message);
  103       }
  104       /*non-public*/ static RuntimeException newIllegalArgumentException(String message, Object obj) {
  105           return new IllegalArgumentException(message(message, obj));
  106       }
  107       /*non-public*/ static RuntimeException newIllegalArgumentException(String message, Object obj, Object obj2) {
  108           return new IllegalArgumentException(message(message, obj, obj2));
  109       }
  110       /*non-public*/ static Error uncaughtException(Exception ex) {
  111           Error err = new InternalError("uncaught exception");
  112           err.initCause(ex);
  113           return err;
  114       }
  115       private static String message(String message, Object obj) {
  116           if (obj != null)  message = message + ": " + obj;
  117           return message;
  118       }
  119       private static String message(String message, Object obj, Object obj2) {
  120           if (obj != null || obj2 != null)  message = message + ": " + obj + ", " + obj2;
  121           return message;
  122       }
  123   }

Home » openjdk-7 » java.lang.invoke » [javadoc | source]