• Post Reply Bookmark Topic Watch Topic
  • New Topic

Byte code manipulation for logging  RSS feed

 
Manu Somasekhar
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, guys

I was reading about byte code manipulation. As i understood it is a mechanism by which we can inject code into the class files.
So my question is like " is it possible do the logging mechanism via byte code manipulation?" are there any existing framework (like log4j) is providing me this functionality?
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What a strange question! Why would you resort to changing a class file if you could simply write the logging statements in the source code?

Or, are you asking for ways to add logging to an existing system for which you don't have the source?
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to another forum: question too difficult for “beginning”.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some uses with this with regard to aspect-oriented programming; see aspectj for example.

The idea with aspect-oriented programming is that you have rules, such as "do this at the beginning of every method", or "do this before and after all methods in this class" etc. A tool then automatically finds the methods that match the rule and inserts code there (via byte code manipulation) to perform the actions that you want to do.

For example, you could have a rule "write a log statement at entry and exit of every public method in this class"; aspectj would find all the public methods in the class and automatically add a log statement to the compiled code for you.
 
Manu Somasekhar
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well my point is if my project contains



and with byte code manipulation i can log what ever i want without changing the original source code. Also if I want to log in a different language, i can use the same idea.


My point is ,"is there any existing frameworks which will help me to achieve this functionality ?"
 
Manu Somasekhar
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks jesper. I will look for aspectJ
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In principle that's possible, but it's not going to be easy to do bytecode manipulation yourself. You'll have to know about the low-level details of bytecode instructions.

There are several libraries which can help you with this, for example: ASM, BCEL, cglib.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!