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));
}
}
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));
}
}
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");
}
}
}
}
}
}
Aggregations