• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Byte code manipulation for logging

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to another forum: question too difficult for “beginning”.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks jesper. I will look for aspectJ
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic