Exemplo n.º 1
0
  /**
   * Registers all annotations specifying in {@link ComponentServiceContext}
   *
   * @param serviceHandler the {@link ServiceHandler}
   * @param annotation the {@link ComponentServiceContext} annotation.
   * @param getProvider if <b>true</b>, the suitable validator will be gotten from the given {@link
   *     Provider}, if <b>false</b>, the instance will be instantiated basing on <code>
   *     default constructor</code> specifying in corresponding object class.
   * @param provider the specified {@link Provider} container, if <code>getInject argument</code> is
   *     <b>true</b> and provider is null, one runtime-exception will be thrown.
   */
  @SuppressWarnings("unchecked")
  private static void registerAnnotationInCSC(
      ServiceHandler serviceHandler,
      ComponentServiceContext annotation,
      boolean getProvider,
      Provider provider) {

    if (getProvider == true && provider == null) {
      throw new JGentleRuntimeException("Provider must not be null while getProvider is true.");
    }
    Class<Annotation>[] annoList = (Class<Annotation>[]) annotation.annotations();
    Class<?>[] validators = annotation.validators();
    if (annoList.length != 0) {
      if (annoList.length < validators.length) {
        throw new JGentleRuntimeException(
            "invalid validators configuration in annotation: " + annotation);
      }
      for (int i = 0; i < annoList.length; i++) {
        if (i < validators.length) {
          AnnotationValidator<Annotation> validator;
          if (getProvider == false)
            validator =
                (AnnotationValidator<Annotation>) ReflectUtils.createInstance(validators[i]);
          else {
            validator = (AnnotationValidator<Annotation>) provider.getBean(validators[i]);
          }
          serviceHandler.registerAnnotation(annoList[i], validator);
        } else {
          serviceHandler.registerAnnotation(annoList[i]);
        }
      }
    }
  }