Uploaded image for project: 'Weld'
  1. Weld
  2. WELD-1823

CDI1.2 section 6.5.3 a null value should be returned for the injection point if it is not being injected into any injection point.

    XMLWordPrintable

Details

    Description

      In CDI 1.2, section 6.5.3,
      The container must ensure that every injection point of type InjectionPoint and qualifier
      @Default of any dependent object instantiated during this process receives:
      • a null value if it is not being injected into any injection point.

      The above is not working on weld 2.2.5. I created a test for this and confirmed that I got a not null injection point.
      ======================

      CDIBean2 .java
      public class CDIBean2 {	
      	private String data;	
      	public String getData() {
      		return data;
      	}
      	public void setData(String data) {
      		this.data = data;
      	}
      }
      
      CDIBean.java
       
      public class CDIBean {
      	
      	CDIBean2 beanManagerInstance;
      
      	public CDIBean(BeanManager manager){
      		Set<Bean<?>> beanList = manager.getBeans(CDIBean2.class);
      		if (beanList!=null && !beanList.isEmpty()){
      			Bean<?> bean = beanList.iterator().next();
      			CreationalContext<?> context = manager.createCreationalContext(bean);
      			beanManagerInstance = (CDIBean2) manager.getReference(bean, CDIBean2.class, context);
      		}
      	}
      	
      	public String getData() {
      		return beanManagerInstance.getData();
      	}
      
      	public void setData(String data) {
      		beanManagerInstance.setData(data);
      	}
      
      CDIBean2Producer.java
      public class CDIBean2Producer {
      	
      	@Produces
      	public CDIBean2 produce(InjectionPoint ip){
      		
      		if(ip != null){			
      			throw new RuntimeException("InjectionPoint exists when producing CDIBean2");
      		}		
      		return new CDIBean2();		
      	}
      }
      
      CDIBeanProducer.java
      public class CDIBeanProducer {
      	
      	@Inject
      	private BeanManager manager;
      	
      	@Produces
      	public CDIBean produce(InjectionPoint ip){
      		
      		if(ip == null){
      			throw new RuntimeException("InjectionPoint is NULL when producing CDIBean");
      		}		
      		return new CDIBean(manager);		
      	}
      }
      
      TestServlet.java
      @WebServlet("/ProducerNullIPServlet")
      public class TestServlet extends HttpServlet {
      
          private static final long serialVersionUID = 1L;
      
          @Inject
          private CDIBean bean;
          @Inject
          private BeanManager manager;
          @Inject
          private Instance<AnotherBean> anotherBeanInstance;
      
          //@Override
          @Override
          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
              doPost(req, resp);
          }
      
          //@Override
          @Override
          protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      
              PrintWriter out = resp.getWriter();
      
              Set<Bean<?>> beanList = manager.getBeans(CDIBean2.class);
              if (beanList != null && !beanList.isEmpty()) {
                  Bean<?> bean = beanList.iterator().next();
                  CreationalContext<?> context = manager.createCreationalContext(bean);
                  CDIBean2 beanManagerInstance = (CDIBean2) manager.getReference(bean, CDIBean2.class, context);
                  beanManagerInstance.setData("Test2");
              }
              out.println("Test Sucessful!");
          }
      }
      

      The injection point I got in @Produces
      public CDIBean2 produce(InjectionPoint ip)

      {..}

      refers to the @Inject
      private CDIBean bean, which is incorrect.

      Attachments

        Activity

          People

            mkouba@redhat.com Martin Kouba
            emijiang6 Emily Jiang (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: