Получение classname'а из .class-файла

Nov 05, 2012 13:48

Быстро, просто, без рефлекшена!


  1. public String getClassNameFromInputStream(InputStream is) {

  2. Map strings = new HashMap<>();

  3. Map classMap = new HashMap<>();

  4. String ret = null;

  5. try {

  6. DataInputStream dis = new DataInputStream(is);

  7. dis.skip(8);

  8. int pool_count = dis.readShort();

  9. int idx = 1;

  10. while (pool_count - idx > 0) {

  11. int type = dis.read();

  12. switch (type) {

  13. case 1:

  14. int len = dis.readShort();

  15. byte[] buf = new byte[len];

  16. dis.read(buf);

  17. strings.put(idx, new String(buf));

  18. break;

  19. case 7:

  20. int index = dis.readShort();

  21. classMap.put(idx, index);

  22. break;

  23. case 8:

  24. case 16:

  25. dis.skip(2);

  26. break;

  27. case 3:

  28. case 4:

  29. case 9:

  30. case 10:

  31. case 11:

  32. case 12:

  33. case 18:

  34. dis.skip(4);

  35. break;

  36. case 5:

  37. case 6:

  38. dis.skip(8);

  39. idx++;

  40. break;

  41. case 15:

  42. dis.skip(3);

  43. default:

  44. System.out.println(idx + " unknown byte " + type);

  45. }

  46. idx++;

  47. }

  48. dis.skip(2);

  49. int thisClass = dis.readShort();

  50. dis.close();

  51. ret = strings.get(classMap.get(thisClass)).replace('/', '.');

  52. } catch (IOException e) {

  53. }

  54. return ret;

  55. }
Previous post Next post
Up