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

net.codestory.http.annotations.MethodAnnotationsFactory Maven / Gradle / Ivy

/**
 * Copyright (C) 2013-2014 [email protected]
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */
package net.codestory.http.annotations;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.Map;

public class MethodAnnotationsFactory {
  private final Map, ApplyAroundAnnotation> aroundAnnotations;
  private final Map, ApplyAfterAnnotation> afterAnnotations;

  public MethodAnnotationsFactory() {
    this.aroundAnnotations = new LinkedHashMap<>();
    this.afterAnnotations = new LinkedHashMap<>();
  }

  public  void registerAroundAnnotation(Class type, ApplyAroundAnnotation apply) {
    aroundAnnotations.put(type, apply);
  }

  public  void registerAfterAnnotation(Class type, ApplyAfterAnnotation apply) {
    afterAnnotations.put(type, apply);
  }

  public MethodAnnotations forMethod(Method method) {
    MethodAnnotations methodAnnotations = new MethodAnnotations();
    aroundAnnotations.forEach((annotationType, apply) -> addAroundOperationIfNecessary(annotationType, apply, method, methodAnnotations));
    afterAnnotations.forEach((annotationType, apply) -> addAfterOperationIfNecessary(annotationType, apply, method, methodAnnotations));
    return methodAnnotations;
  }

  @SuppressWarnings("unchecked")
  private  void addAroundOperationIfNecessary(Class annotationType, ApplyAroundAnnotation apply, Method method, MethodAnnotations methodAnnotations) {
    T annotation = findAnnotationOnMethodOrClass(annotationType, method);
    if (annotation != null) {
      methodAnnotations.addAroundOperation((context, payloadSupplier) -> ((ApplyAroundAnnotation) apply).apply(annotation, context, payloadSupplier));
    }
  }

  @SuppressWarnings("unchecked")
  private  void addAfterOperationIfNecessary(Class annotationType, ApplyAfterAnnotation apply, Method method, MethodAnnotations methodAnnotations) {
    T annotation = findAnnotationOnMethodOrClass(annotationType, method);
    if (annotation != null) {
      methodAnnotations.addAfterOperation((context, payload) -> ((ApplyAfterAnnotation) apply).apply(annotation, context, payload));
    }
  }

  private  T findAnnotationOnMethodOrClass(Class annotationType, Method method) {
    T annotation = method.getDeclaredAnnotation(annotationType);
    if (annotation != null) {
      return annotation;
    }

    annotation = method.getDeclaringClass().getDeclaredAnnotation(annotationType);
    if (annotation != null) {
      return annotation;
    }

    return null;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy