Advertisement
Guest User

Untitled

a guest
Jun 25th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. public static byte[] patchMethod(byte[] bytes, String name, String iFile, String rname) throws IOException
  2. {
  3. ClassNode cn = new ClassNode();
  4. ClassReader cr = new ClassReader(bytes);
  5. //the class node is now visiting the bytes that the class reader has
  6. cr.accept(cn,0);
  7. //giving us something we can look at all the methods
  8. Iterator<MethodNode> methods = cn.methods.iterator();
  9. //creating the method node we are going to mess with
  10. MethodNode mn = null;
  11. while (methods.hasNext())
  12. {
  13. MethodNode m = methods.next();
  14. if (m.name.equals(name))
  15. {
  16. System.out.printf("Found a method we want to transform%n");
  17. mn = m;
  18. break;
  19. }
  20. }
  21.  
  22. //get the file we're going to replace a method with
  23. InputStream in = LanEssentialsTransformer.class.getResourceAsStream(iFile);
  24. byte[] newbyte = IOUtils.toByteArray(in);
  25. //get the method
  26. ClassNode cnr = new ClassNode();
  27. ClassReader crr = new ClassReader(newbyte);
  28. crr.accept(cnr,0);
  29. //find the right one
  30. Iterator<MethodNode> repalcemethods = cnr.methods.iterator();
  31. MethodNode mnr = null;
  32. while (repalcemethods.hasNext())
  33. {
  34. MethodNode m = repalcemethods.next();
  35. if (m.name.equals(rname))
  36. {
  37. System.out.printf("Found the right method that is going to be the replacement");
  38. mnr = m;
  39. break;
  40. }
  41. }
  42. //now we start changing the bodies
  43. mn.instructions = mnr.instructions;
  44. final ClassWriter writer = new ClassWriter(1);
  45. cn.accept((ClassVisitor)writer);
  46.  
  47. byte[] bb = writer.toByteArray();
  48.  
  49. try
  50. {
  51. File f = new File("C:/Users/jredfox/Documents/tst.class");
  52. FileOutputStream stream = new FileOutputStream(f);
  53. for(byte b : bb)
  54. stream.write(b);
  55. stream.close();
  56. }
  57. catch(Exception e)
  58. {
  59. e.printStackTrace();
  60. }
  61. return bb;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement