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