I want to create such object in Java that will contain some "dispatcher" function like Object getAttr(String name)
that will receive all attribute access attempts - so, if I'll do System.out.print(myObj.hello)
, actual code will be translated to something like System.out.print(myObj.getAttr('hello'))
, and if I will do myObj.hello = 123
, it should execute as myObj.setAttr('hello', 123)
. Please, note that I should be able to use ANY attribute name, I don't know list of possible names in advance.
So, in this case, is it ever possible?
UPD#1: I'm writing new language for JVM (somehow (J|P)ython-like, so let's call it Jython) with very tight Java integration intented. One of wanted design features is ability to seamlessly access Jython object attributes from Java code just by typing jythonObject.some_attribute
. So here is the deal.
Closed: Using AOP via AspectJ seems to be the only possible solution for this, so thank you all for help, and especially Thomas for the most extended answer :)