@Override
 public <T extends EventListener> void addListener(final T t) {
   ensureNotInitialized();
   ensureNotProgramaticListener();
   if (ApplicationListeners.listenerState() != NO_LISTENER
       && ServletContextListener.class.isAssignableFrom(t.getClass())) {
     throw UndertowServletMessages.MESSAGES.cannotAddServletContextListener();
   }
   ListenerInfo listener =
       new ListenerInfo(t.getClass(), new ImmediateInstanceFactory<EventListener>(t));
   deploymentInfo.addListener(listener);
   deployment.getApplicationListeners().addListener(new ManagedListener(listener, true));
 }
 @Override
 public void addListener(final Class<? extends EventListener> listenerClass) {
   ensureNotInitialized();
   ensureNotProgramaticListener();
   if (ApplicationListeners.listenerState() != NO_LISTENER
       && ServletContextListener.class.isAssignableFrom(listenerClass)) {
     throw UndertowServletMessages.MESSAGES.cannotAddServletContextListener();
   }
   InstanceFactory<? extends EventListener> factory = null;
   try {
     factory = deploymentInfo.getClassIntrospecter().createInstanceFactory(listenerClass);
   } catch (Exception e) {
     throw new IllegalArgumentException(e);
   }
   final ListenerInfo listener = new ListenerInfo(listenerClass, factory);
   deploymentInfo.addListener(listener);
   deployment.getApplicationListeners().addListener(new ManagedListener(listener, true));
 }
 private void ensureNotProgramaticListener() {
   if (ApplicationListeners.listenerState() == PROGRAMATIC_LISTENER) {
     throw UndertowServletMessages.MESSAGES.cannotCallFromProgramaticListener();
   }
 }