Yang code generator cleanup
[controller.git] / opendaylight / config / yang-jmx-generator-plugin / src / main / java / org / opendaylight / controller / config / yangjmxgenerator / plugin / ftl / model / MethodSerializer.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model;
10
11 import org.opendaylight.controller.config.yangjmxgenerator.plugin.util.StringUtil;
12
13 class MethodSerializer {
14
15     static String toString(Method method) {
16         StringBuilder build = new StringBuilder();
17         if (method.getJavadoc() != null) {
18             build.append(StringUtil.writeComment(method.getJavadoc(), true));
19         }
20
21         for(Annotation a: method.getAnnotations()) {
22             build.append(a);
23         }
24
25         build.append("    " + "public ");
26         for (String mod : method.getModifiers()) {
27             build.append(mod).append(" ");
28         }
29         build.append(method.getReturnType()).append(" ");
30
31         build.append(method.getName()).append("(");
32         boolean firstParam = true;
33         for (Field param : method.getParameters()) {
34             if (!firstParam) {
35                 build.append(", ");
36             }
37             for (String mod : param.getModifiers()) {
38                 build.append(mod).append(" ");
39             }
40             build.append(param.getType()).append(" ");
41             build.append(param.getName());
42             firstParam = false;
43         }
44         build.append(")");
45
46         if (method instanceof MethodDeclaration) {
47             build.append(";");
48             build.append("\n");
49         } else if (method instanceof MethodDefinition) {
50             MethodDefinition definition = (MethodDefinition) method;
51             if (!definition.getThrowsExceptions().isEmpty()) {
52                 build.append(" throws ");
53             }
54             for (String ex : definition.getThrowsExceptions()) {
55                 build.append(ex).append(" ");
56             }
57             build.append(" {");
58             build.append("\n");
59             build.append("        ");
60             build.append(definition.getBody());
61             build.append("\n");
62             build.append("    ");
63             build.append("}");
64             build.append("\n");
65         }
66         build.append("\n");
67         return build.toString();
68     }
69 }