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

play.InvocationContext Maven / Gradle / Ivy

There is a newer version: 2.6.2
Show newest version
package play;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * The class/method that will be invoked by the current operation
 */
public class InvocationContext {

    public static final ThreadLocal current = new ThreadLocal<>();
    private final List annotations;
    private final String invocationType;

    public static InvocationContext current() {
        return current.get();
    }

    public InvocationContext(String invocationType, Annotation[] annotations) {
        this.invocationType = invocationType;
        this.annotations = Arrays.asList(annotations);
    }

    public InvocationContext(String invocationType, Annotation[]... annotations) {
        this.invocationType = invocationType;
        this.annotations = new ArrayList<>();
        for (Annotation[] some : annotations) {
            this.annotations.addAll(Arrays.asList(some));
        }
    }

    public List getAnnotations() {
        return annotations;
    }

    @SuppressWarnings("unchecked")
    public  T getAnnotation(Class clazz) {
        for (Annotation annotation : annotations) {
            if (annotation.annotationType().isAssignableFrom(clazz)) {
                return (T) annotation;
            }
        }
        return null;
    }

    public  boolean isAnnotationPresent(Class clazz) {
        for (Annotation annotation : annotations) {
            if (annotation.annotationType().isAssignableFrom(clazz)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("InvocationType: ");
        builder.append(invocationType);
        builder.append(". annotations: ");
        for (Annotation annotation : annotations) {
            builder.append(annotation.toString()).append(",");
        }
        return builder.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy