Announcement Announcement Module
Collapse
No announcement yet.
proxying final methods Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • proxying final methods

    Hi,

    I have an interface:

    Code:
    public interface MyInterface {
      void doA();
      void doB();
    }
    which has an abstract implementation

    Code:
    public abstract AbstractImpl {
      public final void doA() {
        // method implementation omitted
      }
    }
    In the real code, doA() is fully implemented, and therefore marked final. The concrete implementation of this class is:

    Code:
    public class ConcreteImpl extends AbstractImpl {
    
      Object dependency;
    
      @Resource(name = "dependency")
      void setDependency(Object obj) {
        dependency = obj;
      }
    
      @Transactional(propagation = Propagation.REQUIRED)
      void doB() {
        dependency.toString();
      }
    }
    At runtime a NullPointerException is thrown when doB() is called. Apparently what happens is:
    • A transactional proxy is created by CGLIB for ConcreteImpl
    • setDependency() is called on the target
    • When doB() is called on the proxy, dependency is null, which causes the NPE

    However if I remove the final modifier from doA() in AbstractImpl everything works fine. I guess the proxy is created via inheritance, and uses method overriding, which obviously won't work if a method is declared final. However, given that I really don't want subclasses to override doA() I'm not happy about removing the final modifier - any suggestions?

    Thanks in advance,
    DM

  • #2
    CGLib creates class proxies, it does so by (dynamically) subclassing the parent bean and override any of the methods. Which is not the case for final methods of course.

    You are using interfaces so instead of CGLib you might try JDK Dynamic proxies instead.

    Comment

    Working...
    X