@Override
    @SuppressWarnings("unchecked")
    protected void doConfigure() {
      for (Class<?> clazz : testCase.getClass().getDeclaredClasses()) {
        BindTo bindTo = clazz.getAnnotation(BindTo.class);
        if (bindTo == null) continue;
        List<Annotation> qualifiers = harvestQualifiers(clazz);
        Annotation classifier = qualifiers.isEmpty() ? null : qualifiers.get(0);
        boolean isSingleton = clazz.getAnnotation(Singleton.class) != null;

        AnnotatedBindingBuilder<?> abuilder = bind(bindTo.value());
        if (classifier != null) {
          LinkedBindingBuilder<?> lbuilder = abuilder.withClassifier(classifier);
          if (Provider.class.isAssignableFrom(clazz)) {
            SingletonBindingBuilder<?> sbuilder = lbuilder.toProvider((Class) clazz);
            if (isSingleton) sbuilder.asSingleton();
          } else {
            SingletonBindingBuilder<?> sbuilder = lbuilder.to((Class) clazz);
            if (isSingleton) sbuilder.asSingleton();
          }
        } else {
          if (Provider.class.isAssignableFrom(clazz)) {
            SingletonBindingBuilder<?> sbuilder = abuilder.toProvider((Class) clazz);
            if (isSingleton) sbuilder.asSingleton();
          } else {
            SingletonBindingBuilder<?> sbuilder = abuilder.to((Class) clazz);
            if (isSingleton) sbuilder.asSingleton();
          }
        }
      }
    }
Example #2
0
    public boolean equals(Object o) {
      if (!(o instanceof BindTo)) {
        return false;
      }

      BindTo other = (BindTo) o;
      return value.equals(other.value());
    }
    @Override
    @SuppressWarnings("unchecked")
    protected void doConfigure() {
      for (Field field : testCase.getClass().getDeclaredFields()) {
        BindTo bindTo = field.getAnnotation(BindTo.class);
        if (bindTo == null) continue;
        List<Annotation> qualifiers = harvestQualifiers(field);
        Annotation classifier = qualifiers.isEmpty() ? null : qualifiers.get(0);
        boolean isSingleton = field.getAnnotation(Singleton.class) != null;

        field.setAccessible(true);
        Object instance = null;
        try {
          instance = field.get(testCase);
        } catch (IllegalAccessException e) {
          throw new IllegalArgumentException(e);
        }

        if (instance != null) {
          AnnotatedBindingBuilder<Object> abuilder =
              (AnnotatedBindingBuilder<Object>) bind(bindTo.value());
          if (classifier != null) {
            if (Provider.class.isAssignableFrom(instance.getClass())) {
              SingletonBindingBuilder<?> sbuilder =
                  abuilder.withClassifier(classifier).toProvider((Provider<Object>) instance);
              if (isSingleton) sbuilder.asSingleton();
            } else {
              abuilder.withClassifier(classifier).toInstance(instance);
            }
          } else if (Provider.class.isAssignableFrom(instance.getClass())) {
            SingletonBindingBuilder<?> sbuilder = abuilder.toProvider((Provider<Object>) instance);
            if (isSingleton) sbuilder.asSingleton();
          } else {
            abuilder.toInstance(instance);
          }
        } else {
          AnnotatedBindingBuilder<?> abuilder = bind(bindTo.value());
          if (classifier != null) {
            LinkedBindingBuilder<?> lbuilder = abuilder.withClassifier(classifier);
            if (Provider.class.isAssignableFrom(field.getType())) {
              SingletonBindingBuilder<?> sbuilder = lbuilder.toProvider((Class) field.getType());
              if (isSingleton) sbuilder.asSingleton();
            } else {
              SingletonBindingBuilder<?> sbuilder = lbuilder.to((Class) field.getType());
              if (isSingleton) sbuilder.asSingleton();
            }
          } else {
            if (Provider.class.isAssignableFrom(field.getType())) {
              SingletonBindingBuilder<?> sbuilder = abuilder.toProvider((Class) field.getType());
              if (isSingleton) sbuilder.asSingleton();
            } else {
              SingletonBindingBuilder<?> sbuilder = abuilder.to((Class) field.getType());
              if (isSingleton) sbuilder.asSingleton();
            }
          }
        }
      }
    }