1 17 18 package org.apache.tools.ant.taskdefs.optional.sitraka.bytecode; 19 20 import java.util.Vector ; 21 import org.apache.tools.ant.taskdefs.optional.depend.constantpool.ConstantPool; 22 import org.apache.tools.ant.taskdefs.optional.depend.constantpool.Utf8CPInfo; 23 24 28 public class Utils { 29 30 public static final short ACC_PUBLIC = 1; 31 32 public static final short ACC_PRIVATE = 2; 33 34 public static final short ACC_PROTECTED = 4; 35 36 public static final short ACC_STATIC = 8; 37 38 public static final short ACC_FINAL = 16; 39 40 public static final short ACC_SUPER = 32; 41 42 public static final short ACC_SYNCHRONIZED = 32; 43 44 public static final short ACC_VOLATILE = 64; 45 46 public static final short ACC_TRANSIENT = 128; 47 48 public static final short ACC_NATIVE = 256; 49 50 public static final short ACC_INTERFACE = 512; 51 52 public static final short ACC_ABSTRACT = 1024; 53 54 public static final short ACC_STRICT = 2048; 55 56 57 private Utils() { 58 } 59 60 67 public static String getUTF8Value(ConstantPool pool, int index) { 68 return ((Utf8CPInfo) pool.getEntry(index)).getValue(); 69 } 70 71 78 public static String [] getMethodParams(String descriptor) { 79 int i = 0; 80 if (descriptor.charAt(i) != '(') { 81 throw new IllegalArgumentException ("Method descriptor should start with a '('"); 82 } 83 Vector params = new Vector (); 84 StringBuffer param = new StringBuffer (); 85 i++; 86 while ((i = descriptor2java(descriptor, i, param)) < descriptor.length()) { 87 params.add(param.substring(0)); 88 param = new StringBuffer (); 89 if (descriptor.charAt(i) == ')') { 90 i++; 91 break; 92 } 93 } 94 String [] array = new String [params.size()]; 95 params.copyInto(array); 96 return array; 97 } 98 99 104 public static String getMethodReturnType(String descriptor) { 105 int pos = descriptor.indexOf(')'); 106 StringBuffer rettype = new StringBuffer (); 107 descriptor2java(descriptor, pos + 1, rettype); 108 return rettype.toString(); 109 } 110 111 118 public static int descriptor2java(String descriptor, int i, StringBuffer sb) { 119 StringBuffer dim = new StringBuffer (); 121 for (; descriptor.charAt(i) == '['; i++) { 122 dim.append("[]"); 123 } 124 switch (descriptor.charAt(i)) { 126 case 'B': 127 sb.append("byte"); 128 break; 129 case 'C': 130 sb.append("char"); 131 break; 132 case 'D': 133 sb.append("double"); 134 break; 135 case 'F': 136 sb.append("float"); 137 break; 138 case 'I': 139 sb.append("int"); 140 break; 141 case 'J': 142 sb.append("long"); 143 break; 144 case 'S': 145 sb.append("short"); 146 break; 147 case 'Z': 148 sb.append("boolean"); 149 break; 150 case 'V': 151 sb.append("void"); 152 break; 153 case 'L': 154 int pos = descriptor.indexOf(';', i + 1); 156 String classname = descriptor.substring(i + 1, pos).replace('/', '.'); 157 sb.append(classname); 158 i = pos; 159 break; 160 default: 161 165 } 169 sb.append(dim.toString()); 170 return ++i; 171 } 172 173 177 public static boolean isAbstract(int access_flags) { 178 return (access_flags & ACC_ABSTRACT) != 0; 179 } 180 181 185 public static boolean isPublic(int access_flags) { 186 return (access_flags & ACC_PUBLIC) != 0; 187 } 188 189 193 public static boolean isStatic(int access_flags) { 194 return (access_flags & ACC_STATIC) != 0; 195 } 196 197 201 public static boolean isNative(int access_flags) { 202 return (access_flags & ACC_NATIVE) != 0; 203 } 204 205 209 public static boolean isClass(int access_flags) { 210 return !isInterface(access_flags); 211 } 212 213 217 public static boolean isStrict(int access_flags) { 218 return (access_flags & ACC_STRICT) != 0; 219 } 220 221 225 public static boolean isInterface(int access_flags) { 226 return (access_flags & ACC_INTERFACE) != 0; 227 } 228 229 233 public static boolean isPrivate(int access_flags) { 234 return (access_flags & ACC_PRIVATE) != 0; 235 } 236 237 241 public static boolean isTransient(int access_flags) { 242 return (access_flags & ACC_TRANSIENT) != 0; 243 } 244 245 249 public static boolean isVolatile(int access_flags) { 250 return (access_flags & ACC_VOLATILE) != 0; 251 } 252 253 257 public static boolean isSuper(int access_flags) { 258 return (access_flags & ACC_SUPER) != 0; 259 } 260 261 265 public static boolean isProtected(int access_flags) { 266 return (access_flags & ACC_PROTECTED) != 0; 267 } 268 269 273 public static boolean isFinal(int access_flags) { 274 return (access_flags & ACC_FINAL) != 0; 275 } 276 277 281 public static boolean isSynchronized(int access_flags) { 282 return (access_flags & ACC_SYNCHRONIZED) != 0; 283 } 284 285 290 public static String getMethodAccess(int access_flags) { 291 StringBuffer sb = new StringBuffer (); 292 if (isPublic(access_flags)) { 293 sb.append("public "); 294 } else if (isPrivate(access_flags)) { 295 sb.append("private "); 296 } else if (isProtected(access_flags)) { 297 sb.append("protected "); 298 } 299 if (isFinal(access_flags)) { 300 sb.append("final "); 301 } 302 if (isStatic(access_flags)) { 303 sb.append("static "); 304 } 305 if (isSynchronized(access_flags)) { 306 sb.append("synchronized "); 307 } 308 if (isNative(access_flags)) { 309 sb.append("native "); 310 } 311 if (isAbstract(access_flags)) { 312 sb.append("abstract "); 313 } 314 return sb.toString().trim(); 315 } 316 317 322 public static String getFieldAccess(int access_flags) { 323 StringBuffer sb = new StringBuffer (); 324 if (isPublic(access_flags)) { 325 sb.append("public "); 326 } else if (isPrivate(access_flags)) { 327 sb.append("private "); 328 } else if (isProtected(access_flags)) { 329 sb.append("protected "); 330 } 331 if (isFinal(access_flags)) { 332 sb.append("final "); 333 } 334 if (isStatic(access_flags)) { 335 sb.append("static "); 336 } 337 if (isVolatile(access_flags)) { 338 sb.append("volatile "); 339 } 340 if (isTransient(access_flags)) { 341 sb.append("transient "); 342 } 343 return sb.toString().trim(); 344 } 345 346 351 public static String getClassAccess(int access_flags) { 352 StringBuffer sb = new StringBuffer (); 353 if (isPublic(access_flags)) { 354 sb.append("public "); 355 } else if (isProtected(access_flags)) { 356 sb.append("protected "); 357 } else if (isPrivate(access_flags)) { 358 sb.append("private "); 359 } 360 if (isFinal(access_flags)) { 361 sb.append("final "); 362 } 363 if (isSuper(access_flags)) { 364 sb.append("/*super*/ "); 365 } 366 if (isInterface(access_flags)) { 367 sb.append("interface "); 368 } 369 if (isAbstract(access_flags)) { 370 sb.append("abstract "); 371 } 372 if (isClass(access_flags)) { 373 sb.append("class "); 374 } 375 return sb.toString().trim(); 376 } 377 } 378 379 380 381 | Popular Tags |