On Sat, 8 Aug 2009 12:04:23 -0700 (PDT), Jimmy
<jimmy_please@yahoo.com> wrote, quoted or indirectly quoted someone
who said :
It is possible to access data member of a Java class with pointcut,
but I need to do a field assignment or even hijack/override the method
with a custom implement with AspectJ. Can someone refer me to some
sample code URL and/or tutorial for doing so?
I don't know about AspectJ, but in plain Java you can't modify the
code of the method bodies at run time the way you can in some other
languages.
What you could do is construct a new class, with different source code
for the methods, compile it on the fly, and load the modified class
with a new ClassLoader.
You can also load a number of classes on the fly, knowing only their
names you read from file or resource. Usually they would all
implement a common interface. You then have a set of delegate
objects, each of which implements the interface differently with code
decided long after your mother code was compiled.
You could also construct a class file of byte codes on the fly and
load it.
If you are a glutton for punishment, you can load classes, and probe
them to find out what sorts of methods and parms they have, and
dynamically create parameter lists to invoke them.
Some of these are quite advanced techniques.
see http://mindprod.com/jgloss/onthefly.html
http://mindprod.com/jgloss/javacompiler.html
http://mindprod.com/jgloss/jasm.html
http://mindprod.com/jgloss/classloader.html
http://mindprod.com/jgloss/delegate.html
http://mindprod.com/jgloss/reflection.html
intended to do this kind of stuff.