rt/emul/compact/src/main/java/sun/invoke/util/BytecodeDescriptor.java
branchjdk8
changeset 1674 eca8e9c3ec3e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/main/java/sun/invoke/util/BytecodeDescriptor.java	Sun Aug 17 20:09:05 2014 +0200
     1.3 @@ -0,0 +1,137 @@
     1.4 +/*
     1.5 + * Copyright (c) 2008, 2011, 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 sun.invoke.util;
    1.30 +
    1.31 +import java.lang.invoke.MethodType;
    1.32 +import java.util.ArrayList;
    1.33 +import java.util.List;
    1.34 +
    1.35 +/**
    1.36 + * Utility routines for dealing with bytecode-level signatures.
    1.37 + * @author jrose
    1.38 + */
    1.39 +public class BytecodeDescriptor {
    1.40 +
    1.41 +    private BytecodeDescriptor() { }  // cannot instantiate
    1.42 +
    1.43 +    public static List<Class<?>> parseMethod(String bytecodeSignature, ClassLoader loader) {
    1.44 +        return parseMethod(bytecodeSignature, 0, bytecodeSignature.length(), loader);
    1.45 +    }
    1.46 +
    1.47 +    static List<Class<?>> parseMethod(String bytecodeSignature,
    1.48 +            int start, int end, ClassLoader loader) {
    1.49 +        if (loader == null)
    1.50 +            loader = ClassLoader.getSystemClassLoader();
    1.51 +        String str = bytecodeSignature;
    1.52 +        int[] i = {start};
    1.53 +        ArrayList<Class<?>> ptypes = new ArrayList<Class<?>>();
    1.54 +        if (i[0] < end && str.charAt(i[0]) == '(') {
    1.55 +            ++i[0];  // skip '('
    1.56 +            while (i[0] < end && str.charAt(i[0]) != ')') {
    1.57 +                Class<?> pt = parseSig(str, i, end, loader);
    1.58 +                if (pt == null || pt == void.class)
    1.59 +                    parseError(str, "bad argument type");
    1.60 +                ptypes.add(pt);
    1.61 +            }
    1.62 +            ++i[0];  // skip ')'
    1.63 +        } else {
    1.64 +            parseError(str, "not a method type");
    1.65 +        }
    1.66 +        Class<?> rtype = parseSig(str, i, end, loader);
    1.67 +        if (rtype == null || i[0] != end)
    1.68 +            parseError(str, "bad return type");
    1.69 +        ptypes.add(rtype);
    1.70 +        return ptypes;
    1.71 +    }
    1.72 +
    1.73 +    static private void parseError(String str, String msg) {
    1.74 +        throw new IllegalArgumentException("bad signature: "+str+": "+msg);
    1.75 +    }
    1.76 +
    1.77 +    static private Class<?> parseSig(String str, int[] i, int end, ClassLoader loader) {
    1.78 +        if (i[0] == end)  return null;
    1.79 +        char c = str.charAt(i[0]++);
    1.80 +        if (c == 'L') {
    1.81 +            int begc = i[0], endc = str.indexOf(';', begc);
    1.82 +            if (endc < 0)  return null;
    1.83 +            i[0] = endc+1;
    1.84 +            String name = str.substring(begc, endc).replace('/', '.');
    1.85 +            try {
    1.86 +                return loader.loadClass(name);
    1.87 +            } catch (ClassNotFoundException ex) {
    1.88 +                throw new TypeNotPresentException(name, ex);
    1.89 +            }
    1.90 +        } else if (c == '[') {
    1.91 +            Class<?> t = parseSig(str, i, end, loader);
    1.92 +            if (t != null)
    1.93 +                t = java.lang.reflect.Array.newInstance(t, 0).getClass();
    1.94 +            return t;
    1.95 +        } else {
    1.96 +            return Wrapper.forBasicType(c).primitiveType();
    1.97 +        }
    1.98 +    }
    1.99 +
   1.100 +    public static String unparse(Class<?> type) {
   1.101 +        StringBuilder sb = new StringBuilder();
   1.102 +        unparseSig(type, sb);
   1.103 +        return sb.toString();
   1.104 +    }
   1.105 +
   1.106 +    public static String unparse(MethodType type) {
   1.107 +        return unparseMethod(type.returnType(), type.parameterList());
   1.108 +    }
   1.109 +
   1.110 +    public static String unparse(Object type) {
   1.111 +        if (type instanceof Class<?>)
   1.112 +            return unparse((Class<?>) type);
   1.113 +        if (type instanceof MethodType)
   1.114 +            return unparse((MethodType) type);
   1.115 +        return (String) type;
   1.116 +    }
   1.117 +
   1.118 +    public static String unparseMethod(Class<?> rtype, List<Class<?>> ptypes) {
   1.119 +        StringBuilder sb = new StringBuilder();
   1.120 +        sb.append('(');
   1.121 +        for (Class<?> pt : ptypes)
   1.122 +            unparseSig(pt, sb);
   1.123 +        sb.append(')');
   1.124 +        unparseSig(rtype, sb);
   1.125 +        return sb.toString();
   1.126 +    }
   1.127 +
   1.128 +    static private void unparseSig(Class<?> t, StringBuilder sb) {
   1.129 +        char c = Wrapper.forBasicType(t).basicTypeChar();
   1.130 +        if (c != 'L') {
   1.131 +            sb.append(c);
   1.132 +        } else {
   1.133 +            boolean lsemi = (!t.isArray());
   1.134 +            if (lsemi)  sb.append('L');
   1.135 +            sb.append(t.getName().replace('.', '/'));
   1.136 +            if (lsemi)  sb.append(';');
   1.137 +        }
   1.138 +    }
   1.139 +
   1.140 +}