Problems with an ArrayIndexOutOfBoundsException
Context
I have source code (as a String) that I compile in memory with the Java compiler API. I then transform the bytecode to add instrumentation: mainly tracking line coverage and obtaining variable changes.
What I'm trying to do
Now I want to add logging. For this I want to replace calls to
System.out.println
with my own logging function. (This is necessary, because (1) my custom class loader blocks loading of the System class and (2) because I want the log information stored along with the other data and contain class, method and line number of the call.)
What already works
Simply calling System.out.println("Hello, World!");
works already. Concatenating a string with a variable works now as well, e.g. System.out.println("x = " + x);
The transformation is done with the MethodVisitor from ASM:
So I mainly switch out the call to a printStreams println
with the call to my logging function and I switch out accessing System.out
with fetching a dummy PrintStream (that does nothing) from my Debug class.
In bytecode this means that an access to System.out.println is replaced with:
which in Java means:
4 Replies
⌛
This post has been reserved for your question.
Hey @<Tim>! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
What doesn't work
As soon as I want to do all of that inside a loop, I get this error:
The example class is this:
The
System.out.println
is not transformed but omited from the bytecode after transformation completely
Does anyone have an idea of how I can implement the logging?
It doesn't need to be based on System.out.println
, but if I don't want to mess around with the source code and regexes or something, it needs to be something that is available at compile time of the code.
Could it be that the printStream on the stack causes problems? Can I remove it maybe? 😬
It was in fact the dummy printstream. If I don't load it, it works in loops as well. Hope it doesn't cause any side effects, as I thought I left it there for a reason. But looks good so far :)
Thanks for the help, you were good rubber duckies :)If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Post Closed
This post has been closed by <@330307656105328640>.