You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Play.java 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import org.aspectj.apache.bcel.classfile.Attribute;
  4. import org.aspectj.apache.bcel.classfile.ClassParser;
  5. import org.aspectj.apache.bcel.classfile.Field;
  6. import org.aspectj.apache.bcel.classfile.JavaClass;
  7. import org.aspectj.apache.bcel.classfile.Method;
  8. import org.aspectj.apache.bcel.classfile.Unknown;
  9. public class Play {
  10. public static void printBytes(byte[] bs) {
  11. StringBuilder sb = new StringBuilder("Bytes:"+bs.length+"[");
  12. for (int i=0;i<bs.length;i++) {
  13. if (i>0) sb.append(" ");
  14. sb.append(bs[i]);
  15. }
  16. sb.append("]");
  17. System.out.println(sb);
  18. }
  19. public static void main(String[] args) throws Exception {
  20. if (args==null || args.length==0 ) {
  21. System.out.println("Specify a file");
  22. return;
  23. }
  24. if (!args[0].endsWith(".class")) {
  25. args[0] = args[0]+".class";
  26. }
  27. FileInputStream fis = new FileInputStream(new File(args[0]));
  28. ClassParser cp = new ClassParser(fis,args[0]);
  29. JavaClass jc = cp.parse();
  30. Attribute[] attributes = jc.getAttributes();
  31. printUsefulAttributes(attributes);
  32. System.out.println("Fields");
  33. Field[] fs = jc.getFields();
  34. if (fs!=null) {
  35. for (Field f: fs) {
  36. System.out.println(f);
  37. printUsefulAttributes(f.getAttributes());
  38. }
  39. }
  40. System.out.println("Methods");
  41. Method[] ms = jc.getMethods();
  42. if (ms!=null) {
  43. for (Method m: ms) {
  44. System.out.println(m);
  45. printUsefulAttributes(m.getAttributes());
  46. System.out.println("Code attributes:");
  47. printUsefulAttributes(m.getCode().getAttributes());
  48. }
  49. }
  50. // Method[] ms = jc.getMethods();
  51. // for (Method m: ms) {
  52. // System.out.println("==========");
  53. // System.out.println("Method: "+m.getName()+" modifiers=0x"+Integer.toHexString(m.getModifiers()));
  54. // Attribute[] as = m.getAttributes();
  55. // for (Attribute a: as) {
  56. // if (a.getName().toLowerCase().contains("synthetic")) {
  57. // System.out.println("> "+a.getName());
  58. // }
  59. // }
  60. // }
  61. }
  62. private static void printUsefulAttributes(Attribute[] attributes) {
  63. for (Attribute attribute: attributes) {
  64. String n = attribute.getName();
  65. if (n.equals("RuntimeInvisibleTypeAnnotations") ||
  66. n.equals("RuntimeVisibleTypeAnnotations")) {
  67. Unknown unknown = (Unknown)attribute;
  68. byte[] bs = unknown.getBytes();
  69. printBytes(bs);
  70. }
  71. }
  72. }
  73. }