public static String getJavaType(String desc) { int arrIndex = desc.lastIndexOf("[") + 1; desc = desc.substring(arrIndex); if (desc.startsWith("L")) { desc = desc.substring(1, desc.length() - 1).replace('/', '.'); } else { for (Map.Entry<String, String> entry : primitives.entrySet()) { if (entry.getValue().equals(desc)) { desc = entry.getKey(); break; } } } StringBuilder sb = new StringBuilder(desc); for (int i = 0; i < arrIndex; i++) { sb.append("[]"); } return sb.toString(); }
public static String objectOrArrayType(String type) { StringBuilder buf = new StringBuilder(); int index = 0; while ((index = type.indexOf("[]", index) + 1) > 0) { buf.append('['); } String t = type.substring(0, type.length() - buf.length() * 2); String desc = primitives.get(t); if (desc != null) { buf.append(desc); } else { buf.append('L'); if (t.indexOf('.') < 0) { buf.append(t); } else { buf.append(t.replace('.', '/')); } buf.append(';'); } return buf.toString(); }
public static String declarationToDescriptor(String decl) { int leftParen = decl.indexOf('('); int rightParen = decl.indexOf(')'); if (leftParen == -1 || rightParen == -1) { throw new IllegalArgumentException(); } StringBuilder buf = new StringBuilder(); String descriptor; buf.append('('); String args = decl.substring(leftParen + 1, rightParen); StringTokenizer st = new StringTokenizer(args, ","); while (st.hasMoreTokens()) { String arg = st.nextToken().trim(); descriptor = primitives.get(arg); if (arg.length() == 0) { throw new IllegalArgumentException(); } if (descriptor == null) { descriptor = objectOrArrayType(arg); } buf.append(descriptor); } buf.append(')'); String returnType = decl.substring(0, leftParen).trim(); descriptor = primitives.get(returnType); if (returnType.length() == 0) { throw new IllegalArgumentException(); } if (descriptor == null) { descriptor = objectOrArrayType(returnType); } buf.append(descriptor); return buf.toString(); }