3

I want to insert Print code after every java code. For example:

public void  myMethod() {
   Integer i = 0;
   Long l = 0L;
   Date date = new Date();
}

to:

public void  myMethod() {
   Integer i = 0;
   System.out.println("test");
   Long l = 0L;
   System.out.println("test");
   Date date = new Date();
   System.out.println("test");
}

how should I do?Can you help me?

11
  • 3
    You've just did that. Commented Dec 18, 2015 at 8:15
  • @MarounMaroun nope. He want to insert that through byte code. Commented Dec 18, 2015 at 8:16
  • 2
    @sᴜʀᴇsʜᴀᴛᴛᴀ I don't really understand what's the meaning of inserting print statement to the bytecode :/ Commented Dec 18, 2015 at 8:17
  • 4
    Better use a step by step debugger. Commented Dec 18, 2015 at 8:17
  • 2
    @MarounMaroun asm.ow2.org Commented Dec 18, 2015 at 8:18

1 Answer 1

1

One way to a solution in the compiler

1 parse your code:

Best way to parse Java in Java

2 insert automatically code:

use JET for example

https://eclipse.org/articles/Article-JET2/jet_tutorial2.html

SECOND SOLUTION

create and put some annotation (by hand)

RUNTIME SOLUTION

you have to investigate bytecode.

Some links: http://web.cs.ucla.edu/~msb/cs239-tutorial/

see the chapter: Generating Call Traces

And posts around this tag: https://stackoverflow.com/questions/tagged/java-bytecode-asm

Sign up to request clarification or add additional context in comments.

2 Comments

At first,thank you for your answer.And I have a question,what do you think of using a bytecode library like ASM to do this?Can it work?
@GanZhang , Yes, it could, I put some links: see the link on UCLA.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.