Improve NonJavaCharsConverter
[mdsal.git] / binding2 / mdsal-binding2-generator-impl / src / main / java / org / opendaylight / mdsal / binding / javav2 / generator / impl / AuxiliaryGenUtils.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.mdsal.binding.javav2.generator.impl;
10
11 import static com.google.common.base.Preconditions.checkArgument;
12 import static org.opendaylight.mdsal.binding.javav2.generator.util.BindingGeneratorUtil.encodeAngleBrackets;
13 import static org.opendaylight.mdsal.binding.javav2.generator.util.Types.BOOLEAN;
14
15 import com.google.common.annotations.Beta;
16 import com.google.common.annotations.VisibleForTesting;
17 import com.google.common.base.Splitter;
18 import com.google.common.base.Strings;
19 import com.google.common.collect.Iterables;
20 import com.google.common.collect.Sets;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26 import java.util.regex.Pattern;
27 import org.opendaylight.mdsal.binding.javav2.generator.impl.txt.yangTemplateForModule;
28 import org.opendaylight.mdsal.binding.javav2.generator.impl.txt.yangTemplateForNode;
29 import org.opendaylight.mdsal.binding.javav2.generator.impl.txt.yangTemplateForNodes;
30 import org.opendaylight.mdsal.binding.javav2.generator.impl.util.YangTextTemplate;
31 import org.opendaylight.mdsal.binding.javav2.generator.spi.TypeProvider;
32 import org.opendaylight.mdsal.binding.javav2.generator.util.JavaIdentifier;
33 import org.opendaylight.mdsal.binding.javav2.generator.util.JavaIdentifierNormalizer;
34 import org.opendaylight.mdsal.binding.javav2.generator.util.Types;
35 import org.opendaylight.mdsal.binding.javav2.generator.util.generated.type.builder.GeneratedTOBuilderImpl;
36 import org.opendaylight.mdsal.binding.javav2.generator.yang.types.TypeProviderImpl;
37 import org.opendaylight.mdsal.binding.javav2.model.api.AccessModifier;
38 import org.opendaylight.mdsal.binding.javav2.model.api.Constant;
39 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
40 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.EnumBuilder;
41 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedPropertyBuilder;
42 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTOBuilder;
43 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilder;
44 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilderBase;
45 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.MethodSignatureBuilder;
46 import org.opendaylight.mdsal.binding.javav2.spec.runtime.BindingNamespaceType;
47 import org.opendaylight.yangtools.yang.common.QName;
48 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
49 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
50 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
51 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
52 import org.opendaylight.yangtools.yang.model.api.Module;
53 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
54 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
55 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
56 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
57 import org.opendaylight.yangtools.yang.model.api.Status;
58 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
59 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
60 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
61 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
62 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
63 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
64
65 /**
66  * Auxiliary util class for {@link GenHelperUtil} class
67  */
68 @Beta
69 final class AuxiliaryGenUtils {
70
71     private static final Splitter BSDOT_SPLITTER = Splitter.on("\\.");
72     private static final char NEW_LINE = '\n';
73     private static final Pattern UNICODE_CHAR_PATTERN = Pattern.compile("\\\\+u");
74
75     /**
76      * Constant with the concrete name of identifier.
77      */
78     private static final String AUGMENT_IDENTIFIER_NAME = "augment-identifier";
79
80     /**
81      * Constant with the concrete name of namespace.
82      */
83     private static final String YANG_EXT_NAMESPACE = "urn:opendaylight:yang:extension:yang-ext";
84
85     private AuxiliaryGenUtils() {
86         throw new UnsupportedOperationException("Util class");
87     }
88
89     static void annotateDeprecatedIfNecessary(final Status status, final GeneratedTypeBuilder builder) {
90         if (status == Status.DEPRECATED) {
91             builder.addAnnotation("", "Deprecated");
92         }
93     }
94
95     private static boolean hasBuilderClass(final SchemaNode schemaNode) {
96         if (schemaNode instanceof ContainerSchemaNode || schemaNode instanceof ListSchemaNode ||
97                 schemaNode instanceof RpcDefinition || schemaNode instanceof NotificationDefinition) {
98             return true;
99         }
100         return false;
101     }
102
103     static Constant qNameConstant(final GeneratedTypeBuilderBase<?> toBuilder, final String constantName,
104                                   final QName name) {
105         return toBuilder.addConstant(Types.typeForClass(QName.class), constantName, name);
106     }
107
108     /**
109      * Created a method signature builder as part of
110      * <code>interfaceBuilder</code>.
111      *
112      * The method signature builder is created for the getter method of
113      * <code>schemaNodeName</code>. Also <code>comment</code> and
114      * <code>returnType</code> information are added to the builder.
115      *
116      * @param interfaceBuilder
117      *            generated type builder for which the getter method should be
118      *            created
119      * @param schemaNodeName
120      *            string with schema node name. The name will be the part of the
121      *            getter method name.
122      * @param comment
123      *            string with comment for the getter method
124      * @param returnType
125      *            type which represents the return type of the getter method
126      * @param status
127      *            status from yang file, for deprecated annotation
128      * @return method signature builder which represents the getter method of
129      *         <code>interfaceBuilder</code>
130      */
131     static MethodSignatureBuilder constructGetter(final GeneratedTypeBuilder interfaceBuilder,
132                                                   final String schemaNodeName, final String comment, final Type returnType, final Status status) {
133
134         final MethodSignatureBuilder getMethod = interfaceBuilder
135                 .addMethod(getterMethodName(schemaNodeName, returnType));
136         if (status == Status.DEPRECATED) {
137             getMethod.addAnnotation("", "Deprecated");
138         }
139         getMethod.setComment(encodeAngleBrackets(comment));
140         getMethod.setReturnType(returnType);
141         return getMethod;
142     }
143
144     /**
145      * Creates the name of the getter method name from <code>localName</code>.
146      *
147      * @param localName
148      *            string with the name of the getter method
149      * @param returnType
150      *            return type
151      * @return string with the name of the getter method for
152      *         <code>methodName</code> in JAVA method format
153      */
154     private static String getterMethodName(final String localName, final Type returnType) {
155         final StringBuilder method = new StringBuilder();
156         if (BOOLEAN.equals(returnType)) {
157             method.append("is");
158         } else {
159             method.append("get");
160         }
161         // underscore used as separator for distinction of method parts in convertIdentifier()
162         method.append('_').append(localName);
163         return JavaIdentifierNormalizer.normalizeSpecificIdentifier(method.toString(), JavaIdentifier.METHOD);
164     }
165
166     static String createDescription(final SchemaNode schemaNode, final String fullyQualifiedName,
167                                     final SchemaContext schemaContext, final boolean verboseClassComments) {
168         final StringBuilder sb = new StringBuilder();
169         final String nodeDescription = encodeAngleBrackets(schemaNode.getDescription());
170         final String formattedDescription = YangTextTemplate.formatToParagraph(nodeDescription, 0);
171
172         if (!Strings.isNullOrEmpty(formattedDescription)) {
173             sb.append(formattedDescription);
174             sb.append(NEW_LINE);
175         }
176
177         if (verboseClassComments) {
178             final Module module = SchemaContextUtil.findParentModule(schemaContext, schemaNode);
179             final StringBuilder linkToBuilderClass = new StringBuilder();
180             final String[] namespace = Iterables.toArray(BSDOT_SPLITTER.split(fullyQualifiedName), String.class);
181             final String className = namespace[namespace.length - 1];
182
183             if (hasBuilderClass(schemaNode)) {
184                 linkToBuilderClass.append(className);
185                 linkToBuilderClass.append("Builder");
186             }
187
188             sb.append("<p>");
189             sb.append("This class represents the following YANG schema fragment defined in module <b>");
190             sb.append(module.getName());
191             sb.append("</b>");
192             sb.append(NEW_LINE);
193             sb.append("<pre>");
194             sb.append(NEW_LINE);
195             sb.append(encodeAngleBrackets(yangTemplateForNode.render(schemaNode, module).body()));
196             sb.append("</pre>");
197             sb.append(NEW_LINE);
198             sb.append("The schema path to identify an instance is");
199             sb.append(NEW_LINE);
200             sb.append("<i>");
201             sb.append(YangTextTemplate.formatSchemaPath(module.getName(), schemaNode.getPath().getPathFromRoot()));
202             sb.append("</i>");
203             sb.append(NEW_LINE);
204
205             if (hasBuilderClass(schemaNode)) {
206                 sb.append(NEW_LINE);
207                 sb.append("<p>To create instances of this class use " + "{@link " + linkToBuilderClass + "}.");
208                 sb.append(NEW_LINE);
209                 sb.append("@see ");
210                 sb.append(linkToBuilderClass);
211                 sb.append(NEW_LINE);
212                 if (schemaNode instanceof ListSchemaNode) {
213                     final List<QName> keyDef = ((ListSchemaNode)schemaNode).getKeyDefinition();
214                     if (keyDef != null && !keyDef.isEmpty()) {
215                         sb.append("@see ");
216                         sb.append(className);
217                         sb.append("Key");
218                     }
219                     sb.append(NEW_LINE);
220                 }
221             }
222         }
223
224         return replaceAllIllegalChars(sb);
225     }
226
227     static String createDescription(final Module module, final boolean verboseClassComments) {
228         final StringBuilder sb = new StringBuilder();
229         final String moduleDescription = encodeAngleBrackets(module.getDescription());
230         final String formattedDescription = YangTextTemplate.formatToParagraph(moduleDescription, 0);
231
232         if (!Strings.isNullOrEmpty(formattedDescription)) {
233             sb.append(formattedDescription);
234             sb.append(NEW_LINE);
235         }
236
237         if (verboseClassComments) {
238             sb.append("<p>");
239             sb.append("This class represents the following YANG schema fragment defined in module <b>");
240             sb.append(module.getName());
241             sb.append("</b>");
242             sb.append(NEW_LINE);
243             sb.append("<pre>");
244             sb.append(NEW_LINE);
245             sb.append(encodeAngleBrackets(yangTemplateForModule.render(module).body()));
246             sb.append("</pre>");
247         }
248
249         return replaceAllIllegalChars(sb);
250     }
251
252     static String createDescription(final Set<? extends SchemaNode> schemaNodes, final Module module, final
253             boolean verboseClassComments) {
254         final StringBuilder sb = new StringBuilder();
255
256         if (!isNullOrEmpty(schemaNodes)) {
257             final SchemaNode node = schemaNodes.iterator().next();
258
259             if (node instanceof RpcDefinition) {
260                 sb.append("Interface for implementing the following YANG RPCs defined in module <b>" + module.getName() + "</b>");
261             } else if (node instanceof NotificationDefinition) {
262                 sb.append("Interface for receiving the following YANG notifications defined in module <b>" + module.getName() + "</b>");
263             }
264         }
265         sb.append(NEW_LINE);
266
267         if (verboseClassComments) {
268             sb.append("<pre>");
269             sb.append(NEW_LINE);
270             sb.append(encodeAngleBrackets(yangTemplateForNodes.render(schemaNodes, module).body()));
271             sb.append("</pre>");
272             sb.append(NEW_LINE);
273         }
274
275         return replaceAllIllegalChars(sb);
276     }
277
278     private static boolean isNullOrEmpty(final Collection<?> list) {
279         return list == null || list.isEmpty();
280     }
281
282     /**
283      * Returns first unique name for the augment generated type builder. The
284      * generated type builder name for augment consists from name of augmented
285      * node and serial number of its augmentation.
286      *
287      * @param builders
288      *            map of builders which were created in the package to which the
289      *            augmentation belongs
290      * @param genTypeName
291      *            string with name of augmented node
292      * @return string with unique name for augmentation builder
293      */
294     static String augGenTypeName(final Map<String, GeneratedTypeBuilder> builders, final String genTypeName) {
295         int index = 1;
296         if (builders != null) {
297             while (builders.containsKey(genTypeName + index)) {
298                 index = index + 1;
299             }
300         }
301         return genTypeName + index;
302     }
303
304     /**
305      * @param unknownSchemaNodes unknows schema nodes
306      * @return nodeParameter of UnknownSchemaNode
307      */
308     static String getAugmentIdentifier(final List<UnknownSchemaNode> unknownSchemaNodes) {
309         for (final UnknownSchemaNode unknownSchemaNode : unknownSchemaNodes) {
310             final QName nodeType = unknownSchemaNode.getNodeType();
311             if (AUGMENT_IDENTIFIER_NAME.equals(nodeType.getLocalName())
312                     && YANG_EXT_NAMESPACE.equals(nodeType.getNamespace().toString())) {
313                 return unknownSchemaNode.getNodeParameter();
314             }
315         }
316         return null;
317     }
318
319     /**
320      * Adds enumeration builder created from <code>enumTypeDef</code> to
321      * <code>typeBuilder</code>.
322      *
323      * Each <code>enumTypeDef</code> item is added to builder with its name and
324      * value.
325      *
326      * @param enumTypeDef
327      *            EnumTypeDefinition contains enum data
328      * @param enumName
329      *            string contains name which will be assigned to enumeration
330      *            builder
331      * @param typeBuilder
332      *            GeneratedTypeBuilder to which will be enum builder assigned
333      * @param module
334      *            Module in which type should be generated
335      * @return enumeration builder which contains data from
336      *         <code>enumTypeDef</code>
337      */
338     static EnumBuilder resolveInnerEnumFromTypeDefinition(final EnumTypeDefinition enumTypeDef, final QName enumName,
339         final Map<Module, ModuleContext> genCtx, final GeneratedTypeBuilder typeBuilder, final Module module) {
340         if (enumTypeDef != null && typeBuilder != null && enumTypeDef.getQName().getLocalName() != null) {
341             final EnumBuilder enumBuilder = typeBuilder.addEnumeration(enumName.getLocalName());
342             final String enumTypedefDescription = encodeAngleBrackets(enumTypeDef.getDescription());
343             enumBuilder.setDescription(enumTypedefDescription);
344             enumBuilder.updateEnumPairsFromEnumTypeDef(enumTypeDef);
345             final ModuleContext ctx = genCtx.get(module);
346             ctx.addInnerTypedefType(enumTypeDef.getPath(), enumBuilder);
347             return enumBuilder;
348         }
349         return null;
350     }
351
352
353     /**
354      * Builds generated TO builders for <code>typeDef</code> of type
355      * {@link UnionTypeDefinition} or {@link BitsTypeDefinition} which are
356      * also added to <code>typeBuilder</code> as enclosing transfer object.
357      *
358      * If more then one generated TO builder is created for enclosing then all
359      * of the generated TO builders are added to <code>typeBuilder</code> as
360      * enclosing transfer objects.
361      *
362      * @param typeDef
363      *            type definition which can be of type <code>UnionType</code> or
364      *            <code>BitsTypeDefinition</code>
365      * @param typeBuilder
366      *            generated type builder to which is added generated TO created
367      *            from <code>typeDef</code>
368      * @param leaf
369      *            string with name for generated TO builder
370      * @param parentModule
371      *            parent module
372      * @return generated TO builder for <code>typeDef</code>
373      */
374     static GeneratedTOBuilder addTOToTypeBuilder(final TypeDefinition<?> typeDef, final GeneratedTypeBuilder
375             typeBuilder, final DataSchemaNode leaf, final Module parentModule, final TypeProvider typeProvider,
376             final SchemaContext schemaContext) {
377         final String classNameFromLeaf = leaf.getQName().getLocalName();
378         final List<GeneratedTOBuilder> genTOBuilders = new ArrayList<>();
379         final String packageName = typeBuilder.getFullyQualifiedName();
380         if (typeDef instanceof UnionTypeDefinition) {
381             final List<GeneratedTOBuilder> types = ((TypeProviderImpl) typeProvider)
382                     .provideGeneratedTOBuildersForUnionTypeDef(packageName, ((UnionTypeDefinition) typeDef),
383                             classNameFromLeaf, leaf, schemaContext, ((TypeProviderImpl) typeProvider).getGenTypeDefsContextMap());
384             genTOBuilders.addAll(types);
385
386             GeneratedTOBuilder resultTOBuilder;
387             if (types.isEmpty()) {
388                 throw new IllegalStateException("No GeneratedTOBuilder objects generated from union " + typeDef);
389             }
390             resultTOBuilder = types.remove(0);
391             for (final GeneratedTOBuilder genTOBuilder : types) {
392                 resultTOBuilder.addEnclosingTransferObject(genTOBuilder);
393             }
394
395             final GeneratedPropertyBuilder genPropBuilder = resultTOBuilder.addProperty("value");
396             genPropBuilder.setReturnType(Types.CHAR_ARRAY);
397             resultTOBuilder.addEqualsIdentity(genPropBuilder);
398             resultTOBuilder.addHashIdentity(genPropBuilder);
399             resultTOBuilder.addToStringProperty(genPropBuilder);
400
401         } else if (typeDef instanceof BitsTypeDefinition) {
402             genTOBuilders.add((((TypeProviderImpl) typeProvider)).provideGeneratedTOBuilderForBitsTypeDefinition(
403                     packageName, typeDef, classNameFromLeaf, parentModule.getName()));
404         }
405         if (!genTOBuilders.isEmpty()) {
406             for (final GeneratedTOBuilder genTOBuilder : genTOBuilders) {
407                 typeBuilder.addEnclosingTransferObject(genTOBuilder);
408             }
409             return genTOBuilders.get(0);
410         }
411         return null;
412
413     }
414
415     @SuppressWarnings({ "unchecked", "rawtypes" })
416     static Type createReturnTypeForUnion(final GeneratedTOBuilder genTOBuilder, final TypeDefinition<?> typeDef,
417             final GeneratedTypeBuilder typeBuilder, final Module parentModule, final TypeProvider typeProvider) {
418         final GeneratedTOBuilderImpl returnType = new GeneratedTOBuilderImpl(genTOBuilder.getPackageName(),
419                 genTOBuilder.getName());
420         final String typedefDescription = encodeAngleBrackets(typeDef.getDescription());
421
422         returnType.setDescription(typedefDescription);
423         returnType.setReference(typeDef.getReference());
424         returnType.setSchemaPath((List) typeDef.getPath().getPathFromRoot());
425         returnType.setModuleName(parentModule.getName());
426
427         genTOBuilder.setTypedef(true);
428         genTOBuilder.setIsUnion(true);
429         TypeProviderImpl.addUnitsToGenTO(genTOBuilder, typeDef.getUnits());
430
431
432
433         final GeneratedTOBuilder unionBuilder = createUnionBuilder(genTOBuilder,typeBuilder);
434
435
436         final MethodSignatureBuilder method = unionBuilder.addMethod("getDefaultInstance");
437         method.setReturnType(returnType);
438         method.addParameter(Types.STRING, "defaultValue");
439         method.setAccessModifier(AccessModifier.PUBLIC);
440         method.setStatic(true);
441
442         final Set<Type> types = ((TypeProviderImpl) typeProvider).getAdditionalTypes().get(parentModule);
443         if (types == null) {
444             ((TypeProviderImpl) typeProvider).getAdditionalTypes().put(parentModule,
445                     Sets.newHashSet(unionBuilder.toInstance()));
446         } else {
447             types.add(unionBuilder.toInstance());
448         }
449         return returnType.toInstance();
450     }
451
452     private static GeneratedTOBuilder createUnionBuilder(final GeneratedTOBuilder genTOBuilder, final GeneratedTypeBuilder typeBuilder) {
453         final String outerCls = Types.getOuterClassName(genTOBuilder);
454         final StringBuilder name;
455         if (outerCls != null) {
456             name = new StringBuilder(outerCls);
457         } else {
458             name = new StringBuilder();
459         }
460         name.append(genTOBuilder.getName());
461         name.append("Builder");
462         final GeneratedTOBuilderImpl unionBuilder = new GeneratedTOBuilderImpl(typeBuilder.getPackageName(),name.toString());
463         unionBuilder.setIsUnionBuilder(true);
464         return unionBuilder;
465     }
466
467     static boolean isInnerType(final LeafSchemaNode leaf, final TypeDefinition<?> type) {
468         if (leaf.getPath().equals(type.getPath())) {
469             return true;
470         }
471         if (leaf.getPath().equals(type.getPath().getParent())) {
472             return true;
473         }
474
475         return false;
476     }
477
478     /**
479      * Generates for the <code>list</code> which contains any list keys special
480      * generated TO builder.
481      *
482      * @param packageName
483      *            string with package name to which the list belongs
484      * @param list
485      *            schema node of list
486      * @return generated TO builder which represents the keys of the
487      *         <code>list</code> or empty TO builder if <code>list</code> is null or list of
488      *         key definitions is null or empty.
489      */
490     static GeneratedTOBuilder resolveListKeyTOBuilder(final String packageName, final ListSchemaNode list) {
491         GeneratedTOBuilder genTOBuilder = null;
492         if ((list.getKeyDefinition() != null) && (!list.getKeyDefinition().isEmpty())) {
493             // underscore used as separator for distinction of class name parts
494             final String genTOName =
495                     new StringBuilder(list.getQName().getLocalName()).append('_').append(BindingNamespaceType.Key)
496                             .toString();
497             genTOBuilder =
498                     new GeneratedTOBuilderImpl(new StringBuilder(packageName).append(".wrapper").toString(), genTOName);
499         }
500         return genTOBuilder;
501     }
502
503     /**
504      * Converts <code>leaf</code> schema node to property of generated TO
505      * builder.
506      *
507      * @param toBuilder
508      *            generated TO builder to which is <code>leaf</code> added as
509      *            property
510      * @param leaf
511      *            leaf schema node which is added to <code>toBuilder</code> as
512      *            property
513      * @param returnType
514      *            property type
515      * @param isReadOnly
516      *            boolean value which says if leaf property is|isn't read only
517      * @return boolean value
518      *         <ul>
519      *         <li>false - if <code>leaf</code>, <code>toBuilder</code> or leaf
520      *         name equals null or if leaf is added by <i>uses</i>.</li>
521      *         <li>true - other cases</li>
522      *         </ul>
523      */
524     static boolean resolveLeafSchemaNodeAsProperty(final GeneratedTOBuilder toBuilder, final LeafSchemaNode leaf,
525         final Type returnType, final boolean isReadOnly) {
526
527         if (returnType == null) {
528             return false;
529         }
530         final String leafName = leaf.getQName().getLocalName();
531         final String leafDesc = encodeAngleBrackets(leaf.getDescription());
532         final GeneratedPropertyBuilder propBuilder =
533                 toBuilder.addProperty(JavaIdentifierNormalizer.normalizeSpecificIdentifier(leafName, JavaIdentifier.METHOD));
534         propBuilder.setReadOnly(isReadOnly);
535         propBuilder.setReturnType(returnType);
536         propBuilder.setComment(leafDesc);
537         toBuilder.addEqualsIdentity(propBuilder);
538         toBuilder.addHashIdentity(propBuilder);
539         toBuilder.addToStringProperty(propBuilder);
540         return true;
541     }
542
543     static void checkModuleAndModuleName(final Module module) {
544         checkArgument(module != null, "Module reference cannot be NULL.");
545         checkArgument(module.getName() != null, "Module name cannot be NULL.");
546     }
547
548     @VisibleForTesting
549     public static String replaceAllIllegalChars(final StringBuilder stringBuilder){
550         final String ret = UNICODE_CHAR_PATTERN.matcher(stringBuilder).replaceAll("\\\\\\\\u");
551         return ret.isEmpty() ? "" : ret;
552     }
553 }