01: package tide.bytecode.asm;
02:
03: import tide.utils.SyntaxUtils;
04: import org.objectweb.asm.signature.*;
05: import java.util.*;
06:
07: public final class ASMUtils {
08: private ASMUtils() {
09: }
10:
11: static class SigVisitor extends SignatureVisitorAd {
12: private int arrayDepth = 0;
13: public List<String> javaNames = new ArrayList<String>();
14:
15: /** called at type start */
16: @Override
17: public SignatureVisitor visitParameterType() {
18: //System.out.println("start");
19: arrayDepth = 0; // reset
20: return this ;
21: }
22:
23: @Override
24: public void visitBaseType(char descriptor) {
25: String tn = SyntaxUtils.getNameForVMNativeType(descriptor);
26: add(tn);
27: }
28:
29: @Override
30: public SignatureVisitor visitArrayType() {
31: //if(debug) System.out.println("Array type");
32: arrayDepth++;
33: return this ;
34: }
35:
36: @Override
37: public void visitClassType(String name) {
38: //System.out.println("class type "+name);
39: add(name.replace('/', '.'));
40: }
41:
42: void add(String name) {
43: //System.out.println("Add "+name);
44: for (int i = 0; i < arrayDepth; i++) {
45: name += "[]";
46: }
47: arrayDepth = 0;
48: javaNames.add(name);
49: }
50: }
51:
52: public static List<String> readJavaNamesFromSignature(String sig) {
53: SignatureReader sr = new SignatureReader(sig);
54: SigVisitor sv = new SigVisitor();
55: sr.accept(sv);
56: return sv.javaNames;
57: }
58:
59: // (ID)Ljava/lang/String; => int,double
60: public static String createArgsListFromSig(String sig) {
61: //System.out.println("2 "+sig);
62: List<String> javaNames = readJavaNamesFromSignature(sig);
63:
64: StringBuilder sb = new StringBuilder();
65: for (int i = 0; i < javaNames.size(); i++) {
66: if (i > 0)
67: sb.append(",");
68: sb.append(javaNames.get(i));
69: }
70: return sb.toString();
71: }
72:
73: public static String createArgsListFromMethosSig(String sig) {
74: System.out.println("1 " + sig);
75: List<String> javaNames = readJavaNamesFromSignature(sig);
76:
77: StringBuilder sb = new StringBuilder();
78: for (int i = 0; i < javaNames.size(); i++) {
79: if (i > 0)
80: sb.append(",");
81: sb.append(javaNames.get(i));
82: }
83: return sb.toString();
84: }
85:
86: }
|