Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static byte[] patchMethod(byte[] bytes, String name, String iFile, String rname) throws IOException
- {
- ClassNode cn = new ClassNode();
- ClassReader cr = new ClassReader(bytes);
- //the class node is now visiting the bytes that the class reader has
- cr.accept(cn,0);
- //giving us something we can look at all the methods
- Iterator<MethodNode> methods = cn.methods.iterator();
- //creating the method node we are going to mess with
- MethodNode mn = null;
- while (methods.hasNext())
- {
- MethodNode m = methods.next();
- if (m.name.equals(name))
- {
- System.out.printf("Found a method we want to transform%n");
- mn = m;
- break;
- }
- }
- //get the file we're going to replace a method with
- InputStream in = LanEssentialsTransformer.class.getResourceAsStream(iFile);
- byte[] newbyte = IOUtils.toByteArray(in);
- //get the method
- ClassNode cnr = new ClassNode();
- ClassReader crr = new ClassReader(newbyte);
- crr.accept(cnr,0);
- //find the right one
- Iterator<MethodNode> repalcemethods = cnr.methods.iterator();
- MethodNode mnr = null;
- while (repalcemethods.hasNext())
- {
- MethodNode m = repalcemethods.next();
- if (m.name.equals(rname))
- {
- System.out.printf("Found the right method that is going to be the replacement");
- mnr = m;
- break;
- }
- }
- //now we start changing the bodies
- mn.instructions = mnr.instructions;
- final ClassWriter writer = new ClassWriter(1);
- cn.accept((ClassVisitor)writer);
- byte[] bb = writer.toByteArray();
- try
- {
- File f = new File("C:/Users/jredfox/Documents/tst.class");
- FileOutputStream stream = new FileOutputStream(f);
- for(byte b : bb)
- stream.write(b);
- stream.close();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- return bb;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement