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;
29 import org.opendaylight.controller.sal.binding.model.api.WildcardType;
31 public final class GeneratorUtil {
33 private GeneratorUtil() {
36 public static String createIfcDeclaration(final GeneratedType genType,
38 final Map<String, LinkedHashMap<String, Integer>> availableImports) {
39 return createFileDeclaration(IFC, genType, indent, availableImports,
43 public static String createClassDeclaration(
44 final GeneratedTransferObject genTransferObject,
46 final Map<String, LinkedHashMap<String, Integer>> availableImports,
48 return createFileDeclaration(CLASS, genTransferObject, indent,
49 availableImports, isIdentity);
52 public static String createPackageDeclaration(final String packageName) {
53 return PKG + GAP + packageName + SC;
56 private static String createFileDeclaration(final String type,
57 final GeneratedType genType, final String indent,
58 final Map<String, LinkedHashMap<String, Integer>> availableImports,
60 final StringBuilder builder = new StringBuilder();
61 final String currentPkg = genType.getPackageName();
63 createComment(builder, genType.getComment(), indent);
65 if (!genType.getAnnotations().isEmpty()) {
66 final List<AnnotationType> annotations = genType.getAnnotations();
67 appendAnnotations(builder, annotations);
72 if (!(CLASS.equals(type))) {
73 throw new IllegalArgumentException(
74 "'identity' has to be generated as a class");
76 builder.append(PUBLIC + GAP + ABSTRACT + GAP + type + GAP
77 + genType.getName() + GAP);
79 builder.append(PUBLIC + GAP + type + GAP + genType.getName() + GAP);
82 if (genType instanceof GeneratedTransferObject) {
83 GeneratedTransferObject genTO = (GeneratedTransferObject) genType;
85 if (genTO.getExtends() != null) {
86 builder.append(EXTENDS + GAP);
87 String gtoString = getExplicitType(genTO.getExtends(), availableImports, currentPkg);
88 builder.append(gtoString + GAP);
92 final List<Type> genImplements = genType.getImplements();
93 if (!genImplements.isEmpty()) {
94 if (genType instanceof GeneratedTransferObject) {
95 builder.append(IMPLEMENTS + GAP);
97 builder.append(EXTENDS + GAP);
99 builder.append(getExplicitType(genImplements.get(0),
100 availableImports, currentPkg));
102 for (int i = 1; i < genImplements.size(); ++i) {
103 builder.append(", ");
104 builder.append(getExplicitType(genImplements.get(i),
105 availableImports, currentPkg));
109 builder.append(GAP + LCB);
110 return builder.toString();
113 private static StringBuilder appendAnnotations(final StringBuilder builder,
114 final List<AnnotationType> annotations) {
115 if ((builder != null) && (annotations != null)) {
116 for (final AnnotationType annotation : annotations) {
118 builder.append(annotation.getPackageName());
120 builder.append(annotation.getName());
122 if (annotation.containsParameters()) {
124 final List<AnnotationType.Parameter> parameters = annotation
126 appendAnnotationParams(builder, parameters);
134 private static StringBuilder appendAnnotationParams(
135 final StringBuilder builder,
136 final List<AnnotationType.Parameter> parameters) {
137 if (parameters != null) {
139 for (final AnnotationType.Parameter param : parameters) {
144 builder.append(", ");
146 final String paramName = param.getName();
147 if (param.getValue() != null) {
148 builder.append(paramName);
149 builder.append(" = ");
150 builder.append(param.getValue());
152 builder.append(paramName);
153 builder.append(" = {");
154 final List<String> values = param.getValues();
155 builder.append(values.get(0));
156 for (int j = 1; j < values.size(); ++j) {
157 builder.append(", ");
158 builder.append(values.get(j));
168 public static String createConstant(final Constant constant,
170 final Map<String, LinkedHashMap<String, Integer>> availableImports,
171 final String currentPkg) {
172 final StringBuilder builder = new StringBuilder();
173 builder.append(indent + PUBLIC + GAP + STATIC + GAP + FINAL + GAP);
174 builder.append(getExplicitType(constant.getType(), availableImports,
175 currentPkg) + GAP + constant.getName());
176 builder.append(GAP + "=" + GAP);
177 builder.append(constant.getValue() + SC);
178 return builder.toString();
181 public static String createField(final GeneratedProperty property,
183 Map<String, LinkedHashMap<String, Integer>> availableImports,
184 final String currentPkg) {
185 final StringBuilder builder = new StringBuilder();
186 if (!property.getAnnotations().isEmpty()) {
187 final List<AnnotationType> annotations = property.getAnnotations();
188 appendAnnotations(builder, annotations);
191 builder.append(indent + PRIVATE + GAP);
192 builder.append(getExplicitType(property.getReturnType(),
193 availableImports, currentPkg) + GAP + property.getName());
195 return builder.toString();
199 * Create method declaration in interface.
205 public static String createMethodDeclaration(final MethodSignature method,
207 Map<String, LinkedHashMap<String, Integer>> availableImports,
208 final String currentPkg) {
209 final StringBuilder builder = new StringBuilder();
211 if (method == null) {
212 throw new IllegalArgumentException(
213 "Method Signature parameter MUST be specified and cannot be NULL!");
216 final String comment = method.getComment();
217 final String name = method.getName();
219 throw new IllegalStateException("Method Name cannot be NULL!");
222 final Type type = method.getReturnType();
224 throw new IllegalStateException(
225 "Method Return type cannot be NULL!");
228 final List<Parameter> parameters = method.getParameters();
230 createComment(builder, comment, indent);
232 builder.append(indent);
234 if (!method.getAnnotations().isEmpty()) {
235 final List<AnnotationType> annotations = method.getAnnotations();
236 appendAnnotations(builder, annotations);
240 builder.append(indent
241 + getExplicitType(type, availableImports, currentPkg) + GAP
244 for (int i = 0; i < parameters.size(); i++) {
245 Parameter p = parameters.get(i);
246 String separator = COMMA;
247 if (i + 1 == parameters.size()) {
250 builder.append(getExplicitType(p.getType(), availableImports,
251 currentPkg) + GAP + p.getName() + separator);
256 return builder.toString();
259 public static String createConstructor(
260 GeneratedTransferObject genTransferObject, final String indent,
261 Map<String, LinkedHashMap<String, Integer>> availableImports,
262 boolean isIdentity) {
263 final StringBuilder builder = new StringBuilder();
265 final String currentPkg = genTransferObject.getPackageName();
266 final List<GeneratedProperty> properties = genTransferObject
268 final List<GeneratedProperty> ctorParams = new ArrayList<GeneratedProperty>();
269 for (final GeneratedProperty property : properties) {
270 if (property.isReadOnly()) {
271 ctorParams.add(property);
275 builder.append(indent);
276 builder.append(isIdentity ? PROTECTED : PUBLIC);
278 builder.append(genTransferObject.getName());
281 if (!ctorParams.isEmpty()) {
282 builder.append(getExplicitType(ctorParams.get(0).getReturnType(),
283 availableImports, currentPkg));
285 builder.append(ctorParams.get(0).getName());
286 for (int i = 1; i < ctorParams.size(); ++i) {
287 final GeneratedProperty param = ctorParams.get(i);
288 builder.append(", ");
289 builder.append(getExplicitType(param.getReturnType(),
290 availableImports, currentPkg));
292 builder.append(param.getName());
295 builder.append(RB + GAP + LCB + NL + indent + TAB + "super();" + NL);
296 if (!ctorParams.isEmpty()) {
297 for (final GeneratedProperty property : ctorParams) {
298 builder.append(indent);
300 builder.append("this.");
301 builder.append(property.getName());
302 builder.append(" = ");
303 builder.append(property.getName());
308 builder.append(indent);
310 return builder.toString();
313 public static String createGetter(final GeneratedProperty property,
315 Map<String, LinkedHashMap<String, Integer>> availableImports,
316 final String currentPkg) {
317 final StringBuilder builder = new StringBuilder();
319 final Type type = property.getReturnType();
320 final String varName = property.getName();
321 final char first = Character.toUpperCase(varName.charAt(0));
322 final String methodName = "get" + first + varName.substring(1);
324 builder.append(indent + PUBLIC + GAP
325 + getExplicitType(type, availableImports, currentPkg) + GAP
327 builder.append(LB + RB + LCB + NL);
329 String currentIndent = indent + TAB;
331 builder.append(currentIndent + "return " + varName + SC + NL);
333 builder.append(indent + RCB);
334 return builder.toString();
337 public static String createSetter(final GeneratedProperty property,
339 Map<String, LinkedHashMap<String, Integer>> availableImports,
341 final StringBuilder builder = new StringBuilder();
343 final Type type = property.getReturnType();
344 final String varName = property.getName();
345 final char first = Character.toUpperCase(varName.charAt(0));
346 final String methodName = "set" + first + varName.substring(1);
348 builder.append(indent + PUBLIC + GAP + "void" + GAP + methodName);
349 builder.append(LB + getExplicitType(type, availableImports, currentPkg)
350 + GAP + varName + RB + LCB + NL);
351 String currentIndent = indent + TAB;
352 builder.append(currentIndent + "this." + varName + " = " + varName + SC
354 builder.append(indent + RCB);
355 return builder.toString();
358 public static String createHashCode(
359 final List<GeneratedProperty> properties, final String indent) {
360 StringBuilder builder = new StringBuilder();
361 builder.append(indent + "public int hashCode() {" + NL);
362 builder.append(indent + TAB + "final int prime = 31;" + NL);
363 builder.append(indent + TAB + "int result = 1;" + NL);
365 for (GeneratedProperty property : properties) {
366 String fieldName = property.getName();
367 builder.append(indent + TAB + "result = prime * result + (("
368 + fieldName + " == null) ? 0 : " + fieldName
369 + ".hashCode());" + NL);
372 builder.append(indent + TAB + "return result;" + NL);
373 builder.append(indent + RCB + NL);
374 return builder.toString();
377 public static String createEquals(final GeneratedTransferObject type,
378 final List<GeneratedProperty> properties, final String indent) {
379 StringBuilder builder = new StringBuilder();
380 final String indent1 = indent + TAB;
381 final String indent2 = indent1 + TAB;
382 final String indent3 = indent2 + TAB;
384 builder.append(indent + "public boolean equals(Object obj) {" + NL);
385 builder.append(indent1 + "if (this == obj) {" + NL);
386 builder.append(indent2 + "return true;" + NL);
387 builder.append(indent1 + "}" + NL);
388 builder.append(indent1 + "if (obj == null) {" + NL);
389 builder.append(indent2 + "return false;" + NL);
390 builder.append(indent1 + "}" + NL);
391 builder.append(indent1 + "if (getClass() != obj.getClass()) {" + NL);
392 builder.append(indent2 + "return false;" + NL);
393 builder.append(indent1 + "}" + NL);
395 String typeStr = type.getName();
396 builder.append(indent1 + typeStr + " other = (" + typeStr + ") obj;"
399 for (GeneratedProperty property : properties) {
400 String fieldName = property.getName();
401 builder.append(indent1 + "if (" + fieldName + " == null) {" + NL);
402 builder.append(indent2 + "if (other." + fieldName + " != null) {"
404 builder.append(indent3 + "return false;" + NL);
405 builder.append(indent2 + "}" + NL);
406 builder.append(indent1 + "} else if (!" + fieldName
407 + ".equals(other." + fieldName + ")) {" + NL);
408 builder.append(indent2 + "return false;" + NL);
409 builder.append(indent1 + "}" + NL);
412 builder.append(indent1 + "return true;" + NL);
414 builder.append(indent + RCB + NL);
415 return builder.toString();
418 public static String createToString(final GeneratedTransferObject type,
419 final List<GeneratedProperty> properties, final String indent) {
420 StringBuilder builder = new StringBuilder();
421 builder.append(indent);
422 builder.append("public String toString() {");
424 builder.append(indent);
426 builder.append("StringBuilder builder = new StringBuilder();");
428 builder.append(indent);
430 builder.append("builder.append(\"");
431 builder.append(type.getName());
432 builder.append(" [");
434 boolean first = true;
435 for (GeneratedProperty property : properties) {
437 builder.append(property.getName());
438 builder.append("=\");");
440 builder.append(indent);
442 builder.append("builder.append(");
443 builder.append(property.getName());
444 builder.append(");");
448 builder.append(indent);
450 builder.append("builder.append(\", ");
451 builder.append(property.getName());
452 builder.append("=\");");
454 builder.append(indent);
456 builder.append("builder.append(");
457 builder.append(property.getName());
458 builder.append(");");
462 builder.append(indent);
464 builder.append("builder.append(\"]\");");
466 builder.append(indent);
468 builder.append("return builder.toString();");
471 builder.append(indent);
474 return builder.toString();
477 public static String createEnum(final Enumeration enumeration,
478 final String indent) {
479 if (enumeration == null || indent == null)
480 throw new IllegalArgumentException();
481 final StringBuilder builder = new StringBuilder(indent + PUBLIC + GAP
482 + ENUM + GAP + enumeration.getName() + GAP + LCB + NL);
484 String separator = COMMA + NL;
485 final List<Pair> values = enumeration.getValues();
487 for (int i = 0; i < values.size(); i++) {
488 if (i + 1 == values.size()) {
491 builder.append(indent + TAB + values.get(i).getName() + LB
492 + values.get(i).getValue() + RB + separator);
496 final String ENUMERATION_NAME = "value";
497 final String ENUMERATION_TYPE = "int";
498 builder.append(indent + TAB + ENUMERATION_TYPE + GAP + ENUMERATION_NAME
501 builder.append(indent + TAB + PRIVATE + GAP + enumeration.getName()
502 + LB + ENUMERATION_TYPE + GAP + ENUMERATION_NAME + RB + GAP
504 builder.append(indent + TAB + TAB + "this." + ENUMERATION_NAME + GAP
505 + "=" + GAP + ENUMERATION_NAME + SC + NL);
506 builder.append(indent + TAB + RCB + NL);
508 builder.append(indent + RCB);
510 return builder.toString();
513 private static String getExplicitType(final Type type,
514 Map<String, LinkedHashMap<String, Integer>> availableImports,
515 final String currentPkg) {
517 throw new IllegalArgumentException(
518 "Type parameter MUST be specified and cannot be NULL!");
520 String packageName = type.getPackageName();
522 LinkedHashMap<String, Integer> imports = availableImports.get(type
525 if ((imports != null && packageName
526 .equals(findMaxValue(imports).get(0)))
527 || packageName.equals(currentPkg)) {
528 final StringBuilder builder = new StringBuilder(type.getName());
529 if (type instanceof ParameterizedType) {
530 ParameterizedType pType = (ParameterizedType) type;
531 Type[] pTypes = pType.getActualTypeArguments();
533 builder.append(getParameters(pTypes, availableImports,
537 if (builder.toString().equals("Void")) {
540 return builder.toString();
542 final StringBuilder builder = new StringBuilder();
543 if (packageName.startsWith("java.lang")) {
544 builder.append(type.getName());
546 if (!packageName.isEmpty()) {
547 builder.append(packageName + "." + type.getName());
549 builder.append(type.getName());
553 if (type instanceof ParameterizedType) {
554 ParameterizedType pType = (ParameterizedType) type;
555 Type[] pTypes = pType.getActualTypeArguments();
557 builder.append(getParameters(pTypes, availableImports,
561 if (builder.toString().equals("Void")) {
564 return builder.toString();
568 private static String getParameters(final Type[] pTypes,
569 Map<String, LinkedHashMap<String, Integer>> availableImports,
571 final StringBuilder builder = new StringBuilder();
572 for (int i = 0; i < pTypes.length; i++) {
575 String separator = COMMA;
576 if (i + 1 == pTypes.length) {
580 String wildcardParam = "";
581 if(t instanceof WildcardType) {
582 wildcardParam = "? extends ";
585 builder.append(wildcardParam + getExplicitType(t, availableImports, currentPkg)
588 return builder.toString();
591 private static List<String> findMaxValue(
592 LinkedHashMap<String, Integer> imports) {
593 final List<String> result = new ArrayList<String>();
596 int currentValue = 0;
597 for (Map.Entry<String, Integer> entry : imports.entrySet()) {
598 currentValue = entry.getValue();
599 if (currentValue > maxValue) {
601 result.add(entry.getKey());
602 maxValue = currentValue;
603 } else if (currentValue == maxValue) {
604 result.add(entry.getKey());
610 private static void createComment(final StringBuilder builder,
611 final String comment, final String indent) {
612 if (comment != null && comment.length() > 0) {
613 builder.append(indent + "/*" + NL);
614 builder.append(indent + comment + NL);
615 builder.append(indent + "*/" + NL);
619 public static Map<String, LinkedHashMap<String, Integer>> createImports(
620 GeneratedType genType) {
621 final Map<String, LinkedHashMap<String, Integer>> imports = new HashMap<String, LinkedHashMap<String, Integer>>();
622 final String genTypePkg = genType.getPackageName();
624 final List<Constant> constants = genType.getConstantDefinitions();
625 final List<MethodSignature> methods = genType.getMethodDefinitions();
626 List<Type> impl = genType.getImplements();
630 for (Type t : impl) {
631 addTypeToImports(t, imports, genTypePkg);
636 if (constants != null) {
637 for (Constant c : constants) {
638 Type ct = c.getType();
639 addTypeToImports(ct, imports, genTypePkg);
644 if (methods != null) {
645 for (MethodSignature m : methods) {
646 Type ct = m.getReturnType();
647 addTypeToImports(ct, imports, genTypePkg);
648 for (MethodSignature.Parameter p : m.getParameters()) {
649 addTypeToImports(p.getType(), imports, genTypePkg);
655 if (genType instanceof GeneratedTransferObject) {
656 GeneratedTransferObject genTO = (GeneratedTransferObject) genType;
658 List<GeneratedProperty> props = genTO.getProperties();
660 for (GeneratedProperty prop : props) {
661 Type pt = prop.getReturnType();
662 addTypeToImports(pt, imports, genTypePkg);
670 private static void addTypeToImports(Type type,
671 Map<String, LinkedHashMap<String, Integer>> importedTypes,
673 String typeName = type.getName();
674 String typePkg = type.getPackageName();
675 if (typePkg.startsWith("java.lang") || typePkg.equals(genTypePkg) ||
679 LinkedHashMap<String, Integer> packages = importedTypes.get(typeName);
680 if (packages == null) {
681 packages = new LinkedHashMap<String, Integer>();
682 packages.put(typePkg, 1);
683 importedTypes.put(typeName, packages);
685 Integer occurrence = packages.get(typePkg);
686 if (occurrence == null) {
687 packages.put(typePkg, 1);
690 packages.put(typePkg, occurrence);
694 if (type instanceof ParameterizedType) {
695 ParameterizedType pt = (ParameterizedType) type;
696 Type[] params = pt.getActualTypeArguments();
697 for (Type param : params) {
698 addTypeToImports(param, importedTypes, genTypePkg);
703 public static List<String> createImportLines(
704 Map<String, LinkedHashMap<String, Integer>> imports) {
705 List<String> importLines = new ArrayList<String>();
707 for (Map.Entry<String, LinkedHashMap<String, Integer>> entry : imports
709 String typeName = entry.getKey();
710 LinkedHashMap<String, Integer> typePkgMap = entry.getValue();
711 String typePkg = typePkgMap.keySet().iterator().next();
712 importLines.add("import " + typePkg + "." + typeName + SC);