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.5KB

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