예제 #1
0
  private void introspectInjectMethod(
      AnnotatedType<X> type, ArrayList<ConfigProgram> injectProgramList) {

    for (AnnotatedMethod method : type.getMethods()) {
      if (method.getAnnotations().size() == 0) continue;

      if (method.isAnnotationPresent(Inject.class)) {
        // boolean isOptional = isQualifierOptional(field);

        List<AnnotatedParameter<?>> params = method.getParameters();

        InjectionPoint[] args = new InjectionPoint[params.size()];

        for (int i = 0; i < args.length; i++) {
          InjectionPoint ij = new InjectionPointImpl(getBeanManager(), this, params.get(i));

          _injectionPointSet.add(ij);

          args[i] = ij;
        }

        injectProgramList.add(new MethodInjectProgram(method.getJavaMember(), args));
      } else {
        InjectionPointHandler handler = getBeanManager().getInjectionPointHandler(method);

        if (handler != null) {
          ConfigProgram program = new MethodHandlerProgram(method, handler);

          injectProgramList.add(program);
        }
      }
    }
  }
예제 #2
0
  /**
   * Generates a unique signature for a concrete class
   *
   * @param <X>
   * @param annotatedType
   * @return
   */
  public static <X> String createTypeId(
      Class<X> clazz,
      Collection<Annotation> annotations,
      Collection<AnnotatedMethod<? super X>> methods,
      Collection<AnnotatedField<? super X>> fields,
      Collection<AnnotatedConstructor<X>> constructors) {
    StringBuilder builder = new StringBuilder();

    builder.append(clazz.getName());
    builder.append(createAnnotationCollectionId(annotations));
    builder.append("{");

    // now deal with the fields
    List<AnnotatedField<? super X>> sortedFields = new ArrayList<AnnotatedField<? super X>>();
    sortedFields.addAll(fields);
    Collections.sort(sortedFields, AnnotatedFieldComparator.<X>instance());
    for (AnnotatedField<? super X> field : sortedFields) {
      if (!field.getAnnotations().isEmpty()) {
        builder.append(createFieldId(field));
        builder.append(SEPERATOR);
      }
    }

    // methods
    List<AnnotatedMethod<? super X>> sortedMethods = new ArrayList<AnnotatedMethod<? super X>>();
    sortedMethods.addAll(methods);
    Collections.sort(sortedMethods, AnnotatedMethodComparator.<X>instance());
    for (AnnotatedMethod<? super X> method : sortedMethods) {
      if (!method.getAnnotations().isEmpty() || hasMethodParameters(method)) {
        builder.append(createCallableId(method));
        builder.append(SEPERATOR);
      }
    }

    // constructors
    List<AnnotatedConstructor<? super X>> sortedConstructors =
        new ArrayList<AnnotatedConstructor<? super X>>();
    sortedConstructors.addAll(constructors);
    Collections.sort(sortedConstructors, AnnotatedConstructorComparator.<X>instance());
    for (AnnotatedConstructor<? super X> constructor : sortedConstructors) {
      if (!constructor.getAnnotations().isEmpty() || hasMethodParameters(constructor)) {
        builder.append(createCallableId(constructor));
        builder.append(SEPERATOR);
      }
    }
    builder.append("}");

    return builder.toString();
  }
예제 #3
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);
      }
    }
  }