Search in sources :

Example 1 with AnnotationValidationException

use of io.siddhi.annotation.util.AnnotationValidationException in project siddhi by wso2.

the class SiddhiAnnotationProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    // Iterate over all @Extension annotated elements.
    for (Element element : roundEnv.getElementsAnnotatedWith(Extension.class)) {
        // Check if a class has been annotated with @Extension.
        if (element.getKind() == ElementKind.CLASS) {
            String superClass = getMatchingSuperClass(element, new String[] { AnnotationConstants.SINK_MAPPER_SUPER_CLASS, AnnotationConstants.SINK_SUPER_CLASS, AnnotationConstants.FUNCTION_EXECUTOR_SUPER_CLASS, AnnotationConstants.AGGREGATION_ATTRIBUTE_EXECUTOR_SUPER_CLASS, AnnotationConstants.DISTRIBUTION_STRATEGY_SUPER_CLASS, AnnotationConstants.STREAM_PROCESSOR_SUPER_CLASS, AnnotationConstants.STREAM_FUNCTION_PROCESSOR_SUPER_CLASS, AnnotationConstants.STORE_SUPER_CLASS, AnnotationConstants.SOURCE_SUPER_CLASS, AnnotationConstants.SOURCE_MAPPER_SUPER_CLASS, AnnotationConstants.WINDOW_PROCESSOR_CLASS, AnnotationConstants.SCRIPT_SUPER_CLASS, AnnotationConstants.INCREMENTAL_ATTRIBUTE_AGGREGATOR_SUPER_CLASS });
            AbstractAnnotationProcessor abstractAnnotationProcessor = null;
            Extension annotation = element.getAnnotation(Extension.class);
            String name = annotation.name();
            String description = annotation.description();
            String namespace = annotation.namespace();
            Parameter[] parameters = annotation.parameters();
            ParameterOverload[] parameterOverloads = annotation.parameterOverloads();
            ReturnAttribute[] returnAttributes = annotation.returnAttributes();
            SystemParameter[] systemParameters = annotation.systemParameter();
            Example[] examples = annotation.examples();
            String extensionClassFullName = element.asType().toString();
            if (superClass != null) {
                switch(superClass) {
                    case AnnotationConstants.DISTRIBUTION_STRATEGY_SUPER_CLASS:
                        abstractAnnotationProcessor = new DistributionStrategyValidationAnnotationProcessor(extensionClassFullName);
                        break;
                    case AnnotationConstants.SINK_MAPPER_SUPER_CLASS:
                        abstractAnnotationProcessor = new SinkMapperValidationAnnotationProcessor(extensionClassFullName);
                        break;
                    case AnnotationConstants.SINK_SUPER_CLASS:
                        abstractAnnotationProcessor = new SinkValidationAnnotationProcessor(extensionClassFullName);
                        break;
                    case AnnotationConstants.FUNCTION_EXECUTOR_SUPER_CLASS:
                        abstractAnnotationProcessor = new FunctionExecutorValidationAnnotationProcessor(extensionClassFullName);
                        break;
                    case AnnotationConstants.AGGREGATION_ATTRIBUTE_EXECUTOR_SUPER_CLASS:
                        abstractAnnotationProcessor = new AggregationAttributeValidationAnnotationProcessor(extensionClassFullName);
                        break;
                    case AnnotationConstants.STREAM_PROCESSOR_SUPER_CLASS:
                        abstractAnnotationProcessor = new StreamProcessorValidationAnnotationProcessor(extensionClassFullName);
                        break;
                    case AnnotationConstants.SOURCE_SUPER_CLASS:
                        abstractAnnotationProcessor = new SourceValidationAnnotationProcessor(extensionClassFullName);
                        break;
                    case AnnotationConstants.SOURCE_MAPPER_SUPER_CLASS:
                        abstractAnnotationProcessor = new SourceMapperValidationAnnotationProcessor(extensionClassFullName);
                        break;
                    case AnnotationConstants.STORE_SUPER_CLASS:
                        abstractAnnotationProcessor = new StoreValidationAnnotationProcessor(extensionClassFullName);
                        break;
                    case AnnotationConstants.STREAM_FUNCTION_PROCESSOR_SUPER_CLASS:
                        abstractAnnotationProcessor = new StreamFunctionProcessorValidationAnnotationProcessor(extensionClassFullName);
                        break;
                    case AnnotationConstants.WINDOW_PROCESSOR_CLASS:
                        abstractAnnotationProcessor = new WindowProcessorValidationAnnotationProcessor(extensionClassFullName);
                        break;
                    case AnnotationConstants.SCRIPT_SUPER_CLASS:
                        abstractAnnotationProcessor = new ScriptValidationAnnotationProcessor(extensionClassFullName);
                        break;
                    case AnnotationConstants.INCREMENTAL_ATTRIBUTE_AGGREGATOR_SUPER_CLASS:
                        abstractAnnotationProcessor = new IncrementalAggregationAttributeValidationAnnotationProcessor(extensionClassFullName);
                        break;
                    default:
                        // Throw error if no matching super class.
                        showBuildError(MessageFormat.format("Default switch case executed as there is no " + "matching super class option for @{0}.", superClass), element);
                        break;
                }
                if (abstractAnnotationProcessor != null) {
                    try {
                        abstractAnnotationProcessor.basicParameterValidation(name, description, namespace);
                        abstractAnnotationProcessor.parameterValidation(parameters);
                        abstractAnnotationProcessor.parameterOverloadValidation(parameterOverloads, parameters);
                        abstractAnnotationProcessor.returnAttributesValidation(returnAttributes);
                        abstractAnnotationProcessor.systemParametersValidation(systemParameters);
                        abstractAnnotationProcessor.examplesValidation(examples);
                    } catch (AnnotationValidationException e) {
                        showBuildError(e.getMessage(), element);
                    }
                } else {
                    showBuildError(MessageFormat.format("Error while validation, " + "abstractAnnotationProcessor cannot be null.", superClass), element);
                }
            } else {
                // Throw error if no matching super class.
                showBuildError("Class does not have a matching Siddhi Extension super class.", element);
            }
        } else {
            // Throw error if the element returned is method or package.
            showBuildError(MessageFormat.format("Only classes can be annotated with @{0}.", Extension.class.getCanonicalName()), element);
        }
    }
    // Returning false since this processor only validates.
    return false;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) AnnotationValidationException(io.siddhi.annotation.util.AnnotationValidationException) ReturnAttribute(io.siddhi.annotation.ReturnAttribute) Example(io.siddhi.annotation.Example) SystemParameter(io.siddhi.annotation.SystemParameter) ParameterOverload(io.siddhi.annotation.ParameterOverload) Extension(io.siddhi.annotation.Extension) Parameter(io.siddhi.annotation.Parameter) SystemParameter(io.siddhi.annotation.SystemParameter)

Example 2 with AnnotationValidationException

use of io.siddhi.annotation.util.AnnotationValidationException in project siddhi by wso2.

the class AbstractAnnotationProcessor method parameterOverloadValidation.

/**
 * This method uses for validate @Extension / @ParameterOverload element.
 *
 * @param parameterOverloads parameter array which needs to be validate.
 * @param parameters         the set of supported parameters
 * @throws AnnotationValidationException whenever if the validate rule violate, throws the annotation validate
 *                                       exception with proper message.
 */
public void parameterOverloadValidation(ParameterOverload[] parameterOverloads, Parameter[] parameters) throws AnnotationValidationException {
    Map<String, Parameter> parameterMap = new HashMap<>();
    Set<String> mandatoryParameterSet = new HashSet<>();
    for (Parameter parameter : parameters) {
        parameterMap.put(parameter.name(), parameter);
        if (!parameter.optional()) {
            mandatoryParameterSet.add(parameter.name());
        }
    }
    for (ParameterOverload parameterOverload : parameterOverloads) {
        String[] overloadParameterNames = parameterOverload.parameterNames();
        for (String overloadParameterName : overloadParameterNames) {
            // Check if the @Parameter name is empty.
            if (overloadParameterName.isEmpty()) {
                throw new AnnotationValidationException(MessageFormat.format("The @Extension -> " + "@ParameterOverload -> parameterNames annotated in class {0} is null or empty.", extensionClassFullName));
            } else if (!(PARAMETER_NAME_PATTERN.matcher(overloadParameterName).find() || REPETITIVE_PARAMETER_NOTATION.equals(overloadParameterName))) {
                // Check if the @Parameter name is in a correct format 'abc.def.ghi' using regex pattern.
                throw new AnnotationValidationException(MessageFormat.format("The @Extension -> " + "@ParameterOverload -> parameterNames {0} annotated in class {1} is not " + "in proper format 'abc.def.ghi'.", overloadParameterName, extensionClassFullName));
            }
            if (!(parameterMap.containsKey(overloadParameterName) || REPETITIVE_PARAMETER_NOTATION.equals(overloadParameterName))) {
                throw new AnnotationValidationException(MessageFormat.format("The @Extension -> " + "@ParameterOverload -> parameterNames {0} annotated in class {1} is not defined in " + "@Extension -> @Parameter.", overloadParameterName, extensionClassFullName));
            }
        }
    }
    if (parameterOverloads.length > 0) {
        Set<String> mandatoryParameterSetViaOverload = new HashSet<>(parameterMap.keySet());
        for (Iterator<String> iterator = mandatoryParameterSetViaOverload.iterator(); iterator.hasNext(); ) {
            String parameter = iterator.next();
            for (ParameterOverload parameterOverload : parameterOverloads) {
                boolean contains = false;
                for (String parameterName : parameterOverload.parameterNames()) {
                    if (parameter.equalsIgnoreCase(parameterName)) {
                        contains = true;
                        break;
                    }
                }
                if (!contains) {
                    iterator.remove();
                    break;
                }
            }
        }
        if (!mandatoryParameterSetViaOverload.equals(mandatoryParameterSet)) {
            throw new AnnotationValidationException("Mandatory parameter information in ParameterOverload " + "and based on 'optional' annotation is a mismatch. The parameters '" + mandatoryParameterSetViaOverload + "' always appearing in ParameterOverload, but '" + mandatoryParameterSet + "' are defined as not 'optional' in the annotations.");
        }
    }
}
Also used : HashMap(java.util.HashMap) Parameter(io.siddhi.annotation.Parameter) SystemParameter(io.siddhi.annotation.SystemParameter) AnnotationValidationException(io.siddhi.annotation.util.AnnotationValidationException) HashSet(java.util.HashSet) ParameterOverload(io.siddhi.annotation.ParameterOverload)

Aggregations

Parameter (io.siddhi.annotation.Parameter)2 ParameterOverload (io.siddhi.annotation.ParameterOverload)2 SystemParameter (io.siddhi.annotation.SystemParameter)2 AnnotationValidationException (io.siddhi.annotation.util.AnnotationValidationException)2 Example (io.siddhi.annotation.Example)1 Extension (io.siddhi.annotation.Extension)1 ReturnAttribute (io.siddhi.annotation.ReturnAttribute)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Element (javax.lang.model.element.Element)1 TypeElement (javax.lang.model.element.TypeElement)1