2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.directives;
10 import java.io.IOException;
11 import java.io.Writer;
12 import java.util.List;
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;
21 import com.google.common.collect.Lists;
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;
31 * Add annotations to freemarker template.
33 public class MethodsDirective implements TemplateDirectiveModel {
35 private static final String OBJECT = "object";
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();
44 if (object instanceof SimpleSequence)
45 methods = ((SimpleSequence) object).toList();
46 else if (object instanceof FtlTemplate) {
47 methods = ((FtlTemplate) object).getMethods();
49 throw new IllegalArgumentException(
50 "Object must be a SimpleSequence or instance of "
51 + FtlTemplate.class + "but was "
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(), " ");
61 if (!method.getAnnotations().isEmpty()) {
62 AnnotationsDirective.writeAnnotations(method.getAnnotations(),
66 build.append(" " + "public ");
67 for (String mod : method.getModifiers()) {
68 build.append(mod + " ");
70 build.append(method.getReturnType() + " ");
72 build.append(method.getName() + "(");
73 for (Field param : method.getParameters()) {
74 for (String mod : param.getModifiers()) {
75 build.append(mod + " ");
77 build.append(param.getType() + " ");
78 build.append(param.getName() + ", ");
80 if (method.getParameters().isEmpty())
83 build.deleteCharAt(build.length() - 1);
84 build.deleteCharAt(build.length() - 1);
88 if (method instanceof MethodDeclaration) {
90 build.append(System.lineSeparator());
91 } else if (method instanceof MethodDefinition) {
92 if (!((MethodDefinition) method).getThrowsExceptions()
94 build.append(" throws ");
95 for (String ex : ((MethodDefinition) method)
96 .getThrowsExceptions()) {
97 build.append(ex + " ");
100 build.append(System.lineSeparator());
102 build.append(((MethodDefinition) method).getBody());
103 build.append(System.lineSeparator());
106 build.append(System.lineSeparator());
108 build.append(System.lineSeparator());
112 if (!methods.isEmpty())
113 out.write(build.toString().toCharArray());