Search in sources :

Example 1 with ActionAlreadyDefinedException

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());
            }
        }
    }
}
Also used : Action(com.axway.ats.agent.core.model.Action) CallerRelatedAction(com.axway.ats.agent.core.action.CallerRelatedAction) TemplateAction(com.axway.ats.agent.core.model.TemplateAction) TemplateAction(com.axway.ats.agent.core.model.TemplateAction) ActionMethod(com.axway.ats.agent.core.action.ActionMethod) TemplateActionMethod(com.axway.ats.agent.core.action.TemplateActionMethod) Method(java.lang.reflect.Method) ActionAlreadyDefinedException(com.axway.ats.agent.core.exceptions.ActionAlreadyDefinedException)

Example 2 with ActionAlreadyDefinedException

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);
}
Also used : MethodFinder(com.axway.ats.core.reflect.MethodFinder) Method(java.lang.reflect.Method) ActionAlreadyDefinedException(com.axway.ats.agent.core.exceptions.ActionAlreadyDefinedException) AmbiguousMethodException(com.axway.ats.core.reflect.AmbiguousMethodException)

Aggregations

ActionAlreadyDefinedException (com.axway.ats.agent.core.exceptions.ActionAlreadyDefinedException)2 Method (java.lang.reflect.Method)2 ActionMethod (com.axway.ats.agent.core.action.ActionMethod)1 CallerRelatedAction (com.axway.ats.agent.core.action.CallerRelatedAction)1 TemplateActionMethod (com.axway.ats.agent.core.action.TemplateActionMethod)1 Action (com.axway.ats.agent.core.model.Action)1 TemplateAction (com.axway.ats.agent.core.model.TemplateAction)1 AmbiguousMethodException (com.axway.ats.core.reflect.AmbiguousMethodException)1 MethodFinder (com.axway.ats.core.reflect.MethodFinder)1