예제 #1
0
파일: Dom.java 프로젝트: hk2-project/hk2
  /* package */ void ensureConstraints(List<Child> children) {
    Set<String> nullElements = new HashSet<String>(model.getElementNames());
    for (Child child : children) {
      nullElements.remove(child.name);
    }

    for (String s : nullElements) {
      ConfigModel.Property p = model.getElement(s);
      for (String annotation : p.getAnnotations()) {
        if (annotation.equals(NotNull.class.getName())) {
          if (p instanceof ConfigModel.Node) {
            ConfigModel childModel = ((ConfigModel.Node) p).model;
            Dom child = document.make(getHabitat(), null, this, childModel);
            child.register();

            children.add(new Dom.NodeChild(s, child));

            // recursive call to ensure the children constraints are also respected
            List<Child> grandChildren = new ArrayList<Child>();
            child.ensureConstraints(grandChildren);
            if (!grandChildren.isEmpty()) {
              child.setChildren(grandChildren);
            }

            child.initializationCompleted();
          }
        }
      }
    }
  }
예제 #2
0
파일: Dom.java 프로젝트: hk2-project/hk2
  public Set<Type> getContractTypes() {
    HashSet<Type> retVal = new HashSet<Type>();

    retVal.add(model.getProxyType());

    return retVal;
  }
예제 #3
0
파일: Dom.java 프로젝트: hk2-project/hk2
  /**
   * {@link InvocationHandler} implementation that allows strongly-typed access to the
   * configuration.
   *
   * <p>TODO: it might be a great performance improvement to have APT generate code that does this
   * during the development time by looking at the interface.
   */
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    // serve java.lang.Object methods by ourselves
    Class<?> clazz = method.getDeclaringClass();
    if (clazz == Object.class) {
      try {
        return method.invoke(this, args);
      } catch (InvocationTargetException e) {
        throw e.getTargetException();
      }
    }

    if (method.getAnnotation(DuckTyped.class) != null) {
      return invokeDuckMethod(method, proxy, args);
    }
    if (method.getAnnotation(ConfigExtensionMethod.class) != null) {
      ConfigExtensionMethod cem = method.getAnnotation(ConfigExtensionMethod.class);
      ConfigExtensionHandler handler =
          (ConfigExtensionHandler)
              ((cem.value() != null)
                  ? getServiceLocator().getService(ConfigExtensionHandler.class, cem.value())
                  : getServiceLocator().getService(ConfigExtensionHandler.class));
      return invokeConfigExtensionMethod(handler, this, model.getProxyType(), args);
    }

    ConfigModel.Property p = model.toProperty(method);
    if (p == null)
      throw new IllegalArgumentException("No corresponding property found for method: " + method);

    if (args == null || args.length == 0) {
      // getter
      return getter(p, method.getGenericReturnType());
    } else {
      throw new PropertyVetoException(
          "Instance of "
              + getImplementation()
              + " named '"
              + getKey()
              + "' is not locked for writing when invoking method "
              + method.getName()
              + " you must use transaction semantics to access it.",
          null);
    }
  }
예제 #4
0
파일: Dom.java 프로젝트: hk2-project/hk2
  /**
   * Invoke the user defined static method in the nested "Duck" class so that the user can define
   * convenience methods on the config beans.
   */
  Object invokeDuckMethod(Method method, Object proxy, Object[] args) throws Exception {
    Method duckMethod = model.getDuckMethod(method);

    Object[] duckArgs;
    if (args == null) {
      duckArgs = new Object[] {proxy};
    } else {
      duckArgs = new Object[args.length + 1];
      duckArgs[0] = proxy;
      System.arraycopy(args, 0, duckArgs, 1, args.length);
    }

    try {
      return duckMethod.invoke(null, duckArgs);
    } catch (InvocationTargetException e) {
      Throwable t = e.getTargetException();
      if (t instanceof Exception) throw (Exception) t;
      if (t instanceof Error) throw (Error) t;
      throw e;
    }
  }
예제 #5
0
파일: Dom.java 프로젝트: hk2-project/hk2
 public Class<?> getImplementationClass() {
   Class<?> retVal = (Class<?>) model.getProxyType();
   return retVal;
 }
예제 #6
0
파일: Dom.java 프로젝트: hk2-project/hk2
 /**
  * Returns the proxy type for this configuration object
  *
  * @param <T> the proxy type
  * @return the class object for the proxy type
  */
 public <T extends ConfigBeanProxy> Class<T> getProxyType() {
   return model.getProxyType();
 }
예제 #7
0
파일: Dom.java 프로젝트: hk2-project/hk2
 /** Performs injection to the given object. */
 public void inject(Object target) {
   model.inject(this, target);
 }