下面列出了怎么用javax.enterprise.inject.Stereotype的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public <T extends Annotation> AnnotationInfo getAnnotation(Class<T> metric) {
T annotation = input.getAnnotation(metric);
if (annotation != null) {
return new CDIAnnotationInfoAdapter().convert(annotation);
} else {
// the metric annotation can also be applied via a stereotype, so look for stereotype annotations
for (Annotation stereotypeCandidate : ((AnnotatedElement) input).getAnnotations()) {
if (stereotypeCandidate.annotationType().isAnnotationPresent(Stereotype.class) &&
stereotypeCandidate.annotationType().isAnnotationPresent(metric)) {
return new CDIAnnotationInfoAdapter().convert(stereotypeCandidate.annotationType().getAnnotation(metric));
}
}
return null;
}
}
protected void addViewMetaData(Annotation currentAnnotation, List<Annotation> metaDataList)
{
Class<? extends Annotation> annotationClass = currentAnnotation.annotationType();
if (annotationClass.isAnnotationPresent(ViewMetaData.class))
{
metaDataList.add(currentAnnotation);
}
if (annotationClass.isAnnotationPresent(Stereotype.class))
{
for (Annotation inheritedViaStereotype : annotationClass.getAnnotations())
{
if (inheritedViaStereotype.annotationType().isAnnotationPresent(ViewMetaData.class))
{
metaDataList.add(inheritedViaStereotype);
}
}
}
}
@Override
public <T extends Annotation> boolean isAnnotationPresent(Class<T> metric) {
if (input.isAnnotationPresent(metric)) {
return true;
} else {
// the metric annotation can also be applied via a stereotype, so look for stereotype annotations
for (Annotation stereotypeCandidate : ((AnnotatedElement) input).getAnnotations()) {
if (stereotypeCandidate.annotationType().isAnnotationPresent(Stereotype.class) &&
stereotypeCandidate.annotationType().isAnnotationPresent(metric)) {
return true;
}
}
return false;
}
}
private static Set<Annotation> getStereotypes(AnnotatedElement element) {
Set<Annotation> stereotypes = new HashSet<>();
for (Annotation annotation : element.getAnnotations()) {
if (annotation.annotationType().isAnnotationPresent(Stereotype.class)) {
stereotypes.add(annotation);
stereotypes.addAll(getStereotypes(annotation.annotationType()));
}
}
return stereotypes;
}
public static Annotation resolveSecurityBindingType(Annotation annotation)
{
List<Annotation> result = getAllAnnotations(annotation.annotationType().getAnnotations(),
new HashSet<Integer>());
for (Annotation foundAnnotation : result)
{
if (foundAnnotation.annotationType().isAnnotationPresent(SecurityBindingType.class))
{
return foundAnnotation;
}
}
throw new IllegalStateException(annotation.annotationType().getName() + " is a " + Stereotype.class.getName() +
" but it isn't annotated with " + SecurityBindingType.class.getName());
}
private List<Annotation> mergeMetaData(Set<Annotation> metaData, List<Annotation> inheritedMetaData)
{
//TODO add qualifier support
List<Annotation> nodeViewMetaData = new ArrayList<Annotation>();
List<Annotation> viewMetaDataFromStereotype = new ArrayList<Annotation>();
for (Annotation annotation : metaData)
{
if (annotation.annotationType().isAnnotationPresent(ViewMetaData.class))
{
nodeViewMetaData.add(annotation);
}
//TODO move to stereotype-util, improve it and merge it with DefaultViewConfigInheritanceStrategy
if (annotation.annotationType().isAnnotationPresent(Stereotype.class))
{
for (Annotation metaAnnotation : annotation.annotationType().getAnnotations())
{
if (metaAnnotation.annotationType().isAnnotationPresent(ViewMetaData.class))
{
viewMetaDataFromStereotype.add(metaAnnotation);
}
}
}
}
//merge meta-data of same level
List<Annotation> result = mergeAnnotationInstances(viewMetaDataFromStereotype, nodeViewMetaData);
if (inheritedMetaData != null && !inheritedMetaData.isEmpty())
{
//merge meta-data with levels above
result = mergeAnnotationInstances(inheritedMetaData, result);
}
return result;
}
private static <A extends Annotation> Stream<A> resolveStereotypes(Annotation[] annotations, Class<A> type) {
return Stream.of(annotations)
.map(Annotation::annotationType)
.filter(annotation -> annotation.isAnnotationPresent(Stereotype.class))
.flatMap(a -> resolveAnnotations(a, type));
}
private static boolean hasBeanDefiningAnnotation(Class<?> clazz) {
return isAnnotated(clazz, NormalScope.class) || isAnnotated(clazz, Dependent.class) ||
isAnnotated(clazz, Interceptor.class) || isAnnotated(clazz, Decorator.class) ||
isAnnotated(clazz, Stereotype.class);
}