diff options
author | gjoranv <gv@oath.com> | 2017-08-31 15:35:01 +0200 |
---|---|---|
committer | gjoranv <gv@oath.com> | 2017-09-05 12:07:06 +0200 |
commit | a81650ae405450db737d4dd1777e0cb047579302 (patch) | |
tree | fa3de3e00a7920d51b222cb6fe64406bbe9e4f2c /configgen/src/main | |
parent | dcd280c9de94e318df07f41b74fc24d6f21521d5 (diff) |
Rewrite ConfigGenerator.createClassName to Java.
- In new class ConfiggenUtil.
- This will allow using the function without loading classes
from scala-xml in the future.
- Do not remove the original scala method, as it could still be
used for a while by older config models.
(By classes InstanceResolver and VespaModel.)
Diffstat (limited to 'configgen/src/main')
3 files changed, 54 insertions, 8 deletions
diff --git a/configgen/src/main/java/com/yahoo/config/codegen/ConfiggenUtil.java b/configgen/src/main/java/com/yahoo/config/codegen/ConfiggenUtil.java new file mode 100644 index 00000000000..a7a3eb718a3 --- /dev/null +++ b/configgen/src/main/java/com/yahoo/config/codegen/ConfiggenUtil.java @@ -0,0 +1,43 @@ +package com.yahoo.config.codegen; + +import java.util.Arrays; +import java.util.stream.Collectors; + +/** + * @author gjoranv + */ +public class ConfiggenUtil { + + /** + * Create class name from def name + * @param defName The file name without the '.def' suffix + */ + public static String createClassName(String defName) { + String className = Arrays.stream(defName.split("-")) + .map(ConfiggenUtil::capitalize) + .collect(Collectors.joining()) + + "Config"; + + if (! isLegalJavaIdentifier(className)) + throw new CodegenRuntimeException("Illegal config definition file name '" + defName + + "'. Must be a legal Java identifier."); + + return className; + } + + private static String capitalize(String in) { + StringBuilder sb = new StringBuilder(in); + sb.setCharAt(0, Character.toTitleCase(in.charAt(0))); + return sb.toString(); + } + + private static boolean isLegalJavaIdentifier(String name) { + if (name.isEmpty()) return false; + if (! Character.isJavaIdentifierStart(name.charAt(0))) return false; + + for (char c : name.substring(1).toCharArray()) { + if (! Character.isJavaIdentifierPart(c)) return false; + } + return true; + } +} 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 7ea07762c01..f6876affe5d 100644 --- a/configgen/src/main/scala/com/yahoo/config/codegen/ConfigGenerator.scala +++ b/configgen/src/main/scala/com/yahoo/config/codegen/ConfigGenerator.scala @@ -2,11 +2,12 @@ package com.yahoo.config.codegen +import com.yahoo.config.codegen.BuilderGenerator.getBuilder +import com.yahoo.config.codegen.JavaClassBuilder.Indentation import com.yahoo.config.codegen.LeafCNode._ import com.yahoo.config.codegen.ReservedWords.{INTERNAL_PREFIX => InternalPrefix} -import JavaClassBuilder.Indentation -import BuilderGenerator.getBuilder -import util.parsing.combinator.JavaTokenParsers + +import scala.util.parsing.combinator.JavaTokenParsers /** * @author gjoranv @@ -409,7 +410,7 @@ object ConfigGenerator { node match { case emptyName: CNode if node.getName.length == 0 => throw new CodegenRuntimeException("Node with empty name, under parent " + emptyName.getParent.getName) - case root: InnerCNode if root.getParent == null => createClassName(root.getName) + case root: InnerCNode if root.getParent == null => ConfiggenUtil.createClassName(root.getName) case b: BooleanLeaf => "BooleanNode" case d: DoubleLeaf => "DoubleNode" case f: FileLeaf => "FileNode" @@ -450,9 +451,10 @@ object ConfigGenerator { } /** - * Create class name from def name - * @param defName The file name without the '.def' suffix - */ + * Deprecated! + * TODO: Remove when no longer used in config-model + */ + @deprecated("Use ConfiggenUtil.createClassName() instead", "6.143") def createClassName(defName: String): String = { val className = defName.split("-").map (_.capitalize).mkString + "Config" val parser = new JavaTokenParsers {} diff --git a/configgen/src/main/scala/com/yahoo/config/codegen/JavaClassBuilder.scala b/configgen/src/main/scala/com/yahoo/config/codegen/JavaClassBuilder.scala index 518815d5a10..e03a6d3d04b 100644 --- a/configgen/src/main/scala/com/yahoo/config/codegen/JavaClassBuilder.scala +++ b/configgen/src/main/scala/com/yahoo/config/codegen/JavaClassBuilder.scala @@ -3,7 +3,8 @@ package com.yahoo.config.codegen import java.io.{File, FileNotFoundException, FileOutputStream, PrintStream} -import com.yahoo.config.codegen.ConfigGenerator.{createClassName, indentCode} +import com.yahoo.config.codegen.ConfigGenerator.indentCode +import com.yahoo.config.codegen.ConfiggenUtil.createClassName import com.yahoo.config.codegen.DefParser.DEFAULT_PACKAGE_PREFIX import scala.collection.JavaConverters._ |