blob: 2092441f9df73e9617c61caa2c15a18bd3f0473b [file] [log] [blame]
Bharat saraswal870c56f2016-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 saraswal594bc6d2016-02-22 22:15:21 +0530115 if (returnType == null) {
116 @SuppressWarnings("rawtypes")
117 YangType<?> type = new YangType();
118 type.setDataTypeName(yangName);
119 returnType = type;
120 }
121
Bharat saraswal870c56f2016-02-20 21:57:16 +0530122 if (genFileTypes.equals(GeneratedFileType.INTERFACE)) {
123
124 /**
125 * If interface, only getter will be there.
126 */
127 return getGetterForInterface(yangName, returnType);
128 } else if (genFileTypes.equals(GeneratedFileType.BUILDER_INTERFACE)) {
129
130 /**
131 * If builder interface, getters and setters will be there.
132 */
133 if (methodTypes.equals(GeneratedMethodTypes.GETTER)) {
134
135 return getGetterForInterface(yangName, returnType);
136 } else if (methodTypes.equals(GeneratedMethodTypes.SETTER)) {
137
138 return getSetterForInterface(yangName, returnType);
139 } else if (methodTypes.equals(GeneratedMethodTypes.BUILD)) {
140
141 return getBuildForInterface(yangName);
142 }
143 } else if (genFileTypes.equals(GeneratedFileType.BUILDER_CLASS)) {
144
145 /**
146 * If Builder class , getters, setters ,build and default constructor impls will be there.
147 */
148 if (methodTypes.equals(GeneratedMethodTypes.GETTER)) {
149
150 return getGetterForClass(yangName, returnType);
151 } else if (methodTypes.equals(GeneratedMethodTypes.SETTER)) {
152
153 return getSetterForClass(yangName, returnType);
154 } else if (methodTypes.equals(GeneratedMethodTypes.BUILD)) {
155
156 return getBuild(yangName);
157 } else if (methodTypes.equals(GeneratedMethodTypes.DEFAULT_CONSTRUCTOR)) {
158
159 return getDefaultConstructor(yangName + UtilConstants.BUILDER);
160 }
161 } else if (genFileTypes.equals(GeneratedFileType.IMPL)) {
162
163 if (methodTypes.equals(GeneratedMethodTypes.GETTER)) {
164
165 return getGetterForClass(yangName, returnType);
166 } else if (methodTypes.equals(GeneratedMethodTypes.CONSTRUCTOR)) {
167
168 return getConstructor(yangName);
169 } else if (methodTypes.equals(GeneratedMethodTypes.DEFAULT_CONSTRUCTOR)) {
170
171 return getDefaultConstructor(yangName + UtilConstants.IMPL);
172 }
173 }
174 return null;
175 }
176
177 /**
178 * Returns the methods strings for builder class.
179 *
180 * @param attr attribute info.
181 * @return method string for builder class.
182 */
183 private static String parseBuilderMethodString(AttributeInfo attr) {
184
185 String overrideString = UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.OVERRIDE
186 + UtilConstants.NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530187 String getterString = JavaCodeSnippetGen.getJavaMethodInfo(GeneratedFileType.BUILDER_CLASS,
188 attr.getAttributeName(), GeneratedMethodTypes.GETTER, attr.getAttributeType());
189 String setterString = JavaCodeSnippetGen.getJavaMethodInfo(GeneratedFileType.BUILDER_CLASS,
190 attr.getAttributeName(), GeneratedMethodTypes.SETTER, attr.getAttributeType());
Bharat saraswal870c56f2016-02-20 21:57:16 +0530191 return overrideString + getterString + UtilConstants.NEW_LINE + overrideString + setterString
192 + UtilConstants.NEW_LINE;
193 }
194
195 /**
196 * Returns the methods strings for builder class.
197 *
198 * @param attr attribute info.
199 * @return method string for builder class.
200 */
201 private static String parseImplMethodString(AttributeInfo attr) {
202
203 return UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.OVERRIDE
204 + UtilConstants.NEW_LINE + JavaCodeSnippetGen.getJavaMethodInfo(GeneratedFileType.BUILDER_CLASS,
205 attr.getAttributeName(), GeneratedMethodTypes.GETTER, attr.getAttributeType())
206 + UtilConstants.NEW_LINE;
207 }
208
209 /**
210 * Returns the methods strings for builder interface.
211 *
212 * @param attr attribute info.
213 * @return method string for builder interface.
214 */
215 private static String parseBuilderInterfaceMethodString(AttributeInfo attr) {
216
217 return getGetterString(attr) + UtilConstants.NEW_LINE + getSetterString(attr);
218 }
219
220 /**
221 * Returns the methods strings for builder interface.
222 *
223 * @param name attribute name.
224 * @return method string for builder interface.
225 */
226 public static String parseBuilderInterfaceBuildMethodString(String name) {
227
228 return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.BUILD, name) + JavaCodeSnippetGen
229 .getJavaMethodInfo(GeneratedFileType.BUILDER_INTERFACE, name, GeneratedMethodTypes.BUILD, null);
230 }
231
232 /**
233 * Returns getter string.
234 *
235 * @param attr attribute info.
236 * @return getter string
237 */
238 public static String getGetterString(AttributeInfo attr) {
239
240 return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.GETTER, attr.getAttributeName())
241 + JavaCodeSnippetGen.getJavaMethodInfo(GeneratedFileType.INTERFACE, attr.getAttributeName(),
242 GeneratedMethodTypes.GETTER, attr.getAttributeType())
243 + UtilConstants.NEW_LINE;
244 }
245
246 /**
247 * Returns setter string.
248 *
249 * @param attr attribute info.
250 * @return setter string
251 */
252 private static String getSetterString(AttributeInfo attr) {
253
254 return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.SETTER, attr.getAttributeName())
255 + JavaCodeSnippetGen.getJavaMethodInfo(GeneratedFileType.BUILDER_INTERFACE, attr.getAttributeName(),
256 GeneratedMethodTypes.SETTER, attr.getAttributeType());
257 }
258
259 /**
260 * Returns constructor method string.
261 *
262 * @param name class name
263 * @return constructor string
264 */
265 public static String getConstructorString(String name) {
266
267 return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.CONSTRUCTOR, name) + JavaCodeSnippetGen
268 .getJavaMethodInfo(GeneratedFileType.IMPL, name, GeneratedMethodTypes.CONSTRUCTOR, null);
269 }
270
271 /**
272 * Returns default constructor method string.
273 *
274 * @param type generated file type
275 * @param name class name
276 * @return default constructor string
277 */
278 public static String getDefaultConstructorString(GeneratedFileType type, String name) {
279
280 return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.DEFAULT_CONSTRUCTOR, name)
281 + JavaCodeSnippetGen.getJavaMethodInfo(type, name, GeneratedMethodTypes.DEFAULT_CONSTRUCTOR, null);
282 }
283
284 /**
285 * Returns build method string.
286 *
287 * @param name class name
288 * @return build string
289 */
290 public static String getBuildString(String name) {
291
292 return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.OVERRIDE + UtilConstants.NEW_LINE
293 + JavaCodeSnippetGen.getJavaMethodInfo(GeneratedFileType.BUILDER_CLASS, name,
294 GeneratedMethodTypes.BUILD, null);
295 }
296
297 /**
298 * Returns the getter method strings for class file.
299 *
300 * @param yangName name of the attribute.
301 * @param returnType return type of attribute
302 * @return getter method for class.
303 */
304 private static String getGetterForClass(String yangName, YangType<?> returnType) {
305
306 return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530307 + JavaIdentifierSyntax.getCaptialCase(returnType.getDataTypeName()) + UtilConstants.SPACE
308 + UtilConstants.GET_METHOD_PREFIX + JavaIdentifierSyntax.getCaptialCase(yangName)
Bharat saraswal870c56f2016-02-20 21:57:16 +0530309 + UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE
310 + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE + UtilConstants.EIGHT_SPACE_INDENTATION
311 + UtilConstants.RETURN + UtilConstants.SPACE + yangName + UtilConstants.SEMI_COLAN
312 + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET;
313 }
314
315 /**
316 * Returns the setter method strings for class file.
317 *
318 * @param yangName name of the attribute.
319 * @param returnType return type of attribute
320 * @return setter method for class.
321 */
322 private static String getSetterForClass(String yangName, YangType<?> returnType) {
323
324 return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE + getBuilderClassName()
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530325 + UtilConstants.SPACE + UtilConstants.SET_METHOD_PREFIX + JavaIdentifierSyntax.getCaptialCase(yangName)
326 + UtilConstants.OPEN_PARENTHESIS + JavaIdentifierSyntax.getCaptialCase(returnType.getDataTypeName())
327 + UtilConstants.SPACE + yangName + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE
328 + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE + UtilConstants.EIGHT_SPACE_INDENTATION
329 + UtilConstants.THIS + UtilConstants.PERIOD + yangName + UtilConstants.SPACE + UtilConstants.EQUAL
330 + UtilConstants.SPACE + yangName + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
331 + UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.RETURN + UtilConstants.SPACE
332 + UtilConstants.THIS + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
Bharat saraswal870c56f2016-02-20 21:57:16 +0530333 + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET;
334 }
335
336 /**
337 * Returns the getter method strings for interface file.
338 *
339 * @param yangName name of the attribute.
340 * @param returnType return type of attribute
341 * @return getter method for interface.
342 */
343 private static String getGetterForInterface(String yangName, YangType<?> returnType) {
344 returnType.setDataTypeName(returnType.getDataTypeName().replace("\"", ""));
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530345 return UtilConstants.FOUR_SPACE_INDENTATION + JavaIdentifierSyntax.getCaptialCase(returnType.getDataTypeName())
346 + UtilConstants.SPACE + UtilConstants.GET_METHOD_PREFIX + JavaIdentifierSyntax.getCaptialCase(yangName)
347 + UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530348 }
349
350 /**
351 * Returns the setter method strings for interface file.
352 *
353 * @param yangName name of the attribute.
354 * @param returnType return type of attribute
355 * @return setter method for interface.
356 */
357 private static String getSetterForInterface(String yangName, YangType<?> returnType) {
358 return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.BUILDER + UtilConstants.SPACE
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530359 + UtilConstants.SET_METHOD_PREFIX + JavaIdentifierSyntax.getCaptialCase(yangName)
360 + UtilConstants.OPEN_PARENTHESIS + JavaIdentifierSyntax.getCaptialCase(returnType.getDataTypeName())
361 + UtilConstants.SPACE + yangName + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530362 }
363
364 /**
365 * Returns the build method strings for interface file.
366 *
367 * @param yangName name of the attribute.
368 * @param returnType return type of attribute
369 * @return build method for interface.
370 */
371 private static String getBuildForInterface(String yangName) {
372
373 return UtilConstants.FOUR_SPACE_INDENTATION + yangName + UtilConstants.SPACE + UtilConstants.BUILD
374 + UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN;
375 }
376
377 /**
378 * Returns the constructor strings for class file.
379 *
380 * @param yangName name of the class.
381 * @return constructor for class
382 */
383 private static String getConstructor(String yangName) {
384
385 String builderAttribute = (yangName.substring(0, 1).toLowerCase() + yangName.substring(1));
386 String constructor = UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
387 + yangName + UtilConstants.IMPL + UtilConstants.OPEN_PARENTHESIS + yangName + UtilConstants.BUILDER
388 + UtilConstants.SPACE + builderAttribute + UtilConstants.OBJECT + UtilConstants.CLOSE_PARENTHESIS
389 + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE;
390
391 if (getAttrInfo() != null) {
392 for (AttributeInfo attribute : getAttrInfo()) {
393 attribute.setAttributeName(JavaIdentifierSyntax.getCamelCase(attribute.getAttributeName()));
394 constructor = constructor + UtilConstants.TWELVE_SPACE_INDENTATION + UtilConstants.THIS
395 + UtilConstants.PERIOD + attribute.getAttributeName() + UtilConstants.SPACE
396 + UtilConstants.EQUAL + UtilConstants.SPACE + builderAttribute + UtilConstants.OBJECT
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530397 + UtilConstants.PERIOD + UtilConstants.GET_METHOD_PREFIX
398 + JavaIdentifierSyntax.getCaptialCase(attribute.getAttributeName())
Bharat saraswal870c56f2016-02-20 21:57:16 +0530399 + UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN
400 + UtilConstants.NEW_LINE;
401 }
402 getAttrInfo().clear();
403 }
404 return constructor + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET
405 + UtilConstants.NEW_LINE;
406 }
407
408 /**
409 * Returns the build method strings for class file.
410 *
411 * @param yangName class name
412 * @return build method string for class.
413 */
414 private static String getBuild(String yangName) {
415
416 return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE + yangName
417 + UtilConstants.SPACE + UtilConstants.BUILD + UtilConstants.OPEN_PARENTHESIS
418 + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
419 + UtilConstants.NEW_LINE + UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.RETURN
420 + UtilConstants.SPACE + UtilConstants.NEW + UtilConstants.SPACE + yangName + UtilConstants.IMPL
421 + UtilConstants.OPEN_PARENTHESIS + UtilConstants.THIS + UtilConstants.CLOSE_PARENTHESIS
422 + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
423 + UtilConstants.CLOSE_CURLY_BRACKET;
424 }
425
426 /**
427 * Returns the Default constructor strings for class file.
428 *
429 * @param yangName name of the class.
430 * @return Default constructor for class
431 */
432 private static String getDefaultConstructor(String name) {
433
434 return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE + name
435 + UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE
436 + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
437 + UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE;
438 }
439
440}