Search in sources :

Example 1 with AcceptParameter

use of com.github.nalukit.nalu.client.component.annotation.AcceptParameter in project nalu by NaluKit.

the class CompositeControllerAnnotationScanner method handleAcceptParameters.

private void handleAcceptParameters(RoundEnvironment roundEnvironment, Element element, CompositeModel compositeModel) throws ProcessorException {
    TypeElement typeElement = (TypeElement) element;
    List<Element> annotatedElements = this.processorUtils.getMethodFromTypeElementAnnotatedWith(this.processingEnvironment, typeElement, AcceptParameter.class);
    // get all controllers, that use the compositeModel (for validation)
    for (ControllerModel model : this.getControllerUsingComposite(element)) {
        // validate
        AcceptParameterAnnotationValidator.builder().roundEnvironment(roundEnvironment).processingEnvironment(processingEnvironment).controllerModel(model).listOfAnnotatedElements(annotatedElements).build().validate();
    }
    // add to ControllerModel ...
    for (Element annotatedElement : annotatedElements) {
        ExecutableElement executableElement = (ExecutableElement) annotatedElement;
        AcceptParameter acceptParameterAnnotation = executableElement.getAnnotation(AcceptParameter.class);
        ParameterConstraint parameterConstraintAnnotation = executableElement.getAnnotation(ParameterConstraint.class);
        ParameterConstraintModel parameterConstraintModel = null;
        if (parameterConstraintAnnotation != null) {
            TypeElement parameterConstraintTypeElement = getRuleTypeElement(parameterConstraintAnnotation);
            if (parameterConstraintTypeElement != null) {
                parameterConstraintModel = new ParameterConstraintModel(parameterConstraintTypeElement.toString(), parameterConstraintAnnotation.illegalParameterRoute());
            }
        }
        compositeModel.getParameterAcceptors().add(new ParameterAcceptorModel(acceptParameterAnnotation.value(), executableElement.getSimpleName().toString(), parameterConstraintModel));
    }
}
Also used : ControllerModel(com.github.nalukit.nalu.processor.model.intern.ControllerModel) ParameterAcceptorModel(com.github.nalukit.nalu.processor.model.intern.ParameterAcceptorModel) AcceptParameter(com.github.nalukit.nalu.client.component.annotation.AcceptParameter) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) ParameterConstraint(com.github.nalukit.nalu.client.constraint.annotation.ParameterConstraint) ParameterConstraintModel(com.github.nalukit.nalu.processor.model.intern.ParameterConstraintModel)

Example 2 with AcceptParameter

use of com.github.nalukit.nalu.client.component.annotation.AcceptParameter in project nalu by NaluKit.

the class ControllerAnnotationScanner method handleAcceptParameters.

private void handleAcceptParameters(RoundEnvironment roundEnvironment, Element element, ControllerModel controllerModel) throws ProcessorException {
    TypeElement typeElement = (TypeElement) element;
    List<Element> annotatedElements = this.processorUtils.getMethodFromTypeElementAnnotatedWith(this.processingEnvironment, typeElement, AcceptParameter.class);
    // validate
    AcceptParameterAnnotationValidator.builder().roundEnvironment(roundEnvironment).processingEnvironment(processingEnvironment).controllerModel(controllerModel).listOfAnnotatedElements(annotatedElements).build().validate();
    // add to ControllerModel ...
    for (Element annotatedElement : annotatedElements) {
        ExecutableElement executableElement = (ExecutableElement) annotatedElement;
        AcceptParameter acceptParameterAnnotation = executableElement.getAnnotation(AcceptParameter.class);
        ParameterConstraint parameterConstraintAnnotation = executableElement.getAnnotation(ParameterConstraint.class);
        ParameterConstraintModel parameterConstraintModel = null;
        if (parameterConstraintAnnotation != null) {
            TypeElement parameterConstraintTypeElement = getRuleTypeElement(parameterConstraintAnnotation);
            if (parameterConstraintTypeElement != null) {
                parameterConstraintModel = new ParameterConstraintModel(parameterConstraintTypeElement.toString(), parameterConstraintAnnotation.illegalParameterRoute());
            }
        }
        controllerModel.getParameterAcceptors().add(new ParameterAcceptorModel(acceptParameterAnnotation.value(), executableElement.getSimpleName().toString(), parameterConstraintModel));
    }
}
Also used : ParameterAcceptorModel(com.github.nalukit.nalu.processor.model.intern.ParameterAcceptorModel) AcceptParameter(com.github.nalukit.nalu.client.component.annotation.AcceptParameter) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) ParameterConstraint(com.github.nalukit.nalu.client.constraint.annotation.ParameterConstraint) ParameterConstraintModel(com.github.nalukit.nalu.processor.model.intern.ParameterConstraintModel)

Example 3 with AcceptParameter

use of com.github.nalukit.nalu.client.component.annotation.AcceptParameter in project nalu by NaluKit.

the class ControllerAnnotationValidator method validate.

public void validate() throws ProcessorException {
    TypeElement typeElement = (TypeElement) this.controllerElement;
    // @Controller can only be used on a class
    if (!typeElement.getKind().isClass()) {
        throw new ProcessorException("Nalu-Processor: @Controller can only be used with an class");
    }
    // @Controller can only be used on a interface that extends IsController
    if (!this.processorUtils.extendsClassOrInterface(this.processingEnvironment.getTypeUtils(), typeElement.asType(), this.processingEnvironment.getElementUtils().getTypeElement(IsController.class.getCanonicalName()).asType())) {
        throw new ProcessorException("Nalu-Processor: @Controller can only be used on a class that extends IsController or IsShell");
    }
    // check if route start with "/"
    Controller controllerAnnotation = this.controllerElement.getAnnotation(Controller.class);
    for (String route : controllerAnnotation.route()) {
        if (!route.startsWith("/")) {
            throw new ProcessorException("Nalu-Processor: @Controller - route attribute muss begin with a '/'");
        }
    }
    String[] routes = controllerAnnotation.route();
    // 1. check, that all routes are valid
    for (String route : routes) {
        validateRoute(route);
    }
    validateParameterNamesAreUnique(routes[0]);
    if (routes.length > 1) {
        // only, if we have more than one route!
        validateParameters(routes);
        searchForDuplicateRoutes(routes);
    }
    // AcceptParameter annotation
    for (String route : controllerAnnotation.route()) {
        List<String> parametersFromRoute = this.getParametersFromRoute(route);
        for (Element element : this.processingEnvironment.getElementUtils().getAllMembers((TypeElement) this.controllerElement)) {
            if (ElementKind.METHOD.equals(element.getKind())) {
                if (!Objects.isNull(element.getAnnotation(AcceptParameter.class))) {
                    AcceptParameter annotation = element.getAnnotation(AcceptParameter.class);
                    if (!parametersFromRoute.contains(annotation.value())) {
                        throw new ProcessorException("Nalu-Processor: controller >>" + controllerElement.toString() + "<< - @AcceptParameter with value >>" + annotation.value() + "<< is not represented in the route as parameter");
                    }
                    ExecutableType executableType = (ExecutableType) element.asType();
                    List<? extends TypeMirror> parameters = executableType.getParameterTypes();
                    if (parameters.size() != 1) {
                        throw new ProcessorException("Nalu-Processor: controller >>" + controllerElement.toString() + "<< - @AcceptParameter annotated on >>" + executableType.toString() + "<< need one parameter of type String");
                    }
                    if (!String.class.getCanonicalName().equals(parameters.get(0).toString())) {
                        throw new ProcessorException("Nalu-Processor: controller >>" + controllerElement.toString() + "<< - @AcceptParameter on >>" + element.toString() + "<< parameter has the wrong type -> must be a String");
                    }
                }
            }
        }
    }
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) ProcessorException(com.github.nalukit.nalu.processor.ProcessorException) AcceptParameter(com.github.nalukit.nalu.client.component.annotation.AcceptParameter) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) IsController(com.github.nalukit.nalu.client.component.IsController) Controller(com.github.nalukit.nalu.client.component.annotation.Controller)

Aggregations

AcceptParameter (com.github.nalukit.nalu.client.component.annotation.AcceptParameter)3 Element (javax.lang.model.element.Element)3 TypeElement (javax.lang.model.element.TypeElement)3 ParameterConstraint (com.github.nalukit.nalu.client.constraint.annotation.ParameterConstraint)2 ParameterAcceptorModel (com.github.nalukit.nalu.processor.model.intern.ParameterAcceptorModel)2 ParameterConstraintModel (com.github.nalukit.nalu.processor.model.intern.ParameterConstraintModel)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 IsController (com.github.nalukit.nalu.client.component.IsController)1 Controller (com.github.nalukit.nalu.client.component.annotation.Controller)1 ProcessorException (com.github.nalukit.nalu.processor.ProcessorException)1 ControllerModel (com.github.nalukit.nalu.processor.model.intern.ControllerModel)1 ExecutableType (javax.lang.model.type.ExecutableType)1