All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.inject.throwingproviders.CheckedProvideUtils Maven / Gradle / Ivy

There is a newer version: 7.0.0
Show newest version
// Copyright 2012 Google Inc. All Rights Reserved.

package com.google.inject.throwingproviders;

import com.google.inject.Binder;
import com.google.inject.TypeLiteral;
import com.google.inject.internal.Annotations;
import com.google.inject.internal.Errors;
import com.google.inject.spi.Message;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Constructor;

/**
 * Utilities for the throwing provider module.
 * 
 * @author [email protected] (Sam Berlin)
 */
class CheckedProvideUtils {
  
  private CheckedProvideUtils() {}
  
  private static final String CONSTRUCTOR_RULES =
      "Classes must have either one (and only one) constructor annotated with @ThrowingInject.";
  
  @SuppressWarnings("unchecked") // safe because it's a constructor of the typeLiteral
  static  Constructor findThrowingConstructor(
      TypeLiteral typeLiteral, Binder binder) {
    
    Class rawType = typeLiteral.getRawType();
    Errors errors = new Errors(rawType);
    Constructor cxtor = null;
    for (Constructor constructor : rawType.getDeclaredConstructors()) {
      if (constructor.isAnnotationPresent(ThrowingInject.class)) {
        if (cxtor != null) {
          errors.addMessage("%s has more than one constructor annotated with @ThrowingInject. "
              + CONSTRUCTOR_RULES, rawType);
        }

        cxtor = constructor;
        Annotation misplacedBindingAnnotation = Annotations.findBindingAnnotation(
            errors, cxtor, ((AnnotatedElement) cxtor).getAnnotations());
        if (misplacedBindingAnnotation != null) {
          errors.misplacedBindingAnnotation(cxtor, misplacedBindingAnnotation);
        }
      }
    }
    
    if (cxtor == null) {
      errors.addMessage(
          "Could not find a suitable constructor in %s. " + CONSTRUCTOR_RULES, rawType);
    }

    for (Message msg : errors.getMessages()) {
      binder.addError(msg);
    }
    return (Constructor) cxtor;
  }
  
  /** Adds errors to the binder if the exceptions aren't valid. */
  static void validateExceptions(Binder binder,
      Iterable> actualExceptionTypes,
      Iterable> expectedExceptionTypes,
      Class checkedProvider) {
    // Validate the exceptions in the method match the exceptions
    // in the CheckedProvider.
    for (TypeLiteral exType : actualExceptionTypes) {
      Class exActual = exType.getRawType();
      // Ignore runtime exceptions & errors.
      if (RuntimeException.class.isAssignableFrom(exActual)
          || Error.class.isAssignableFrom(exActual)) {
        continue;
      }

      boolean notAssignable = true;
      for (Class exExpected : expectedExceptionTypes) {
        if (exExpected.isAssignableFrom(exActual)) {
          notAssignable = false;
          break;
        }
      }
      if (notAssignable) {
        binder.addError(
            "%s is not compatible with the exceptions (%s) declared in " 
            + "the CheckedProvider interface (%s)",
            exActual, expectedExceptionTypes, checkedProvider);
      }
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy