Batch of classes necessary to implement invoke dynamic interfaces. Taken from JDK8 build 132
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.
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.
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).
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.
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
26 package sun.invoke.util;
28 import java.lang.invoke.MethodType;
29 import java.util.ArrayList;
30 import java.util.List;
33 * Utility routines for dealing with bytecode-level signatures.
36 public class BytecodeDescriptor {
38 private BytecodeDescriptor() { } // cannot instantiate
40 public static List<Class<?>> parseMethod(String bytecodeSignature, ClassLoader loader) {
41 return parseMethod(bytecodeSignature, 0, bytecodeSignature.length(), loader);
44 static List<Class<?>> parseMethod(String bytecodeSignature,
45 int start, int end, ClassLoader loader) {
47 loader = ClassLoader.getSystemClassLoader();
48 String str = bytecodeSignature;
50 ArrayList<Class<?>> ptypes = new ArrayList<Class<?>>();
51 if (i[0] < end && str.charAt(i[0]) == '(') {
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");
61 parseError(str, "not a method type");
63 Class<?> rtype = parseSig(str, i, end, loader);
64 if (rtype == null || i[0] != end)
65 parseError(str, "bad return type");
70 static private void parseError(String str, String msg) {
71 throw new IllegalArgumentException("bad signature: "+str+": "+msg);
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]++);
78 int begc = i[0], endc = str.indexOf(';', begc);
79 if (endc < 0) return null;
81 String name = str.substring(begc, endc).replace('/', '.');
83 return loader.loadClass(name);
84 } catch (ClassNotFoundException ex) {
85 throw new TypeNotPresentException(name, ex);
87 } else if (c == '[') {
88 Class<?> t = parseSig(str, i, end, loader);
90 t = java.lang.reflect.Array.newInstance(t, 0).getClass();
93 return Wrapper.forBasicType(c).primitiveType();
97 public static String unparse(Class<?> type) {
98 StringBuilder sb = new StringBuilder();
100 return sb.toString();
103 public static String unparse(MethodType type) {
104 return unparseMethod(type.returnType(), type.parameterList());
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;
115 public static String unparseMethod(Class<?> rtype, List<Class<?>> ptypes) {
116 StringBuilder sb = new StringBuilder();
118 for (Class<?> pt : ptypes)
121 unparseSig(rtype, sb);
122 return sb.toString();
125 static private void unparseSig(Class<?> t, StringBuilder sb) {
126 char c = Wrapper.forBasicType(t).basicTypeChar();
130 boolean lsemi = (!t.isArray());
131 if (lsemi) sb.append('L');
132 sb.append(t.getName().replace('.', '/'));
133 if (lsemi) sb.append(';');