diff options
author | gjoranv <gv@yahoo-inc.com> | 2017-06-12 13:10:15 +0200 |
---|---|---|
committer | gjoranv <gv@yahoo-inc.com> | 2017-06-12 13:10:15 +0200 |
commit | 41fdfe00ac0dfeb1aab3a9c2db89b717e9b59558 (patch) | |
tree | 4b5c9e3edee55b8353f5fe9696e80169939e7e93 | |
parent | 0d9a4d0a7d933fe207082a32d82ada45d301bea2 (diff) |
Set the default constructor on config classes private.
- Default constructors for arrays are still public.
3 files changed, 28 insertions, 9 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 72ca0138a53..eec2d3cd500 100644 --- a/config-lib/src/main/java/com/yahoo/config/InnerNodeVector.java +++ b/config-lib/src/main/java/com/yahoo/config/InnerNodeVector.java @@ -1,6 +1,8 @@ // 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; /** @@ -38,9 +40,11 @@ public class InnerNodeVector<NODE extends InnerNode> extends NodeVector<NODE> { @SuppressWarnings("unchecked") protected NODE createNew() { try { - return (NODE) defaultNode.getClass().newInstance(); - } catch (IllegalAccessException | InstantiationException ex) { - throw new ConfigurationRuntimeException(ex); + Constructor<? extends InnerNode> ctor = defaultNode.getClass().getDeclaredConstructor(); + ctor.setAccessible(true); + return (NODE) ctor.newInstance(); + } catch (InvocationTargetException | IllegalAccessException | InstantiationException | NoSuchMethodException e) { + throw new ConfigurationRuntimeException(e); } } diff --git a/config-model/src/main/java/com/yahoo/vespa/model/InstanceResolver.java b/config-model/src/main/java/com/yahoo/vespa/model/InstanceResolver.java index 9c761e425a7..ab095ec8d42 100644 --- a/config-model/src/main/java/com/yahoo/vespa/model/InstanceResolver.java +++ b/config-model/src/main/java/com/yahoo/vespa/model/InstanceResolver.java @@ -1,17 +1,26 @@ // Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.model; -import com.yahoo.config.*; +import com.yahoo.config.ConfigBuilder; +import com.yahoo.config.ConfigInstance; +import com.yahoo.config.ConfigurationRuntimeException; import com.yahoo.config.codegen.CNode; import com.yahoo.config.codegen.ConfigGenerator; import com.yahoo.config.codegen.InnerCNode; import com.yahoo.config.codegen.LeafCNode; import com.yahoo.log.LogLevel; +import com.yahoo.vespa.config.ConfigDefinitionKey; +import com.yahoo.vespa.config.ConfigKey; +import com.yahoo.vespa.config.ConfigPayload; +import com.yahoo.vespa.config.ConfigPayloadBuilder; +import com.yahoo.vespa.config.ConfigTransformer; +import com.yahoo.vespa.config.GenericConfig; import com.yahoo.vespa.config.buildergen.ConfigDefinition; import com.yahoo.yolean.Exceptions; -import com.yahoo.vespa.config.*; +import java.lang.reflect.Constructor; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.List; import java.util.Map; @@ -167,7 +176,7 @@ class InstanceResolver { * @param cKey a ConfigKey * @return a {@link ConfigInstance} or null if not available in classpath */ - static ConfigInstance getInstance(ConfigDefinitionKey cKey, ClassLoader instanceLoader) { + private static ConfigInstance getInstance(ConfigDefinitionKey cKey, ClassLoader instanceLoader) { String className = ConfigGenerator.createClassName(cKey.getName()); Class<?> clazz; String fullClassName = packageName(cKey) + "." + className; @@ -178,8 +187,10 @@ class InstanceResolver { } Object i; try { - i = clazz.newInstance(); - } catch (InstantiationException | IllegalAccessException e) { + Constructor<?> configConstructor = clazz.getDeclaredConstructor(); + configConstructor.setAccessible(true); + i = configConstructor.newInstance(); + } catch (InvocationTargetException | InstantiationException | IllegalAccessException | NoSuchMethodException e) { throw new ConfigurationRuntimeException(e); } if (!(i instanceof ConfigInstance)) { diff --git a/configgen/src/main/scala/com/yahoo/config/codegen/ConfigGenerator.scala b/configgen/src/main/scala/com/yahoo/config/codegen/ConfigGenerator.scala index 716f2a60c33..1716b831773 100644 --- a/configgen/src/main/scala/com/yahoo/config/codegen/ConfigGenerator.scala +++ b/configgen/src/main/scala/com/yahoo/config/codegen/ConfigGenerator.scala @@ -186,6 +186,10 @@ object ConfigGenerator { // TODO @link gives javadoc warnings, although the syntax seems to be valid //def link = "{@link " + {nodeClass(inner)} + "#" + {nodeClass(inner)} + "(Builder)}" def link = {nodeClass(inner)} + "(Builder)" + def visibility = inner match { + case array: InnerCNode if inner.isArray => "public" + case _ => "private" + } <code> |/** @@ -194,7 +198,7 @@ object ConfigGenerator { | * Replaced by {link} | */ |@Deprecated - |public {nodeClass(inner)}() {{ + |{visibility} {nodeClass(inner)}() {{ | this(new Builder(), false); |}} </code>.text.stripMargin.trim |