Bump versions for mdsal-5.0.0
[mdsal.git] / binding / mdsal-binding-generator-impl / src / main / java / org / opendaylight / mdsal / binding / generator / util / DefaultSourceCodeGenerator.java
index 2e97ecb47607c441d6c2c83b4d151fdddaa40cdf..ce44f64c5be51d618f7701a17d66b1cf58fb404f 100644 (file)
@@ -5,12 +5,14 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.mdsal.binding.generator.util;
 
+import com.google.common.io.Files;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
+import java.io.BufferedWriter;
 import java.io.File;
-import java.io.FileWriter;
 import java.io.IOException;
+import java.nio.charset.StandardCharsets;
 import javassist.CtClass;
 import javassist.CtField;
 import javassist.CtMethod;
@@ -26,7 +28,10 @@ import org.slf4j.LoggerFactory;
  * written to a file under a specified directory.
  *
  * @author Thomas Pantelis
+ *
+ * @deprecated Code generation is a concert separate from type mapping and is an implementation detail.
  */
+@Deprecated
 public class DefaultSourceCodeGenerator implements SourceCodeGenerator {
     private static final Logger LOG = LoggerFactory.getLogger(DefaultSourceCodeGenerator.class);
 
@@ -63,7 +68,7 @@ public class DefaultSourceCodeGenerator implements SourceCodeGenerator {
 
             builder.append(";\n");
         } catch (NotFoundException e) {
-            LOG.error("Error building field source for " + field.getName(), e);
+            LOG.error("Error building field source for {}", field.getName(), e);
         }
     }
 
@@ -88,11 +93,12 @@ public class DefaultSourceCodeGenerator implements SourceCodeGenerator {
 
             builder.append(" )\n").append(code).append("\n\n");
         } catch (NotFoundException e) {
-            LOG.error("Error building method source for " + method.getName(), e);
+            LOG.error("Error building method source for {}", method.getName(), e);
         }
     }
 
     @Override
+    @SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE")
     public void outputGeneratedSource(final CtClass ctClass) {
         String name = ctClass.getName();
 
@@ -118,20 +124,22 @@ public class DefaultSourceCodeGenerator implements SourceCodeGenerator {
                 }
             }
 
-            classBuilder.append(" {\n").append(builder.toString())
-                    .append("\n}");
+            classBuilder.append(" {\n").append(builder).append("\n}");
         } catch (NotFoundException e) {
-            LOG.error("Error building class source for " + name, e);
+            LOG.error("Error building class source for {}", name, e);
             return;
         }
 
         File dir = new File(generatedSourceDir);
-        dir.mkdir();
-        try (FileWriter writer = new FileWriter(new File(dir, name + ".java"))) {
-            writer.append(classBuilder.toString());
+        if (!dir.mkdir()) {
+            LOG.warn("Failed to create directory {}, attempting to continue", generatedSourceDir);
+        }
+
+        try (BufferedWriter writer = Files.newWriter(new File(dir, name + ".java"), StandardCharsets.UTF_8)) {
+            writer.append(classBuilder);
             writer.flush();
         } catch (IOException e) {
-            LOG.error("Error writing class source for " + name, e);
+            LOG.error("Error writing class source for {}", name, e);
         }
     }
 }