1193N/A/*
3793N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
1193N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1193N/A *
1193N/A * This code is free software; you can redistribute it and/or modify it
1193N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
1193N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1193N/A *
1193N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1193N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1193N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1193N/A * version 2 for more details (a copy is included in the LICENSE file that
1193N/A * accompanied this code).
1193N/A *
1193N/A * You should have received a copy of the GNU General Public License version
1193N/A * 2 along with this work; if not, write to the Free Software Foundation,
1193N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1193N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1193N/A */
1193N/A
3793N/Apackage sun.invoke.util;
1193N/A
3793N/Aimport java.lang.invoke.MethodType;
1193N/Aimport java.util.ArrayList;
1193N/Aimport java.util.List;
1193N/A
1193N/A/**
1193N/A * Utility routines for dealing with bytecode-level signatures.
1193N/A * @author jrose
1193N/A */
2040N/Apublic class BytecodeDescriptor {
1193N/A
2040N/A private BytecodeDescriptor() { } // cannot instantiate
1193N/A
1193N/A public static List<Class<?>> parseMethod(String bytecodeSignature, ClassLoader loader) {
1193N/A return parseMethod(bytecodeSignature, 0, bytecodeSignature.length(), loader);
1193N/A }
1193N/A
1193N/A static List<Class<?>> parseMethod(String bytecodeSignature,
1193N/A int start, int end, ClassLoader loader) {
1193N/A if (loader == null)
1193N/A loader = ClassLoader.getSystemClassLoader();
1193N/A String str = bytecodeSignature;
1193N/A int[] i = {start};
1193N/A ArrayList<Class<?>> ptypes = new ArrayList<Class<?>>();
1193N/A if (i[0] < end && str.charAt(i[0]) == '(') {
1193N/A ++i[0]; // skip '('
1193N/A while (i[0] < end && str.charAt(i[0]) != ')') {
1193N/A Class<?> pt = parseSig(str, i, end, loader);
1193N/A if (pt == null || pt == void.class)
1193N/A parseError(str, "bad argument type");
1193N/A ptypes.add(pt);
1193N/A }
1193N/A ++i[0]; // skip ')'
1193N/A } else {
1193N/A parseError(str, "not a method type");
1193N/A }
1193N/A Class<?> rtype = parseSig(str, i, end, loader);
1193N/A if (rtype == null || i[0] != end)
1193N/A parseError(str, "bad return type");
1193N/A ptypes.add(rtype);
1193N/A return ptypes;
1193N/A }
1193N/A
1193N/A static private void parseError(String str, String msg) {
1193N/A throw new IllegalArgumentException("bad signature: "+str+": "+msg);
1193N/A }
1193N/A
1193N/A static private Class<?> parseSig(String str, int[] i, int end, ClassLoader loader) {
1193N/A if (i[0] == end) return null;
1193N/A char c = str.charAt(i[0]++);
1193N/A if (c == 'L') {
1193N/A int begc = i[0], endc = str.indexOf(';', begc);
1193N/A if (endc < 0) return null;
1193N/A i[0] = endc+1;
1193N/A String name = str.substring(begc, endc).replace('/', '.');
1193N/A try {
1193N/A return loader.loadClass(name);
1193N/A } catch (ClassNotFoundException ex) {
1193N/A throw new TypeNotPresentException(name, ex);
1193N/A }
1193N/A } else if (c == '[') {
1193N/A Class<?> t = parseSig(str, i, end, loader);
1193N/A if (t != null)
1193N/A t = java.lang.reflect.Array.newInstance(t, 0).getClass();
1193N/A return t;
1193N/A } else {
1193N/A return Wrapper.forBasicType(c).primitiveType();
1193N/A }
1193N/A }
1193N/A
1193N/A public static String unparse(Class<?> type) {
1193N/A StringBuilder sb = new StringBuilder();
1193N/A unparseSig(type, sb);
1193N/A return sb.toString();
1193N/A }
1193N/A
1193N/A public static String unparse(MethodType type) {
1193N/A return unparseMethod(type.returnType(), type.parameterList());
1193N/A }
1193N/A
1193N/A public static String unparse(Object type) {
1193N/A if (type instanceof Class<?>)
1193N/A return unparse((Class<?>) type);
1193N/A if (type instanceof MethodType)
1193N/A return unparse((MethodType) type);
1193N/A return (String) type;
1193N/A }
1193N/A
1193N/A public static String unparseMethod(Class<?> rtype, List<Class<?>> ptypes) {
1193N/A StringBuilder sb = new StringBuilder();
1193N/A sb.append('(');
1193N/A for (Class<?> pt : ptypes)
1193N/A unparseSig(pt, sb);
1193N/A sb.append(')');
1193N/A unparseSig(rtype, sb);
1193N/A return sb.toString();
1193N/A }
1193N/A
1193N/A static private void unparseSig(Class<?> t, StringBuilder sb) {
1193N/A char c = Wrapper.forBasicType(t).basicTypeChar();
1193N/A if (c != 'L') {
1193N/A sb.append(c);
1193N/A } else {
1193N/A boolean lsemi = (!t.isArray());
1193N/A if (lsemi) sb.append('L');
1193N/A sb.append(t.getName().replace('.', '/'));
1193N/A if (lsemi) sb.append(';');
1193N/A }
1193N/A }
1193N/A
1193N/A}