Replace java operators with methods in bytecode using javassist

My goal

To be able to detect when a comparison is being performed at runtime (or any other operation, for example, *, -, /,>, <, ...

This should be done to edit the class bytecode using Javassist or ow2 ASM

What must be achieved

This code

public class Test{ public void m(){ if(a>2){ //blablabla } } } 

Must become

 public class Test{ public void m(){ if(someExternalClass.greaterThan(a,2)){ //blalbla } } } 

The larger the value will be returned in exactly the same way as the '>', it will also be used to save the number of comparisons. Then the outer class will be notified every time the comparison is made.

Additional note

This should be done wherever there is an operation. Thus, not only in if statements.

It means

 int a = c+d; 

should also become

 int a = someExternalClass.add(c,d); 

Do you have any suggestions on how I can achieve this using Javassist or other libraries.

I guess this will be related to OpCodes such as IFLT, IFGT

+6
source share
2 answers

This may be easier to do with the source code or at compile time using the annotation processor. For the annotation processing approach, you can use Juast or Lombok .

OperatorOverload seems to almost do what you want, it replaces binary expressions with method calls.

+2
source

This smart hack: http://www.jroller.com/eu/entry/operation_overloading_in_java can give you a hint. The initial idea is to provide some operator support overload for Java. You do not do the same, but it is somewhat related.

+2
source

Source: https://habr.com/ru/post/912743/


All Articles