Re: Constructor Instrumentation : super()

"Nikhil C. Khedkar" <[email protected]>
Newsgroups gmane.comp.jakarta.bcel.devel
Message-ID <20040219094849.89605.qmail__5280.4670799107$1077184146@web13609.mail.yahoo.com>
Hi Erik and Andrew,
	Finally able to get this thing fixed. Here's the
approach I have followed (Just for everyone's
information).

1)Firstly detect that a method is constructor from its
name "<init>".
2)Put a try catch block.
3)Take care to skip super() calls which should be the
first line in the constructor. [Andrew won't agree
with this. He has indeed provided me with some code,
but I think my assumption is true for normal Sun javac
compiler. Andrew please confirm]
4)Now the approach to find super() call and
distinguish it from an explict new() call.
Scan all the instructions in the constructor from 0
onwards. Keep a counter initialized at 0. Increment
the counter when you encounter a NEW instruction.
Decrement it when you encounter a INVOKESPECIAL
(that's how you create an object). When this counter
becomes negative, you have encountered a super() call.
When this counter becomes 0, its the new() call.
[Thanks Eric]

That's all.

Thanks,
	Nikhil Khedkar


Here's my problem statement if you haven't read it
previously:

Hi,
	I have a query regarding the instrumentation of the
constructor. I have read all the archived mail in this
regard, but none of them served useful. I have the
same requirement i.e. put a method call at each method
start and method end. Have already put a try catch for
that. Now the task is to skip super() call before I
put my method call in the constructor.
	
	I have a code that works for 99% of the cases. 
	
	for(int j = 1; j < ihs.length; j++)
	{
		if(ihs[j].getInstruction() instanceof INVOKESPECIAL)
		{
			il.append(ihs[j], instrumentedPatchList);
			break;
		}
	}
	
	It works for 
	
	1)public ABCD(XYZ obj)
	{
		super(obj);
	}
	
	Now a more complicated case
	
	2)public Tree(int type, MainPanel mainPanel)
	{
		super(new DefaultTreeModel(new RuleNode("Rules")));
		method1();
		method2();
	}
	
	The above code fails because the Special Instruction
is not at first position. Hence A refined approach
will be to get the first instruction handle of the
second line in the constructor (first instruction
handle for method1()). Now I scan all the instructions
of the first line (super(new DefaultTreeModel(new
RuleNode("Rules")))). If there is any INVOKESPECIAL in
this list I put my code at the end of first line. This
thing works fine.
	
	A bit more complex case
	
	3)private XYZHandler(EventHandler commandHandler)
	{
		super(
			ABCD.getInstance(),
			Helper.getInstance(),
			commandHandler,
			100);
		method1();
	}
	
	Now the above mentioned approach doesn't work and I
end up putting my method call before super() and get
VerifyError. Has anyone (especially the ones who have
posted those questions on constructor instrumentation
previously) been able to build a fullproof code?
	
Thanks,
	Nikhil Khedkar

__________________________________
Do you Yahoo!?
Yahoo! Mail SpamGuard - Read only the mail you want.
http://antispam.yahoo.com/tools
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.