Search in sources :

Example 1 with Stereotype

use of javax.enterprise.inject.Stereotype in project kernel by exoplatform.

the class ContainerUtil method getScope.

/**
 * Gives the scope defined for the given class
 * @param clazz the class for which we want the scope
 * @param ignoreExplicit indicates whether the explicit scope must be ignored
 * @return a class representing the annotation type of the scope
 * @throws DefinitionException in case the definition of the scope is not correct
 */
public static Class<? extends Annotation> getScope(Class<?> clazz, boolean ignoreExplicit) throws DefinitionException {
    Annotation[] annotations = clazz.getAnnotations();
    Class<? extends Annotation> scope = null;
    Class<? extends Annotation> defaultScope = null;
    boolean hasStereotype = false;
    for (int i = 0; i < annotations.length; i++) {
        Annotation annotation = annotations[i];
        Class<? extends Annotation> annotationType = annotation.annotationType();
        if (!ignoreExplicit && (annotationType.isAnnotationPresent(Scope.class) || annotationType.isAnnotationPresent(NormalScope.class))) {
            if (scope != null) {
                throw new DefinitionException("You cannot set several scopes to the class " + clazz.getName());
            }
            scope = annotationType;
        } else if (annotationType.isAnnotationPresent(Stereotype.class)) {
            hasStereotype = true;
            Annotation[] stereotypeAnnotations = annotationType.getAnnotations();
            for (int j = 0; j < stereotypeAnnotations.length; j++) {
                Annotation stereotypeAnnotation = stereotypeAnnotations[j];
                Class<? extends Annotation> stereotypeAnnotationType = stereotypeAnnotation.annotationType();
                if (stereotypeAnnotationType.isAnnotationPresent(Scope.class) || stereotypeAnnotationType.isAnnotationPresent(NormalScope.class)) {
                    if (defaultScope != null && !defaultScope.equals(stereotypeAnnotationType)) {
                        throw new DefinitionException("The class " + clazz.getName() + " has stereotypes with different default scope");
                    }
                    defaultScope = stereotypeAnnotationType;
                }
            }
        }
    }
    if (scope != null)
        return scope;
    if (defaultScope != null)
        return defaultScope;
    if (hasStereotype) {
        throw new DefinitionException("The class " + clazz.getName() + " has at least one stereotype but doesn't have any scope, please set an explicit scope");
    }
    return null;
}
Also used : NormalScope(javax.enterprise.context.NormalScope) Scope(javax.inject.Scope) NormalScope(javax.enterprise.context.NormalScope) Stereotype(javax.enterprise.inject.Stereotype) DefinitionException(org.exoplatform.container.context.DefinitionException) Annotation(java.lang.annotation.Annotation)

Aggregations

Annotation (java.lang.annotation.Annotation)1 NormalScope (javax.enterprise.context.NormalScope)1 Stereotype (javax.enterprise.inject.Stereotype)1 Scope (javax.inject.Scope)1 DefinitionException (org.exoplatform.container.context.DefinitionException)1