package org.jboss.shamrock.beanvalidation;

import java.util.Set;

import org.jboss.jandex.AnnotationTarget.Kind;
import org.jboss.jandex.DotName;
import org.jboss.jandex.MethodInfo;
import org.jboss.protean.arc.processor.AnnotationsTransformer;
import org.jboss.shamrock.beanvalidation.runtime.interceptor.MethodValidated;
import org.jboss.shamrock.beanvalidation.runtime.jaxrs.JaxrsEndPointValidated;

/**
 * Add {@link MethodValidated} annotations to the methods requiring validation.
 */
public class MethodValidatedAnnotationsTransformer implements AnnotationsTransformer {

    private static final DotName JAXRS_PATH = DotName.createSimple("javax.ws.rs.Path");

    private final Set<DotName> consideredAnnotations;

    MethodValidatedAnnotationsTransformer(Set<DotName> consideredAnnotations) {
        this.consideredAnnotations = consideredAnnotations;
    }

    @Override
    public boolean appliesTo(Kind kind) {
        return Kind.METHOD == kind;
    }

    @Override
    public void transform(TransformationContext transformationContext) {
        MethodInfo method = transformationContext.getTarget().asMethod();

        if (requiresValidation(method)) {
            if (method.hasAnnotation(JAXRS_PATH) || method.declaringClass().annotations().containsKey(JAXRS_PATH)) {
                transformationContext.transform().add(DotName.createSimple(JaxrsEndPointValidated.class.getName())).done();
            } else {
                transformationContext.transform().add(DotName.createSimple(MethodValidated.class.getName())).done();
            }
        }
    }

    private boolean requiresValidation(MethodInfo method) {
        if (method.annotations().isEmpty()) {
            return false;
        }

        for (DotName consideredAnnotation : consideredAnnotations) {
            if (method.hasAnnotation(consideredAnnotation)) {
                return true;
            }
        }

        return false;
    }
}

@@ -609,62 +609,427 @@
 Name
+%5B%5D
  JAXRS_
-PATH = DotName.createSimple(%22javax.ws.rs.Path%22)
+METHOD_ANNOTATIONS = %7B%0A            DotName.createSimple(%22javax.ws.rs.GET%22),%0A            DotName.createSimple(%22javax.ws.rs.HEAD%22),%0A            DotName.createSimple(%22javax.ws.rs.DELETE%22),%0A            DotName.createSimple(%22javax.ws.rs.OPTIONS%22),%0A            DotName.createSimple(%22javax.ws.rs.PATCH%22),%0A            DotName.createSimple(%22javax.ws.rs.POST%22),%0A            DotName.createSimple(%22javax.ws.rs.PUT%22),%0A    %7D
 ;%0A%0A 
@@ -1552,104 +1552,28 @@
 if (
-method.hasAnnotation(JAXRS_PATH) %7C%7C method.declaringClass().annotations().containsKey(JAXRS_PATH
+isJaxrsMethod(method
 )) %7B
@@ -2221,11 +2221,284 @@
 e;%0A    %7D
+%0A%0A    private boolean isJaxrsMethod(MethodInfo method) %7B%0A        for (DotName jaxrsMethodAnnotation : JAXRS_METHOD_ANNOTATIONS) %7B%0A            if (method.hasAnnotation(jaxrsMethodAnnotation)) %7B%0A                return true;%0A            %7D%0A        %7D%0A        return false;%0A    %7D
 %0A%7D%0A

Change how JAX-RS methods are detected