protected Class<?> findRootType(String source, Node sourceNode, Class<?>[] ctorArguments) {

    try {
      Class<?> sourceClass = loadClass(source, this, null);
      for (Class<?> ctorArg : ctorArguments) {
        if (ReflectionUtils.lookupConstructor(sourceClass, ctorArg) != null) {
          return ctorArg;
        }
      }
    } catch (ClassNotFoundException cnfe) {
      throw new ConfigurationException(
          buildMessage(MessageFormat.format("Unable to find class ''{0}''", source), sourceNode),
          cnfe);
    }

    return null;
  }
  protected Object createInstance(String className, Class rootType, Object root, Node source) {
    Class clazz;
    Object returnObject = null;
    if (className != null) {
      try {
        clazz = loadClass(className, returnObject, null);
        if (clazz != null) {
          if (isDevModeEnabled()) {
            Class<?>[] interfaces = clazz.getInterfaces();
            if (interfaces != null) {
              for (Class<?> c : interfaces) {
                if ("groovy.lang.GroovyObject".equals(c.getName())) {
                  // all groovy classes will implement this interface
                  returnObject = createScriptProxy(rootType, className, root);
                  break;
                }
              }
            }
          }
          if (returnObject == null) {
            // Look for an adapter constructor if we've got
            // an object to adapt
            if ((rootType != null) && (root != null)) {
              Constructor construct = ReflectionUtils.lookupConstructor(clazz, rootType);
              if (construct != null) {
                returnObject = construct.newInstance(root);
              }
            }
          }
          if (clazz != null && returnObject == null) {
            returnObject = clazz.newInstance();
          }
        }

      } catch (ClassNotFoundException cnfe) {
        throw new ConfigurationException(
            buildMessage(MessageFormat.format("Unable to find class ''{0}''", className), source),
            cnfe);
      } catch (NoClassDefFoundError ncdfe) {
        throw new ConfigurationException(
            buildMessage(
                MessageFormat.format(
                    "Class ''{0}'' is missing a runtime dependency: {1}",
                    className, ncdfe.toString()),
                source),
            ncdfe);
      } catch (ClassCastException cce) {
        throw new ConfigurationException(
            buildMessage(
                MessageFormat.format(
                    "Class ''{0}'' is not an instance of ''{1}''", className, rootType),
                source),
            cce);
      } catch (Exception e) {
        throw new ConfigurationException(
            buildMessage(
                MessageFormat.format(
                    "Unable to create a new instance of ''{0}'': {1}", className, e.toString()),
                source),
            e);
      }
    }

    return returnObject;
  }