Esempio n. 1
0
  /**
   * Return true if the type is not abstract and not an interface, and has a constructor annotated
   * with {@link Inject} or its only constructor is the default constructor.
   *
   * @param type A class type
   * @return True if the class type is instantiable
   */
  public static boolean isInstantiable(Class<?> type) {
    if (!Modifier.isAbstract(type.getModifiers()) && !type.isInterface()) {
      // first check for a constructor annotated with @Inject,
      //  - this doesn't care how many we'll let the injector complain
      //    if there are more than one
      for (Constructor<?> c : type.getDeclaredConstructors()) {
        if (c.getAnnotation(Inject.class) != null) {
          return true;
        }
      }

      // check if we only have the public default constructor
      if (type.getConstructors().length == 1
          && type.getConstructors()[0].getParameterTypes().length == 0) {
        return true;
      }
    }

    // no constructor available
    return false;
  }