Implemented build of default instance for union types.
[mdsal.git] / code-generator / binding-java-api-generator / src / main / java / org / opendaylight / yangtools / sal / java / api / generator / GeneratorJavaFile.java
index ea33be2e9ffc4af319e1828aefd9d1a5cff9fb7f..d6556157562fa89f1a5d7d939e17dac26f53fab3 100644 (file)
@@ -9,24 +9,31 @@ package org.opendaylight.yangtools.sal.java.api.generator;
 
 import java.io.BufferedWriter;
 import java.io.File;
-import java.io.FileWriter;
 import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
 
 import org.opendaylight.yangtools.sal.binding.model.api.CodeGenerator;
+import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
 import org.opendaylight.yangtools.sal.binding.model.api.Type;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.sonatype.plexus.build.incremental.BuildContext;
+import org.sonatype.plexus.build.incremental.DefaultBuildContext;
+
+import com.google.common.base.Preconditions;
 
 /**
  * Generates files with JAVA source codes for every specified type.
- * 
+ *
  */
 public final class GeneratorJavaFile {
 
-    private static final Logger log = LoggerFactory.getLogger(GeneratorJavaFile.class);
+    private static final Logger LOG = LoggerFactory.getLogger(GeneratorJavaFile.class);
 
     /**
      * List of <code>CodeGenerator</code> instances.
@@ -38,40 +45,79 @@ public final class GeneratorJavaFile {
      */
     private final Set<? extends Type> types;
 
+    /**
+     * BuildContext used for instantiating files
+     */
+    private final BuildContext buildContext;
+
     /**
      * Creates instance of this class with the set of <code>types</code> for
      * which the JAVA code is generated.
-     * 
+     *
      * The instances of concrete JAVA code generator are created.
-     * 
+     *
+     * @param buildContext
+     *            build context to use for accessing files
      * @param types
      *            set of types for which JAVA code should be generated
      */
-    public GeneratorJavaFile(final Set<? extends Type> types) {
-        this.types = types;
+    public GeneratorJavaFile(final BuildContext buildContext, final Set<? extends Type> types) {
+        this.buildContext = Preconditions.checkNotNull(buildContext);
+        this.types = Preconditions.checkNotNull(types);
         generators.add(new InterfaceGenerator());
         generators.add(new TOGenerator());
         generators.add(new EnumGenerator());
         generators.add(new BuilderGenerator());
     }
 
+    /**
+     * Creates instance of this class with the set of <code>types</code> for
+     * which the JAVA code is generated. Generator instantiated this way uses
+     * the default build context, e.g. it will re-generate any and all files.
+     *
+     * The instances of concrete JAVA code generator are created.
+     *
+     * @param types
+     *            set of types for which JAVA code should be generated
+     */
+    public GeneratorJavaFile(final Set<? extends Type> types) {
+        this(new DefaultBuildContext(), types);
+    }
+
     /**
      * Generates list of files with JAVA source code. Only the suitable code
      * generator is used to generate the source code for the concrete type.
-     * 
-     * @param parentDirectory
+     *
+     * @param generatedSourcesDirectory
      *            directory to which the output source codes should be generated
      * @return list of output files
      * @throws IOException
-     *             if the error during writting to the file occures
+     *             if the error during writing to the file occurs
      */
-    public List<File> generateToFile(final File parentDirectory) throws IOException {
+    public List<File> generateToFile(final File generatedSourcesDirectory) throws IOException {
+        return generateToFile(generatedSourcesDirectory, generatedSourcesDirectory);
+    }
+
+    public List<File> generateToFile(final File generatedSourcesDirectory, final File persistenSourcesDirectory)
+            throws IOException {
         final List<File> result = new ArrayList<>();
         for (Type type : types) {
-            for (CodeGenerator generator : generators) {
-                File generatedJavaFile = generateTypeToJavaFile(parentDirectory, type, generator);
-                if (generatedJavaFile != null) {
-                    result.add(generatedJavaFile);
+            if (type != null) {
+                for (CodeGenerator generator : generators) {
+                    File generatedJavaFile = null;
+                    if (type instanceof GeneratedTransferObject
+                            && ((GeneratedTransferObject) type).isUnionTypeBuilder()) {
+                        File packageDir = packageToDirectory(persistenSourcesDirectory, type.getPackageName());
+                        File file = new File(packageDir, generator.getUnitName(type) + ".java");
+                        if (!file.exists()) {
+                            generatedJavaFile = generateTypeToJavaFile(persistenSourcesDirectory, type, generator);
+                        }
+                    } else {
+                        generatedJavaFile = generateTypeToJavaFile(generatedSourcesDirectory, type, generator);
+                    }
+                    if (generatedJavaFile != null) {
+                        result.add(generatedJavaFile);
+                    }
                 }
             }
         }
@@ -84,7 +130,7 @@ public final class GeneratorJavaFile {
      * are generated according to packages to which the type belongs (e. g. if
      * type belongs to the package <i>org.pcg</i> then in <code>parentDir</code>
      * is created directory <i>org</i> which contains <i>pcg</i>).
-     * 
+     *
      * @param parentDir
      *            directory where should be the new file generated
      * @param type
@@ -94,7 +140,7 @@ public final class GeneratorJavaFile {
      *            code generator which is used for generating of the source code
      * @return file which contains JAVA source code
      * @throws IOException
-     *             if the error during writting to the file occures
+     *             if the error during writing to the file occurs
      * @throws IllegalArgumentException
      *             if <code>type</code> equals <code>null</code>
      * @throws IllegalStateException
@@ -103,15 +149,15 @@ public final class GeneratorJavaFile {
     private File generateTypeToJavaFile(final File parentDir, final Type type, final CodeGenerator generator)
             throws IOException {
         if (parentDir == null) {
-            log.warn("Parent Directory not specified, files will be generated "
+            LOG.warn("Parent Directory not specified, files will be generated "
                     + "accordingly to generated Type package path.");
         }
         if (type == null) {
-            log.error("Cannot generate Type into Java File because " + "Generated Type is NULL!");
+            LOG.error("Cannot generate Type into Java File because " + "Generated Type is NULL!");
             throw new IllegalArgumentException("Generated Type Cannot be NULL!");
         }
         if (generator == null) {
-            log.error("Cannot generate Type into Java File because " + "Code Generator instance is NULL!");
+            LOG.error("Cannot generate Type into Java File because " + "Code Generator instance is NULL!");
             throw new IllegalArgumentException("Code Generator Cannot be NULL!");
         }
         final File packageDir = packageToDirectory(parentDir, type.getPackageName());
@@ -121,19 +167,20 @@ public final class GeneratorJavaFile {
         }
 
         if (generator.isAcceptable(type)) {
-            String generatedCode = generator.generate(type);
+            final String generatedCode = generator.generate(type);
             if (generatedCode.isEmpty()) {
                 throw new IllegalStateException("Generated code should not be empty!");
             }
             final File file = new File(packageDir, generator.getUnitName(type) + ".java");
-            try (final FileWriter fw = new FileWriter(file)) {
-                file.createNewFile();
-                try (final BufferedWriter bw = new BufferedWriter(fw)) {
-                    bw.write(generatedCode);
+            try (final OutputStream stream = buildContext.newFileOutputStream(file)) {
+                try (final Writer fw = new OutputStreamWriter(stream)) {
+                    try (final BufferedWriter bw = new BufferedWriter(fw)) {
+                        bw.write(generatedCode);
+                    }
+                } catch (IOException e) {
+                    LOG.error("Failed to write generate output into {}", file.getPath(), e);
+                    throw e;
                 }
-            } catch (IOException e) {
-                log.error(e.getMessage());
-                throw new IOException(e.getMessage());
             }
             return file;
         }
@@ -145,7 +192,7 @@ public final class GeneratorJavaFile {
      * <code>parentDirectory</code> and parsed <code>packageName</code>. The
      * parsing of <code>packageName</code> is realized as replacement of the
      * package name dots with the file system separator.
-     * 
+     *
      * @param parentDirectory
      *            <code>File</code> object with reference to parent directory
      * @param packageName