title: Cast Bean<?> from BeanManager to class instance in Servlet
body: <p>I have an EJB project, and have the following annotation:</p>
  
  <pre><code>@Retention(RUNTIME)
  @Target(TYPE)
  @Qualifier
  public @interface AfterComplete {
  }
  </code></pre>
  
  <p>I have 2 classes that are decorated with this annotation.  They are also <code>@Stateless</code></p>
  
  <pre><code>@AfterComplete
  @Stateless
  public class AfterCompletePrinter implements IAfterComplete {
  
    public void afterComplete(String someValue) {
      System.out.println("After complete printer: " + someValue);
    }
  
  }
  
  
  @AfterComplete
  @Stateless
  public class AfterCompleteErrPrinter implements IAfterComplete{
  
    public void afterComplete(String someValue) {
      System.err.println(someValue);
    }
  }
  </code></pre>
  
  <p>In a servlet, I have:</p>
  
  <pre><code>@WebServlet("/")
  public class DemoServlet extends HttpServlet {
    @Inject
    BeanManager beanManager;
  
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
      Set&lt;Bean&lt;?&gt;&gt; beans = beanManager
          .getBeans(Object.class,new AnnotationLiteral&lt;AfterComplete&gt;() {});
  
      for (Bean&lt;?&gt; bean : beans) {
        response.getWriter().print(bean.getBeanClass());
        // IAfterComplete iac = (IAfterComplete) bean;
      }
    }
  }
  </code></pre>
  
  <p>My servlet prints:</p>
  
  <blockquote>
    <blockquote>
      <p>net.mikeski.ejb_annotations.AfterCompletePrinterclass 
      net.mikeski.ejb_annotations.AfterCompleteErrPrinter</p>
    </blockquote>
  </blockquote>
  
  <p>However, this line throws an exception if it's not commented:</p>
  
  <blockquote>
    <blockquote>
      <p>IAfterComplete iac = (IAfterComplete) bean;</p>
    </blockquote>
  </blockquote>
  
  <p>The exception is:</p>
  
  <blockquote>
    <blockquote>
      <p>java.lang.ClassCastException: org.jboss.weld.bean.SessionBean cannot be cast to net.mikeski.ejb_annotations.IAfterComplete</p>
    </blockquote>
  </blockquote>
  
  <p>How can I do something with <code>bean</code> to get the instance of the class that implements <code>IAfterComplete</code>?  The <code>bean.getName()</code> returns null.</p>
  
  <p>I was thinking if I can get its JNDI name I can do a lookup, but don't see how to do that.</p>
  
  <p>This is Wildfly 10 if it makes any difference.</p>