Update the API generation code and code generation sample
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-java-api-generator / src / main / java / org / opendaylight / controller / sal / java / api / generator / GeneratorUtil.java
1 /*\r
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 package org.opendaylight.controller.sal.java.api.generator;\r
9 \r
10 import static org.opendaylight.controller.sal.java.api.generator.Constants.CLASS;\r
11 import static org.opendaylight.controller.sal.java.api.generator.Constants.COMMA;\r
12 import static org.opendaylight.controller.sal.java.api.generator.Constants.ENUM;\r
13 import static org.opendaylight.controller.sal.java.api.generator.Constants.FINAL;\r
14 import static org.opendaylight.controller.sal.java.api.generator.Constants.GAP;\r
15 import static org.opendaylight.controller.sal.java.api.generator.Constants.IFC;\r
16 import static org.opendaylight.controller.sal.java.api.generator.Constants.LB;\r
17 import static org.opendaylight.controller.sal.java.api.generator.Constants.LCB;\r
18 import static org.opendaylight.controller.sal.java.api.generator.Constants.NL;\r
19 import static org.opendaylight.controller.sal.java.api.generator.Constants.PKG;\r
20 import static org.opendaylight.controller.sal.java.api.generator.Constants.PRIVATE;\r
21 import static org.opendaylight.controller.sal.java.api.generator.Constants.PUBLIC;\r
22 import static org.opendaylight.controller.sal.java.api.generator.Constants.RB;\r
23 import static org.opendaylight.controller.sal.java.api.generator.Constants.RCB;\r
24 import static org.opendaylight.controller.sal.java.api.generator.Constants.SC;\r
25 import static org.opendaylight.controller.sal.java.api.generator.Constants.STATIC;\r
26 import static org.opendaylight.controller.sal.java.api.generator.Constants.TAB;\r
27 \r
28 import java.util.Arrays;\r
29 import java.util.HashSet;\r
30 import java.util.List;\r
31 import java.util.Set;\r
32 \r
33 import org.opendaylight.controller.sal.binding.model.api.Constant;\r
34 import org.opendaylight.controller.sal.binding.model.api.Enumeration;\r
35 import org.opendaylight.controller.sal.binding.model.api.Enumeration.Pair;\r
36 import org.opendaylight.controller.sal.binding.model.api.GeneratedProperty;\r
37 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;\r
38 import org.opendaylight.controller.sal.binding.model.api.MethodSignature;\r
39 import org.opendaylight.controller.sal.binding.model.api.MethodSignature.Parameter;\r
40 import org.opendaylight.controller.sal.binding.model.api.ParameterizedType;\r
41 import org.opendaylight.controller.sal.binding.model.api.Type;\r
42 \r
43 public class GeneratorUtil {\r
44 \r
45     private static final String[] SET_VALUES = new String[] { "abstract",\r
46             "assert", "boolean", "break", "byte", "case", "catch", "char",\r
47             "class", "const", "continue", "default", "double", "do", "else",\r
48             "enum", "extends", "false", "final", "finally", "float", "for",\r
49             "goto", "if", "implements", "import", "instanceof", "int",\r
50             "interface", "long", "native", "new", "null", "package", "private",\r
51             "protected", "public", "return", "short", "static", "strictfp",\r
52             "super", "switch", "synchronized", "this", "throw", "throws",\r
53             "transient", "true", "try", "void", "volatile", "while" };\r
54 \r
55     public static final Set<String> JAVA_RESERVED_WORDS = new HashSet<String>(\r
56             Arrays.asList(SET_VALUES));\r
57 \r
58     private GeneratorUtil() {\r
59     }\r
60 \r
61     private static String validateParamName(final String paramName) {\r
62         if (paramName != null) {\r
63             if (JAVA_RESERVED_WORDS.contains(paramName)) {\r
64                 return "_" + paramName;\r
65             }\r
66         }\r
67         return paramName;\r
68     }\r
69 \r
70     public static String createIfcDeclarationWithPkgName(\r
71             final String packageName, final String name, final String indent) {\r
72         return createFileDeclarationWithPkgName(IFC,\r
73                 packageName, validateParamName(name), indent);\r
74     }\r
75 \r
76     public static String createClassDeclarationWithPkgName(\r
77             final String packageName, final String name, final String indent) {\r
78         return createFileDeclarationWithPkgName(CLASS,\r
79                 packageName, validateParamName(name), indent);\r
80     }\r
81 \r
82     private static String createFileDeclarationWithPkgName(final String type,\r
83             final String packageName, final String name, final String indent) {\r
84         final StringBuilder builder = new StringBuilder();\r
85         builder.append(PKG + GAP + packageName + SC);\r
86         builder.append(NL);\r
87         builder.append(NL);\r
88         builder.append(PUBLIC + GAP + type + GAP + validateParamName(name) + GAP + LCB);\r
89         return builder.toString();\r
90     }\r
91 \r
92     public static String createConstant(final Constant constant,\r
93             final String indent) {\r
94         final StringBuilder builder = new StringBuilder();\r
95         builder.append(indent + PUBLIC + GAP + STATIC + GAP + FINAL + GAP);\r
96         builder.append(getExplicitType(constant.getType()) + GAP\r
97                 + constant.getName());\r
98         builder.append(GAP + "=" + GAP);\r
99         builder.append(constant.getValue() + SC);\r
100         return builder.toString();\r
101     }\r
102 \r
103     public static String createField(final GeneratedProperty property,\r
104             final String indent) {\r
105         final StringBuilder builder = new StringBuilder();\r
106         builder.append(indent + PRIVATE + GAP);\r
107         builder.append(getExplicitType(property.getReturnType()) + GAP\r
108                 + property.getName());\r
109         builder.append(SC);\r
110         return builder.toString();\r
111     }\r
112 \r
113     /**\r
114      * Create method declaration in interface.\r
115      * \r
116      * @param method\r
117      * @param indent\r
118      * @return\r
119      */\r
120     public static String createMethodDeclaration(final MethodSignature method,\r
121             final String indent) {\r
122         final String comment = method.getComment();\r
123         final Type type = method.getReturnType();\r
124         final String name = method.getName();\r
125         final List<Parameter> parameters = method.getParameters();\r
126 \r
127         final StringBuilder builder = new StringBuilder();\r
128         createComment(builder, comment, indent);\r
129 \r
130         builder.append(indent + getExplicitType(type) + GAP + name);\r
131         builder.append(LB);\r
132         for (int i = 0; i < parameters.size(); i++) {\r
133             Parameter p = parameters.get(i);\r
134             String separator = COMMA;\r
135             if (i + 1 == parameters.size()) {\r
136                 separator = "";\r
137             }\r
138             builder.append(getExplicitType(p.getType()) + GAP + validateParamName(p.getName())\r
139                     + separator);\r
140         }\r
141         builder.append(RB);\r
142         builder.append(SC);\r
143 \r
144         return builder.toString();\r
145     }\r
146 \r
147     public static String createConstructor(\r
148             GeneratedTransferObject genTransferObject, final String indent) {\r
149         final StringBuilder builder = new StringBuilder();\r
150 \r
151         final List<GeneratedProperty> properties = genTransferObject\r
152                 .getProperties();\r
153         builder.append(indent);\r
154         builder.append(PUBLIC);\r
155         builder.append(GAP);\r
156         builder.append(genTransferObject.getName());\r
157         builder.append(LB);\r
158 \r
159         boolean first = true;\r
160         if (properties != null) {\r
161             for (final GeneratedProperty property : properties) {\r
162                 if (first) {\r
163                     builder.append(getExplicitType(property.getReturnType()));\r
164                     builder.append(" ");\r
165                     builder.append(property.getName());\r
166                     first = false;\r
167                 } else {\r
168                     builder.append(", ");\r
169                     builder.append(getExplicitType(property.getReturnType()));\r
170                     builder.append(builder.append(" "));\r
171                     builder.append(property.getName());\r
172                 }\r
173             }\r
174         }\r
175 \r
176         builder.append(RB);\r
177         builder.append(GAP);\r
178         builder.append(LCB);\r
179         builder.append(NL);\r
180         builder.append(indent);\r
181         builder.append(TAB);\r
182         builder.append("super();");\r
183         builder.append(NL);\r
184 \r
185         if (properties != null) {\r
186             for (final GeneratedProperty property : properties) {\r
187                 builder.append(indent);\r
188                 builder.append(TAB);\r
189                 builder.append("this.");\r
190                 builder.append(property.getName());\r
191                 builder.append(" = ");\r
192                 builder.append(property.getName());\r
193                 builder.append(SC);\r
194                 builder.append(NL);\r
195             }\r
196         }\r
197 \r
198         builder.append(indent);\r
199         builder.append(RCB);\r
200 \r
201         return builder.toString();\r
202     }\r
203 \r
204     public static String createGetter(final GeneratedProperty property,\r
205             final String indent) {\r
206         final StringBuilder builder = new StringBuilder();\r
207 \r
208         final Type type = property.getReturnType();\r
209         final String varName = property.getName();\r
210         final char first = Character.toUpperCase(varName.charAt(0));\r
211         final String methodName = "get" + first + varName.substring(1);\r
212 \r
213         builder.append(indent + PUBLIC + GAP + getExplicitType(type) + GAP\r
214                 + methodName);\r
215         builder.append(LB + RB + LCB + NL);\r
216 \r
217         String currentIndent = indent + TAB;\r
218 \r
219         builder.append(currentIndent + "return " + varName + SC + NL);\r
220 \r
221         builder.append(indent + RCB);\r
222         return builder.toString();\r
223     }\r
224 \r
225     public static String createHashCode(\r
226             final List<GeneratedProperty> properties, final String indent) {\r
227         StringBuilder builder = new StringBuilder();\r
228         builder.append(indent + "public int hashCode() {" + NL);\r
229         builder.append(indent + TAB + "final int prime = 31;" + NL);\r
230         builder.append(indent + TAB + "int result = 1;" + NL);\r
231 \r
232         for (GeneratedProperty property : properties) {\r
233             String fieldName = property.getName();\r
234             builder.append(indent + TAB + "result = prime * result + (("\r
235                     + fieldName + " == null) ? 0 : " + fieldName\r
236                     + ".hashCode());" + NL);\r
237         }\r
238 \r
239         builder.append(indent + TAB + "return result;" + NL);\r
240         builder.append(indent + RCB + NL);\r
241         return builder.toString();\r
242     }\r
243 \r
244     public static String createEquals(final GeneratedTransferObject type,\r
245             final List<GeneratedProperty> properties, final String indent) {\r
246         StringBuilder builder = new StringBuilder();\r
247         final String indent1 = indent + TAB;\r
248         final String indent2 = indent + TAB + TAB;\r
249         final String indent3 = indent + TAB + TAB + TAB;\r
250 \r
251         builder.append(indent + "public boolean equals(Object obj) {" + NL);\r
252         builder.append(indent1 + "if (this == obj) {" + NL);\r
253         builder.append(indent2 + "return true;" + NL);\r
254         builder.append(indent1 + "}" + NL);\r
255         builder.append(indent1 + "if (obj == null) {" + NL);\r
256         builder.append(indent2 + "return false;" + NL);\r
257         builder.append(indent1 + "}" + NL);\r
258         builder.append(indent1 + "if (getClass() != obj.getClass()) {" + NL);\r
259         builder.append(indent2 + "return false;" + NL);\r
260         builder.append(indent1 + "}" + NL);\r
261 \r
262         String typeStr = type.getPackageName() + "." + type.getName();\r
263         builder.append(indent1 + typeStr + " other = (" + typeStr + ") obj;"\r
264                 + NL);\r
265 \r
266         for (GeneratedProperty property : properties) {\r
267             String fieldName = property.getName();\r
268             builder.append(indent1 + "if (" + fieldName + " == null) {" + NL);\r
269             builder.append(indent2 + "if (other." + fieldName + " != null) {"\r
270                     + NL);\r
271             builder.append(indent3 + "return false;" + NL);\r
272             builder.append(indent2 + "}" + NL);\r
273             builder.append(indent1 + "} else if (!" + fieldName\r
274                     + ".equals(other." + fieldName + ")) {" + NL);\r
275             builder.append(indent2 + "return false;" + NL);\r
276             builder.append(indent1 + "}" + NL);\r
277         }\r
278 \r
279         builder.append(indent1 + "return true;" + NL);\r
280 \r
281         builder.append(indent + RCB + NL);\r
282         return builder.toString();\r
283     }\r
284 \r
285     public static String createToString(final GeneratedTransferObject type,\r
286             final List<GeneratedProperty> properties, final String indent) {\r
287         StringBuilder builder = new StringBuilder();\r
288         builder.append(indent);\r
289         builder.append("public String toString() {");\r
290         builder.append(NL);\r
291         builder.append(indent);\r
292         builder.append(TAB);\r
293         builder.append("StringBuilder builder = new StringBuilder();");\r
294         builder.append(NL);\r
295         builder.append(indent);\r
296         builder.append(TAB);\r
297         builder.append("builder.append(\"");\r
298         builder.append(type.getName());\r
299         builder.append(" [");\r
300 \r
301         boolean first = true;\r
302         for (GeneratedProperty property : properties) {\r
303             if (first) {\r
304                 builder.append(property.getName());\r
305                 builder.append("=\");");\r
306                 builder.append(NL);\r
307                 builder.append(indent);\r
308                 builder.append(TAB);\r
309                 builder.append("builder.append(");\r
310                 builder.append(property.getName());\r
311                 builder.append(");");\r
312                 first = false;\r
313             } else {\r
314                 builder.append(NL);\r
315                 builder.append(indent);\r
316                 builder.append(TAB);\r
317                 builder.append("builder.append(\", ");\r
318                 builder.append(property.getName());\r
319                 builder.append("=\");");\r
320                 builder.append(NL);\r
321                 builder.append(indent);\r
322                 builder.append(TAB);\r
323                 builder.append("builder.append(\", ");\r
324                 builder.append(property.getName());\r
325                 builder.append(");");\r
326             }\r
327         }\r
328         builder.append(NL);\r
329         builder.append(indent);\r
330         builder.append(TAB);\r
331         builder.append("builder.append(\"]\");");\r
332         builder.append(NL);\r
333         builder.append(indent);\r
334         builder.append(TAB);\r
335         builder.append("return builder.toString();");\r
336 \r
337         builder.append(NL);\r
338         builder.append(indent);\r
339         builder.append(RCB);\r
340         builder.append(NL);\r
341         return builder.toString();\r
342     }\r
343 \r
344     public static String createEnum(final Enumeration enumeration,\r
345             final String indent) {\r
346         final StringBuilder builder = new StringBuilder(indent + ENUM + GAP\r
347                 + enumeration.getName() + GAP + LCB + NL);\r
348 \r
349         String separator = COMMA;\r
350         final List<Pair> values = enumeration.getValues();\r
351         builder.append(indent + TAB);\r
352         for (int i = 0; i < values.size(); i++) {\r
353             if (i + 1 == values.size()) {\r
354                 separator = SC;\r
355             }\r
356             builder.append(values.get(i).getName() + separator);\r
357         }\r
358         builder.append(NL);\r
359         builder.append(indent + RCB);\r
360         return builder.toString();\r
361     }\r
362 \r
363     private static String getExplicitType(final Type type) {\r
364         String packageName = type.getPackageName();\r
365         if (packageName.endsWith(".")) {\r
366             packageName = packageName.substring(0, packageName.length() - 1);\r
367         }\r
368         final StringBuilder builder = new StringBuilder(packageName + "."\r
369                 + type.getName());\r
370         if (type instanceof ParameterizedType) {\r
371             ParameterizedType pType = (ParameterizedType) type;\r
372             Type[] pTypes = pType.getActualTypeArguments();\r
373             builder.append("<");\r
374             builder.append(getParameters(pTypes));\r
375             builder.append(">");\r
376         }\r
377         if (builder.toString().equals("java.lang.Void")) {\r
378             return "void";\r
379         }\r
380         return builder.toString();\r
381     }\r
382 \r
383     private static String getParameters(final Type[] pTypes) {\r
384         final StringBuilder builder = new StringBuilder();\r
385         for (int i = 0; i < pTypes.length; i++) {\r
386             Type t = pTypes[i];\r
387 \r
388             String separator = COMMA;\r
389             if (i + 1 == pTypes.length) {\r
390                 separator = "";\r
391             }\r
392             builder.append(getExplicitType(t) + separator);\r
393         }\r
394         return builder.toString();\r
395     }\r
396 \r
397     private static void createComment(final StringBuilder builder,\r
398             final String comment, final String indent) {\r
399         if (comment != null && comment.length() > 0) {\r
400             builder.append(indent + "/*" + NL);\r
401             builder.append(indent + comment + NL);\r
402             builder.append(indent + "*/" + NL);\r
403         }\r
404     }\r
405 \r
406 }\r

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.