BUG-509: add missing copyright headers
[controller.git] / opendaylight / config / yang-jmx-generator-plugin / src / main / java / org / opendaylight / controller / config / yangjmxgenerator / plugin / ftl / directives / MethodsDirective.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 package org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.directives;
9
10 import java.io.IOException;
11 import java.io.Writer;
12 import java.util.List;
13 import java.util.Map;
14
15 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.FtlTemplate;
16 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Field;
17 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Method;
18 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.MethodDeclaration;
19 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.MethodDefinition;
20
21 import com.google.common.collect.Lists;
22
23 import freemarker.core.Environment;
24 import freemarker.template.SimpleSequence;
25 import freemarker.template.TemplateDirectiveBody;
26 import freemarker.template.TemplateDirectiveModel;
27 import freemarker.template.TemplateException;
28 import freemarker.template.TemplateModel;
29
30 /**
31  * Add annotations to freemarker template.
32  */
33 public class MethodsDirective implements TemplateDirectiveModel {
34
35     private static final String OBJECT = "object";
36
37     @Override
38     public void execute(Environment env, Map params, TemplateModel[] loopVars,
39             TemplateDirectiveBody body) throws TemplateException, IOException {
40         Object object = params.get(OBJECT);
41         List<? extends Method> methods = Lists.newArrayList();
42
43         if (object != null) {
44             if (object instanceof SimpleSequence)
45                 methods = ((SimpleSequence) object).toList();
46             else if (object instanceof FtlTemplate) {
47                 methods = ((FtlTemplate) object).getMethods();
48             } else
49                 throw new IllegalArgumentException(
50                         "Object must be a SimpleSequence or instance of "
51                                 + FtlTemplate.class + "but was "
52                                 + object.getClass());
53         }
54
55         Writer out = env.getOut();
56         StringBuilder build = new StringBuilder();
57         for (Method method : methods) {
58             if (method.getJavadoc() != null)
59                 JavadocDirective.writeJavadoc(build, method.getJavadoc(), "    ");
60
61             if (!method.getAnnotations().isEmpty()) {
62                 AnnotationsDirective.writeAnnotations(method.getAnnotations(),
63                         build, "    ");
64             }
65
66             build.append("    " + "public ");
67             for (String mod : method.getModifiers()) {
68                 build.append(mod + " ");
69             }
70             build.append(method.getReturnType() + " ");
71
72             build.append(method.getName() + "(");
73             for (Field param : method.getParameters()) {
74                 for (String mod : param.getModifiers()) {
75                     build.append(mod + " ");
76                 }
77                 build.append(param.getType() + " ");
78                 build.append(param.getName() + ", ");
79             }
80             if (method.getParameters().isEmpty())
81                 build.append(")");
82             else {
83                 build.deleteCharAt(build.length() - 1);
84                 build.deleteCharAt(build.length() - 1);
85                 build.append(')');
86             }
87
88             if (method instanceof MethodDeclaration) {
89                 build.append(";");
90                 build.append(System.lineSeparator());
91             } else if (method instanceof MethodDefinition) {
92                 if (!((MethodDefinition) method).getThrowsExceptions()
93                         .isEmpty())
94                     build.append(" throws ");
95                 for (String ex : ((MethodDefinition) method)
96                         .getThrowsExceptions()) {
97                     build.append(ex + " ");
98                 }
99                 build.append(" {");
100                 build.append(System.lineSeparator());
101                 build.append("        ");
102                 build.append(((MethodDefinition) method).getBody());
103                 build.append(System.lineSeparator());
104                 build.append("    ");
105                 build.append("}");
106                 build.append(System.lineSeparator());
107             }
108             build.append(System.lineSeparator());
109
110         }
111
112         if (!methods.isEmpty())
113             out.write(build.toString().toCharArray());
114     }
115 }