diff options
-rw-r--r-- | config-lib/src/main/java/com/yahoo/config/InnerNodeVector.java | 20 | ||||
-rw-r--r-- | config-lib/src/main/java/com/yahoo/config/NodeVector.java | 2 |
2 files changed, 17 insertions, 5 deletions
diff --git a/config-lib/src/main/java/com/yahoo/config/InnerNodeVector.java b/config-lib/src/main/java/com/yahoo/config/InnerNodeVector.java index eec2d3cd500..14c72ee8b14 100644 --- a/config-lib/src/main/java/com/yahoo/config/InnerNodeVector.java +++ b/config-lib/src/main/java/com/yahoo/config/InnerNodeVector.java @@ -3,6 +3,7 @@ package com.yahoo.config; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; +import java.util.Arrays; import java.util.List; /** @@ -36,13 +37,24 @@ public class InnerNodeVector<NODE extends InnerNode> extends NodeVector<NODE> { /** * Creates a new Node by creating a new instance with the 0-argument constructor */ - // TODO: remove when the library uses reflection via builders + // TODO: remove, only used for NodeVector.setSize which only seems to be used by unit tests. @SuppressWarnings("unchecked") protected NODE createNew() { try { - Constructor<? extends InnerNode> ctor = defaultNode.getClass().getDeclaredConstructor(); - ctor.setAccessible(true); - return (NODE) ctor.newInstance(); + Class<? extends InnerNode> nodeClass = defaultNode.getClass(); + Class<?> builderClass = Arrays.stream(nodeClass.getClasses()) + .filter(klass -> klass.getSimpleName().equals("Builder")) + .findFirst() + .orElseThrow(() -> new ConfigurationRuntimeException("Could not find builder class for " + nodeClass.getName())); + + Constructor<?> builderCtor = builderClass.getConstructor(); + Object builderInstance = builderCtor.newInstance(); + if (! (builderInstance instanceof ConfigBuilder)) + throw new ConfigurationRuntimeException("Builder is not a ConfigBuilder, has class: " + builderInstance.getClass().getName()); + + Constructor<? extends InnerNode> nodeCtor = nodeClass.getDeclaredConstructor(builderClass, boolean.class); + nodeCtor.setAccessible(true); + return (NODE) nodeCtor.newInstance(builderInstance, false); } catch (InvocationTargetException | IllegalAccessException | InstantiationException | NoSuchMethodException e) { throw new ConfigurationRuntimeException(e); } diff --git a/config-lib/src/main/java/com/yahoo/config/NodeVector.java b/config-lib/src/main/java/com/yahoo/config/NodeVector.java index 8ce5689937e..5279db65b85 100644 --- a/config-lib/src/main/java/com/yahoo/config/NodeVector.java +++ b/config-lib/src/main/java/com/yahoo/config/NodeVector.java @@ -34,7 +34,7 @@ public abstract class NodeVector<NODE> implements java.util.List<NODE> { * * @param n the new size of this NodeVector */ - // TODO: remove when the library uses reflection via builders, and resizing won't be necessary + // TODO: remove, only used by unit tests public void setSize(int n) { while (size() > n) vector.remove(n); while (size() < n) vector.add(createNew()); |