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/05 18:37:08 UTC
[plc4x] branch feature/code-gen updated: Several fixed in the
PojoFactory.java. Pojos are now fully runnable java code. Also started
BufferUtil.java.
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
The following commit(s) were added to refs/heads/feature/code-gen by this push:
new 09d47b5 Several fixed in the PojoFactory.java. Pojos are now fully runnable java code. Also started BufferUtil.java.
09d47b5 is described below
commit 09d47b53801f0b2e5111a38d87cddbb341aea1a6
Author: Julian Feinauer <j....@pragmaticminds.de>
AuthorDate: Sun May 5 20:36:43 2019 +0200
Several fixed in the PojoFactory.java. Pojos are now fully runnable java code. Also started BufferUtil.java.
---
.../java/org/apache/plc4x/codegen/api/Buffer.java | 13 ++++++
.../org/apache/plc4x/codegen/ast/Expressions.java | 4 ++
.../apache/plc4x/codegen/ast/JavaGenerator.java | 5 ++-
.../org/apache/plc4x/codegen/util/BufferUtil.java | 27 +++++++++++++
.../org/apache/plc4x/codegen/util/EnumFactory.java | 2 +-
.../org/apache/plc4x/codegen/util/PojoFactory.java | 15 +++----
.../java/org/apache/plc4x/codegen/util/MyPojo.java | 47 ++++++++++++++++++++++
7 files changed, 100 insertions(+), 13 deletions(-)
diff --git a/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/api/Buffer.java b/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/api/Buffer.java
new file mode 100644
index 0000000..9e9c095
--- /dev/null
+++ b/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/api/Buffer.java
@@ -0,0 +1,13 @@
+package org.apache.plc4x.codegen.api;
+
+/**
+ * The Interface as described in {@link org.apache.plc4x.codegen.util.BufferUtil}.
+ */
+public interface Buffer {
+
+ Integer readUint8();
+
+ Integer readUint16();
+
+ Long readUint32();
+}
diff --git a/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/ast/Expressions.java b/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/ast/Expressions.java
index 7372c33..cc9f505 100644
--- a/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/ast/Expressions.java
+++ b/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/ast/Expressions.java
@@ -224,6 +224,10 @@ public class Expressions {
return new DeclarationStatement(parameter(variable, initializer.getType()), initializer);
}
+ public static Statement declaration(ParameterExpression variable, Expression initializer) {
+ return new DeclarationStatement(variable, initializer);
+ }
+
/**
* Declares a constant (no field), which is not initialized.
*/
diff --git a/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/ast/JavaGenerator.java b/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/ast/JavaGenerator.java
index 90b9b35..da87f90 100644
--- a/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/ast/JavaGenerator.java
+++ b/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/ast/JavaGenerator.java
@@ -174,8 +174,9 @@ public class JavaGenerator implements Generator {
if (methodDefinition.getModifiers().contains(Modifier.STATIC)) {
writer.write(STATIC_);
}
- // Special handling of VOID is necessary, in fact... is it?
- if ("Void".equals(methodDefinition.getResultType().toString())) {
+ // Special handling of VOID is necessary, to avoid having to return null in the end.
+ if (methodDefinition.getResultType() instanceof Primitive &&
+ ((Primitive) methodDefinition.getResultType()).getType() == Primitive.DataType.VOID) {
writer.write("void");
} else {
methodDefinition.getResultType().write(this);
diff --git a/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/util/BufferUtil.java b/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/util/BufferUtil.java
new file mode 100644
index 0000000..48d122f
--- /dev/null
+++ b/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/util/BufferUtil.java
@@ -0,0 +1,27 @@
+package org.apache.plc4x.codegen.util;
+
+import org.apache.plc4x.codegen.ast.Expressions;
+import org.apache.plc4x.codegen.ast.Method;
+import org.apache.plc4x.codegen.ast.Primitive;
+import org.apache.plc4x.codegen.ast.TypeDefinition;
+
+import java.util.Collections;
+
+/**
+ * This class defines constants necessary for the code generation related to the
+ * "Buffer API" which has to be implemented natively.
+ */
+public class BufferUtil {
+
+ static final TypeDefinition BUFFER_TYPE = Expressions.typeOf("org.apache.plc4x.codegen.api.Buffer");
+
+ // Read Methods
+ static final Method READ_UINT8 = new Method(BUFFER_TYPE, "readUint8", Primitive.INTEGER, Collections.emptyList(), Collections.emptyList());
+ static final Method READ_UINT16 = new Method(BUFFER_TYPE, "readUint16", Primitive.INTEGER, Collections.emptyList(), Collections.emptyList());
+ static final Method READ_UINT32 = new Method(BUFFER_TYPE, "readUint32", Primitive.LONG, Collections.emptyList(), Collections.emptyList());
+
+ private BufferUtil() {
+ }
+
+
+}
diff --git a/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/util/EnumFactory.java b/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/util/EnumFactory.java
index 0a84ce7..c5866e0 100644
--- a/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/util/EnumFactory.java
+++ b/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/util/EnumFactory.java
@@ -85,7 +85,7 @@ public class EnumFactory {
String getter = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
final ParameterExpression param = Expressions.parameter(name, type);
Block body = Block.build().add(Expressions.assignment(Expressions.field(name), param)).toBlock();
- return new MethodDefinition(getter, type, Collections.singletonList(param), body);
+ return new MethodDefinition(getter, Primitive.VOID, Collections.singletonList(param), body);
}
public static class EnumEntry {
diff --git a/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/util/PojoFactory.java b/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/util/PojoFactory.java
index 1085acb..c7a1fac 100644
--- a/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/util/PojoFactory.java
+++ b/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/util/PojoFactory.java
@@ -31,14 +31,10 @@ import static org.apache.plc4x.codegen.util.EnumFactory.getSetterDefinition;
public class PojoFactory {
- private TypeDefinition BUFFER_TYPE;
-
- private Method readByte = new Method(BUFFER_TYPE, "readByte", Primitive.INTEGER, Collections.emptyList(), Collections.emptyList());
-
public ClassDeclaration create(PojoDescription desc) {
// Create all Fields first
final List<FieldDeclaration> fields = desc.fields.stream()
- .map(field -> new FieldDeclaration(field.getType(), field.getName()))
+ .map(field -> new FieldDeclaration(field.getType(), field.getName(), Modifier.PRIVATE))
.collect(Collectors.toList());
// Create the Getters
@@ -58,19 +54,18 @@ public class PojoFactory {
// Encode Method
methods.add(new MethodDefinition("encode", Primitive.VOID, Collections.singletonList(
- Expressions.parameter("buffer", Expressions.typeOf("org.apache.plc4x.api.Buffer"))
+ Expressions.parameter("buffer", BufferUtil.BUFFER_TYPE)
), Block.build().toBlock()));
// Decode Method
- BUFFER_TYPE = Expressions.typeOf("org.apache.plc4x.api.Buffer");
- final ParameterExpression buffer = Expressions.parameter("buffer", BUFFER_TYPE);
+ final ParameterExpression buffer = Expressions.parameter("buffer", BufferUtil.BUFFER_TYPE);
final TypeDefinition clazz = Expressions.typeOf(desc.getName());
final ParameterExpression instance = Expressions.parameter("instance", clazz);
methods.add(new MethodDefinition(Collections.singleton(Modifier.STATIC), "decode", clazz, Collections.singletonList(
buffer
), Block.build()
- .add(Expressions.assignment(instance, Expressions.new_(clazz)))
- .add(Expressions.call(buffer, readByte))
+ .add(Expressions.declaration(instance, Expressions.new_(clazz)))
+ .add(Expressions.call(buffer, BufferUtil.READ_UINT8))
.add(Expressions.return_(instance))
.toBlock()
));
diff --git a/sandbox/code-gen/src/test/java/org/apache/plc4x/codegen/util/MyPojo.java b/sandbox/code-gen/src/test/java/org/apache/plc4x/codegen/util/MyPojo.java
new file mode 100644
index 0000000..dc9690d
--- /dev/null
+++ b/sandbox/code-gen/src/test/java/org/apache/plc4x/codegen/util/MyPojo.java
@@ -0,0 +1,47 @@
+package org.apache.plc4x.codegen.util;
+
+public class MyPojo {
+
+ private Double field1;
+
+ private Double field2;
+
+ private Double field3;
+
+ public MyPojo() {
+ }
+
+ public Double getField1() {
+ return this.field1;
+ }
+
+ public Double getField2() {
+ return this.field2;
+ }
+
+ public Double getField3() {
+ return this.field3;
+ }
+
+ public void setField1(Double field1) {
+ this.field1 = field1;
+ }
+
+ public void setField2(Double field2) {
+ this.field2 = field2;
+ }
+
+ public void setField3(Double field3) {
+ this.field3 = field3;
+ }
+
+ public void encode(org.apache.plc4x.codegen.api.Buffer buffer) {
+ }
+
+ public static MyPojo decode(org.apache.plc4x.codegen.api.Buffer buffer) {
+ MyPojo instance = new MyPojo();
+ buffer.readUint8();
+ return instance;
+ }
+
+}
\ No newline at end of file