Uploaded image for project: 'CDI Specification Issues'
  1. CDI Specification Issues
  2. CDI-740

Proxy fields can be accessed if they are protected or package private

XMLWordPrintable

    • Hide
      • Create a @RequestScoped bean
      • Add a protected or package private field
      • Inject the bean into another bean with different context
      • Access the field, it will come from the proxy object
      • Value will not be initialized and incorrect
      Show
      Create a @RequestScoped bean Add a protected or package private field Inject the bean into another bean with different context Access the field, it will come from the proxy object Value will not be initialized and incorrect

      CDI detects and rejects proxyable beans if they expose a public field. However protected and package private fields are silently accepted. In the following example the method returns 0 instead of the correct value of 8:

      @ApplicationScoped
      public class Car {
      
          @Inject
          private Engine engine;
      
          public int numberOfEngineCylinders() {
              return engine.cylinders;
          }
      
      }
      
      @RequestScoped
      public class Engine {
      
          protected int cylinders;
      
          @PostConstruct
          public void init() {
              cylinders = 8;
          }
      
      }
      

              Unassigned Unassigned
              frigocoder Frigo Coder (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Created:
                Updated:
                Resolved: