3

In the following test case , direct field t is not intercepted by CGLIB. So can I do it use CGLIB?

public class Test {

@Test
public void testCGLib() {
    A a = (A) Enhancer.create(A.class, new Class[] {}, new B());
    System.out.println(a.t);
    a.t();
}

public static class A {

    public int t = 0;

    public void t() {
        System.out.println("bbb");
    }

}

public static class B implements LazyLoader {

    @Override
    public Object loadObject() throws Exception {
        System.out.println("xxx");
        return new A();
    }
}
}
Lucifer
  • 29,392
  • 25
  • 90
  • 143
jackalope
  • 1,554
  • 3
  • 17
  • 37

1 Answers1

2

No, that is not possible. Field access is not delegated to some byte code of the class that defines the field. it is directly loaded by the method that uses the field. (In your case, this is Test#testCGLib.)

cglib creates a subclass and intercepts method calls. Fields can however not be overriden, they are not polimorphic, with or without cglib. Therefore, you can never intercept field access. You can only hide fields in subclasses.

But even if you were hiding the field A#t in the instrumented class: Since the cglib class is not visible at compile time, you can not reference the field by the instrumented type. If the instrumented class A$$cglib had a field t you could only access it by

a.getClass().getField("x").get(a)

in order to emulate some sort of dynamic field binding. (Basically, the methods are dynamically bound what will result in the right field being selected dynamically.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192