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;
}
Aggregations