Search code examples
javaattributesproxyintrospectionintercept

Intercepting Java field and method access, creating proxy objects


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 :)


Solution

  • It is not possible using pure Java, except via:

    Bytecode Manipulation

    For example using AspectJ.

    Annotation Processor

    Using a custom annotation processor, which actually is a kind of bytecode manipulation as well. Projekt Lombok is doing something like this.

    Synthetic Accessor Method

    That is, if the code is anyway using a synthetic accessor method (in which case you could in theory create a proxy):

    public class Test {
        public static void main(String... args) {
            TestClass t = new TestClass();
            // this is actually calling a synthetic accessor method
            t.hello = "x";
        }
        static class TestClass {
            private String hello;
        }
    }