jakarta-bcel-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rreye...@attbi.com
Subject Instrumenting a constructor
Date Tue, 01 Oct 2002 14:47:01 GMT
I'm working on a tool that needs to instrument
constructors of a class by ensuring that a few lines of
code are always called at the end of the constructor
execution unless an exception takes place. For example,

public class Foo {
  public Foo() {
    // normal code
    // instrumented code 
  }
}

I'm having a problem, though, figuring out the best way
to do this. The toughest problem seems to be related to
flow control. For example, the user may return from the
constructor from many different points, and I have to
ensure that my code is always called.

Originally, I wanted to skip the flow control issues by
"renaming" the user's constructor and then implementing a
new constructor, like:

public class Foo {
  public Foo() {
    user_Foo();
    // instrumented code
  }

  public void user_Foo() {
    // old constructor code
  }
}

But this gets complicated fast, because I have to deal
with the super call to the parent class.

My next thought was to use finally to skip flow control
again, but little did I realize that the finally language
construct is actually achieved by the compiler
determining flow control, and not through any special
support by the VM.

So, I've given up trying to skip flow control, and I'm
currently working with the idea of replacing each return
statement with a goto that jumps to the new logic which
is added to the end. (Should be able to do this, because
all returns from a constructor should be the same).

So, my primary question is this: What's the easiest way
to instrument the constructor to add the new logic?

Some other questions:

There's no guarantee that the "this" reference will be in
slot 0, by the time my code executes, right? (There
doesn't seem to be any hard and fast rule that says
argument slots shouldn't be overwritten - I've seen javac
overwrite them with impugnity). So I'll have to add some
instructions at the top of the method to immediately
store it to an unused slot, correct?

When instrumenting a method, I need to create a new one
and selectively copy instructions from the old one. Will
the jump targets remain correct when I add or remove
instructions as long as I don't remove the targetted
instructions? 

If I use InstructionFactory.addLocalVariable before I
begin adding any of the old instructions to the new
method, will the old instructions retarget adjusted
variable slots?

Thanks much and God bless,
-Toby Reyelts

--
To unsubscribe, e-mail:   <mailto:bcel-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:bcel-user-help@jakarta.apache.org>


Mime
View raw message