use of com.axway.ats.agent.core.exceptions.ActionAlreadyDefinedException in project ats-framework by Axway.
the class ComponentActionMap method registerActionClass.
/**
* Register a class with action methods
*
* @param actionClass the action class
*/
public void registerActionClass(Class<?> actionClass) {
log.info("Registering action class '" + actionClass.getName() + "'");
for (Method classMethod : actionClass.getMethods()) {
Action actionAnnotation = classMethod.getAnnotation(Action.class);
TemplateAction templateActionAnnotation = classMethod.getAnnotation(TemplateAction.class);
if (actionAnnotation != null || templateActionAnnotation != null) {
String actionClassName = actionClass.getSimpleName();
String actionMethodName = classMethod.getName();
String actionName = actionAnnotation != null ? actionAnnotation.name() : templateActionAnnotation.name();
if (StringUtils.isNullOrEmpty(actionName)) {
actionName = ActionMethod.buildActionMethodName(classMethod);
}
try {
addAction(actionName, actionClassName, actionMethodName, classMethod, actionClass, actionAnnotation != null);
} catch (ActionAlreadyDefinedException aaee) {
// log an error in case of a duplicate action and continue
log.error(aaee.getMessage());
}
}
}
}
use of com.axway.ats.agent.core.exceptions.ActionAlreadyDefinedException in project ats-framework by Axway.
the class ActionMethodContainer method add.
/**
* Add a new action method
*
* @param actionMethod the action method to add
* @return
* @throws ActionAlreadyDefinedException if the new action has the exact same arguments of an existing one
*/
public boolean add(ActionMethod actionMethod) throws ActionAlreadyDefinedException {
// if there is at least one method check for ambiguity
if (actionMethods.size() > 0) {
Class<?>[] newMethodParamTypes = actionMethod.getMethod().getParameterTypes();
// find the action method implementation based on the arguments
MethodFinder methodFinder = new MethodFinder("methods for action " + actionName, getMethods(), customComparisonRules);
// get the most specific method which accepts these parameters
try {
Method mostSpecificMethod = methodFinder.findMethod(newMethodParamTypes);
// then we have ambiguity, which should not be allowed
if (Arrays.equals(newMethodParamTypes, mostSpecificMethod.getParameterTypes())) {
throw new ActionAlreadyDefinedException(actionName, componentName, mostSpecificMethod);
}
} catch (NoSuchMethodException e) {
// method which accepts there argument does not exist, we can safely add it
} catch (AmbiguousMethodException e) {
// unless they are exactly the same, which we don't allow to happen
throw new RuntimeException("AmbiguousMethodException caught while searching for action methods");
}
}
// add the action method
return actionMethods.add(actionMethod);
}
Aggregations