blob: 3ffc5e219397d7aa6e4df7b43b215668dae19ed1 [file] [log] [blame]
Bharat saraswal97459962016-02-20 21:57:16 +05301/*
2 * Copyright 2016 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.yangutils.translator.tojava.utils;
18
19import java.util.List;
20
21import org.onosproject.yangutils.datamodel.YangType;
22import org.onosproject.yangutils.translator.GeneratedFileType;
23import org.onosproject.yangutils.translator.tojava.AttributeInfo;
24import org.onosproject.yangutils.translator.tojava.GeneratedMethodTypes;
25import org.onosproject.yangutils.utils.UtilConstants;
26import org.onosproject.yangutils.utils.io.impl.JavaDocGen;
27
28/**
29 * Generated methods for generated files based on the file type.
30 */
31public final class MethodsGenerator {
32
33 private static String builderClassName;
34 private static List<AttributeInfo> attrInfo;
35
36 /**
37 * Sets the builder class name for setter methods of builder class.
38 *
39 * @param name builder class name
40 */
41 public static void setBuilderClassName(String name) {
42 builderClassName = name;
43 }
44
45 /**
46 * Sets the attribute info for the impl class's constructor method.
47 *
48 * @param attr list of attribute info
49 */
50 public static void setAttrInfo(List<AttributeInfo> attr) {
51 attrInfo = attr;
52 }
53
54 /**
55 * Returns attribute info for the impl class's constructor method.
56 *
57 * @return list of attribute info
58 */
59 public static List<AttributeInfo> getAttrInfo() {
60 return attrInfo;
61 }
62
63 /**
64 * Return the class name.
65 *
66 * @return class name
67 */
68 public static String getBuilderClassName() {
69 return builderClassName;
70 }
71
72 /**
73 * Default constructor.
74 */
75 private MethodsGenerator() {
76 }
77
78 /**
79 * Return method strings.
80 *
81 * @param attr attribute info.
82 * @param type generated file type
83 * @return method string
84 */
85 public static String getMethodString(AttributeInfo attr, GeneratedFileType type) {
86
87 if (type.equals(GeneratedFileType.BUILDER_CLASS)) {
88
89 return parseBuilderMethodString(attr);
90 } else if (type.equals(GeneratedFileType.INTERFACE)) {
91
92 return getGetterString(attr);
93 } else if (type.equals(GeneratedFileType.BUILDER_INTERFACE)) {
94
95 return parseBuilderInterfaceMethodString(attr);
96 } else if (type.equals(GeneratedFileType.IMPL)) {
97
98 return parseImplMethodString(attr);
99 }
100 return null;
101 }
102
103 /**
104 * Returns constructed method impl for specific generated file type.
105 *
106 * @param genFileTypes generated file type
107 * @param yangName class name
108 * @param methodTypes method types
109 * @param returnType return type of method
110 * @return constructed method impl
111 */
112 public static String constructMethodInfo(GeneratedFileType genFileTypes, String yangName,
113 GeneratedMethodTypes methodTypes, YangType<?> returnType) {
114
Bharat saraswal5e3c45c2016-02-22 22:15:21 +0530115 if (returnType == null) {
Bharat saraswal5e3c45c2016-02-22 22:15:21 +0530116 YangType<?> type = new YangType();
117 type.setDataTypeName(yangName);
118 returnType = type;
119 }
120
Bharat saraswal97459962016-02-20 21:57:16 +0530121 if (genFileTypes.equals(GeneratedFileType.INTERFACE)) {
122
123 /**
124 * If interface, only getter will be there.
125 */
126 return getGetterForInterface(yangName, returnType);
127 } else if (genFileTypes.equals(GeneratedFileType.BUILDER_INTERFACE)) {
128
129 /**
130 * If builder interface, getters and setters will be there.
131 */
132 if (methodTypes.equals(GeneratedMethodTypes.GETTER)) {
133
134 return getGetterForInterface(yangName, returnType);
135 } else if (methodTypes.equals(GeneratedMethodTypes.SETTER)) {
136
137 return getSetterForInterface(yangName, returnType);
138 } else if (methodTypes.equals(GeneratedMethodTypes.BUILD)) {
139
140 return getBuildForInterface(yangName);
141 }
142 } else if (genFileTypes.equals(GeneratedFileType.BUILDER_CLASS)) {
143
144 /**
145 * If Builder class , getters, setters ,build and default constructor impls will be there.
146 */
147 if (methodTypes.equals(GeneratedMethodTypes.GETTER)) {
148
149 return getGetterForClass(yangName, returnType);
150 } else if (methodTypes.equals(GeneratedMethodTypes.SETTER)) {
151
152 return getSetterForClass(yangName, returnType);
153 } else if (methodTypes.equals(GeneratedMethodTypes.BUILD)) {
154
155 return getBuild(yangName);
156 } else if (methodTypes.equals(GeneratedMethodTypes.DEFAULT_CONSTRUCTOR)) {
157
158 return getDefaultConstructor(yangName + UtilConstants.BUILDER);
159 }
160 } else if (genFileTypes.equals(GeneratedFileType.IMPL)) {
161
162 if (methodTypes.equals(GeneratedMethodTypes.GETTER)) {
163
164 return getGetterForClass(yangName, returnType);
165 } else if (methodTypes.equals(GeneratedMethodTypes.CONSTRUCTOR)) {
166
167 return getConstructor(yangName);
168 } else if (methodTypes.equals(GeneratedMethodTypes.DEFAULT_CONSTRUCTOR)) {
169
170 return getDefaultConstructor(yangName + UtilConstants.IMPL);
171 }
172 }
173 return null;
174 }
175
176 /**
177 * Returns the methods strings for builder class.
178 *
179 * @param attr attribute info.
180 * @return method string for builder class.
181 */
182 private static String parseBuilderMethodString(AttributeInfo attr) {
183
184 String overrideString = UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.OVERRIDE
185 + UtilConstants.NEW_LINE;
Bharat saraswal97459962016-02-20 21:57:16 +0530186 String getterString = JavaCodeSnippetGen.getJavaMethodInfo(GeneratedFileType.BUILDER_CLASS,
187 attr.getAttributeName(), GeneratedMethodTypes.GETTER, attr.getAttributeType());
188 String setterString = JavaCodeSnippetGen.getJavaMethodInfo(GeneratedFileType.BUILDER_CLASS,
189 attr.getAttributeName(), GeneratedMethodTypes.SETTER, attr.getAttributeType());
Bharat saraswalc46ee2a2016-02-25 02:26:43 +0530190 return overrideString + getterString + UtilConstants.NEW_LINE + overrideString + setterString;
Bharat saraswal97459962016-02-20 21:57:16 +0530191 }
192
193 /**
194 * Returns the methods strings for builder class.
195 *
196 * @param attr attribute info.
197 * @return method string for builder class.
198 */
199 private static String parseImplMethodString(AttributeInfo attr) {
200
201 return UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.OVERRIDE
202 + UtilConstants.NEW_LINE + JavaCodeSnippetGen.getJavaMethodInfo(GeneratedFileType.BUILDER_CLASS,
Bharat saraswalc46ee2a2016-02-25 02:26:43 +0530203 attr.getAttributeName(), GeneratedMethodTypes.GETTER, attr.getAttributeType());
Bharat saraswal97459962016-02-20 21:57:16 +0530204 }
205
206 /**
207 * Returns the methods strings for builder interface.
208 *
209 * @param attr attribute info.
210 * @return method string for builder interface.
211 */
212 private static String parseBuilderInterfaceMethodString(AttributeInfo attr) {
213
214 return getGetterString(attr) + UtilConstants.NEW_LINE + getSetterString(attr);
215 }
216
217 /**
218 * Returns the methods strings for builder interface.
219 *
220 * @param name attribute name.
221 * @return method string for builder interface.
222 */
223 public static String parseBuilderInterfaceBuildMethodString(String name) {
224
225 return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.BUILD, name) + JavaCodeSnippetGen
226 .getJavaMethodInfo(GeneratedFileType.BUILDER_INTERFACE, name, GeneratedMethodTypes.BUILD, null);
227 }
228
229 /**
230 * Returns getter string.
231 *
232 * @param attr attribute info.
233 * @return getter string
234 */
235 public static String getGetterString(AttributeInfo attr) {
236
237 return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.GETTER, attr.getAttributeName())
238 + JavaCodeSnippetGen.getJavaMethodInfo(GeneratedFileType.INTERFACE, attr.getAttributeName(),
239 GeneratedMethodTypes.GETTER, attr.getAttributeType())
240 + UtilConstants.NEW_LINE;
241 }
242
243 /**
244 * Returns setter string.
245 *
246 * @param attr attribute info.
247 * @return setter string
248 */
249 private static String getSetterString(AttributeInfo attr) {
250
251 return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.SETTER, attr.getAttributeName())
252 + JavaCodeSnippetGen.getJavaMethodInfo(GeneratedFileType.BUILDER_INTERFACE, attr.getAttributeName(),
253 GeneratedMethodTypes.SETTER, attr.getAttributeType());
254 }
255
256 /**
257 * Returns constructor method string.
258 *
259 * @param name class name
260 * @return constructor string
261 */
262 public static String getConstructorString(String name) {
263
264 return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.CONSTRUCTOR, name) + JavaCodeSnippetGen
265 .getJavaMethodInfo(GeneratedFileType.IMPL, name, GeneratedMethodTypes.CONSTRUCTOR, null);
266 }
267
268 /**
269 * Returns default constructor method string.
270 *
271 * @param type generated file type
272 * @param name class name
273 * @return default constructor string
274 */
275 public static String getDefaultConstructorString(GeneratedFileType type, String name) {
276
277 return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.DEFAULT_CONSTRUCTOR, name)
278 + JavaCodeSnippetGen.getJavaMethodInfo(type, name, GeneratedMethodTypes.DEFAULT_CONSTRUCTOR, null);
279 }
280
281 /**
282 * Returns build method string.
283 *
284 * @param name class name
285 * @return build string
286 */
287 public static String getBuildString(String name) {
288
289 return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.OVERRIDE + UtilConstants.NEW_LINE
290 + JavaCodeSnippetGen.getJavaMethodInfo(GeneratedFileType.BUILDER_CLASS, name,
291 GeneratedMethodTypes.BUILD, null);
292 }
293
294 /**
295 * Returns the getter method strings for class file.
296 *
297 * @param yangName name of the attribute.
298 * @param returnType return type of attribute
299 * @return getter method for class.
300 */
301 private static String getGetterForClass(String yangName, YangType<?> returnType) {
302
303 return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
Bharat saraswal5e3c45c2016-02-22 22:15:21 +0530304 + JavaIdentifierSyntax.getCaptialCase(returnType.getDataTypeName()) + UtilConstants.SPACE
305 + UtilConstants.GET_METHOD_PREFIX + JavaIdentifierSyntax.getCaptialCase(yangName)
Bharat saraswal97459962016-02-20 21:57:16 +0530306 + UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE
307 + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE + UtilConstants.EIGHT_SPACE_INDENTATION
308 + UtilConstants.RETURN + UtilConstants.SPACE + yangName + UtilConstants.SEMI_COLAN
309 + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET;
310 }
311
312 /**
313 * Returns the setter method strings for class file.
314 *
315 * @param yangName name of the attribute.
316 * @param returnType return type of attribute
317 * @return setter method for class.
318 */
319 private static String getSetterForClass(String yangName, YangType<?> returnType) {
320
321 return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE + getBuilderClassName()
Bharat saraswal5e3c45c2016-02-22 22:15:21 +0530322 + UtilConstants.SPACE + UtilConstants.SET_METHOD_PREFIX + JavaIdentifierSyntax.getCaptialCase(yangName)
323 + UtilConstants.OPEN_PARENTHESIS + JavaIdentifierSyntax.getCaptialCase(returnType.getDataTypeName())
324 + UtilConstants.SPACE + yangName + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE
325 + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE + UtilConstants.EIGHT_SPACE_INDENTATION
326 + UtilConstants.THIS + UtilConstants.PERIOD + yangName + UtilConstants.SPACE + UtilConstants.EQUAL
327 + UtilConstants.SPACE + yangName + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
328 + UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.RETURN + UtilConstants.SPACE
329 + UtilConstants.THIS + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
Bharat saraswal97459962016-02-20 21:57:16 +0530330 + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET;
331 }
332
333 /**
334 * Returns the getter method strings for interface file.
335 *
336 * @param yangName name of the attribute.
337 * @param returnType return type of attribute
338 * @return getter method for interface.
339 */
340 private static String getGetterForInterface(String yangName, YangType<?> returnType) {
341 returnType.setDataTypeName(returnType.getDataTypeName().replace("\"", ""));
Bharat saraswal5e3c45c2016-02-22 22:15:21 +0530342 return UtilConstants.FOUR_SPACE_INDENTATION + JavaIdentifierSyntax.getCaptialCase(returnType.getDataTypeName())
343 + UtilConstants.SPACE + UtilConstants.GET_METHOD_PREFIX + JavaIdentifierSyntax.getCaptialCase(yangName)
344 + UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN;
Bharat saraswal97459962016-02-20 21:57:16 +0530345 }
346
347 /**
348 * Returns the setter method strings for interface file.
349 *
350 * @param yangName name of the attribute.
351 * @param returnType return type of attribute
352 * @return setter method for interface.
353 */
354 private static String getSetterForInterface(String yangName, YangType<?> returnType) {
355 return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.BUILDER + UtilConstants.SPACE
Bharat saraswal5e3c45c2016-02-22 22:15:21 +0530356 + UtilConstants.SET_METHOD_PREFIX + JavaIdentifierSyntax.getCaptialCase(yangName)
357 + UtilConstants.OPEN_PARENTHESIS + JavaIdentifierSyntax.getCaptialCase(returnType.getDataTypeName())
358 + UtilConstants.SPACE + yangName + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN;
Bharat saraswal97459962016-02-20 21:57:16 +0530359 }
360
361 /**
362 * Returns the build method strings for interface file.
363 *
364 * @param yangName name of the attribute.
365 * @param returnType return type of attribute
366 * @return build method for interface.
367 */
368 private static String getBuildForInterface(String yangName) {
369
370 return UtilConstants.FOUR_SPACE_INDENTATION + yangName + UtilConstants.SPACE + UtilConstants.BUILD
371 + UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN;
372 }
373
374 /**
375 * Returns the constructor strings for class file.
376 *
377 * @param yangName name of the class.
378 * @return constructor for class
379 */
380 private static String getConstructor(String yangName) {
381
382 String builderAttribute = (yangName.substring(0, 1).toLowerCase() + yangName.substring(1));
383 String constructor = UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
384 + yangName + UtilConstants.IMPL + UtilConstants.OPEN_PARENTHESIS + yangName + UtilConstants.BUILDER
385 + UtilConstants.SPACE + builderAttribute + UtilConstants.OBJECT + UtilConstants.CLOSE_PARENTHESIS
386 + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE;
387
388 if (getAttrInfo() != null) {
389 for (AttributeInfo attribute : getAttrInfo()) {
390 attribute.setAttributeName(JavaIdentifierSyntax.getCamelCase(attribute.getAttributeName()));
391 constructor = constructor + UtilConstants.TWELVE_SPACE_INDENTATION + UtilConstants.THIS
392 + UtilConstants.PERIOD + attribute.getAttributeName() + UtilConstants.SPACE
393 + UtilConstants.EQUAL + UtilConstants.SPACE + builderAttribute + UtilConstants.OBJECT
Bharat saraswal5e3c45c2016-02-22 22:15:21 +0530394 + UtilConstants.PERIOD + UtilConstants.GET_METHOD_PREFIX
395 + JavaIdentifierSyntax.getCaptialCase(attribute.getAttributeName())
Bharat saraswal97459962016-02-20 21:57:16 +0530396 + UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN
397 + UtilConstants.NEW_LINE;
398 }
399 getAttrInfo().clear();
400 }
Bharat saraswalc46ee2a2016-02-25 02:26:43 +0530401 return constructor + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET;
Bharat saraswal97459962016-02-20 21:57:16 +0530402 }
403
404 /**
405 * Returns the build method strings for class file.
406 *
407 * @param yangName class name
408 * @return build method string for class.
409 */
410 private static String getBuild(String yangName) {
411
412 return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE + yangName
413 + UtilConstants.SPACE + UtilConstants.BUILD + UtilConstants.OPEN_PARENTHESIS
414 + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
415 + UtilConstants.NEW_LINE + UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.RETURN
416 + UtilConstants.SPACE + UtilConstants.NEW + UtilConstants.SPACE + yangName + UtilConstants.IMPL
417 + UtilConstants.OPEN_PARENTHESIS + UtilConstants.THIS + UtilConstants.CLOSE_PARENTHESIS
418 + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
419 + UtilConstants.CLOSE_CURLY_BRACKET;
420 }
421
422 /**
423 * Returns the Default constructor strings for class file.
424 *
425 * @param yangName name of the class.
426 * @return Default constructor for class
427 */
428 private static String getDefaultConstructor(String name) {
429
430 return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE + name
431 + UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE
432 + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
433 + UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE;
434 }
435
436}