示例#1
0
 private void camelFactoryProducers(
     @Observes ProcessAnnotatedType<CdiCamelFactory> pat, BeanManager manager) {
   pat.setAnnotatedType(
       new AnnotatedTypeDelegate<>(
           pat.getAnnotatedType(),
           pat.getAnnotatedType()
               .getMethods()
               .stream()
               .filter(am -> am.isAnnotationPresent(Produces.class))
               .filter(am -> am.getTypeClosure().stream().noneMatch(isEqual(TypeConverter.class)))
               .peek(am -> producerQualifiers.put(am.getJavaMember(), getQualifiers(am, manager)))
               .map(
                   am ->
                       new AnnotatedMethodDelegate<>(
                           am,
                           am.getAnnotations()
                               .stream()
                               .filter(
                                   annotation -> !manager.isQualifier(annotation.annotationType()))
                               .collect(
                                   collectingAndThen(
                                       toSet(),
                                       annotations -> {
                                         annotations.add(EXCLUDED);
                                         return annotations;
                                       }))))
               .collect(toSet())));
 }
 <X> void processType(@Observes ProcessAnnotatedType<X> event) {
   AnnotatedType<X> original = event.getAnnotatedType();
   if (original.isAnnotationPresent(ManagedBean.class)
       && !original.isAnnotationPresent(Named.class)) {
     AnnotatedType<X> modified =
         new AnnotatedTypeBuilder<X>()
             .readFromType(event.getAnnotatedType(), true)
             .addToClass(new NamedLiteral(original.getAnnotation(ManagedBean.class).value()))
             .create();
     event.setAnnotatedType(modified);
   }
 }
  /**
   * Annotates the superclass of the mocks with @Alternative, to make sure we get no ambiguous
   * dependencies.
   *
   * @param pat
   * @param  <T>
   */
  public <T extends ServiceInterface> void processAnnotatedType(
      @Observes ProcessAnnotatedType<T> pat) {
    boolean annotateWithAlternative = false;

    // test, if the current type is a superclass of a mock
    Class<?> serviceClass = pat.getAnnotatedType().getJavaClass();
    for (Class<? extends ServiceInterface> mock : getMocks()) {
      if (serviceClass.isAssignableFrom(mock)) {
        annotateWithAlternative = true;
      }
    }

    if (annotateWithAlternative) {
      final AnnotatedType<T> type = pat.getAnnotatedType();
      AlternativeWrapperAnnotatedType<T> wrapperAnnotatedType =
          new AlternativeWrapperAnnotatedType<T>(type);
      pat.setAnnotatedType(wrapperAnnotatedType);
    }
  }
  /**
   * Used to register the method validation interceptor bindings.
   *
   * @param processAnnotatedTypeEvent event fired for each annotated type
   * @param <T> the annotated type
   */
  public <T> void processAnnotatedType(
      @Observes @WithAnnotations({Constraint.class, Valid.class, ValidateOnExecution.class})
          ProcessAnnotatedType<T> processAnnotatedTypeEvent) {
    Contracts.assertNotNull(
        processAnnotatedTypeEvent, "The ProcessAnnotatedType event cannot be null");

    // validation globally disabled
    if (!isExecutableValidationEnabled) {
      return;
    }

    AnnotatedType<T> type = processAnnotatedTypeEvent.getAnnotatedType();
    Set<AnnotatedCallable<? super T>> constrainedCallables = determineConstrainedCallables(type);

    if (!constrainedCallables.isEmpty()) {
      ValidationEnabledAnnotatedType<T> wrappedType =
          new ValidationEnabledAnnotatedType<T>(type, constrainedCallables);
      processAnnotatedTypeEvent.setAnnotatedType(wrappedType);
    }
  }
  <X> void replaceInjectOnGenericBeans(@Observes ProcessAnnotatedType<X> event) {
    AnnotatedType<X> type = event.getAnnotatedType();
    if (type.isAnnotationPresent(GenericConfiguration.class)) {
      final Class<? extends Annotation> genericConfigurationType =
          type.getAnnotation(GenericConfiguration.class).value();
      // validate that the configuration type is annotated correctly
      if (!genericConfigurationType.isAnnotationPresent(GenericType.class)) {
        errors.add(
            "Bean "
                + type.getJavaClass().getName()
                + " specifies generic annotation "
                + type.getAnnotation(GenericConfiguration.class)
                + " however "
                + genericConfigurationType
                + " is not annotated @GenericConfiguration.");
      } else {
        Class<?> configType = genericConfigurationType.getAnnotation(GenericType.class).value();
        if (configType.isAnnotationPresent(GenericConfiguration.class)) {
          errors.add(
              "Generic configuration type "
                  + genericConfigurationType
                  + " specifies a value() of "
                  + configType
                  + " however "
                  + configType
                  + " is a generic bean. Generic configuration types may not be generic beans");
        }
      }

      final AnnotatedTypeBuilder<X> builder = new AnnotatedTypeBuilder<X>().readFromType(type);
      builder.addToClass(genericBeanQualifier);
      builder.redefine(
          Inject.class,
          new AnnotationRedefiner<Inject>() {

            public void redefine(RedefinitionContext<Inject> ctx) {
              if (ctx.getAnnotatedElement() instanceof Field) {
                if (ctx.getAnnotatedElement().isAnnotationPresent(Generic.class)) {
                  // This is a Generic bean injection point
                  ctx.getAnnotationBuilder()
                      .remove(Inject.class)
                      .add(InjectGenericLiteral.INSTANCE);
                }
              }
            }
          });
      builder.redefine(
          Produces.class,
          new AnnotationRedefiner<Produces>() {

            public void redefine(RedefinitionContext<Produces> ctx) {
              // Add the marker qualifier
              ctx.getAnnotationBuilder()
                  .add(GenericMarkerLiteral.INSTANCE)
                  .add(genericBeanQualifier);
            }
          });
      builder.redefine(
          Disposes.class,
          new AnnotationRedefiner<Disposes>() {

            public void redefine(RedefinitionContext<Disposes> ctx) {
              // Add the marker qualifier
              ctx.getAnnotationBuilder()
                  .add(GenericMarkerLiteral.INSTANCE)
                  .add(genericBeanQualifier);
            }
          });

      builder.redefine(
          Generic.class,
          new AnnotationRedefiner<Generic>() {
            public void redefine(RedefinitionContext<Generic> ctx) {
              // if it is a parameter annotation
              if (!(ctx.getAnnotatedElement() instanceof AccessibleObject)) {
                // stick an InjectGeneric as a marker.
                ctx.getAnnotationBuilder().remove(Generic.class).add(InjectGenericLiteral.INSTANCE);
                if (ctx.getRawType().isAnnotationPresent(GenericConfiguration.class)) {
                  ctx.getAnnotationBuilder().add(genericBeanQualifier);
                }
              }
            }
          });
      event.setAnnotatedType(builder.create());
    }
  }