package core.framework.impl.web.service;

import core.framework.api.util.Exceptions;
import core.framework.api.web.service.PathParam;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * @author neo
 */
public class WebServiceImplValidator<T> {
    private final Class<T> serviceInterface;
    private final T service;

    public WebServiceImplValidator(Class<T> serviceInterface, T service) {
        this.serviceInterface = serviceInterface;
        this.service = service;
    }

    public void validate() {
        if (!serviceInterface.isInstance(service))
            throw Exceptions.error("service must impl service interface, serviceInterface={}", serviceInterface.getCanonicalName(), service);

        Class<?> serviceClass = service.getClass();
        for (Method method : serviceInterface.getDeclaredMethods()) {
            Class<?>[] parameterTypes = method.getParameterTypes();
            try {
                Method serviceMethod = serviceClass.getMethod(method.getName(), parameterTypes);
                Annotation[][] parameterAnnotations = serviceMethod.getParameterAnnotations();
                for (Annotation[] parameterAnnotation : parameterAnnotations) {
                    for (Annotation annotation : parameterAnnotation) {
                        if (PathParam.class.equals(annotation.annotationType())) {
                            throw Exceptions.error("service impl must not have @PathParam, method={}", serviceMethod);
                        }
                    }
                }
            } catch (NoSuchMethodException e) {
                throw new Error("failed to find impl method", e);
            }
        }
    }
}

@@ -1034,32 +1034,272 @@
 Types);%0A        
+        validateMethod(serviceMethod);%0A            %7D catch (NoSuchMethodException e) %7B%0A                throw new Error(%22failed to find impl method%22, e);%0A            %7D%0A        %7D%0A    %7D%0A%0A    private void validateMethod(Method serviceMethod) %7B%0A
         Annotati
@@ -1373,32 +1373,24 @@
 ();%0A        
-     
-   
 for (Annotat
@@ -1453,24 +1453,16 @@
         
-        
 for (Ann
@@ -1521,24 +1521,16 @@
         
-   
-     
 if (Path
@@ -1576,32 +1576,24 @@
 nType())) %7B%0A
-        
             
@@ -1711,171 +1711,9 @@
     
-        %7D%0A                    %7D%0A                %7D%0A            %7D catch (NoSuchMethodException e) %7B%0A                throw new Error(%22failed to find impl method%22, e);
+%7D
 %0A   

fix checkstyle