All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mangofactory.swagger.readers.operation.parameter.ParameterAnnotationReader Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
package com.mangofactory.swagger.readers.operation.parameter;

import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import org.springframework.core.MethodParameter;

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

import static com.google.common.collect.Iterables.*;
import static com.google.common.collect.Lists.*;

class ParameterAnnotationReader {
  private static  Predicate annotationOfType(final Class annotationType) {
    return new Predicate() {
      @Override
      public boolean apply(Annotation input) {
        return input.annotationType().equals(annotationType);
      }
    };
  }

  private static Optional interfaceMethod(Class iface, Method method) {
    try {
      return Optional.of(iface.getMethod(method.getName(), method.getParameterTypes()));
    } catch (NoSuchMethodException ex) {
      return Optional.absent();
    }
  }

  @SuppressWarnings("unchecked")
  static  A searchOnInterfaces(Method method,
                                              int parameterIndex,
                                              Class annotationType,
                                              Class[] interfaces) {

    A annotation = null;
    for (Class interfaze : interfaces) {
      Optional interfaceMethod = interfaceMethod(interfaze, method);
      if (interfaceMethod.isPresent()) {
        Method superMethod = interfaceMethod.get();
        Optional found = tryFind(
                newArrayList(superMethod.getParameterAnnotations()[parameterIndex]), annotationOfType(annotationType));
        if (found.isPresent()) {
          annotation = (A) found.get();
          break;
        }
        Class[] superInterfaces = superMethod.getDeclaringClass().getInterfaces();
        annotation = searchOnInterfaces(superMethod, parameterIndex, annotationType, superInterfaces);
      }
    }
    return annotation;
  }

   Optional fromHierarchy(MethodParameter methodParameter, Class annotationType) {
    return Optional.fromNullable(searchOnInterfaces(methodParameter.getMethod(),
            methodParameter.getParameterIndex(),
            annotationType,
            getParentInterfaces(methodParameter)));
  }

  Class[] getParentInterfaces(MethodParameter methodParameter) {
    return methodParameter.getMethod().getDeclaringClass().getInterfaces();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy