/** Then one bean holding a reference to another (leaf-level) bean */
 public void testBeanHolder() throws Exception {
   ObjectMapper mapper = new ObjectMapper();
   mapper.registerModule(new MrBeanModule());
   BeanHolder holder = mapper.readValue("{\"bean\":{\"a\":\"b\",\"x\":-4 }}", BeanHolder.class);
   assertNotNull(holder);
   Bean bean = holder.getBean();
   assertNotNull(bean);
   assertEquals("b", bean.getA());
   assertEquals(-4, bean.getX());
 }
  public <T> T registerBeanInstance(Bean<T> bean, CreationalContext<T> ctx, T obj) {
    BeanHolder beanHolder = new BeanHolder();
    beanHolder.bean = bean;
    beanHolder.ctx = ctx;
    beanHolder.instance = obj;

    String key = getBeanKey(bean);
    HttpSession session = getSession();
    if (session != null) {
      session.setAttribute(key, beanHolder);
    } else {
      Map m = (Map) backup.get();
      if (m == null) backup.set(m = new HashMap());
      m.put(key, beanHolder);
    }
    return obj;
  }
Esempio n. 3
0
 private <X> Bean<X> createGenericBean(
     BeanHolder<X> holder,
     GenericIdentifier identifier,
     BeanManager beanManager,
     Class<? extends Annotation> scopeOverride,
     boolean alternative,
     Class<?> beanClass) {
   Set<Annotation> qualifiers =
       getQualifiers(beanManager, identifier, Collections.<Annotation>emptySet());
   return new GenericManagedBean<X>(
       holder.getBean(),
       identifier,
       (InjectionTarget<X>) genericInjectionTargets.get(holder.getType()),
       holder.getType(),
       qualifiers,
       scopeOverride,
       annotatedMemberInjectionProvider,
       alternative,
       beanClass,
       beanManager);
 }
Esempio n. 4
0
  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);
      }
    }
  }