jakarta-bcel-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Koduru, Rajendra Kumar Reddy" <rajendra.kumar.reddy.kod...@sap.com>
Subject Please help me regarding finally block
Date Fri, 04 Feb 2005 20:07:22 GMT
Hi All, 
	
	Currently i am planning to instrument each method of a class
dynamically with system.out.println() statements.
	
	as it is not possible to get it executed, due to the return
statements which are available in middle of method, 

	As seen from many sources, implementing it with try and finally
is a good idea.

	finally seems to be more complex to handle compared to catch
(atleast to me).

	however i got the source from internet, i could not understand
few things in the source, could some one please clarify me.

	i have directly put put my understandings and queries in the
middle of code IN CAPS, [code is given below, of course not too big]. So
please correct me and help me in this regard.

Thank you
Reddy



public void instrumentMethod (ClassGen cg, MethodGen mg)
	{
		InstructionList il = mg.getInstructionList();

		InstructionFactory ifact = new InstructionFactory(cg);

		InstructionHandle start = il.getStart();
		InstructionHandle end = il.getEnd();


		//  IN THE FOLLOWING SNIPPET OF CODE, CALL TO
SUBROUTINES ARE INSERTED AND ALL THE TARGETS OF RETURN INSTRUCTIONS ARE
CHANGED

		Collection<JSR> jsrs = new ArrayList<JSR>();
		for (Iterator i = il.iterator(); i.hasNext(); ) {
			InstructionHandle ih =
(InstructionHandle)i.next();

			if (ih.getInstruction() instanceof RETURN) {
				JSR jsr2 = new JSR(null);
				jsrs.add(jsr2);
				InstructionHandle n = il.insert(ih,
jsr2);
							
				il.redirectBranches(ih, n);
			}
		}

		// END OF ABOVE UNDERSTANDING. AM I CORRECT ??

		
		
		InstructionHandle h1 = il.append(new ASTORE(0)); // AT
THE CURRENT SITUATION, WHAT IS ON THE TOP OF STACK AND WHY IS IT
STORED??

		JSR jsr = new JSR(null);
// WHY A SUBROUTINE CALL IS DONE HERE??? 

		jsrs.add(jsr);
		il.append(jsr);

		InstructionHandle h2 = il.append(new ALOAD(0));	  //
REFLECTS THE ABOVE STORE OPERATION; SO WHAT IS ACTUALLY IN THIS NOW??

		il.append(new ATHROW());
// I THINK IT IS TO RETHROW THE EXCEPTION IF AN EXCEPTION ARISES

		InstructionHandle handler = il.append(new ASTORE(1)); //
STORES THE EXCEPTION VARIABLE

		// I WILL APPEND AN PRINTLN STATEMENT HERE 

		il.append(new RET(1));
// RETURNING FROM SUBROUTINE, BUT DOESNT THE INDEX OF RET START AT 0

		for (JSR j : jsrs) {
			j.setTarget(handler);
		}	

		mg.addExceptionHandler(start, end, h1, null);	//
DOESNT THE EXCEPTION HANDLER START AT HANDLE handler(which is just 3
lines above in this code)

		il.setPositions(true);
//  IS THIS NECESSARY ??
	}



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


Mime
View raw message