Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing a method in a compiled .class file

Interesting question here. So I have a .jar I received and it is obfuscated, meaning when I decompile the .class files it doesnt show up 100% perfect so I cannot recompile it. However the only method I need to change has been converted perfectly (but the class does not)). Is there a way to somehow change the .java code and inject replace the method within the class file without totally recompiling?

If this fails im going to bytecode.

Thanks!

EDIT: As a follow up question / or a hack around replacing the WHOLE method. I'm really just trying to change a variable that the method generates locally. If there are any better ways to do that.

like image 245
k9b Avatar asked Dec 17 '15 09:12

k9b


People also ask

Can we make changes in .class file?

Unless you are someone who can talk and write Java byte code, you can't directly make changes to a . class file.

Can you edit a class file Java?

Features of Java Class File Editor Allows modifications of various parts of a class file, like methods, strings, constants, etc.


Video Answer


1 Answers

Depending on what you really want to do, I do not recommand to decompile / modify / recompile the code (be it for legal, maintainance, understandability, testability reasons.)

Bytecode manipulation may not be the best solution either, but if you want to follow this way have a look at the ASM project, it's a widespread bytecode manipulation framework used by many known projects.

If I were you I would first give a try to aspects (AspectJ.) The power of aspects is that you don't touch existing code, but tell the VM what to do when / before / after / in place of calling a specific method. It allows you to point out the exact context and change, enhance the behavior of the code, by writing your own code in a decoupled fashion.

Hope it helps.

like image 92
Kraal Avatar answered Oct 05 '22 01:10

Kraal