use of com.axway.ats.agent.core.model.Parameter in project ats-framework by Axway.
the class ActionClassGenerator method generateActionDefinition.
private String generateActionDefinition(String actionName, Method actionImplementation) {
log.info("Generating method implementation for action '" + actionName + "'");
String[] paramNames = new String[actionImplementation.getParameterTypes().length];
String[] paramTypes = new String[actionImplementation.getParameterTypes().length];
Annotation[][] parameterAnnotations = actionImplementation.getParameterAnnotations();
for (int i = 0; i < parameterAnnotations.length; i++) {
Class<?> paramType = actionImplementation.getParameterTypes()[i];
Annotation[] currentParamAnnotations = parameterAnnotations[i];
Parameter paramAnnotation = null;
for (int j = 0; j < currentParamAnnotations.length; j++) {
if (currentParamAnnotations[j] instanceof Parameter) {
paramAnnotation = (Parameter) currentParamAnnotations[j];
break;
}
}
if (paramAnnotation == null) {
throw new BuildException("No @Parameter annotation for one of the parameters of action method " + actionImplementation.toString());
}
paramNames[i] = paramAnnotation.name();
if (paramType.isArray() && paramType.getComponentType().isEnum()) {
//array of enums should be represented by array of String in the generated stub
paramTypes[i] = "String[]";
} else if (paramType.isEnum()) {
//enums should be represented by Strings in the generated stub
paramTypes[i] = "String";
} else {
paramTypes[i] = paramType.getSimpleName();
}
}
//parameters and arguments
if (paramNames.length != paramTypes.length) {
throw new BuildException("Parameter names count different than parameter types count for action method " + actionImplementation.toString());
}
Annotation deprecatedAnnotation = actionImplementation.getAnnotation(Deprecated.class);
boolean isDeprecated = (deprecatedAnnotation != null);
try {
return new MethodTemplateProcessor(actionImplementation, actionName, paramNames, paramTypes, isDeprecated).processTemplate();
} catch (IOException ioe) {
throw new BuildException(ioe);
}
}
use of com.axway.ats.agent.core.model.Parameter in project ats-framework by Axway.
the class ActionMethod method validateArguments.
/**
* Validate the arguments according to the rules specified in the action
* using the Parameter annotations
*
* @param actionMethod the implementation of the action
* @param args the arguments to validate
* @throws ActionExecutionException if exception occurs during arguments validation
*/
protected void validateArguments(Object[] args) throws ActionExecutionException {
Annotation[][] annotations = method.getParameterAnnotations();
for (int i = 0; i < annotations.length; i++) {
Annotation[] paramAnnotations = annotations[i];
for (Annotation paramAnnotation : paramAnnotations) {
if (paramAnnotation instanceof Parameter) {
Parameter paramDescriptionAnnotation = (Parameter) paramAnnotation;
ValidationType validationType = paramDescriptionAnnotation.validation();
String[] validationArgs;
// the name of the array holding the valid constants
if (validationType == ValidationType.STRING_CONSTANT || validationType == ValidationType.NUMBER_CONSTANT) {
try {
String arrayName = paramDescriptionAnnotation.args()[0];
// get the field and set access level if
// necessary
Field arrayField = method.getDeclaringClass().getDeclaredField(arrayName);
if (!arrayField.isAccessible()) {
arrayField.setAccessible(true);
}
Object arrayValidConstants = arrayField.get(null);
// convert the object array to string array
String[] arrayValidConstatnsStr = new String[Array.getLength(arrayValidConstants)];
for (int j = 0; j < Array.getLength(arrayValidConstants); j++) {
arrayValidConstatnsStr[j] = Array.get(arrayValidConstants, j).toString();
}
validationArgs = arrayValidConstatnsStr;
} catch (IndexOutOfBoundsException iobe) {
// this is a fatal error
throw new ActionExecutionException("You need to specify the name of the array with valid constants in the 'args' field of the Parameter annotation");
} catch (Exception e) {
// this is a fatal error
throw new ActionExecutionException("Could not get array with valid constants - action annotations are incorrect");
}
} else {
validationArgs = paramDescriptionAnnotation.args();
}
List<BaseType> typeValidators = createBaseTypes(paramDescriptionAnnotation.validation(), paramDescriptionAnnotation.name(), args[i], validationArgs);
//perform validation
for (BaseType baseType : typeValidators) {
if (baseType != null) {
try {
baseType.validate();
} catch (TypeException e) {
throw new InvalidInputArgumentsException("Validation failed while validating argument " + paramDescriptionAnnotation.name() + e.getMessage());
}
} else {
log.warn("Could not perform validation on argument " + paramDescriptionAnnotation.name());
}
}
}
}
}
}
Aggregations