You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by jf...@apache.org on 2019/05/04 18:35:42 UTC

[plc4x] 02/03: Added a Pojo Factoryy.

This is an automated email from the ASF dual-hosted git repository.

jfeinauer pushed a commit to branch feature/code-gen
in repository https://gitbox.apache.org/repos/asf/plc4x.git

commit ac7969378025bff4773eeefa0340868c63f69f9b
Author: Julian Feinauer <j....@pragmaticminds.de>
AuthorDate: Sat May 4 20:30:08 2019 +0200

    Added a Pojo Factoryy.
---
 .../plc4x/codegen/version2/JavaGenerator.java      |  8 ++-
 .../apache/plc4x/codegen/version2/PojoFactory.java | 75 ++++++++++++++++++++++
 .../plc4x/codegen/version2/PythonGenerator.java    |  8 ++-
 .../plc4x/codegen/version2/PojoFactoryTest.java    | 48 ++++++++++++++
 4 files changed, 133 insertions(+), 6 deletions(-)

diff --git a/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/version2/JavaGenerator.java b/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/version2/JavaGenerator.java
index 4a195be..5cbd180 100644
--- a/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/version2/JavaGenerator.java
+++ b/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/version2/JavaGenerator.java
@@ -165,9 +165,11 @@ public class JavaGenerator implements Generator {
         }
 
         // Constructors
-        for (ConstructorDeclaration constructor : constructors) {
-            this.generateConstructor(className, constructor.getParameters(), constructor.getBody());
-            writer.writeLine("");
+        if (constructors != null) {
+            for (ConstructorDeclaration constructor : constructors) {
+                this.generateConstructor(className, constructor.getParameters(), constructor.getBody());
+                writer.writeLine("");
+            }
         }
 
         // Methods
diff --git a/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/version2/PojoFactory.java b/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/version2/PojoFactory.java
new file mode 100644
index 0000000..94de8f1
--- /dev/null
+++ b/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/version2/PojoFactory.java
@@ -0,0 +1,75 @@
+package org.apache.plc4x.codegen.version2;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+public class PojoFactory {
+
+    public ClassDefinition create(PojoDescription desc) {
+        // Create all Fields first
+        final List<FieldDeclaration> fields = desc.fields.stream()
+            .map(field -> new FieldDeclaration(field.getType(), field.getName()))
+            .collect(Collectors.toList());
+
+        // Create the Getters
+        final List<MethodDefinition> getters = desc.fields.stream()
+            .map(new Function<Field, MethodDefinition>() {
+                @Override public MethodDefinition apply(Field field) {
+                    String getter = "get" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1, field.getName().length());
+                    Block body = new Block(new ReturnStatement(new FieldReference(field.getType(), field.getName())));
+                    return new MethodDefinition(getter, field.getType(), Collections.emptyList(), body);
+                }
+            })
+            .collect(Collectors.toList());
+
+
+        return new ClassDefinition("", desc.getName(), fields, null, getters, null);
+    }
+
+    public static class PojoDescription {
+
+        private final String name;
+        private final List<Field> fields;
+
+        public PojoDescription(String name, Field... fields) {
+            this.name = name;
+            this.fields = Arrays.asList(fields);
+        }
+
+        public PojoDescription(String name, List<Field> fields) {
+            this.name = name;
+            this.fields = fields;
+        }
+
+        public String getName() {
+            return name;
+        }
+
+        public List<Field> getFields() {
+            return fields;
+        }
+    }
+
+    public static class Field {
+
+        private final TypeNode type;
+        private final String name;
+
+        public Field(TypeNode type, String name) {
+            this.type = type;
+            this.name = name;
+        }
+
+        public TypeNode getType() {
+            return type;
+        }
+
+        public String getName() {
+            return name;
+        }
+    }
+
+}
diff --git a/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/version2/PythonGenerator.java b/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/version2/PythonGenerator.java
index 50c2416..70844e6 100644
--- a/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/version2/PythonGenerator.java
+++ b/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/version2/PythonGenerator.java
@@ -165,9 +165,11 @@ public class PythonGenerator implements Generator {
         }
 
         // Constructors
-        for (ConstructorDeclaration constructor : constructors) {
-            this.generateConstructor(className, constructor.getParameters(), constructor.getBody());
-            writer.writeLine("");
+        if (constructors != null) {
+            for (ConstructorDeclaration constructor : constructors) {
+                this.generateConstructor(className, constructor.getParameters(), constructor.getBody());
+                writer.writeLine("");
+            }
         }
 
         // Methods
diff --git a/sandbox/code-gen/src/test/java/org/apache/plc4x/codegen/version2/PojoFactoryTest.java b/sandbox/code-gen/src/test/java/org/apache/plc4x/codegen/version2/PojoFactoryTest.java
new file mode 100644
index 0000000..2d1abea
--- /dev/null
+++ b/sandbox/code-gen/src/test/java/org/apache/plc4x/codegen/version2/PojoFactoryTest.java
@@ -0,0 +1,48 @@
+package org.apache.plc4x.codegen.version2;
+
+import org.junit.Test;
+
+import java.sql.SQLOutput;
+
+import static org.junit.Assert.*;
+
+public class PojoFactoryTest {
+
+    @Test
+    public void createPojoJava() {
+        System.out.println("Java:");
+        System.out.println("----------");
+        final ClassDefinition pojo = newPojo();
+
+        final CodeWriter writer = new CodeWriter(4);
+        final JavaGenerator generator = new JavaGenerator(writer);
+
+        pojo.write(generator);
+
+        System.out.println(writer.getCode());
+    }
+
+    @Test
+    public void createPojoPython() {
+        System.out.println("Python:");
+        System.out.println("----------");
+        final ClassDefinition pojo = newPojo();
+
+        final CodeWriter writer = new CodeWriter(4);
+        final PythonGenerator generator = new PythonGenerator(writer);
+
+        pojo.write(generator);
+
+        System.out.println(writer.getCode());
+    }
+
+    private ClassDefinition newPojo() {
+        final PojoFactory factory = new PojoFactory();
+        final PojoFactory.PojoDescription description = new PojoFactory.PojoDescription("MyPojo",
+            new PojoFactory.Field(Primitive.DOUBLE, "field1"),
+            new PojoFactory.Field(Primitive.DOUBLE, "field2"),
+            new PojoFactory.Field(Primitive.DOUBLE, "field3")
+        );
+        return factory.create(description);
+    }
+}
\ No newline at end of file