org.ak.trafficController.annotations.impl.MethodInvoker Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of traffic-controller-annotations Show documentation
Show all versions of traffic-controller-annotations Show documentation
This deals with different aspects like In memory queue, throttling, event management etc
The newest version!
/**
*
*/
package org.ak.trafficController.annotations.impl;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Named;
import org.springframework.context.ApplicationContext;
/**
* @author amit.khosla
*
*/
@Named
public class MethodInvoker {
/**
* Context to find the class from spring context.
*/
@Inject
ApplicationContext context;
protected Object getObjectHavingMethod(Class consumerClass) throws InstantiationException, IllegalAccessException {
Map beansPresent = context.getBeansOfType(consumerClass);
if (beansPresent.size() > 0) {
return beansPresent.values().iterator().next();
}
return consumerClass.newInstance();
}
public Object extractData(Class consumerClass, String method) throws InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
Object objectToWorkOn = getObjectHavingMethod(consumerClass);
Method m = consumerClass.getMethod(method);
return m.invoke(objectToWorkOn);
}
public void executeMethod(Class consumerClass, String method, Object param) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Object objectToWorkOn = getObjectHavingMethod(consumerClass);
for (Method m : consumerClass.getMethods()) {
if (m.getName().equals(method) && m.getParameterCount() == 1) {
m.invoke(objectToWorkOn, param);
return;
}
}
}
}