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.HashMap;
14 import java.util.LinkedHashMap;
15 import java.util.List;
18 import org.opendaylight.controller.sal.binding.model.api.AnnotationType;
19 import org.opendaylight.controller.sal.binding.model.api.Constant;
20 import org.opendaylight.controller.sal.binding.model.api.Enumeration;
21 import org.opendaylight.controller.sal.binding.model.api.Enumeration.Pair;
22 import org.opendaylight.controller.sal.binding.model.api.GeneratedProperty;
23 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;
24 import org.opendaylight.controller.sal.binding.model.api.GeneratedType;
25 import org.opendaylight.controller.sal.binding.model.api.MethodSignature;
26 import org.opendaylight.controller.sal.binding.model.api.MethodSignature.Parameter;
27 import org.opendaylight.controller.sal.binding.model.api.ParameterizedType;
28 import org.opendaylight.controller.sal.binding.model.api.Type;
30 public final class GeneratorUtil {
32 private GeneratorUtil() {
35 public static String createIfcDeclaration(final GeneratedType genType,
37 final Map<String, LinkedHashMap<String, Integer>> availableImports) {
38 return createFileDeclaration(IFC, genType, indent, availableImports);
41 public static String createClassDeclaration(
42 final GeneratedTransferObject genTransferObject,
44 final Map<String, LinkedHashMap<String, Integer>> availableImports) {
45 return createFileDeclaration(CLASS, genTransferObject, indent,
49 public static String createPackageDeclaration(final String packageName) {
50 return PKG + GAP + packageName + SC;
53 private static String createFileDeclaration(final String type,
54 final GeneratedType genType, final String indent,
55 final Map<String, LinkedHashMap<String, Integer>> availableImports) {
56 final StringBuilder builder = new StringBuilder();
57 final String currentPkg = genType.getPackageName();
59 createComment(builder, genType.getComment(), indent);
61 if (!genType.getAnnotations().isEmpty()) {
62 final List<AnnotationType> annotations = genType.getAnnotations();
63 appendAnnotations(builder, annotations);
66 builder.append(PUBLIC + GAP + type + GAP + genType.getName() + GAP);
68 if (genType instanceof GeneratedTransferObject) {
69 GeneratedTransferObject genTO = (GeneratedTransferObject) genType;
71 if (genTO.getExtends() != null) {
72 builder.append(EXTENDS + GAP);
73 builder.append(genTO.getExtends() + GAP);
77 final List<Type> genImplements = genType.getImplements();
78 if (!genImplements.isEmpty()) {
79 if (genType instanceof GeneratedTransferObject) {
80 builder.append(IMPLEMENTS + GAP);
82 builder.append(EXTENDS + GAP);
84 builder.append(getExplicitType(genImplements.get(0),
85 availableImports, currentPkg));
87 for (int i = 1; i < genImplements.size(); ++i) {
89 builder.append(getExplicitType(genImplements.get(i),
90 availableImports, currentPkg));
94 builder.append(GAP + LCB);
95 return builder.toString();
98 private static StringBuilder appendAnnotations(final StringBuilder builder,
99 final List<AnnotationType> annotations) {
100 if ((builder != null) && (annotations != null)) {
101 for (final AnnotationType annotation : annotations) {
103 builder.append(annotation.getPackageName());
105 builder.append(annotation.getName());
107 if (annotation.containsParameters()) {
109 final List<AnnotationType.Parameter> parameters = annotation
111 appendAnnotationParams(builder, parameters);
119 private static StringBuilder appendAnnotationParams(
120 final StringBuilder builder,
121 final List<AnnotationType.Parameter> parameters) {
122 if (parameters != null) {
124 for (final AnnotationType.Parameter param : parameters) {
129 builder.append(", ");
131 final String paramName = param.getName();
132 if (param.getValue() != null) {
133 builder.append(paramName);
134 builder.append(" = ");
135 builder.append(param.getValue());
137 builder.append(paramName);
138 builder.append(" = {");
139 final List<String> values = param.getValues();
140 builder.append(values.get(0));
141 for (int j = 1; j < values.size(); ++j) {
142 builder.append(", ");
143 builder.append(values.get(j));
153 public static String createConstant(final Constant constant,
155 final Map<String, LinkedHashMap<String, Integer>> availableImports,
156 final String currentPkg) {
157 final StringBuilder builder = new StringBuilder();
158 builder.append(indent + PUBLIC + GAP + STATIC + GAP + FINAL + GAP);
159 builder.append(getExplicitType(constant.getType(), availableImports,
160 currentPkg) + GAP + constant.getName());
161 builder.append(GAP + "=" + GAP);
162 builder.append(constant.getValue() + SC);
163 return builder.toString();
166 public static String createField(final GeneratedProperty property,
168 Map<String, LinkedHashMap<String, Integer>> availableImports,
169 final String currentPkg) {
170 final StringBuilder builder = new StringBuilder();
171 builder.append(indent);
172 if (!property.getAnnotations().isEmpty()) {
173 final List<AnnotationType> annotations = property.getAnnotations();
174 appendAnnotations(builder, annotations);
177 builder.append(indent + PRIVATE + GAP);
178 builder.append(getExplicitType(property.getReturnType(),
179 availableImports, currentPkg) + GAP + property.getName());
181 return builder.toString();
185 * Create method declaration in interface.
191 public static String createMethodDeclaration(final MethodSignature method,
193 Map<String, LinkedHashMap<String, Integer>> availableImports,
194 final String currentPkg) {
195 final StringBuilder builder = new StringBuilder();
197 if (method == null) {
198 throw new IllegalArgumentException(
199 "Method Signature parameter MUST be specified and cannot be NULL!");
202 final String comment = method.getComment();
203 final String name = method.getName();
205 throw new IllegalStateException("Method Name cannot be NULL!");
208 final Type type = method.getReturnType();
210 throw new IllegalStateException(
211 "Method Return type cannot be NULL!");
214 final List<Parameter> parameters = method.getParameters();
216 createComment(builder, comment, indent);
218 builder.append(indent);
220 if (!method.getAnnotations().isEmpty()) {
221 final List<AnnotationType> annotations = method.getAnnotations();
222 appendAnnotations(builder, annotations);
226 builder.append(indent
227 + getExplicitType(type, availableImports, currentPkg) + GAP
230 for (int i = 0; i < parameters.size(); i++) {
231 Parameter p = parameters.get(i);
232 String separator = COMMA;
233 if (i + 1 == parameters.size()) {
236 builder.append(getExplicitType(p.getType(), availableImports,
237 currentPkg) + GAP + p.getName() + separator);
242 return builder.toString();
245 public static String createConstructor(
246 GeneratedTransferObject genTransferObject, final String indent,
247 Map<String, LinkedHashMap<String, Integer>> availableImports) {
248 final StringBuilder builder = new StringBuilder();
250 final String currentPkg = genTransferObject.getPackageName();
251 final List<GeneratedProperty> properties = genTransferObject
253 final List<GeneratedProperty> ctorParams = new ArrayList<GeneratedProperty>();
254 for (final GeneratedProperty property : properties) {
255 if (property.isReadOnly()) {
256 ctorParams.add(property);
260 builder.append(indent);
261 builder.append(PUBLIC);
263 builder.append(genTransferObject.getName());
266 if (!ctorParams.isEmpty()) {
267 builder.append(getExplicitType(ctorParams.get(0).getReturnType(),
268 availableImports, currentPkg));
270 builder.append(ctorParams.get(0).getName());
271 for (int i = 1; i < ctorParams.size(); ++i) {
272 final GeneratedProperty param = ctorParams.get(i);
273 builder.append(", ");
274 builder.append(getExplicitType(param.getReturnType(),
275 availableImports, currentPkg));
277 builder.append(param.getName());
280 builder.append(RB + GAP + LCB + NL + indent + TAB + "super();" + NL);
281 if (!ctorParams.isEmpty()) {
282 for (final GeneratedProperty property : ctorParams) {
283 builder.append(indent);
285 builder.append("this.");
286 builder.append(property.getName());
287 builder.append(" = ");
288 builder.append(property.getName());
293 builder.append(indent);
295 return builder.toString();
298 public static String createGetter(final GeneratedProperty property,
300 Map<String, LinkedHashMap<String, Integer>> availableImports,
301 final String currentPkg) {
302 final StringBuilder builder = new StringBuilder();
304 final Type type = property.getReturnType();
305 final String varName = property.getName();
306 final char first = Character.toUpperCase(varName.charAt(0));
307 final String methodName = "get" + first + varName.substring(1);
309 builder.append(indent + PUBLIC + GAP
310 + getExplicitType(type, availableImports, currentPkg) + GAP
312 builder.append(LB + RB + LCB + NL);
314 String currentIndent = indent + TAB;
316 builder.append(currentIndent + "return " + varName + SC + NL);
318 builder.append(indent + RCB);
319 return builder.toString();
322 public static String createSetter(final GeneratedProperty property,
324 Map<String, LinkedHashMap<String, Integer>> availableImports,
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 = "set" + first + varName.substring(1);
333 builder.append(indent + PUBLIC + GAP + "void" + GAP + methodName);
334 builder.append(LB + getExplicitType(type, availableImports, currentPkg)
335 + GAP + varName + RB + LCB + NL);
336 String currentIndent = indent + TAB;
337 builder.append(currentIndent + "this." + varName + " = " + varName + SC
339 builder.append(indent + RCB);
340 return builder.toString();
343 public static String createHashCode(
344 final List<GeneratedProperty> properties, final String indent) {
345 StringBuilder builder = new StringBuilder();
346 builder.append(indent + "public int hashCode() {" + NL);
347 builder.append(indent + TAB + "final int prime = 31;" + NL);
348 builder.append(indent + TAB + "int result = 1;" + NL);
350 for (GeneratedProperty property : properties) {
351 String fieldName = property.getName();
352 builder.append(indent + TAB + "result = prime * result + (("
353 + fieldName + " == null) ? 0 : " + fieldName
354 + ".hashCode());" + NL);
357 builder.append(indent + TAB + "return result;" + NL);
358 builder.append(indent + RCB + NL);
359 return builder.toString();
362 public static String createEquals(final GeneratedTransferObject type,
363 final List<GeneratedProperty> properties, final String indent) {
364 StringBuilder builder = new StringBuilder();
365 final String indent1 = indent + TAB;
366 final String indent2 = indent1 + TAB;
367 final String indent3 = indent2 + TAB;
369 builder.append(indent + "public boolean equals(Object obj) {" + NL);
370 builder.append(indent1 + "if (this == obj) {" + NL);
371 builder.append(indent2 + "return true;" + NL);
372 builder.append(indent1 + "}" + NL);
373 builder.append(indent1 + "if (obj == null) {" + NL);
374 builder.append(indent2 + "return false;" + NL);
375 builder.append(indent1 + "}" + NL);
376 builder.append(indent1 + "if (getClass() != obj.getClass()) {" + NL);
377 builder.append(indent2 + "return false;" + NL);
378 builder.append(indent1 + "}" + NL);
380 String typeStr = type.getName();
381 builder.append(indent1 + typeStr + " other = (" + typeStr + ") obj;"
384 for (GeneratedProperty property : properties) {
385 String fieldName = property.getName();
386 builder.append(indent1 + "if (" + fieldName + " == null) {" + NL);
387 builder.append(indent2 + "if (other." + fieldName + " != null) {"
389 builder.append(indent3 + "return false;" + NL);
390 builder.append(indent2 + "}" + NL);
391 builder.append(indent1 + "} else if (!" + fieldName
392 + ".equals(other." + fieldName + ")) {" + NL);
393 builder.append(indent2 + "return false;" + NL);
394 builder.append(indent1 + "}" + NL);
397 builder.append(indent1 + "return true;" + NL);
399 builder.append(indent + RCB + NL);
400 return builder.toString();
403 public static String createToString(final GeneratedTransferObject type,
404 final List<GeneratedProperty> properties, final String indent) {
405 StringBuilder builder = new StringBuilder();
406 builder.append(indent);
407 builder.append("public String toString() {");
409 builder.append(indent);
411 builder.append("StringBuilder builder = new StringBuilder();");
413 builder.append(indent);
415 builder.append("builder.append(\"");
416 builder.append(type.getName());
417 builder.append(" [");
419 boolean first = true;
420 for (GeneratedProperty property : properties) {
422 builder.append(property.getName());
423 builder.append("=\");");
425 builder.append(indent);
427 builder.append("builder.append(");
428 builder.append(property.getName());
429 builder.append(");");
433 builder.append(indent);
435 builder.append("builder.append(\", ");
436 builder.append(property.getName());
437 builder.append("=\");");
439 builder.append(indent);
441 builder.append("builder.append(");
442 builder.append(property.getName());
443 builder.append(");");
447 builder.append(indent);
449 builder.append("builder.append(\"]\");");
451 builder.append(indent);
453 builder.append("return builder.toString();");
456 builder.append(indent);
459 return builder.toString();
462 public static String createEnum(final Enumeration enumeration,
463 final String indent) {
464 if (enumeration == null || indent == null)
465 throw new IllegalArgumentException();
466 final StringBuilder builder = new StringBuilder(indent + PUBLIC + GAP
467 + ENUM + GAP + enumeration.getName() + GAP + LCB + NL);
469 String separator = COMMA + NL;
470 final List<Pair> values = enumeration.getValues();
472 for (int i = 0; i < values.size(); i++) {
473 if (i + 1 == values.size()) {
476 builder.append(indent + TAB + values.get(i).getName() + LB
477 + values.get(i).getValue() + RB + separator);
481 final String ENUMERATION_NAME = "value";
482 final String ENUMERATION_TYPE = "int";
483 builder.append(indent + TAB + ENUMERATION_TYPE + GAP + ENUMERATION_NAME
486 builder.append(indent + TAB + PRIVATE + GAP + enumeration.getName()
487 + LB + ENUMERATION_TYPE + GAP + ENUMERATION_NAME + RB + GAP
489 builder.append(indent + TAB + TAB + "this." + ENUMERATION_NAME + GAP
490 + "=" + GAP + ENUMERATION_NAME + SC + NL);
491 builder.append(indent + TAB + RCB + NL);
493 builder.append(indent + RCB);
495 return builder.toString();
498 private static String getExplicitType(final Type type,
499 Map<String, LinkedHashMap<String, Integer>> availableImports,
500 final String currentPkg) {
502 throw new IllegalArgumentException(
503 "Type parameter MUST be specified and cannot be NULL!");
505 String packageName = type.getPackageName();
507 LinkedHashMap<String, Integer> imports = availableImports.get(type
510 if ((imports != null && packageName
511 .equals(findMaxValue(imports).get(0)))
512 || packageName.equals(currentPkg)) {
513 final StringBuilder builder = new StringBuilder(type.getName());
514 if (type instanceof ParameterizedType) {
515 ParameterizedType pType = (ParameterizedType) type;
516 Type[] pTypes = pType.getActualTypeArguments();
518 builder.append(getParameters(pTypes, availableImports,
522 if (builder.toString().equals("Void")) {
525 return builder.toString();
527 final StringBuilder builder = new StringBuilder();
528 if (packageName.startsWith("java.lang")) {
529 builder.append(type.getName());
531 if (!packageName.isEmpty()) {
532 builder.append(packageName + "." + type.getName());
534 builder.append(type.getName());
538 if (type instanceof ParameterizedType) {
539 ParameterizedType pType = (ParameterizedType) type;
540 Type[] pTypes = pType.getActualTypeArguments();
542 builder.append(getParameters(pTypes, availableImports,
546 if (builder.toString().equals("Void")) {
549 return builder.toString();
553 private static String getParameters(final Type[] pTypes,
554 Map<String, LinkedHashMap<String, Integer>> availableImports,
556 final StringBuilder builder = new StringBuilder();
557 for (int i = 0; i < pTypes.length; i++) {
560 String separator = COMMA;
561 if (i + 1 == pTypes.length) {
564 builder.append(getExplicitType(t, availableImports, currentPkg)
567 return builder.toString();
570 private static List<String> findMaxValue(
571 LinkedHashMap<String, Integer> imports) {
572 final List<String> result = new ArrayList<String>();
575 int currentValue = 0;
576 for (Map.Entry<String, Integer> entry : imports.entrySet()) {
577 currentValue = entry.getValue();
578 if (currentValue > maxValue) {
580 result.add(entry.getKey());
581 } else if (currentValue == maxValue) {
582 result.add(entry.getKey());
588 private static void createComment(final StringBuilder builder,
589 final String comment, final String indent) {
590 if (comment != null && comment.length() > 0) {
591 builder.append(indent + "/*" + NL);
592 builder.append(indent + comment + NL);
593 builder.append(indent + "*/" + NL);
597 public static Map<String, LinkedHashMap<String, Integer>> createImports(
598 GeneratedType genType) {
599 final Map<String, LinkedHashMap<String, Integer>> imports = new HashMap<String, LinkedHashMap<String, Integer>>();
600 final String genTypePkg = genType.getPackageName();
602 final List<Constant> constants = genType.getConstantDefinitions();
603 final List<MethodSignature> methods = genType.getMethodDefinitions();
604 List<Type> impl = genType.getImplements();
608 for (Type t : impl) {
609 addTypeToImports(t, imports, genTypePkg);
614 if (constants != null) {
615 for (Constant c : constants) {
616 Type ct = c.getType();
617 addTypeToImports(ct, imports, genTypePkg);
622 if (methods != null) {
623 for (MethodSignature m : methods) {
624 Type ct = m.getReturnType();
625 addTypeToImports(ct, imports, genTypePkg);
626 for (MethodSignature.Parameter p : m.getParameters()) {
627 addTypeToImports(p.getType(), imports, genTypePkg);
633 if (genType instanceof GeneratedTransferObject) {
634 GeneratedTransferObject genTO = (GeneratedTransferObject) genType;
636 List<GeneratedProperty> props = genTO.getProperties();
638 for (GeneratedProperty prop : props) {
639 Type pt = prop.getReturnType();
640 addTypeToImports(pt, imports, genTypePkg);
648 private static void addTypeToImports(Type type,
649 Map<String, LinkedHashMap<String, Integer>> importedTypes,
651 String typeName = type.getName();
652 String typePkg = type.getPackageName();
653 if (typePkg.startsWith("java.lang") || typePkg.equals(genTypePkg) ||
657 LinkedHashMap<String, Integer> packages = importedTypes.get(typeName);
658 if (packages == null) {
659 packages = new LinkedHashMap<String, Integer>();
660 packages.put(typePkg, 1);
661 importedTypes.put(typeName, packages);
663 Integer occurrence = packages.get(typePkg);
664 if (occurrence == null) {
665 packages.put(typePkg, 1);
668 packages.put(typePkg, occurrence);
672 if (type instanceof ParameterizedType) {
673 ParameterizedType pt = (ParameterizedType) type;
674 Type[] params = pt.getActualTypeArguments();
675 for (Type param : params) {
676 addTypeToImports(param, importedTypes, genTypePkg);
681 public static List<String> createImportLines(
682 Map<String, LinkedHashMap<String, Integer>> imports) {
683 List<String> importLines = new ArrayList<String>();
685 for (Map.Entry<String, LinkedHashMap<String, Integer>> entry : imports
687 String typeName = entry.getKey();
688 LinkedHashMap<String, Integer> typePkgMap = entry.getValue();
689 String typePkg = typePkgMap.keySet().iterator().next();
690 importLines.add("import " + typePkg + "." + typeName + SC);