2 * Copyright (c) 2014 Brocade Communications 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
9 package org.opendaylight.mdsal.binding.generator.util;
12 import java.io.FileWriter;
13 import java.io.IOException;
14 import javassist.CtClass;
15 import javassist.CtField;
16 import javassist.CtMethod;
17 import javassist.Modifier;
18 import javassist.NotFoundException;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * The default implementation of the SourceCodeGenerator interface that generates readable source code
24 * for a runtime generated class. The appendField/appendMethod methods output source code to a temporary
25 * StringBuilder. When outputGeneratedSource is called, the entire class source code is generated and
26 * written to a file under a specified directory.
28 * @author Thomas Pantelis
30 public class DefaultSourceCodeGenerator implements SourceCodeGenerator {
31 private static final Logger LOG = LoggerFactory.getLogger(DefaultSourceCodeGenerator.class);
33 private static final String GENERATED_SOURCE_DIR_PROP = "org.opendaylight.yangtools.sal.generatedCodecSourceDir";
35 private final StringBuilder builder = new StringBuilder();
36 private final String generatedSourceDir;
41 * @param generatedSourceDir the directory in which to put generated source files. If null, the directory
42 * is obtained from a system property (<i>org.opendaylight.yangtools.sal.generatedCodecSourceDir</i>) or
43 * defaults to "generated-codecs".
45 public DefaultSourceCodeGenerator(final String generatedSourceDir) {
46 if (generatedSourceDir != null) {
47 this.generatedSourceDir = generatedSourceDir;
49 this.generatedSourceDir = System.getProperty(GENERATED_SOURCE_DIR_PROP, "generated-codecs");
54 public void appendField(final CtField field, final String value) {
57 .append(Modifier.toString(field.getModifiers()))
58 .append(' ').append(field.getType().getName()).append(' ')
59 .append(field.getName());
61 builder.append(" = ").append(value);
64 builder.append(";\n");
65 } catch (NotFoundException e) {
66 LOG.error("Error building field source for " + field.getName(), e);
71 public void appendMethod(final CtMethod method, final String code) {
74 .append(Modifier.toString(method.getModifiers()))
75 .append(' ').append(method.getReturnType().getName())
76 .append(' ').append(method.getName()).append("( ");
78 CtClass[] paramTypes = method.getParameterTypes();
79 if (paramTypes != null) {
80 for (int i = 0; i < paramTypes.length; i++) {
84 builder.append(paramTypes[i].getName()).append(" $")
89 builder.append(" )\n").append(code).append("\n\n");
90 } catch (NotFoundException e) {
91 LOG.error("Error building method source for " + method.getName(), e);
96 public void outputGeneratedSource(final CtClass ctClass) {
97 String name = ctClass.getName();
99 StringBuilder classBuilder = new StringBuilder();
100 classBuilder.append(Modifier.toString(ctClass.getModifiers()))
101 .append(" class ").append(ctClass.getSimpleName());
104 CtClass superClass = ctClass.getSuperclass();
105 if (superClass != null) {
106 classBuilder.append(" extends ").append(superClass.getName());
109 CtClass[] interfaces = ctClass.getInterfaces();
110 if (interfaces.length > 0) {
111 classBuilder.append(" implements ");
112 for (int i = 0; i < interfaces.length; i++) {
114 classBuilder.append(", ");
117 classBuilder.append(interfaces[i].getName());
121 classBuilder.append(" {\n").append(builder.toString())
123 } catch (NotFoundException e) {
124 LOG.error("Error building class source for " + name, e);
128 File dir = new File(generatedSourceDir);
130 try (FileWriter writer = new FileWriter(new File(dir, name + ".java"))) {
131 writer.append(classBuilder.toString());
133 } catch (IOException e) {
134 LOG.error("Error writing class source for " + name, e);