RE: Help needed to Intercept and overide getter and setter method calls

"Shankar Unni" <[email protected]>
Newsgroups gmane.comp.java.beanshell.devel
Message-ID <034801c3a35d$983545e0$0300a8c0@HQSHANKAR>
>         property.put("value1", new Integer(2));
>         property.put("value2", new Integer(3));
>         property.put("value2", new Integer(0));

>         i.eval("property.value3 = property.value2 * 
> property.value1;");
> 
>         // I want to intercept the calls to Properties getter 
> and setter values and call the Property.get("value1") etc

Not directly, no. I, too, had the same idea for our integration with
BeanShell (my product has objects that keep attributes in a simple hashmap,
exactly like this).

There is a way to do this without BeanShell code changes, but it's a bit
painful: you use reflection to examine the list of keys in your map, and
create a wrapper class on the fly with something like BCEL (Byte Code
Engineering Library), like a proxy class, that you wrap your object with
before passing to BeanShell. This wrapper would have getValue1(), etc.,
methods.

But it would be nice if there was a simple hook or hooks in the field
resolution code that could get invoked when a field resolution failed using
the current mechanisms. Exactly like the "invoke()" mechanism already
available for method resolution.. 

(Actually, there would need to be two hooks along the lines of:

   Object getField(..., String fieldName);
   void setField(..., String fieldName, Object value);

And the appropriate one would be invoked depending on whether an lvalue or
an rvalue was required).

What do you think, Pat?




-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive?  Does it
help you create better code?   SHARE THE LOVE, and help us help
YOU!  Click Here: http://sourceforge.net/donate/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.