I have the following problem to solve:
I need to detect assignments to a specific field, where the right-hand side is also of a specific form, e.g. it is the same field (or the field of the same specific type) of another object:
obj1.fieldX = obj2.fieldX
The reason for looking for such a pattern is an (peephole) optimization of the code that I generate using Javassist. Currently, I have my basic transformation working (using javassist.expr.FieldAccess), but it treats such assignments as two independent operations:
read from fieldX of obj2
write into fieldX of obj1
and I cannot see that they are used in combination and cannot optimize such a typical use-case. As a result, the generated code is rather inefficient at run-time and is about 2-3 times slower, because both transformations done independently result in prodicing a lot of heap-allocated intermediary objects of complex types. Those objects are generated as a result of a read and some of their fields are initialized from obj2.fieldX. Then write does the opposite action, i.e. copies some of those fields of a newly allocated object into a target object, i.e. into ob1.fieldX. And then the intermediary object is not used any more. My wish is to directly copy some information from obj2.fieldX into obj1.fieldX, without generating all those useless intermediary objects on heap.
Therefore I have questions:
1) Is it in principle possible using Javassist to detect something like this and to replace the whole expression by a new code or transform it?
I've seen that BCEL has something like this for matching sequences of byte-codes - it has code matching using regular expressions, like "NOP+(ILOAD|ALOAD)*" (http://commons.apache.org/bcel/manual.html, section 3.3.7). May be Javassist also has it in a certain form?
2) May be FieldAccess allows access to additional information using $_, $0 or $1, so that one can try to match a pattern against a the target (i.e. $_) of read or value argument of a write and perform a dedicated action if it is detected?
3) May be there are other ways rather than code matching at the bytecode level to achieve my goal?