Esempio n. 1
0
  public Object inject(InjectionContext context) {
    if (injectionProvider == null) {
      return null;
    }
    Object injectedValue;
    try {
      injectedValue = injectionProvider.provideInjection(context);
    } catch (InjectionProviderException e) {
      Throwable ex = e;
      if (ex.getCause() != null) {
        ex = ex.getCause();
      }

      String message =
          "InjectionProvider unable to resolve @"
              + injectionAnnotation.annotationType().getSimpleName()
              + " "
              + injectionType.toString();
      throw new ConstructionException(message, ex);
    }
    if (injectedValue == null && !optional) {
      String message =
          "Non-optional @"
              + injectionAnnotation.annotationType().getSimpleName()
              + " "
              + injectionType.toString()
              + " was null in "
              + injectedClass.getName();
      throw new ConstructionException(message);
    }
    return getInjectedValue(injectedValue);
  }
Esempio n. 2
0
  public void bind(Resolution resolution) throws BindingException {
    InjectionProviderFactory providerFactory = resolution.application().injectionProviderFactory();

    try {
      injectionProvider = providerFactory.newInjectionProvider(resolution, this);

      if (injectionProvider == null && !optional) {
        String message =
            "Non-optional @"
                + rawInjectionClass.getName()
                + " was not bound in "
                + injectedClass.getName();
        throw new ConstructionException(message);
      }
    } catch (InvalidInjectionException e) {
      throw new BindingException("Could not bind dependency injection", e);
    }
  }
Esempio n. 3
0
 private Class<?> mapPrimitiveTypes(Class<?> rawInjectionType) {
   if (rawInjectionType == null || !rawInjectionType.isPrimitive()) {
     return rawInjectionType;
   }
   for (int i = 0; i < primitiveTypeMapping.length; i += 2) {
     if (primitiveTypeMapping[i].equals(rawInjectionType)) {
       return primitiveTypeMapping[i + 1];
     }
   }
   return rawInjectionType;
 }
Esempio n. 4
0
  private Object getInjectedValue(Object injectionResult) {
    if (injectionResult == null) {
      return null;
    }

    if (injectionResult instanceof Iterable) {
      if (Iterable.class.isAssignableFrom(rawInjectionClass)
          || rawInjectionClass.isInstance(injectionResult)) {
        return injectionResult;
      } else {
        return firstElementOrNull((Iterable) injectionResult);
      }
    } else {
      if (Iterable.class.equals(injectionType)) {
        return Collections.singleton(injectionResult);
      }
    }
    return injectionResult;
  }
Esempio n. 5
0
 @Override
 public String toString() {
   return injectionAnnotation + " for " + injectionType + " in " + injectedClass.getName();
 }
Esempio n. 6
0
 public boolean hasScope(final Class<? extends Annotation> scope) {
   return scope == null || scope.equals(injectionAnnotation().annotationType());
 }