rt/emul/compact/src/main/java/sun/invoke/util/BytecodeDescriptor.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 09 Aug 2014 11:11:13 +0200
branchjdk8-b132
changeset 1646 c880a8a8803b
permissions -rw-r--r--
Batch of classes necessary to implement invoke dynamic interfaces. Taken from JDK8 build 132
     1 /*
     2  * Copyright (c) 2008, 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 sun.invoke.util;
    27 
    28 import java.lang.invoke.MethodType;
    29 import java.util.ArrayList;
    30 import java.util.List;
    31 
    32 /**
    33  * Utility routines for dealing with bytecode-level signatures.
    34  * @author jrose
    35  */
    36 public class BytecodeDescriptor {
    37 
    38     private BytecodeDescriptor() { }  // cannot instantiate
    39 
    40     public static List<Class<?>> parseMethod(String bytecodeSignature, ClassLoader loader) {
    41         return parseMethod(bytecodeSignature, 0, bytecodeSignature.length(), loader);
    42     }
    43 
    44     static List<Class<?>> parseMethod(String bytecodeSignature,
    45             int start, int end, ClassLoader loader) {
    46         if (loader == null)
    47             loader = ClassLoader.getSystemClassLoader();
    48         String str = bytecodeSignature;
    49         int[] i = {start};
    50         ArrayList<Class<?>> ptypes = new ArrayList<Class<?>>();
    51         if (i[0] < end && str.charAt(i[0]) == '(') {
    52             ++i[0];  // skip '('
    53             while (i[0] < end && str.charAt(i[0]) != ')') {
    54                 Class<?> pt = parseSig(str, i, end, loader);
    55                 if (pt == null || pt == void.class)
    56                     parseError(str, "bad argument type");
    57                 ptypes.add(pt);
    58             }
    59             ++i[0];  // skip ')'
    60         } else {
    61             parseError(str, "not a method type");
    62         }
    63         Class<?> rtype = parseSig(str, i, end, loader);
    64         if (rtype == null || i[0] != end)
    65             parseError(str, "bad return type");
    66         ptypes.add(rtype);
    67         return ptypes;
    68     }
    69 
    70     static private void parseError(String str, String msg) {
    71         throw new IllegalArgumentException("bad signature: "+str+": "+msg);
    72     }
    73 
    74     static private Class<?> parseSig(String str, int[] i, int end, ClassLoader loader) {
    75         if (i[0] == end)  return null;
    76         char c = str.charAt(i[0]++);
    77         if (c == 'L') {
    78             int begc = i[0], endc = str.indexOf(';', begc);
    79             if (endc < 0)  return null;
    80             i[0] = endc+1;
    81             String name = str.substring(begc, endc).replace('/', '.');
    82             try {
    83                 return loader.loadClass(name);
    84             } catch (ClassNotFoundException ex) {
    85                 throw new TypeNotPresentException(name, ex);
    86             }
    87         } else if (c == '[') {
    88             Class<?> t = parseSig(str, i, end, loader);
    89             if (t != null)
    90                 t = java.lang.reflect.Array.newInstance(t, 0).getClass();
    91             return t;
    92         } else {
    93             return Wrapper.forBasicType(c).primitiveType();
    94         }
    95     }
    96 
    97     public static String unparse(Class<?> type) {
    98         StringBuilder sb = new StringBuilder();
    99         unparseSig(type, sb);
   100         return sb.toString();
   101     }
   102 
   103     public static String unparse(MethodType type) {
   104         return unparseMethod(type.returnType(), type.parameterList());
   105     }
   106 
   107     public static String unparse(Object type) {
   108         if (type instanceof Class<?>)
   109             return unparse((Class<?>) type);
   110         if (type instanceof MethodType)
   111             return unparse((MethodType) type);
   112         return (String) type;
   113     }
   114 
   115     public static String unparseMethod(Class<?> rtype, List<Class<?>> ptypes) {
   116         StringBuilder sb = new StringBuilder();
   117         sb.append('(');
   118         for (Class<?> pt : ptypes)
   119             unparseSig(pt, sb);
   120         sb.append(')');
   121         unparseSig(rtype, sb);
   122         return sb.toString();
   123     }
   124 
   125     static private void unparseSig(Class<?> t, StringBuilder sb) {
   126         char c = Wrapper.forBasicType(t).basicTypeChar();
   127         if (c != 'L') {
   128             sb.append(c);
   129         } else {
   130             boolean lsemi = (!t.isArray());
   131             if (lsemi)  sb.append('L');
   132             sb.append(t.getName().replace('.', '/'));
   133             if (lsemi)  sb.append(';');
   134         }
   135     }
   136 
   137 }