Results 1 to 2 of 2

Thread: proxying final methods

  1. #1
    Join Date
    Oct 2006
    Posts
    156

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •