2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.java.api.generator;
10 import static org.opendaylight.controller.sal.java.api.generator.Constants.*;
12 import java.util.ArrayList;
13 import java.util.LinkedHashMap;
14 import java.util.List;
17 import org.opendaylight.controller.binding.generator.util.TypeConstants;
18 import org.opendaylight.controller.sal.binding.model.api.*;
19 import org.opendaylight.controller.binding.generator.util.Types;
20 import org.opendaylight.controller.sal.binding.model.api.Enumeration.Pair;
21 import org.opendaylight.controller.sal.binding.model.api.MethodSignature.Parameter;
23 public final class GeneratorUtil {
25 private GeneratorUtil() {
28 public static String createIfcDeclaration(final GeneratedType genType, final String indent,
29 final Map<String, String> availableImports) {
30 return createFileDeclaration(IFC, genType, indent, availableImports, false);
33 public static String createClassDeclaration(final GeneratedTransferObject genTransferObject, final String indent,
34 final Map<String, String> availableImports, boolean isIdentity) {
35 return createFileDeclaration(CLASS, genTransferObject, indent, availableImports, isIdentity);
38 public static String createPackageDeclaration(final String packageName) {
39 return PKG + GAP + packageName + SC;
42 private static String createFileDeclaration(final String type, final GeneratedType genType, final String indent,
43 final Map<String, String> availableImports, boolean isIdentity) {
44 final StringBuilder builder = new StringBuilder();
45 final String currentPkg = genType.getPackageName();
47 createComment(builder, genType.getComment(), indent);
49 if (!genType.getAnnotations().isEmpty()) {
50 final List<AnnotationType> annotations = genType.getAnnotations();
51 appendAnnotations(builder, annotations);
56 if (!(CLASS.equals(type))) {
57 throw new IllegalArgumentException("'identity' has to be generated as a class");
59 builder.append(PUBLIC + GAP + ABSTRACT + GAP + type + GAP + genType.getName() + GAP);
61 builder.append(PUBLIC + GAP + type + GAP + genType.getName() + GAP);
64 if (genType instanceof GeneratedTransferObject) {
65 GeneratedTransferObject genTO = (GeneratedTransferObject) genType;
67 if (genTO.getExtends() != null) {
68 builder.append(EXTENDS + GAP);
69 String gtoString = getExplicitType(genTO.getExtends(), availableImports, currentPkg);
70 builder.append(gtoString + GAP);
74 final List<Type> genImplements = genType.getImplements();
75 if (!genImplements.isEmpty()) {
76 if (genType instanceof GeneratedTransferObject) {
77 builder.append(IMPLEMENTS + GAP);
79 builder.append(EXTENDS + GAP);
81 builder.append(getExplicitType(genImplements.get(0), availableImports, currentPkg));
83 for (int i = 1; i < genImplements.size(); ++i) {
85 builder.append(getExplicitType(genImplements.get(i), availableImports, currentPkg));
88 builder.append(GAP + LCB);
89 return builder.toString();
92 private static StringBuilder appendAnnotations(final StringBuilder builder, final List<AnnotationType> annotations) {
93 if ((builder != null) && (annotations != null)) {
94 for (final AnnotationType annotation : annotations) {
96 builder.append(annotation.getPackageName());
98 builder.append(annotation.getName());
100 if (annotation.containsParameters()) {
102 final List<AnnotationType.Parameter> parameters = annotation.getParameters();
103 appendAnnotationParams(builder, parameters);
111 private static StringBuilder appendAnnotationParams(final StringBuilder builder,
112 final List<AnnotationType.Parameter> parameters) {
113 if (parameters != null) {
115 for (final AnnotationType.Parameter param : parameters) {
120 builder.append(", ");
122 final String paramName = param.getName();
123 if (param.getValue() != null) {
124 builder.append(paramName);
125 builder.append(" = ");
126 builder.append(param.getValue());
128 builder.append(paramName);
129 builder.append(" = {");
130 final List<String> values = param.getValues();
131 builder.append(values.get(0));
132 for (int j = 1; j < values.size(); ++j) {
133 builder.append(", ");
134 builder.append(values.get(j));
144 public static String createConstant(final Constant constant, final String indent,
145 final Map<String, String> availableImports, final String currentPkg) {
146 final StringBuilder builder = new StringBuilder();
147 if (constant == null)
148 throw new IllegalArgumentException();
149 builder.append(indent + PUBLIC + GAP + STATIC + GAP + FINAL + GAP);
150 builder.append(getExplicitType(constant.getType(), availableImports, currentPkg) + GAP + constant.getName());
151 builder.append(GAP + "=" + GAP);
152 final Object constValue = constant.getValue();
154 if (constant.getName().equals(TypeConstants.PATTERN_CONSTANT_NAME)) {
155 if (constant.getName() == null || constant.getType() == null || constant.getValue() == null)
156 throw new IllegalArgumentException();
157 if (constValue instanceof List) {
158 builder.append("Arrays.asList" + LB);
159 final List<?> constantValues = (List<?>) constValue;
160 int stringsCount = 0;
161 for (Object value : constantValues) {
162 if (value instanceof String) {
163 if (stringsCount > 0) {
164 builder.append(COMMA);
167 builder.append(DOUBLE_QUOTE + (String) value + DOUBLE_QUOTE);
173 builder.append(constant.getValue());
177 return builder.toString();
180 public static String createField(final GeneratedProperty property, final String indent,
181 final Map<String, String> availableImports, final String currentPkg) {
182 final StringBuilder builder = new StringBuilder();
183 if (!property.getAnnotations().isEmpty()) {
184 final List<AnnotationType> annotations = property.getAnnotations();
185 appendAnnotations(builder, annotations);
188 builder.append(indent + PRIVATE + GAP);
189 builder.append(getExplicitType(property.getReturnType(), availableImports, currentPkg) + GAP
190 + property.getName());
192 return builder.toString();
196 * Create method declaration in interface.
202 public static String createMethodDeclaration(final MethodSignature method, final String indent,
203 Map<String, String> availableImports, final String currentPkg) {
204 final StringBuilder builder = new StringBuilder();
206 if (method == null) {
207 throw new IllegalArgumentException("Method Signature parameter MUST be specified and cannot be NULL!");
210 final String comment = method.getComment();
211 final String name = method.getName();
213 throw new IllegalStateException("Method Name cannot be NULL!");
216 final Type type = method.getReturnType();
218 throw new IllegalStateException("Method Return type cannot be NULL!");
221 final List<Parameter> parameters = method.getParameters();
223 createComment(builder, comment, indent);
225 builder.append(indent);
227 if (!method.getAnnotations().isEmpty()) {
228 final List<AnnotationType> annotations = method.getAnnotations();
229 appendAnnotations(builder, annotations);
233 builder.append(indent + getExplicitType(type, availableImports, currentPkg) + GAP + name);
235 for (int i = 0; i < parameters.size(); i++) {
236 Parameter p = parameters.get(i);
237 String separator = COMMA;
238 if (i + 1 == parameters.size()) {
241 builder.append(getExplicitType(p.getType(), availableImports, currentPkg) + GAP + p.getName() + separator);
246 return builder.toString();
249 public static String createConstructor(GeneratedTransferObject genTransferObject, final String indent,
250 final Map<String, String> availableImports, boolean isIdentity) {
251 final StringBuilder builder = new StringBuilder();
253 final String currentPkg = genTransferObject.getPackageName();
254 final List<GeneratedProperty> properties = genTransferObject.getProperties();
255 final List<GeneratedProperty> ctorParams = new ArrayList<GeneratedProperty>();
256 if (properties != null) {
257 for (final GeneratedProperty property : properties) {
258 if (property.isReadOnly()) {
259 ctorParams.add(property);
264 builder.append(indent);
265 builder.append(isIdentity ? PROTECTED : PUBLIC);
267 builder.append(genTransferObject.getName());
270 if (!ctorParams.isEmpty()) {
271 builder.append(getExplicitType(ctorParams.get(0).getReturnType(), availableImports, currentPkg));
273 builder.append(ctorParams.get(0).getName());
274 for (int i = 1; i < ctorParams.size(); ++i) {
275 final GeneratedProperty param = ctorParams.get(i);
276 builder.append(", ");
277 builder.append(getExplicitType(param.getReturnType(), availableImports, currentPkg));
279 builder.append(param.getName());
282 builder.append(RB + GAP + LCB + NL + indent + TAB + "super();" + NL);
283 if (!ctorParams.isEmpty()) {
284 for (final GeneratedProperty property : ctorParams) {
285 builder.append(indent);
287 builder.append("this.");
288 builder.append(property.getName());
289 builder.append(" = ");
290 builder.append(property.getName());
295 List<Constant> consts = genTransferObject.getConstantDefinitions();
296 for (Constant con : consts) {
297 if (con.getName() == null || con.getType() == null || con.getValue() == null)
299 if (con.getName().equals(TypeConstants.PATTERN_CONSTANT_NAME)) {
300 Object values = con.getValue();
301 if (values instanceof List) {
302 for (Object regEx : (List<?>) values) {
303 if (regEx instanceof String) {
304 builder.append(indent + TAB + "for (String regEx : " + TypeConstants.PATTERN_CONSTANT_NAME
306 builder.append(indent + TAB + TAB + "this." + MEMBER_PATTERN_LIST
307 + ".add(Pattern.compile(regEx))" + SC + NL);
308 builder.append(indent + TAB + RCB + NL);
319 builder.append(indent);
321 return builder.toString();
324 public static String createGetter(final GeneratedProperty property, final String indent,
325 final Map<String, String> availableImports, final String currentPkg) {
326 final StringBuilder builder = new StringBuilder();
328 final Type type = property.getReturnType();
329 final String varName = property.getName();
330 final char first = Character.toUpperCase(varName.charAt(0));
331 final String methodName = "get" + first + varName.substring(1);
333 builder.append(indent + PUBLIC + GAP + getExplicitType(type, availableImports, currentPkg) + GAP + methodName);
334 builder.append(LB + RB + LCB + NL);
336 String currentIndent = indent + TAB;
338 builder.append(currentIndent + "return " + varName + SC + NL);
340 builder.append(indent + RCB);
341 return builder.toString();
344 public static String createSetter(final GeneratedProperty property, final String indent,
345 final Map<String, String> availableImports, final String currentPkg) {
346 final StringBuilder builder = new StringBuilder();
348 final Type type = property.getReturnType();
349 final String varName = property.getName();
350 final char first = Character.toUpperCase(varName.charAt(0));
351 final String methodName = "set" + first + varName.substring(1);
353 builder.append(indent + PUBLIC + GAP + "void" + GAP + methodName);
354 builder.append(LB + getExplicitType(type, availableImports, currentPkg) + GAP + varName + RB + LCB + NL);
355 String currentIndent = indent + TAB;
356 builder.append(currentIndent + "this." + varName + " = " + varName + SC + NL);
357 builder.append(indent + RCB);
358 return builder.toString();
361 public static String createHashCode(final List<GeneratedProperty> properties, final String indent) {
362 StringBuilder builder = new StringBuilder();
363 builder.append(indent + "public int hashCode() {" + NL);
364 builder.append(indent + TAB + "final int prime = 31;" + NL);
365 builder.append(indent + TAB + "int result = 1;" + NL);
367 for (GeneratedProperty property : properties) {
368 String fieldName = property.getName();
369 builder.append(indent + TAB + "result = prime * result + ((" + fieldName + " == null) ? 0 : " + fieldName
370 + ".hashCode());" + NL);
373 builder.append(indent + TAB + "return result;" + NL);
374 builder.append(indent + RCB + NL);
375 return builder.toString();
378 public static String createEquals(final GeneratedTransferObject type, final List<GeneratedProperty> properties,
379 final String indent) {
380 final StringBuilder builder = new StringBuilder();
381 final String indent1 = indent + TAB;
382 final String indent2 = indent1 + TAB;
383 final String indent3 = indent2 + TAB;
385 builder.append(indent + "public boolean equals(Object obj) {" + NL);
386 builder.append(indent1 + "if (this == obj) {" + NL);
387 builder.append(indent2 + "return true;" + NL);
388 builder.append(indent1 + "}" + NL);
389 builder.append(indent1 + "if (obj == null) {" + NL);
390 builder.append(indent2 + "return false;" + NL);
391 builder.append(indent1 + "}" + NL);
392 builder.append(indent1 + "if (getClass() != obj.getClass()) {" + NL);
393 builder.append(indent2 + "return false;" + NL);
394 builder.append(indent1 + "}" + NL);
396 String typeStr = type.getName();
397 builder.append(indent1 + typeStr + " other = (" + typeStr + ") obj;" + NL);
399 for (final GeneratedProperty property : properties) {
400 String fieldName = property.getName();
401 builder.append(indent1 + "if (" + fieldName + " == null) {" + NL);
402 builder.append(indent2 + "if (other." + fieldName + " != null) {" + NL);
403 builder.append(indent3 + "return false;" + NL);
404 builder.append(indent2 + "}" + NL);
405 builder.append(indent1 + "} else if (!" + fieldName + ".equals(other." + fieldName + ")) {" + NL);
406 builder.append(indent2 + "return false;" + NL);
407 builder.append(indent1 + "}" + NL);
410 builder.append(indent1 + "return true;" + NL);
411 builder.append(indent + RCB + NL);
412 return builder.toString();
415 public static String createToString(final GeneratedTransferObject type, final List<GeneratedProperty> properties,
416 final String indent) {
417 StringBuilder builder = new StringBuilder();
418 builder.append(indent);
419 builder.append("public String toString() {");
421 builder.append(indent);
423 builder.append("StringBuilder builder = new StringBuilder();");
425 builder.append(indent);
427 builder.append("builder.append(\"");
428 builder.append(type.getName());
429 builder.append(" [");
431 boolean first = true;
432 for (final GeneratedProperty property : properties) {
434 builder.append(property.getName());
435 builder.append("=\");");
437 builder.append(indent);
439 builder.append("builder.append(");
440 builder.append(property.getName());
441 builder.append(");");
445 builder.append(indent);
447 builder.append("builder.append(\", ");
448 builder.append(property.getName());
449 builder.append("=\");");
451 builder.append(indent);
453 builder.append("builder.append(");
454 builder.append(property.getName());
455 builder.append(");");
459 builder.append(indent);
461 builder.append("builder.append(\"]\");");
463 builder.append(indent);
465 builder.append("return builder.toString();");
468 builder.append(indent);
471 return builder.toString();
474 public static String createEnum(final Enumeration enumeration, final String indent) {
475 if (enumeration == null || indent == null)
476 throw new IllegalArgumentException();
477 final StringBuilder builder = new StringBuilder(indent + PUBLIC + GAP + ENUM + GAP + enumeration.getName()
480 String separator = COMMA + NL;
481 final List<Pair> values = enumeration.getValues();
483 for (int i = 0; i < values.size(); i++) {
484 if (i + 1 == values.size()) {
487 builder.append(indent + TAB + values.get(i).getName() + LB + values.get(i).getValue() + RB + separator);
491 final String ENUMERATION_NAME = "value";
492 final String ENUMERATION_TYPE = "int";
493 builder.append(indent + TAB + ENUMERATION_TYPE + GAP + ENUMERATION_NAME + SC);
495 builder.append(indent + TAB + PRIVATE + GAP + enumeration.getName() + LB + ENUMERATION_TYPE + GAP
496 + ENUMERATION_NAME + RB + GAP + LCB + NL);
497 builder.append(indent + TAB + TAB + "this." + ENUMERATION_NAME + GAP + "=" + GAP + ENUMERATION_NAME + SC + NL);
498 builder.append(indent + TAB + RCB + NL);
500 builder.append(indent + RCB);
502 return builder.toString();
505 private static String getExplicitType(final Type type, final Map<String, String> imports, final String currentPkg) {
507 throw new IllegalArgumentException("Type parameter MUST be specified and cannot be NULL!");
509 if (type.getName() == null) {
510 throw new IllegalArgumentException("Type name cannot be NULL!");
512 if (type.getPackageName() == null) {
513 throw new IllegalArgumentException("Type cannot have Package Name referenced as NULL!");
515 if (imports == null) {
516 throw new IllegalArgumentException("Imports Map cannot be NULL!");
519 final String typePackageName = type.getPackageName();
520 final String typeName = type.getName();
521 final String importedPackageName = imports.get(typeName);
522 if (typePackageName.equals(importedPackageName) || typePackageName.equals(currentPkg)) {
523 final StringBuilder builder = new StringBuilder(type.getName());
524 if (type instanceof ParameterizedType) {
525 final ParameterizedType pType = (ParameterizedType) type;
526 final Type[] pTypes = pType.getActualTypeArguments();
528 builder.append(getParameters(pTypes, imports, currentPkg));
531 if (builder.toString().equals("Void")) {
534 return builder.toString();
536 final StringBuilder builder = new StringBuilder();
537 if (typePackageName.startsWith("java.lang")) {
538 builder.append(type.getName());
540 if (!typePackageName.isEmpty()) {
541 builder.append(typePackageName + "." + type.getName());
543 builder.append(type.getName());
546 if (type instanceof ParameterizedType) {
547 final ParameterizedType pType = (ParameterizedType) type;
548 final Type[] pTypes = pType.getActualTypeArguments();
550 builder.append(getParameters(pTypes, imports, currentPkg));
553 if (builder.toString().equals("Void")) {
556 return builder.toString();
560 private static String getParameters(final Type[] pTypes, Map<String, String> availableImports, String currentPkg) {
561 final StringBuilder builder = new StringBuilder();
562 for (int i = 0; i < pTypes.length; i++) {
563 final Type t = pTypes[i];
565 String separator = COMMA;
566 if (i == (pTypes.length - 1)) {
570 String wildcardParam = "";
571 if (t instanceof WildcardType) {
572 wildcardParam = "? extends ";
574 builder.append(wildcardParam + getExplicitType(t, availableImports, currentPkg) + separator);
576 return builder.toString();
579 private static void createComment(final StringBuilder builder, final String comment, final String indent) {
580 if (comment != null && comment.length() > 0) {
581 builder.append(indent + "/*" + NL);
582 builder.append(indent + comment + NL);
583 builder.append(indent + "*/" + NL);
587 public static Map<String, String> createImports(final GeneratedType genType) {
588 if (genType == null) {
589 throw new IllegalArgumentException("Generated Type cannot be NULL!");
592 final Map<String, String> imports = new LinkedHashMap<>();
593 final List<Constant> constants = genType.getConstantDefinitions();
594 final List<MethodSignature> methods = genType.getMethodDefinitions();
595 final List<Type> impl = genType.getImplements();
599 for (final Type type : impl) {
600 putTypeIntoImports(genType, type, imports);
605 if (constants != null) {
606 for (final Constant constant : constants) {
607 final Type constantType = constant.getType();
608 putTypeIntoImports(genType, constantType, imports);
612 // REGULAR EXPRESSION
613 if (genType instanceof GeneratedTransferObject) {
614 if (isConstantInTO(TypeConstants.PATTERN_CONSTANT_NAME, (GeneratedTransferObject) genType)) {
615 putTypeIntoImports(genType, Types.typeForClass(java.util.regex.Pattern.class), imports);
616 putTypeIntoImports(genType, Types.typeForClass(java.util.Arrays.class), imports);
617 putTypeIntoImports(genType, Types.typeForClass(java.util.ArrayList.class), imports);
622 if (methods != null) {
623 for (final MethodSignature method : methods) {
624 final Type methodReturnType = method.getReturnType();
625 putTypeIntoImports(genType, methodReturnType, imports);
626 for (final MethodSignature.Parameter methodParam : method.getParameters()) {
627 putTypeIntoImports(genType, methodParam.getType(), imports);
633 if (genType instanceof GeneratedTransferObject) {
634 final GeneratedTransferObject genTO = (GeneratedTransferObject) genType;
635 final List<GeneratedProperty> properties = genTO.getProperties();
636 if (properties != null) {
637 for (GeneratedProperty property : properties) {
638 final Type propertyType = property.getReturnType();
639 putTypeIntoImports(genType, propertyType, imports);
647 private static void putTypeIntoImports(final GeneratedType parentGenType, final Type type,
648 final Map<String, String> imports) {
649 if (parentGenType == null) {
650 throw new IllegalArgumentException("Parent Generated Type parameter MUST be specified and cannot be "
653 if (parentGenType.getName() == null) {
654 throw new IllegalArgumentException("Parent Generated Type name cannot be NULL!");
656 if (parentGenType.getPackageName() == null) {
657 throw new IllegalArgumentException("Parent Generated Type cannot have Package Name referenced as NULL!");
660 throw new IllegalArgumentException("Type parameter MUST be specified and cannot be NULL!");
662 if (type.getName() == null) {
663 throw new IllegalArgumentException("Type name cannot be NULL!");
665 if (type.getPackageName() == null) {
666 throw new IllegalArgumentException("Type cannot have Package Name referenced as NULL!");
669 final String typeName = type.getName();
670 final String typePackageName = type.getPackageName();
671 final String parentTypeName = parentGenType.getName();
672 final String parentTypePackageName = parentGenType.getPackageName();
673 if (typeName.equals(parentTypeName) || typePackageName.startsWith("java.lang")
674 || typePackageName.equals(parentTypePackageName) || typePackageName.isEmpty()) {
677 if (!imports.containsKey(typeName)) {
678 imports.put(typeName, typePackageName);
680 if (type instanceof ParameterizedType) {
681 final ParameterizedType paramType = (ParameterizedType) type;
682 final Type[] params = paramType.getActualTypeArguments();
683 for (Type param : params) {
684 putTypeIntoImports(parentGenType, param, imports);
689 public static List<String> createImportLines(final Map<String, String> imports) {
690 final List<String> importLines = new ArrayList<>();
692 for (Map.Entry<String, String> entry : imports.entrySet()) {
693 final String typeName = entry.getKey();
694 final String packageName = entry.getValue();
695 importLines.add("import " + packageName + "." + typeName + SC);
700 public static boolean isConstantInTO(String constName, GeneratedTransferObject genTO) {
701 if (constName == null || genTO == null)
702 throw new IllegalArgumentException();
703 List<Constant> consts = genTO.getConstantDefinitions();
704 for (Constant cons : consts) {
705 if (cons.getName().equals(constName)) {