You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2021/10/03 11:19:43 UTC

[plc4x] branch develop updated: feat(plc4j/codegen): format java sources and optimize import after codegen

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

sruehl pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/develop by this push:
     new 4156659  feat(plc4j/codegen): format java sources and optimize import after codegen
4156659 is described below

commit 415665932c892ca8c044138ff1f884b1a73ac413
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Sun Oct 3 13:19:33 2021 +0200

    feat(plc4j/codegen): format java sources and optimize import after codegen
---
 .../freemarker/FreemarkerLanguageOutput.java       |  11 +-
 code-generation/language-java/pom.xml              |  13 +
 .../plc4x/language/java/JavaLanguageOutput.java    |  26 +-
 .../src/test/resources/integration-test/pom.xml    | 337 +++++++++++----------
 4 files changed, 215 insertions(+), 172 deletions(-)

diff --git a/code-generation/language-base-freemarker/src/main/java/org/apache/plc4x/plugins/codegenerator/protocol/freemarker/FreemarkerLanguageOutput.java b/code-generation/language-base-freemarker/src/main/java/org/apache/plc4x/plugins/codegenerator/protocol/freemarker/FreemarkerLanguageOutput.java
index a89aa92..fbcf6f8 100644
--- a/code-generation/language-base-freemarker/src/main/java/org/apache/plc4x/plugins/codegenerator/protocol/freemarker/FreemarkerLanguageOutput.java
+++ b/code-generation/language-base-freemarker/src/main/java/org/apache/plc4x/plugins/codegenerator/protocol/freemarker/FreemarkerLanguageOutput.java
@@ -41,7 +41,7 @@ public abstract class FreemarkerLanguageOutput implements LanguageOutput {
 
     @Override
     public void generate(File outputDir, String languageName, String protocolName, String outputFlavor, Map<String, TypeDefinition> types,
-        Map<String, String> options)
+                         Map<String, String> options)
         throws GenerationException {
 
         // Configure the Freemarker template engine
@@ -157,9 +157,16 @@ public abstract class FreemarkerLanguageOutput implements LanguageOutput {
                 }
                 outputFileWriter.flush();
             }
+
+            // Apply post-processing to the template
+            postProcessTemplateOutput(outputFile);
         }
     }
 
+    protected void postProcessTemplateOutput(File outputFile) {
+        // NOOP
+    }
+
     private Configuration getFreemarkerConfiguration() throws GenerationException {
         Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
         configuration.setDefaultEncoding("UTF-8");
@@ -183,6 +190,6 @@ public abstract class FreemarkerLanguageOutput implements LanguageOutput {
     protected abstract List<Template> getDataIoTemplates(Configuration freemarkerConfiguration) throws IOException;
 
     protected abstract FreemarkerLanguageTemplateHelper getHelper(TypeDefinition thisType, String protocolName, String flavorName, Map<String, TypeDefinition> types,
-        Map<String, String> options);
+                                                                  Map<String, String> options);
 
 }
diff --git a/code-generation/language-java/pom.xml b/code-generation/language-java/pom.xml
index b6853a9..734b112 100644
--- a/code-generation/language-java/pom.xml
+++ b/code-generation/language-java/pom.xml
@@ -50,6 +50,14 @@
               <goal>verify</goal>
             </goals>
             <configuration>
+              <!-- These option allow that openjdk >16 can run the google-code-format -->
+              <mavenOpts>
+                --add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
+                --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
+                --add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
+                --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED
+                --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
+              </mavenOpts>
               <debug>true</debug>
               <localRepositoryPath>${project.build.directory}/local-repo</localRepositoryPath>
               <projectsDirectory>src/test/resources</projectsDirectory>
@@ -96,6 +104,11 @@
       <version>0.10.0-SNAPSHOT</version>
       <scope>compile</scope>
     </dependency>
+    <dependency>
+      <groupId>com.google.googlejavaformat</groupId>
+      <artifactId>google-java-format</artifactId>
+      <version>1.11.0</version>
+    </dependency>
 
     <!-- Make sure the dependencies of the module are built first -->
     <dependency>
diff --git a/code-generation/language-java/src/main/java/org/apache/plc4x/language/java/JavaLanguageOutput.java b/code-generation/language-java/src/main/java/org/apache/plc4x/language/java/JavaLanguageOutput.java
index fd05d0d..f935fcc 100644
--- a/code-generation/language-java/src/main/java/org/apache/plc4x/language/java/JavaLanguageOutput.java
+++ b/code-generation/language-java/src/main/java/org/apache/plc4x/language/java/JavaLanguageOutput.java
@@ -18,16 +18,25 @@
  */
 package org.apache.plc4x.language.java;
 
+import com.google.googlejavaformat.java.FormatterException;
 import freemarker.template.*;
+import org.apache.commons.io.FileUtils;
+import org.apache.plc4x.plugins.codegenerator.protocol.freemarker.FreemarkerException;
 import org.apache.plc4x.plugins.codegenerator.protocol.freemarker.FreemarkerLanguageOutput;
 import org.apache.plc4x.plugins.codegenerator.protocol.freemarker.FreemarkerLanguageTemplateHelper;
 import org.apache.plc4x.plugins.codegenerator.types.definitions.TypeDefinition;
 
 import java.io.*;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
 import java.util.*;
 
+import com.google.googlejavaformat.java.Formatter;
+
 public class JavaLanguageOutput extends FreemarkerLanguageOutput {
 
+    private Formatter formatter = new Formatter();
+
     @Override
     public String getName() {
         return "Java";
@@ -70,9 +79,22 @@ public class JavaLanguageOutput extends FreemarkerLanguageOutput {
 
     @Override
     protected FreemarkerLanguageTemplateHelper getHelper(TypeDefinition thisType, String protocolName, String flavorName, Map<String, TypeDefinition> types,
-        Map<String, String> options) {
+                                                         Map<String, String> options) {
         return new JavaLanguageTemplateHelper(thisType, protocolName, flavorName, types, options);
     }
 
-
+    @Override
+    protected void postProcessTemplateOutput(File outputFile) {
+        try {
+            FileUtils.writeStringToFile(
+                outputFile,
+                formatter.formatSourceAndFixImports(
+                    FileUtils.readFileToString(outputFile, StandardCharsets.UTF_8)
+                ),
+                StandardCharsets.UTF_8
+            );
+        } catch (IOException | FormatterException e) {
+            throw new RuntimeException(e);
+        }
+    }
 }
diff --git a/code-generation/language-java/src/test/resources/integration-test/pom.xml b/code-generation/language-java/src/test/resources/integration-test/pom.xml
index f63ff70..f435ad8 100644
--- a/code-generation/language-java/src/test/resources/integration-test/pom.xml
+++ b/code-generation/language-java/src/test/resources/integration-test/pom.xml
@@ -17,7 +17,8 @@
   specific language governing permissions and limitations
   under the License.
   -->
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
 
   <parent>
@@ -32,174 +33,174 @@
   <name>PLC4J: Driver: Java Mpsec Test</name>
   <description></description>
 
-<build>
-  <plugins>
-    <plugin>
-      <groupId>org.apache.plc4x.plugins</groupId>
-      <artifactId>plc4x-maven-plugin</artifactId>
-      <version>${plc4x-code-generation.version}</version>
-      <executions>
-        <execution>
-          <id>generate-driver</id>
-          <phase>generate-sources</phase>
-          <goals>
-            <goal>generate-driver</goal>
-          </goals>
-          <configuration>
-            <protocolName>test</protocolName>
-            <languageName>java</languageName>
-            <outputFlavor>read-write</outputFlavor>
-          </configuration>
-        </execution>
-      </executions>
-    </plugin>
-    <plugin>
-      <groupId>org.apache.karaf.tooling</groupId>
-      <artifactId>karaf-maven-plugin</artifactId>
-      <executions>
-        <execution>
-          <id>generate-feature-xml</id>
-          <phase>compile</phase>
-          <goals>
-            <!-- Generate the feature.xml -->
-            <goal>features-generate-descriptor</goal>
-            <!-- Check the feature.xml -->
-            <goal>verify</goal>
-          </goals>
-          <configuration>
-            <enableGeneration>true</enableGeneration>
-            <aggregateFeatures>true</aggregateFeatures>
-          </configuration>
-        </execution>
-        <execution>
-          <id>build-kar</id>
-          <phase>package</phase>
-          <goals>
-            <!--
-              Build a kar archive (Jar containing the feature.xml
-              as well as the module content and it's dependencies.
-            -->
-            <goal>kar</goal>
-          </goals>
-        </execution>
-      </executions>
-    </plugin>
-    <plugin>
-      <groupId>org.apache.felix</groupId>
-      <artifactId>maven-bundle-plugin</artifactId>
-      <extensions>true</extensions>
-      <configuration>
-        <instructions>
-          <Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
-          <Bundle-Activator>org.apache.plc4x.java.osgi.DriverActivator</Bundle-Activator>
-          <Export-Service>org.apache.plc4x.java.api.PlcDriver,org.apache.plc4x.protocol.test
-          </Export-Service>
-          <Import-Package>
-            com.fasterxml.jackson.annotation;resolution:=optional,
-            *
-          </Import-Package>
-        </instructions>
-      </configuration>
-    </plugin>
-    <plugin>
-      <groupId>org.apache.maven.plugins</groupId>
-      <artifactId>maven-dependency-plugin</artifactId>
-      <configuration>
-        <usedDependencies combine.children="append">
-          <usedDependency>org.apache.plc4x:plc4x-code-generation-language-java</usedDependency>
-          <usedDependency>org.apache.plc4x:plc4x-code-generation-protocol-test</usedDependency>
-        </usedDependencies>
-      </configuration>
-    </plugin>
-
-    <!--
-        Make the failsafe execute all integration-tests
-      -->
-    <plugin>
-      <groupId>org.apache.maven.plugins</groupId>
-      <artifactId>maven-failsafe-plugin</artifactId>
-      <executions>
-        <execution>
-          <goals>
-            <goal>integration-test</goal>
-            <goal>verify</goal>
-          </goals>
-        </execution>
-      </executions>
-      <configuration>
-        <!--
-              Notice the @ instead of the $ as prefix? That's late evaluation.
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.plc4x.plugins</groupId>
+        <artifactId>plc4x-maven-plugin</artifactId>
+        <version>${plc4x-code-generation.version}</version>
+        <executions>
+          <execution>
+            <id>generate-driver</id>
+            <phase>generate-sources</phase>
+            <goals>
+              <goal>generate-driver</goal>
+            </goals>
+            <configuration>
+              <protocolName>test</protocolName>
+              <languageName>java</languageName>
+              <outputFlavor>read-write</outputFlavor>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.karaf.tooling</groupId>
+        <artifactId>karaf-maven-plugin</artifactId>
+        <executions>
+          <execution>
+            <id>generate-feature-xml</id>
+            <phase>compile</phase>
+            <goals>
+              <!-- Generate the feature.xml -->
+              <goal>features-generate-descriptor</goal>
+              <!-- Check the feature.xml -->
+              <goal>verify</goal>
+            </goals>
+            <configuration>
+              <enableGeneration>true</enableGeneration>
+              <aggregateFeatures>true</aggregateFeatures>
+            </configuration>
+          </execution>
+          <execution>
+            <id>build-kar</id>
+            <phase>package</phase>
+            <goals>
+              <!--
+                Build a kar archive (Jar containing the feature.xml
+                as well as the module content and it's dependencies.
+              -->
+              <goal>kar</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.felix</groupId>
+        <artifactId>maven-bundle-plugin</artifactId>
+        <extensions>true</extensions>
+        <configuration>
+          <instructions>
+            <Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
+            <Bundle-Activator>org.apache.plc4x.java.osgi.DriverActivator</Bundle-Activator>
+            <Export-Service>org.apache.plc4x.java.api.PlcDriver,org.apache.plc4x.protocol.test
+            </Export-Service>
+            <Import-Package>
+              com.fasterxml.jackson.annotation;resolution:=optional,
+              *
+            </Import-Package>
+          </instructions>
+        </configuration>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-dependency-plugin</artifactId>
+        <configuration>
+          <usedDependencies combine.children="append">
+            <usedDependency>org.apache.plc4x:plc4x-code-generation-language-java</usedDependency>
+            <usedDependency>org.apache.plc4x:plc4x-code-generation-protocol-test</usedDependency>
+          </usedDependencies>
+        </configuration>
+      </plugin>
+
+      <!--
+          Make the failsafe execute all integration-tests
         -->
-        <!--argLine>@{failsafeArgLine}</argLine-->
-      </configuration>
-    </plugin>
-  </plugins>
-</build>
-
-<dependencies>
-  <dependency>
-    <groupId>org.apache.plc4x</groupId>
-    <artifactId>plc4j-api</artifactId>
-    <version>@project.version@</version>
-  </dependency>
-  <dependency>
-    <groupId>org.apache.plc4x</groupId>
-    <artifactId>plc4j-spi</artifactId>
-    <version>@project.version@</version>
-  </dependency>
-
-  <dependency>
-    <groupId>org.apache.plc4x</groupId>
-    <artifactId>plc4j-transport-tcp</artifactId>
-    <version>@project.version@</version>
-  </dependency>
-
-  <dependency>
-    <groupId>io.netty</groupId>
-    <artifactId>netty-buffer</artifactId>
-  </dependency>
-
-  <dependency>
-    <groupId>org.apache.commons</groupId>
-    <artifactId>commons-lang3</artifactId>
-  </dependency>
-
-  <dependency>
-    <groupId>com.fasterxml.jackson.core</groupId>
-    <artifactId>jackson-annotations</artifactId>
-  </dependency>
-
-  <dependency>
-    <groupId>org.apache.plc4x</groupId>
-    <artifactId>plc4j-utils-test-utils</artifactId>
-    <version>@project.version@</version>
-    <scope>test</scope>
-  </dependency>
-
-  <dependency>
-    <groupId>org.apache.plc4x</groupId>
-    <artifactId>plc4x-code-generation-language-java</artifactId>
-    <version>@project.version@</version>
-    <!-- Scope is 'provided' as this way it's not shipped with the driver -->
-    <scope>provided</scope>
-  </dependency>
-
-  <dependency>
-    <groupId>org.apache.plc4x</groupId>
-    <artifactId>plc4x-code-generation-protocol-test</artifactId>
-    <version>@project.version@</version>
-  </dependency>
-
-  <dependency>
-    <groupId>org.apache.plc4x</groupId>
-    <artifactId>plc4x-code-generation-protocol-test</artifactId>
-    <version>@project.version@</version>
-    <classifier>tests</classifier>
-    <type>test-jar</type>
-    <scope>test</scope>
-  </dependency>
-
-</dependencies>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-failsafe-plugin</artifactId>
+        <executions>
+          <execution>
+            <goals>
+              <goal>integration-test</goal>
+              <goal>verify</goal>
+            </goals>
+          </execution>
+        </executions>
+        <configuration>
+          <!--
+                Notice the @ instead of the $ as prefix? That's late evaluation.
+          -->
+          <!--argLine>@{failsafeArgLine}</argLine-->
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.plc4x</groupId>
+      <artifactId>plc4j-api</artifactId>
+      <version>@project.version@</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.plc4x</groupId>
+      <artifactId>plc4j-spi</artifactId>
+      <version>@project.version@</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.plc4x</groupId>
+      <artifactId>plc4j-transport-tcp</artifactId>
+      <version>@project.version@</version>
+    </dependency>
+
+    <dependency>
+      <groupId>io.netty</groupId>
+      <artifactId>netty-buffer</artifactId>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-lang3</artifactId>
+    </dependency>
+
+    <dependency>
+      <groupId>com.fasterxml.jackson.core</groupId>
+      <artifactId>jackson-annotations</artifactId>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.plc4x</groupId>
+      <artifactId>plc4j-utils-test-utils</artifactId>
+      <version>@project.version@</version>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.plc4x</groupId>
+      <artifactId>plc4x-code-generation-language-java</artifactId>
+      <version>@project.version@</version>
+      <!-- Scope is 'provided' as this way it's not shipped with the driver -->
+      <scope>provided</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.plc4x</groupId>
+      <artifactId>plc4x-code-generation-protocol-test</artifactId>
+      <version>@project.version@</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.plc4x</groupId>
+      <artifactId>plc4x-code-generation-protocol-test</artifactId>
+      <version>@project.version@</version>
+      <classifier>tests</classifier>
+      <type>test-jar</type>
+      <scope>test</scope>
+    </dependency>
+
+  </dependencies>
 
 </project>