Rework MethodSerializer (+ fallout)
[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 java.util.Optional;
12 import java.util.function.Consumer;
13 import java.util.stream.Collectors;
14 import javax.lang.model.element.Modifier;
15 import org.opendaylight.controller.config.yangjmxgenerator.plugin.util.StringUtil;
16
17 class MethodSerializer {
18
19     static String toString(Method method) {
20         StringBuilder build = new StringBuilder();
21         Consumer<Modifier> appendWithSpace = string -> build.append(string).append(" ");
22
23         if (method.getJavadoc() != null) {
24             build.append(StringUtil.writeComment(method.getJavadoc(), true));
25         }
26
27         method.getAnnotations().forEach(build::append);
28
29         build.append("    ");
30         method.getVisibility().ifPresent(appendWithSpace);
31         method.getModifiers().forEach(appendWithSpace);
32         build.append(method.getReturnType()).append(" ");
33
34         build.append(method.getName()).append("(");
35         boolean firstParam = true;
36         for (Field param : method.getParameters()) {
37             if (!firstParam) {
38                 build.append(", ");
39             }
40             param.getModifiers().forEach(appendWithSpace);
41             build.append(param.getType()).append(" ");
42             build.append(param.getName());
43             firstParam = false;
44         }
45         build.append(")");
46
47         if (!method.getThrowsExceptions().isEmpty()) {
48             build.append(" throws ");
49             build.append(method.getThrowsExceptions().stream().collect(Collectors.joining(", ")));
50         }
51
52         Optional<String> body = method.getBody();
53         if (!body.isPresent()) {
54             build.append(";");
55             build.append("\n");
56         } else {
57             build.append(" {");
58             build.append("\n");
59             build.append("        ");
60             build.append(body.get());
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 }