Merge "Bug 509: Improve logging in InMemoryDataStore."
[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 + " ");
28         }
29         build.append(method.getReturnType() + " ");
30
31         build.append(method.getName() + "(");
32         for (Field param : method.getParameters()) {
33             for (String mod : param.getModifiers()) {
34                 build.append(mod + " ");
35             }
36             build.append(param.getType() + " ");
37             build.append(param.getName() + ", ");
38         }
39         if (method.getParameters().isEmpty()) {
40             build.append(")");
41         } else {
42             build.deleteCharAt(build.length() - 1);
43             build.deleteCharAt(build.length() - 1);
44             build.append(')');
45         }
46
47         if (method instanceof MethodDeclaration) {
48             build.append(";");
49             build.append("\n");
50         } else if (method instanceof MethodDefinition) {
51             if (!((MethodDefinition) method).getThrowsExceptions()
52                     .isEmpty()) {
53                 build.append(" throws ");
54             }
55             for (String ex : ((MethodDefinition) method)
56                     .getThrowsExceptions()) {
57                 build.append(ex + " ");
58             }
59             build.append(" {");
60             build.append("\n");
61             build.append("        ");
62             build.append(((MethodDefinition) method).getBody());
63             build.append("\n");
64             build.append("    ");
65             build.append("}");
66             build.append("\n");
67         }
68         build.append("\n");
69         return build.toString();
70     }
71 }