/** * Finds an annotation in an Annotated, taking stereo types into account * * @param beanManager the current bean manager * @param annotated the Annotated in which to search * @param annotationType the type of the annotation to search for * @return An Optional that contains an instance of annotation type for which was searched if the * annotated contained this. */ public static <A extends Annotation> Optional<A> getAnnotation( BeanManager beanManager, Annotated annotated, Class<A> annotationType) { annotated.getAnnotation(annotationType); if (annotated.getAnnotations().isEmpty()) { return empty(); } if (annotated.isAnnotationPresent(annotationType)) { return Optional.of(annotated.getAnnotation(annotationType)); } Queue<Annotation> annotations = new LinkedList<>(annotated.getAnnotations()); while (!annotations.isEmpty()) { Annotation annotation = annotations.remove(); if (annotation.annotationType().equals(annotationType)) { return Optional.of(annotationType.cast(annotation)); } if (beanManager.isStereotype(annotation.annotationType())) { annotations.addAll(beanManager.getStereotypeDefinition(annotation.annotationType())); } } return empty(); }
private Arg<X> introspectArg(Annotated ann, AnnotatedParameter<X> param) { Annotation[] qualifiers = getQualifiers(param); InjectionPoint ip = new InjectionPointImpl<X>(getBeanManager(), this, param); if (ann.isAnnotationPresent(Inject.class)) { // ioc/022k _injectionPointSet.add(ip); } if (param.isAnnotationPresent(Disposes.class)) { throw new ConfigException( L.l( "{0} is an invalid managed bean because its constructor has a @Disposes parameter", getAnnotatedType().getJavaClass().getName())); } if (param.isAnnotationPresent(Observes.class)) { throw new ConfigException( L.l( "{0} is an invalid managed bean because its constructor has an @Observes parameter", getAnnotatedType().getJavaClass().getName())); } return new BeanArg<X>(getBeanManager(), param.getBaseType(), qualifiers, ip); }
<T> void saveRemoteInjectionPoints( @Observes ProcessInjectionTarget<T> event, BeanManager beanManager) { final InjectionTarget<T> injectionTarget = event.getInjectionTarget(); for (InjectionPoint injectionPoint : injectionTarget.getInjectionPoints()) { final Annotated annotated = injectionPoint.getAnnotated(); final Type type = annotated.getBaseType(); final Class<?> rawType = Reflections.getRawType(annotated.getBaseType()); final Set<Annotation> qualifiers = Reflections.getQualifiers(beanManager, annotated.getAnnotations()); if (rawType.equals(RemoteCache.class) && qualifiers.isEmpty()) { qualifiers.add(new AnnotationLiteral<Default>() {}); addRemoteCacheInjectionPoint(type, qualifiers); } else if (!annotated.isAnnotationPresent(Remote.class) && Reflections.getMetaAnnotation(annotated, Remote.class) != null && rawType.isAssignableFrom(RemoteCache.class)) { addRemoteCacheInjectionPoint(type, qualifiers); } } }
private Annotation[] getQualifiers(Annotated annotated) { ArrayList<Annotation> qualifierList = new ArrayList<Annotation>(); for (Annotation ann : annotated.getAnnotations()) { if (ann.annotationType().isAnnotationPresent(Qualifier.class)) { qualifierList.add(ann); } } if (qualifierList.size() == 0) qualifierList.add(CurrentLiteral.CURRENT); Annotation[] qualifiers = new Annotation[qualifierList.size()]; qualifierList.toArray(qualifiers); return qualifiers; }
void createGenericBeans(@Observes AfterBeanDiscovery event, BeanManager beanManager) { beanDiscoveryOver = true; // For each generic configuration type, we iterate the generic configurations for (Entry<GenericIdentifier, GenericConfigurationHolder> genericConfigurationEntry : genericConfigurationPoints.entrySet()) { Class<? extends Annotation> producerScope = Dependent.class; for (Annotation annotation : genericConfigurationEntry.getValue().getAnnotated().getAnnotations()) { if (beanManager.isScope(annotation.annotationType())) { producerScope = annotation.annotationType(); } } GenericConfigurationHolder genericConfigurationHolder = genericConfigurationEntry.getValue(); GenericIdentifier identifier = genericConfigurationEntry.getKey(); Class<? extends Annotation> genericConfigurationType = identifier.getAnnotationType(); if (!genericBeans.containsKey(genericConfigurationType)) { throw new IllegalStateException( "No generic bean definition exists for " + genericConfigurationType + ", but a generic producer does: " + genericConfigurationHolder.getAnnotated()); } // Add a generic configuration bean for each generic configuration producer (allows us to // inject the generic configuration annotation back into the generic bean) event.addBean(createGenericConfigurationBean(beanManager, identifier)); // Register the GenericProduct bean event.addBean(createGenericProductAnnotatedMemberBean(beanManager, identifier)); boolean alternative = genericConfigurationHolder.getAnnotated().isAnnotationPresent(Alternative.class); Class<?> javaClass = genericConfigurationHolder.getJavaClass(); if (genericBeanProducerMethods.containsKey(genericConfigurationType)) { for (ProducerMethodHolder<?, ?> holder : genericBeanProducerMethods.get(genericConfigurationType)) { Class<? extends Annotation> scopeOverride = null; if (holder.getProducerMethod().isAnnotationPresent(ApplyScope.class)) { scopeOverride = producerScope; } event.addBean( createGenericProducerMethod( holder, identifier, beanManager, scopeOverride, alternative, javaClass)); } } if (genericBeanProducerFields.containsKey(genericConfigurationType)) { for (FieldHolder<?, ?> holder : genericBeanProducerFields.get(genericConfigurationType)) { Class<? extends Annotation> scopeOverride = null; if (holder.getField().isAnnotationPresent(ApplyScope.class)) { scopeOverride = producerScope; } event.addBean( createGenericProducerField( holder.getBean(), identifier, holder.getField(), beanManager, scopeOverride, alternative, javaClass)); } } if (genericBeanObserverMethods.containsKey(genericConfigurationType)) { for (ObserverMethodHolder<?, ?> holder : genericBeanObserverMethods.get(genericConfigurationType)) { event.addObserverMethod( createGenericObserverMethod( holder.getObserverMethod(), identifier, holder.getMethod(), null, beanManager)); } } if (unwrapsMethods.containsKey(genericConfigurationType)) { for (AnnotatedMethod<?> i : unwrapsMethods.get(genericConfigurationType)) { Annotated annotated = genericConfigurationHolder.getAnnotated(); Set<Annotation> unwrapsQualifiers = Beans.getQualifiers(beanManager, i.getAnnotations(), annotated.getAnnotations()); if (unwrapsQualifiers.isEmpty()) { unwrapsQualifiers.add(DefaultLiteral.INSTANCE); } Set<Annotation> beanQualifiers = Beans.getQualifiers( beanManager, i.getDeclaringType().getAnnotations(), annotated.getAnnotations()); beanQualifiers.remove(AnyLiteral.INSTANCE); if (beanQualifiers.isEmpty()) { beanQualifiers.add(DefaultLiteral.INSTANCE); } beanQualifiers.remove(genericBeanQualifier); event.addBean(new UnwrapsProducerBean(i, unwrapsQualifiers, beanQualifiers, beanManager)); } } // For each generic bean that uses this genericConfigurationType, register a generic bean for // this generic configuration for (BeanHolder<?> genericBeanHolder : genericBeans.get(genericConfigurationType)) { // Register the generic bean, this is the underlying definition, with the synthetic // qualifier Class<? extends Annotation> scopeOverride = null; if (genericBeanHolder.getType().isAnnotationPresent(ApplyScope.class)) { scopeOverride = producerScope; } Bean<?> genericBean = createGenericBean( genericBeanHolder, identifier, beanManager, scopeOverride, alternative, javaClass); event.addBean(genericBean); } } }
@Produces Logger produceLog(InjectionPoint injectionPoint) { Annotated annotated = injectionPoint.getAnnotated(); if (annotated.isAnnotationPresent(Category.class)) { if (annotated.isAnnotationPresent(Suffix.class)) { return getLogger( annotated.getAnnotation(Category.class).value(), annotated.getAnnotation(Suffix.class).value()); } else { return getLogger(annotated.getAnnotation(Category.class).value()); } } else if (annotated.isAnnotationPresent(TypedCategory.class)) { if (annotated.isAnnotationPresent(Suffix.class)) { return getLogger( annotated.getAnnotation(TypedCategory.class).value(), annotated.getAnnotation(Suffix.class).value()); } else { return getLogger(annotated.getAnnotation(TypedCategory.class).value()); } } else { if (annotated.isAnnotationPresent(Suffix.class)) { return getLogger( getDeclaringRawType(injectionPoint), annotated.getAnnotation(Suffix.class).value()); } else { return getLogger(getDeclaringRawType(injectionPoint)); } } }
/** * compares two annotated elemetes to see if they have the same annotations * * @param a1 * @param a2 * @return */ private static boolean compareAnnotated(Annotated a1, Annotated a2) { return a1.getAnnotations().equals(a2.getAnnotations()); }
/** Adds the stereotypes from the bean's annotations */ @Override protected void introspectSpecializes(Annotated annotated) { if (!annotated.isAnnotationPresent(Specializes.class)) return; }