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;
11 import com.google.common.io.Files;
12 import java.io.BufferedWriter;
14 import java.io.IOException;
15 import java.nio.charset.StandardCharsets;
16 import javassist.CtClass;
17 import javassist.CtField;
18 import javassist.CtMethod;
19 import javassist.Modifier;
20 import javassist.NotFoundException;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * The default implementation of the SourceCodeGenerator interface that generates readable source code
26 * for a runtime generated class. The appendField/appendMethod methods output source code to a temporary
27 * StringBuilder. When outputGeneratedSource is called, the entire class source code is generated and
28 * written to a file under a specified directory.
30 * @author Thomas Pantelis
32 public class DefaultSourceCodeGenerator implements SourceCodeGenerator {
33 private static final Logger LOG = LoggerFactory.getLogger(DefaultSourceCodeGenerator.class);
35 private static final String GENERATED_SOURCE_DIR_PROP = "org.opendaylight.yangtools.sal.generatedCodecSourceDir";
37 private final StringBuilder builder = new StringBuilder();
38 private final String generatedSourceDir;
43 * @param generatedSourceDir the directory in which to put generated source files. If null, the directory
44 * is obtained from a system property (<i>org.opendaylight.yangtools.sal.generatedCodecSourceDir</i>) or
45 * defaults to "generated-codecs".
47 public DefaultSourceCodeGenerator(final String generatedSourceDir) {
48 if (generatedSourceDir != null) {
49 this.generatedSourceDir = generatedSourceDir;
51 this.generatedSourceDir = System.getProperty(GENERATED_SOURCE_DIR_PROP, "generated-codecs");
56 public void appendField(final CtField field, final String value) {
59 .append(Modifier.toString(field.getModifiers()))
60 .append(' ').append(field.getType().getName()).append(' ')
61 .append(field.getName());
63 builder.append(" = ").append(value);
66 builder.append(";\n");
67 } catch (NotFoundException e) {
68 LOG.error("Error building field source for {}", field.getName(), e);
73 public void appendMethod(final CtMethod method, final String code) {
76 .append(Modifier.toString(method.getModifiers()))
77 .append(' ').append(method.getReturnType().getName())
78 .append(' ').append(method.getName()).append("( ");
80 CtClass[] paramTypes = method.getParameterTypes();
81 if (paramTypes != null) {
82 for (int i = 0; i < paramTypes.length; i++) {
86 builder.append(paramTypes[i].getName()).append(" $")
91 builder.append(" )\n").append(code).append("\n\n");
92 } catch (NotFoundException e) {
93 LOG.error("Error building method source for {}", method.getName(), e);
98 public void outputGeneratedSource(final CtClass ctClass) {
99 String name = ctClass.getName();
101 StringBuilder classBuilder = new StringBuilder();
102 classBuilder.append(Modifier.toString(ctClass.getModifiers()))
103 .append(" class ").append(ctClass.getSimpleName());
106 CtClass superClass = ctClass.getSuperclass();
107 if (superClass != null) {
108 classBuilder.append(" extends ").append(superClass.getName());
111 CtClass[] interfaces = ctClass.getInterfaces();
112 if (interfaces.length > 0) {
113 classBuilder.append(" implements ");
114 for (int i = 0; i < interfaces.length; i++) {
116 classBuilder.append(", ");
119 classBuilder.append(interfaces[i].getName());
123 classBuilder.append(" {\n").append(builder).append("\n}");
124 } catch (NotFoundException e) {
125 LOG.error("Error building class source for {}", name, e);
129 File dir = new File(generatedSourceDir);
131 LOG.warn("Failed to create directory {}, attempting to continue", generatedSourceDir);
134 try (BufferedWriter writer = Files.newWriter(new File(dir, name + ".java"), StandardCharsets.UTF_8)) {
135 writer.append(classBuilder);
137 } catch (IOException e) {
138 LOG.error("Error writing class source for {}", name, e);