1 52 53 package com.go.trove.classfile; 54 55 import java.util.List ; 56 import java.util.ArrayList ; 57 58 66 public class MethodDescriptor extends Descriptor { 67 private static TypeDescriptor[] cEmptyParams = new TypeDescriptor[0]; 68 69 private String mStr; 70 private TypeDescriptor mRetType; 71 private TypeDescriptor[] mParams; 72 73 public MethodDescriptor() { 74 this(null, null); 75 } 76 77 81 public MethodDescriptor(TypeDescriptor ret, TypeDescriptor[] params) { 82 if (params == null) { 83 params = cEmptyParams; 84 } 85 86 mStr = generate(ret, params); 87 mRetType = ret; 88 mParams = params; 89 } 90 91 public TypeDescriptor getReturnType() { 92 return mRetType; 93 } 94 95 public int getParameterCount() { 96 return mParams.length; 97 } 98 99 public TypeDescriptor[] getParameterTypes() { 100 return mParams; 101 } 102 103 public String toString() { 104 return mStr; 105 } 106 107 public static String generate() { 108 return generate(null, null); 109 } 110 111 115 public static String generate(TypeDescriptor ret, 116 TypeDescriptor[] params) { 117 118 StringBuffer desc = new StringBuffer (20); 119 120 desc.append('('); 121 122 if (params != null) { 123 for (int i=0; i<params.length; i++) { 124 desc.append(params[i]); 125 } 126 } 127 128 desc.append(')'); 129 130 if (ret == null) ret = new TypeDescriptor(Void.TYPE); 131 desc.append(ret); 132 133 return desc.toString(); 134 } 135 136 public static MethodDescriptor parseMethodDesc(String desc) 137 throws IllegalArgumentException { 138 139 MethodDescriptor md; 140 int cursor = 0; 141 try { 142 char c; 143 144 if ((c = desc.charAt(cursor++)) != '(') { 145 throw new IllegalArgumentException 146 ("Invalid descriptor: " + desc); 147 } 148 149 StringBuffer buf = new StringBuffer (); 150 List list = new ArrayList (); 151 152 while ((c = desc.charAt(cursor++)) != ')') { 153 switch (c) { 154 case 'V': 155 case 'I': 156 case 'C': 157 case 'Z': 158 case 'D': 159 case 'F': 160 case 'J': 161 case 'B': 162 case 'S': 163 buf.append(c); 164 break; 165 case '[': 166 buf.append(c); 167 continue; 168 case 'L': 169 while (true) { 170 buf.append(c); 171 if (c == ';') { 172 break; 173 } 174 c = desc.charAt(cursor++); 175 } 176 break; 177 default: 178 throw new IllegalArgumentException 179 ("Invalid descriptor: " + desc); 180 } 181 182 list.add(TypeDescriptor.parseTypeDesc(buf.toString())); 183 buf.setLength(0); 184 } 185 186 TypeDescriptor ret = 187 TypeDescriptor.parseTypeDesc(desc.substring(cursor)); 188 189 TypeDescriptor[] tds = new TypeDescriptor[list.size()]; 190 tds = (TypeDescriptor[])list.toArray(tds); 191 192 md = new MethodDescriptor(ret, tds); 193 } 194 catch (NullPointerException e) { 195 throw new IllegalArgumentException ("Invalid descriptor: " + desc); 196 } 197 catch (IndexOutOfBoundsException e) { 198 throw new IllegalArgumentException ("Invalid descriptor: " + desc); 199 } 200 201 return md; 202 } 203 } 204 | Popular Tags |