jakarta-bcel-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dick Eimers <dweim...@cs.uu.nl>
Subject Re: JavaClassFileOutputException: Stack underflow
Date Thu, 15 Jul 2004 13:09:51 GMT
Instrumenting object initialization methods, methods with the special
name <init> (constructors are a Java programming language concept)
require caution. Various discussions covered the topic, but it comes
down to that all object initializers should invoke another object
initializer either in the current class or the superclass (chaining). If
your code does not rely on a reference to the object that is being
initialized (the Java programming language *this*) then it is safe to
insert the code at the beginning of the instructionlist.. 

> I'm trying to insert some code in the constructor of certain classes. The code should
be getting the current singleton instance of a class 'MySingleton' and call a method 'myMethod'
on this instance. My code is as follows :
> 
> InstructionList il = mg.getInstructionList();         
> il.insert(factory.createInvoke("MySingleTon", "getInstance", new ObjectType("MySingleton"),
Type.NO_ARGS, Constants.INVOKESTATIC));
> il.insert(InstructionFactory.createLoad(Type.OBJECT, 0));
> il.insert(factory.createInvoke("java.lang.Object", "getClass", new ObjectType("java.lang.Class"),
Type.NO_ARGS, Constants.INVOKEVIRTUAL));
> il.insert(factory.createInvoke("java.lang.Class", "getName", Type.STRING, Type.NO_ARGS,
Constants.INVOKEVIRTUAL));
> il.insert(InstructionConstants.ACONST_NULL);
> il.insert(factory.createInvoke("MySingleton", "myMethod", Type.VOID, new Type[] { Type.STRING,
new ObjectType("MyObject") }, Constants.INVOKEVIRTUAL));
>             
> mg.setInstructionList(il);
> mg.setMaxStack();

If mg is a <init> then you're about to have a conflict with the
verifier.. 


---------------------------------------------------------------------
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