// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * @author gjoranv
 * @since 5.1.4
 */
public class InnerNodeVector<NODE extends InnerNode> extends NodeVector<NODE> {

    NODE defaultNode;

    /**
     * Creates a new vector with the given default node.
     */
    // TODO: remove this ctor when the library uses reflection via builders, and resizing won't be necessary
    public InnerNodeVector(NODE defaultNode) {
        assert (defaultNode != null) : "The default node cannot be null";

        this.defaultNode = defaultNode;
        if (createNew() == null) {
            throw new NullPointerException("Unable to duplicate the default node.");
        }
    }

    public InnerNodeVector(List<NODE> nodes, NODE defaultNode) {
        this(defaultNode);
        for (NODE node : nodes) {
            vector.add(node);
        }
    }

    /**
     * Creates a new Node by creating a new instance with the 0-argument constructor
     */
    // TODO: remove when the library uses reflection via builders
    @SuppressWarnings("unchecked")
    protected NODE createNew() {
        try {
            Constructor<? extends InnerNode> ctor  = defaultNode.getClass().getDeclaredConstructor();
            ctor.setAccessible(true);
            return (NODE) ctor.newInstance();
        } catch (InvocationTargetException | IllegalAccessException | InstantiationException | NoSuchMethodException e) {
            throw new ConfigurationRuntimeException(e);
        }
    }

}

@@ -224,16 +224,41 @@
 eption;%0A
+import java.util.Arrays;%0A
 import j
@@ -1340,94 +1340,848 @@
    C
-onstructor%3C? extends InnerNode%3E ctor  = defaultNode.getClass().getDeclaredConstructor(
+lass%3C? extends InnerNode%3E nodeClass = defaultNode.getClass();%0A            Class%3C?%3E builderClass = Arrays.stream(nodeClass.getClasses())%0A                    .filter(klass -%3E klass.getSimpleName().equals(%22Builder%22))%0A                    .findFirst()%0A                    .orElseThrow(() -%3E new ConfigurationRuntimeException(%22Could not find builder class for %22 + nodeClass.getName()));%0A%0A            Constructor%3C?%3E builderCtor = builderClass.getConstructor();%0A            Object builderInstance = builderCtor.newInstance();%0A            if (! (builderInstance instanceof ConfigBuilder))%0A                throw new ConfigurationRuntimeException(%22Builder is not a ConfigBuilder, has class: %22 + builderInstance.getClass().getName());%0A%0A            Constructor%3C? extends InnerNode%3E nodeCtor = nodeClass.getDeclaredConstructor(builderClass, boolean.class
 );%0A 
@@ -2191,17 +2191,21 @@
         
-c
+nodeC
 tor.setA
@@ -2247,17 +2247,21 @@
  (NODE) 
-c
+nodeC
 tor.newI
@@ -2260,32 +2260,54 @@
 tor.newInstance(
+builderInstance, false
 );%0A        %7D cat

Use builder to instantiate default node.