Hello,

I am a beginner, and I have a doubt... I want to modify an existing class. So, I used 
BCELifier to generate the code of the class, and then tried to used it to modify my 
class. 
The modification is a call to a static method called 
Monitor.incluirTraceEstado(String), passing ("Par = " + par) as a parameter (par is 
int private attribute).
When I insert the call in the source code, without using BCEL, the code of the method 
that calls de static method is this (just the beginning):

Code(max_stack = 3, max_locals = 3, code_length = 88)
0:    new               <java.lang.StringBuffer> (6)
3:    dup
4:    invokespecial     java.lang.StringBuffer.<init> ()V (7)
7:    ldc               "Par = " (8)
9:    invokevirtual     java.lang.StringBuffer.append 
(Ljava/lang/String;)Ljava/lang/StringBuffer; (9)
12:   iload_1
13:   invokevirtual     java.lang.StringBuffer.append (I)Ljava/lang/StringBuffer; (10)
16:   invokevirtual     java.lang.StringBuffer.toString ()Ljava/lang/String; (11)
19:   invokestatic      Monitor.incluirTraceEstado (Ljava/lang/String;)V (12)
22:   aload_0
23:   getfield          Fila.vetor_par [I (2)
26:   aload_0
27:   getfield          Fila.ipar I (4)


But, when I insert it using BCEL, the code is this (lines 0 to 19 are new). It's 
almost the same, the differences are some number in lines 7 and 19. And it doesn't 
work!

0:    new               <java.lang.StringBuffer> (7)
3:    dup
4:    invokespecial     java.lang.StringBuffer.<init> ()V (8)
7:    ldc               "Par = " (76)
9:    invokevirtual     java.lang.StringBuffer.append 
(Ljava/lang/String;)Ljava/lang/StringBuffer; (10)
12:   iload_1
13:   invokevirtual     java.lang.StringBuffer.append (I)Ljava/lang/StringBuffer; (11)
16:   invokevirtual     java.lang.StringBuffer.toString ()Ljava/lang/String; (13)
19:   invokestatic      Monitor.incluirTraceEstado (Ljava/lang/String;)V (74)
22:   aload_0
23:   getfield          Fila.vetor_par [I (2)
26:   aload_0
27:   getfield          Fila.ipar I (4)

My question is: what are these numbers? Are they causing the problem? How can I fix 
this code? The BCEL code that I wrote to create it is: (I'm inserting the last 
instruction first, and so on... )

JavaClass clazz = Repository.lookupClass("Fila");
Method[] methods = clazz.getMethods();
ConstantPoolGen cp = new ConstantPoolGen(clazz.getConstantPool());
MethodGen mg = new MethodGen(methods[1], clazz.getClassName(), cp);
InstructionList il = mg.getInstructionList();
InstructionFactory factory = new InstructionFactory(cp);
il.insert(factory.createInvoke("Monitor", "incluirTraceEstado", Type.VOID, new Type[] 
{ Type.STRING }, (short)184));
il.insert(factory.createInvoke("java.lang.StringBuffer", "toString", Type.STRING, 
Type.NO_ARGS, (short)182));
il.insert(factory.createInvoke("java.lang.StringBuffer", "append", Type.STRINGBUFFER, 
new Type[] { Type.INT }, (short)182));
il.insert(factory.createLoad(Type.INT, 1));
il.insert(factory.createInvoke("java.lang.StringBuffer", "append", Type.STRINGBUFFER, 
new Type[] { Type.STRING }, (short)182));
il.insert(new PUSH(cp, "Par = "));
il.insert(factory.createInvoke("java.lang.StringBuffer", "<init>", Type.VOID, 
Type.NO_ARGS, (short)183));
il.insert(InstructionConstants.DUP);
InstructionHandle ih_0 = il.insert(factory.createNew("java.lang.StringBuffer"));
methods[1] = mg.getMethod();

Sorry about the huge email and my poor English! I'll be very glad with some help.

Thanks!

Camila Rocha
------------------------------------------------------------------------
Master Student in Computer Science
Institute of Computing - UNICAMP - Brazil
www.ic.unicamp.br/~ra022247

Reply via email to