You are viewing a plain text version of this content. The canonical link for it is here.
Posted to bcel-user@jakarta.apache.org by Eric Chow <ec...@macaucabletv.com> on 2004/06/28 11:26:40 UTC

modify class !!!

Hello, 

I am a beginner in using BCEL. 

Is it possible to modify the variable values and variable name with BCEL? 

For example, 

class test { 
public void init(int a, String x) { 
int i; 
String o; 

i = a + 1; 
o = x + " ... "; 
} 
} 


In the above java, after I compiled it to test.class, how can I change the 

1. init(int a, String x) to init(int intVar, String strVar) 
2. int i ----> int tmpVar; 
3. String o ----> String oVar; 
4. i = a + 1 ----> i = 10; 



Best regards, 
Eric 


==========================
If you know what you are doing, 
it is not called RESEARCH!
==========================

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


Re: modify class !!!

Posted by SianLun Lau <sl...@comtec.e-technik.uni-kassel.de>.
On Mon, 28 Jun 2004 11:58:52 +0200
Dick Eimers <dw...@cs.uu.nl> wrote:

Hi all,

> > Is it possible to modify the variable values and variable name
> > with BCEL? 

yes. but i don see why variable names should be changed :)

[snipped]
> > 4. i = a + 1 ----> i = 10; 
> 
> Can't say anything about this since there is no pattern. I mean, do
> you mean rewriting the 'i' variable to 10? The value of 'i' is the
> result of a evaluating a bytecode sequence. Consider the following; 
[snipped]
> the key issue however is locating a pattern that works for all
> methods.... 

if this is just an assignment or a self learn project, i would do it
with the help of BCELifier with the original code, and then
again with the new code. by comparing both outputs, you will see what
is actually needed to be changed.

that makes learning faster, and also, erm, more fun.

newbie,
SianLun

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


Re: modify class !!!

Posted by Dick Eimers <dw...@cs.uu.nl>.
> Is it possible to modify the variable values and variable name with BCEL? 
> 
> For example, 
> 
> class test { 
> public void init(int a, String x) { 
> int i; 
> String o; 
> 
> i = a + 1; 
> o = x + " ... "; 
> } 
> } 

Don't know if I correctly understand your intentions,  but let me give
it a shot:

> 1. init(int a, String x) to init(int intVar, String strVar) 

Done :) names of variables are a Java language notion, in a Java class
file these are mapped do local variable indexes, so this is a non-issue.

> 2. int i ----> int tmpVar; 

'i' is just local variable slot for some time. If the variable is out of
scope, the local variable slot could be reused. non-issue. 'i' doesn't
really exist, just added to the LocalVariableTable attribute by the
compiler for debugging purposes.
 
> 3. String o ----> String oVar; 

idem. 

> 4. i = a + 1 ----> i = 10; 

Can't say anything about this since there is no pattern. I mean, do you
mean rewriting the 'i' variable to 10? The value of 'i' is the result of
a evaluating a bytecode sequence. Consider the following; 

.. 
iload_1
iconst_1
iadd
istore ?
..

At some point you get to the assignment of the 'i' variable. The value
of 'a' is in local variable slot 1 (becuase you are describing an
instance methods, thus slot 0 is used by the reference to the object
invoked on (this)). The iload_1 places a copy on the stack, next the
iconst_1 places constant 1 on the stack and the iadd instruction sums
the two top stack values. Finally, the istore instruction stores the
result of the addition in the local variable that represents the 'a'
variable.

So based on your intentions you can try to find a pattern in the
bytecode and rewrite it like, 

iload_1  
iconst_1  => iconst 10  
iadd         
istore ?     istore ?     

the key issue however is locating a pattern that works for all
methods.... 





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