@Test
 public void testWatchAlternatives_unstereotypedField_veto() throws Exception {
   // prepare, global alternative
   when(at.isAnnotationPresent(Priority.class)).thenReturn(true);
   when(at.isAnnotationPresent(TestStereotype.class)).thenReturn(false);
   when(at.getJavaClass()).thenReturn(UnstereotypedField.class);
   // act
   subject.watchAlternatives(pat);
   // assert, vetoed
   verify(pat).veto();
 }
 @Test
 public void testWatchAlternatives_stereotypedClass_enable() throws Exception {
   // prepare, global alternative
   when(at.isAnnotationPresent(Priority.class)).thenReturn(true);
   when(at.isAnnotationPresent(TestStereotype.class)).thenReturn(true);
   when(at.getJavaClass()).thenReturn(StereotypedClass.class);
   // act
   subject.watchAlternatives(pat);
   // assert, not vetoed
   verify(pat, times(0)).veto();
 }
 <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);
   }
 }
 @Test
 public void testWatchAlternatives_stereotypedMethod_enable() throws Exception {
   // prepare, global alternative
   when(at.isAnnotationPresent(Priority.class)).thenReturn(true);
   when(at.isAnnotationPresent(TestStereotype.class)).thenReturn(false);
   final Set<Annotated> annotated = asAnnotatedSet(StereotypedMethod.class.getDeclaredMethods());
   when(at.getMethods()).thenReturn(annotated);
   when(at.getJavaClass()).thenReturn(StereotypedMethod.class);
   // act
   subject.watchAlternatives(pat);
   // assert, not vetoed
   verify(pat, times(0)).veto();
 }
  private void introspectDestroy(ArrayList<ConfigProgram> destroyList, AnnotatedType<?> type)
      throws ConfigException {
    if (type == null || type.equals(Object.class)) return;

    if (type.isAnnotationPresent(Interceptor.class)) {
      return;
    }

    for (AnnotatedMethod<?> method : type.getMethods()) {
      if (method.isAnnotationPresent(PreDestroy.class)) {
        Method javaMethod = method.getJavaMember();

        Class<?>[] types = javaMethod.getParameterTypes();

        if (types.length == 0) {
        } else if (types.length == 1 && types[0].equals(InvocationContext.class)) {
          // XXX:
          continue;
        } else
          throw new ConfigException(
              location(javaMethod) + L.l("@PreDestroy is requires zero arguments"));

        PreDestroyInject destroyProgram = new PreDestroyInject(javaMethod);

        if (!destroyList.contains(destroyProgram)) destroyList.add(destroyProgram);
      }
    }
  }
 @Test
 public void testWatchAlternatives_noGlobalAlternative() throws Exception {
   // prepare, no global alternative
   when(at.isAnnotationPresent(Priority.class)).thenReturn(false);
   // act
   subject.watchAlternatives(pat);
   // assert, not vetoed
   verify(pat, times(0)).veto();
 }
 @Test
 public void testWatchAlternatives_disabledAlternativeClass() throws Exception {
   // prepare, global alternative
   when(at.isAnnotationPresent(Priority.class)).thenReturn(true);
   when(at.getJavaClass()).thenReturn(DisabledAlternativeClass.class);
   // act
   subject.watchAlternatives(pat);
   // assert, vetoed
   verify(pat).veto();
 }
 <T, X> void registerGenericBeanObserverMethod(@Observes ProcessObserverMethod<T, X> event) {
   AnnotatedType<X> declaringType = event.getAnnotatedMethod().getDeclaringType();
   if (declaringType.isAnnotationPresent(GenericConfiguration.class)) {
     AnnotatedMethod<X> method = event.getAnnotatedMethod();
     Class<? extends Annotation> genericConfigurationType =
         declaringType.getAnnotation(GenericConfiguration.class).value();
     genericBeanObserverMethods.put(
         genericConfigurationType,
         new ObserverMethodHolder<X, T>(method, event.getObserverMethod()));
   }
 }
  private ConfigProgram[] introspectPostConstruct(AnnotatedType<X> annType) {
    if (annType.isAnnotationPresent(Interceptor.class)) {
      return new ConfigProgram[0];
    }

    ArrayList<ConfigProgram> initList = new ArrayList<ConfigProgram>();
    introspectInit(initList, annType);
    ConfigProgram[] initProgram = new ConfigProgram[initList.size()];
    initList.toArray(initProgram);

    Arrays.sort(initProgram);

    return initProgram;
  }
示例#10
0
 <X, T> void registerGenericBeanProducerMethod(@Observes ProcessProducerMethod<X, T> event) {
   AnnotatedType<X> declaringType = event.getAnnotatedProducerMethod().getDeclaringType();
   Annotation genericConfiguration = getGenericConfiguration(event.getAnnotated());
   if (declaringType.isAnnotationPresent(GenericConfiguration.class)) {
     genericBeanProducerMethods.put(
         declaringType.getAnnotation(GenericConfiguration.class).value(),
         getProducerMethodHolder(event));
   } else if (genericConfiguration != null) {
     if (validateGenericProducer(
         genericConfiguration, event.getBean(), event.getAnnotatedProducerMethod())) {
       genericProducerBeans.put(event.getAnnotatedProducerMethod(), event.getBean());
     }
   }
 }
示例#11
0
 <X> void registerGenericBean(@Observes ProcessManagedBean<X> event) {
   AnnotatedType<X> type = event.getAnnotatedBeanClass();
   if (type.isAnnotationPresent(GenericConfiguration.class)) {
     Class<? extends Annotation> genericType =
         type.getAnnotation(GenericConfiguration.class).value();
     genericBeans.put(
         genericType, new BeanHolder<X>(event.getAnnotatedBeanClass(), event.getBean()));
     for (AnnotatedMethod<? super X> m : event.getAnnotatedBeanClass().getMethods()) {
       if (m.isAnnotationPresent(Unwraps.class)) {
         unwrapsMethods.put(genericType, m);
       }
     }
   }
 }
示例#12
0
  <X, T> void registerGenericBeanProducerField(@Observes ProcessProducerField<X, T> event) {

    AnnotatedType<X> declaringType = event.getAnnotatedProducerField().getDeclaringType();
    Annotation genericConfiguration = getGenericConfiguration(event.getAnnotated());
    if (declaringType.isAnnotationPresent(GenericConfiguration.class)) {
      AnnotatedField<X> field = event.getAnnotatedProducerField();
      Class<? extends Annotation> genericConfigurationType =
          declaringType.getAnnotation(GenericConfiguration.class).value();
      genericBeanProducerFields.put(
          genericConfigurationType, new FieldHolder<X, T>(field, event.getBean()));
    } else if (genericConfiguration != null) {
      if (validateGenericProducer(
          genericConfiguration, event.getBean(), event.getAnnotatedProducerField())) {
        genericProducerBeans.put(event.getAnnotatedProducerField(), event.getBean());
      }
    }
  }
示例#13
0
  private void validatePassivating(Bean<?> bean) {
    Type baseType = _annotatedType.getBaseType();

    Class<?> cl = getBeanManager().createTargetBaseType(baseType).getRawClass();
    boolean isStateful = _annotatedType.isAnnotationPresent(Stateful.class);

    if (!Serializable.class.isAssignableFrom(cl) && !isStateful) {
      throw new ConfigException(
          L.l(
              "'{0}' is an invalid @{1} bean because it's not serializable for {2}.",
              cl.getSimpleName(), bean.getScope().getSimpleName(), bean));
    }

    for (InjectionPoint ip : bean.getInjectionPoints()) {
      if (ip.isTransient()) continue;

      Type type = ip.getType();

      if (ip.getBean() instanceof CdiStatefulBean) continue;

      if (type instanceof Class<?>) {
        Class<?> ipClass = (Class<?>) type;

        if (!ipClass.isInterface()
            && !Serializable.class.isAssignableFrom(ipClass)
            && !getBeanManager().isNormalScope(ip.getBean().getScope())) {
          throw new ConfigException(
              L.l(
                  "'{0}' is an invalid @{1} bean because '{2}' value {3} is not serializable for {4}.",
                  cl.getSimpleName(),
                  bean.getScope().getSimpleName(),
                  ip.getType(),
                  ip.getMember().getName(),
                  bean));
        }
      }
    }
  }
示例#14
0
  /** Binds parameters */
  private CandiProducer<X> build() {
    Thread thread = Thread.currentThread();
    ClassLoader oldLoader = thread.getContextClassLoader();

    try {
      thread.setContextClassLoader(getBeanManager().getClassLoader());

      introspect();

      Class<X> cl = (Class<X>) _annotatedType.getBaseType();

      if (_beanCtor == null) {
        // XXX:
        AnnotatedType<X> beanType = _annotatedType;

        if (beanType == null) beanType = ReflectionAnnotatedFactory.introspectType(cl);

        introspectConstructor(beanType);
      }

      Class<X> instanceClass = null;

      if (_isGenerateInterception) {
        if (!_annotatedType.isAnnotationPresent(javax.interceptor.Interceptor.class)
            && !_annotatedType.isAnnotationPresent(javax.decorator.Decorator.class)) {
          CandiBeanGenerator<X> bean = new CandiBeanGenerator<X>(getBeanManager(), _annotatedType);
          bean.introspect();

          instanceClass = (Class<X>) bean.generateClass();
        }

        if (instanceClass == cl && isSerializeHandle()) {
          instanceClass = SerializationAdapter.gen(instanceClass);
        }
      }

      if (instanceClass != null && instanceClass != _instanceClass) {
        try {
          if (_javaCtor != null) {
            _javaCtor =
                (Constructor<X>) getConstructor(instanceClass, _javaCtor.getParameterTypes());
            _javaCtor.setAccessible(true);
          }
        } catch (Exception e) {
          // server/2423
          log.log(Level.FINE, e.toString(), e);
          // throw ConfigException.create(e);
        }
      }

      ConfigProgram[] injectProgram = introspectInject(_annotatedType);
      ConfigProgram[] initProgram = introspectPostConstruct(_annotatedType);

      ArrayList<ConfigProgram> destroyList = new ArrayList<ConfigProgram>();
      introspectDestroy(destroyList, _annotatedType);
      ConfigProgram[] destroyProgram = new ConfigProgram[destroyList.size()];
      destroyList.toArray(destroyProgram);

      Arg[] args = null;

      if (_beanCtor != null) args = introspectArguments(_beanCtor, _beanCtor.getParameters());

      CandiProducer<X> producer =
          new CandiProducer<X>(
              _bean,
              instanceClass,
              _javaCtor,
              args,
              injectProgram,
              initProgram,
              destroyProgram,
              _injectionPointSet);

      return producer;
    } finally {
      thread.setContextClassLoader(oldLoader);
    }
  }
示例#15
0
 <X> void registerGenericBeanInjectionTarget(@Observes ProcessInjectionTarget<X> event) {
   AnnotatedType<X> type = event.getAnnotatedType();
   if (type.isAnnotationPresent(GenericConfiguration.class)) {
     genericInjectionTargets.put(type, event.getInjectionTarget());
   }
 }
示例#16
0
  <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());
    }
  }
示例#17
0
  /**
   * Register managed beans as Errai services
   *
   * @param event -
   * @param <T> -
   */
  @SuppressWarnings("UnusedDeclaration")
  public <T> void observeResources(@Observes final ProcessAnnotatedType<T> event) {
    final AnnotatedType<T> type = event.getAnnotatedType();

    for (final Annotation a : type.getJavaClass().getAnnotations()) {
      if (a.annotationType().isAnnotationPresent(Qualifier.class)) {
        beanQualifiers.put(a.annotationType().getName(), a);
      }
    }

    // services
    if (type.isAnnotationPresent(Service.class)) {
      log.debug("discovered Errai annotation on type: " + type);
      boolean isRpc = false;

      final Class<T> javaClass = type.getJavaClass();
      for (final Class<?> intf : javaClass.getInterfaces()) {
        isRpc = intf.isAnnotationPresent(Remote.class);

        if (isRpc) {
          if (!managedTypes.getRemoteInterfaces().contains(intf)) {
            managedTypes.addRemoteInterface(intf);
          }
        }
      }

      if (!isRpc) {
        managedTypes.addServiceEndpoint(type);
      }
    } else {
      for (final AnnotatedMethod method : type.getMethods()) {
        if (method.isAnnotationPresent(Service.class)) {
          managedTypes.addServiceMethod(type, method);
        }
      }
    }

    // veto on client side implementations that contain CDI annotations
    // (i.e. @Observes) Otherwise Weld might try to invoke on them
    if (vetoClasses.contains(type.getJavaClass().getName())
        || (type.getJavaClass().getPackage().getName().contains("client")
            && !type.getJavaClass().isInterface())) {
      event.veto();
    }
    /** We must scan for Event consumer injection points to build the tables */
    final Class clazz = type.getJavaClass();

    for (final Field f : clazz.getDeclaredFields()) {
      if (f.isAnnotationPresent(Inject.class) && f.isAnnotationPresent(ObserverModel.class)) {
        processEventInjector(f.getType(), f.getGenericType(), f.getAnnotations());
      }
    }
    for (final Method m : clazz.getDeclaredMethods()) {
      if (m.isAnnotationPresent(Inject.class) && m.isAnnotationPresent(ObserverModel.class)) {
        final Class<?>[] parameterTypes = m.getParameterTypes();
        for (int i = 0, parameterTypesLength = parameterTypes.length;
            i < parameterTypesLength;
            i++) {
          final Class<?> parmType = parameterTypes[i];
          processEventInjector(
              parmType, m.getGenericParameterTypes()[i], m.getParameterAnnotations()[i]);
        }
      }
    }
    for (final Constructor c : clazz.getDeclaredConstructors()) {
      if (c.isAnnotationPresent(Inject.class) && c.isAnnotationPresent(ObserverModel.class)) {
        final Class<?>[] parameterTypes = c.getParameterTypes();
        for (int i = 0, parameterTypesLength = parameterTypes.length;
            i < parameterTypesLength;
            i++) {
          final Class<?> parmType = parameterTypes[i];
          processEventInjector(
              parmType, c.getGenericParameterTypes()[i], c.getParameterAnnotations()[i]);
        }
      }
    }
  }