1 package org.testng.internal.invokers; 2 3 import org.testng.IInvokedMethodListener; 4 import org.testng.IInvokedMethodListener2; 5 import org.testng.TestNGException; 6 7 /** 8 * Indicates whether a {@link InvokedMethodListenerMethod} is to be called on a simple or an 9 * extended invoked method listener. All {@link IInvokedMethodListener}s are considered 10 * {@link #SIMPLE_LISTENER}, instances of {@link IInvokedMethodListener2} are all considered 11 * {@link #EXTENDED_LISTENER}. 12 * 13 * @author Ansgar Konermann 14 */ 15 enum InvokedMethodListenerSubtype { 16 17 EXTENDED_LISTENER(IInvokedMethodListener2.class), 18 SIMPLE_LISTENER(IInvokedMethodListener.class); 19 20 private Class<? extends IInvokedMethodListener> m_matchingInterface; 21 InvokedMethodListenerSubtype(Class<? extends IInvokedMethodListener> listenerClass)22 private InvokedMethodListenerSubtype(Class<? extends IInvokedMethodListener> listenerClass) { 23 m_matchingInterface = listenerClass; 24 } 25 isInstance(IInvokedMethodListener listenerInstance)26 private boolean isInstance(IInvokedMethodListener listenerInstance) { 27 return m_matchingInterface.isInstance(listenerInstance); 28 } 29 fromListener(IInvokedMethodListener listenerInstance)30 public static InvokedMethodListenerSubtype fromListener(IInvokedMethodListener listenerInstance) { 31 if (EXTENDED_LISTENER.isInstance(listenerInstance)) { 32 return EXTENDED_LISTENER; 33 } 34 else if (SIMPLE_LISTENER.isInstance(listenerInstance)) { 35 return SIMPLE_LISTENER; 36 } 37 throw new TestNGException("Illegal " + IInvokedMethodListener.class.getSimpleName() 38 + " instance: " + listenerInstance.getClass().getName() + "."); 39 } 40 } 41