Esempio n. 1
0
  /**
   * Returns the correct implementation instance for the interface <code>type</code>. For convention
   * the implentation is named <code>InterfaceName + Impl</code>.
   *
   * @param type The interface type
   * @return The correct ModelProxy subclass (if exists), a ModelProxy instance otherwise
   * @throws MalformedModelException is the interface or the implementation are not well formed
   *     (they don't respect the conventions).
   * @throws ModelRuntimeException is any error occurs during the instantiation
   */
  protected ModelProxy createInstance(Class type) {
    Class backClass;
    if (implementations.containsKey(type)) backClass = implementations.get(type);
    else {
      /* type never seen */

      try {
        Package pkg = type.getPackage();
        String pkgN = pkg == null ? "" : pkg.getName();

        backClass = Class.forName(pkgN + tableName(type) + CommonStatic.getImplementationSuffix());

      } catch (Exception e) {
        backClass = ModelProxy.class;
      }

      Validator.validateModel(type, backClass);

      initFieldsTypes(type);
      implementations.put(type, backClass);
    }

    ModelProxy impl = null;
    try {
      impl = (ModelProxy) backClass.newInstance();
    } catch (Exception e) {
      throw new ModelRuntimeException(e.getMessage());
    }

    return impl;
  }
Esempio n. 2
-21
  /**
   * Returns all the classes related to <code>type</code> by a {@link Connection}
   *
   * @param type The given {@link Model} interface.
   * @return A list that contains all the models in a relationship with <code>type</code> interface.
   */
  protected List<Class> getAllReleatedClasses(Class type) {

    if (relatedClasses.containsKey(type)) return relatedClasses.get(type);

    List<Class> related = new ArrayList<Class>();
    Method[] getters = CommonStatic.getDeclaredGetters(type);

    for (Method g : getters)
      if (g.isAnnotationPresent(Connection.class)) {

        Class t = g.getReturnType();
        t = t.isArray() ? t.getComponentType() : t;

        if (!related.contains(t)) related.add(t);
      }

    relatedClasses.put(type, related);

    return related;
  }