From 73fceb2f18776dc559c625820bd1f163fedba118 Mon Sep 17 00:00:00 2001 From: a31u Date: Tue, 2 Mar 2021 11:20:02 +0100 Subject: [PATCH] Add Generated annotation to classes and includeGeneratedAnnotation config option --- .../ant/Jsonschema2PojoTask.java | 5 ++ .../src/site/Jsonschema2PojoTask.html | 7 +- .../org/jsonschema2pojo/cli/Arguments.java | 6 ++ .../DefaultGenerationConfig.java | 8 +++ .../org/jsonschema2pojo/GenerationConfig.java | 8 ++- .../org/jsonschema2pojo/rules/EnumRule.java | 6 ++ .../org/jsonschema2pojo/rules/ObjectRule.java | 7 +- .../util/AnnotationHelper.java | 31 ++++++++ .../gradle/JsonSchemaExtension.groovy | 8 +++ .../config/IncludeGeneratedAnnotationIT.java | 71 +++++++++++++++++++ .../includeGeneratedAnnotation.json | 9 +++ .../maven/Jsonschema2PojoMojo.java | 11 +++ 12 files changed, 173 insertions(+), 4 deletions(-) create mode 100644 jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/util/AnnotationHelper.java create mode 100644 jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/config/IncludeGeneratedAnnotationIT.java create mode 100644 jsonschema2pojo-integration-tests/src/test/resources/schema/includeGeneratedAnnotation/includeGeneratedAnnotation.json diff --git a/jsonschema2pojo-ant/src/main/java/org/jsonschema2pojo/ant/Jsonschema2PojoTask.java b/jsonschema2pojo-ant/src/main/java/org/jsonschema2pojo/ant/Jsonschema2PojoTask.java index 1b1be080c..6928eb69e 100644 --- a/jsonschema2pojo-ant/src/main/java/org/jsonschema2pojo/ant/Jsonschema2PojoTask.java +++ b/jsonschema2pojo-ant/src/main/java/org/jsonschema2pojo/ant/Jsonschema2PojoTask.java @@ -192,6 +192,8 @@ public class Jsonschema2PojoTask extends Task implements GenerationConfig { private SourceSortOrder sourceSortOrder = SourceSortOrder.OS; private Map formatTypeMapping = new HashMap<>(); + + private boolean includeGeneratedAnnotation = true; /** * Execute this task (it's expected that all relevant setters will have been @@ -1312,4 +1314,7 @@ public boolean isUseInnerClassBuilders() { public boolean isIncludeConstructorPropertiesAnnotation() { return includeConstructorPropertiesAnnotation; } + + @Override + public boolean isIncludeGeneratedAnnotation() { return includeGeneratedAnnotation; } } diff --git a/jsonschema2pojo-ant/src/site/Jsonschema2PojoTask.html b/jsonschema2pojo-ant/src/site/Jsonschema2PojoTask.html index 65217454c..6dab8f419 100644 --- a/jsonschema2pojo-ant/src/site/Jsonschema2PojoTask.html +++ b/jsonschema2pojo-ant/src/site/Jsonschema2PojoTask.html @@ -614,9 +614,12 @@

Parameters

None (default '' (none)) - + + includeGeneratedAnnotation + Include @javax.annotation.Generated annotation to generated types + No (default true) + -

Examples

 <taskdef name="jsonschema2pojo" classname="org.jsonschema2pojo.ant.Jsonschema2PojoTask">
diff --git a/jsonschema2pojo-cli/src/main/java/org/jsonschema2pojo/cli/Arguments.java b/jsonschema2pojo-cli/src/main/java/org/jsonschema2pojo/cli/Arguments.java
index ffe61d619..5a08a6ba9 100644
--- a/jsonschema2pojo-cli/src/main/java/org/jsonschema2pojo/cli/Arguments.java
+++ b/jsonschema2pojo-cli/src/main/java/org/jsonschema2pojo/cli/Arguments.java
@@ -243,6 +243,9 @@ public class Arguments implements GenerationConfig {
     @Parameter(names = {"--print-log-levels"}, description = "Prints available log levels and exit.")
     private boolean printLogLevels = false;
 
+    @Parameter(names = {"--omit-generated-annotation"}, description = "Omit @Generated annotation on generated types")
+    private boolean omitGeneratedAnnotation = false;
+
     private static final int EXIT_OKAY = 0;
     private static final int EXIT_ERROR = 1;
 
@@ -598,4 +601,7 @@ public Map getFormatTypeMapping() {
                 .stream()
                 .collect(Collectors.toMap(m -> m.split(":")[0], m -> m.split(":")[1]));
     }
+    
+    @Override
+    public boolean isIncludeGeneratedAnnotation() { return !omitGeneratedAnnotation; }
 }
diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/DefaultGenerationConfig.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/DefaultGenerationConfig.java
index b53fca525..25718324e 100644
--- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/DefaultGenerationConfig.java
+++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/DefaultGenerationConfig.java
@@ -478,4 +478,12 @@ public Map getFormatTypeMapping() {
     public boolean isIncludeConstructorPropertiesAnnotation() {
         return false;
     }
+
+    /**
+     * @return false
+     */
+    @Override
+    public boolean isIncludeGeneratedAnnotation() {
+    	return true;
+    }
 }
diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/GenerationConfig.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/GenerationConfig.java
index 16cac177b..629f82df1 100644
--- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/GenerationConfig.java
+++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/GenerationConfig.java
@@ -610,5 +610,11 @@ public interface GenerationConfig {
   default boolean isUseInnerClassBuilders() {
     return false;
   }
-
+  
+  /**
+   * Whether to mark generated classes with the annotation javax.annotation.@Generated
+   *
+   */
+  boolean isIncludeGeneratedAnnotation();
+  
 }
diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/EnumRule.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/EnumRule.java
index 4888e83e7..3f0649c39 100644
--- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/EnumRule.java
+++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/EnumRule.java
@@ -36,10 +36,12 @@
 import org.jsonschema2pojo.model.EnumDefinition;
 import org.jsonschema2pojo.model.EnumDefinitionExtensionType;
 import org.jsonschema2pojo.model.EnumValueDefinition;
+import org.jsonschema2pojo.util.AnnotationHelper;
 
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.node.ObjectNode;
 import com.sun.codemodel.ClassType;
+import com.sun.codemodel.JAnnotationUse;
 import com.sun.codemodel.JBlock;
 import com.sun.codemodel.JClass;
 import com.sun.codemodel.JClassAlreadyExistsException;
@@ -145,6 +147,10 @@ public JType apply(String nodeName, JsonNode node, JsonNode parent, JClassContai
 
         EnumDefinition enumDefinition = buildEnumDefinition(nodeName, node, backingType);
 
+        if(ruleFactory.getGenerationConfig() != null && ruleFactory.getGenerationConfig().isIncludeGeneratedAnnotation()) {
+            AnnotationHelper.addGeneratedAnnotation(_enum);
+        }
+
         JFieldVar valueField = addConstructorAndFields(enumDefinition, _enum);
 
         // override toString only if we have a sensible string to return
diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/ObjectRule.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/ObjectRule.java
index 8b39e3874..6cf6a3c65 100644
--- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/ObjectRule.java
+++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/ObjectRule.java
@@ -35,9 +35,11 @@
 import org.jsonschema2pojo.util.ParcelableHelper;
 import org.jsonschema2pojo.util.ReflectionHelper;
 import org.jsonschema2pojo.util.SerializableHelper;
+import org.jsonschema2pojo.util.AnnotationHelper;
 
 import com.fasterxml.jackson.databind.JsonNode;
 import com.sun.codemodel.ClassType;
+import com.sun.codemodel.JAnnotationUse;
 import com.sun.codemodel.JBlock;
 import com.sun.codemodel.JClass;
 import com.sun.codemodel.JClassAlreadyExistsException;
@@ -125,7 +127,10 @@ public JType apply(String nodeName, JsonNode node, JsonNode parent, JPackage _pa
         if (node.has("required")) {
             ruleFactory.getRequiredArrayRule().apply(nodeName, node.get("required"), node, jclass, schema);
         }
-
+       
+        if (ruleFactory.getGenerationConfig().isIncludeGeneratedAnnotation()) {
+        	AnnotationHelper.addGeneratedAnnotation(jclass);
+        }
         if (ruleFactory.getGenerationConfig().isIncludeToString()) {
             addToString(jclass);
         }
diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/util/AnnotationHelper.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/util/AnnotationHelper.java
new file mode 100644
index 000000000..03cf04a71
--- /dev/null
+++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/util/AnnotationHelper.java
@@ -0,0 +1,31 @@
+/**
+ * Copyright © 2010-2020 Nokia
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.jsonschema2pojo.util;
+
+import com.sun.codemodel.JAnnotationUse;
+import com.sun.codemodel.JDefinedClass;
+
+public class AnnotationHelper {
+
+    static final String GENERATOR_NAME = "jsonschema2pojo";
+
+    public static void addGeneratedAnnotation(JDefinedClass jclass) {
+        JAnnotationUse generated = jclass.annotate(javax.annotation.Generated.class);
+        generated.param("value", GENERATOR_NAME);
+    }
+
+}
diff --git a/jsonschema2pojo-gradle-plugin/src/main/groovy/org/jsonschema2pojo/gradle/JsonSchemaExtension.groovy b/jsonschema2pojo-gradle-plugin/src/main/groovy/org/jsonschema2pojo/gradle/JsonSchemaExtension.groovy
index 2e52c18ba..eb8348db6 100644
--- a/jsonschema2pojo-gradle-plugin/src/main/groovy/org/jsonschema2pojo/gradle/JsonSchemaExtension.groovy
+++ b/jsonschema2pojo-gradle-plugin/src/main/groovy/org/jsonschema2pojo/gradle/JsonSchemaExtension.groovy
@@ -95,6 +95,7 @@ public class JsonSchemaExtension implements GenerationConfig {
   String refFragmentPathDelimiters
   SourceSortOrder sourceSortOrder
   Map formatTypeMapping
+  boolean includeGeneratedAnnotation
 
   public JsonSchemaExtension() {
     // See DefaultGenerationConfig
@@ -154,6 +155,7 @@ public class JsonSchemaExtension implements GenerationConfig {
     refFragmentPathDelimiters = "#/."
     sourceSortOrder = SourceSortOrder.OS
     formatTypeMapping = Collections.emptyMap()
+    includeGeneratedAnnotation = true
   }
 
   @Override
@@ -282,6 +284,7 @@ public class JsonSchemaExtension implements GenerationConfig {
        |formatTypeMapping = ${formatTypeMapping}
        |useInnerClassBuilders = ${useInnerClassBuilders}
        |includeConstructorPropertiesAnnotation = ${includeConstructorPropertiesAnnotation}
+       |includeGeneratedAnnotation = ${includeGeneratedAnnotation}
      """.stripMargin()
   }
   
@@ -289,4 +292,9 @@ public class JsonSchemaExtension implements GenerationConfig {
     return formatDateTimes
   }
 
+  @Override
+  boolean isIncludeGeneratedAnnotation() {
+    return includeGeneratedAnnotation
+  }
+
 }
diff --git a/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/config/IncludeGeneratedAnnotationIT.java b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/config/IncludeGeneratedAnnotationIT.java
new file mode 100644
index 000000000..4dce0ffc5
--- /dev/null
+++ b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/config/IncludeGeneratedAnnotationIT.java
@@ -0,0 +1,71 @@
+/**
+ * Copyright © 2010-2020 Nokia
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.jsonschema2pojo.integration.config;
+
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
+import org.hamcrest.Matchers;
+import org.jsonschema2pojo.integration.util.FileSearchMatcher;
+import org.jsonschema2pojo.integration.util.Jsonschema2PojoRule;
+import org.junit.Rule;
+import org.junit.Test;
+
+import javax.annotation.Generated;
+
+import java.io.File;
+import java.lang.annotation.Annotation;
+import java.nio.file.Files;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+import static org.jsonschema2pojo.integration.util.CodeGenerationHelper.config;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertNotNull;
+
+public class IncludeGeneratedAnnotationIT
+{
+	private static final String PROP_KEY = "includeGeneratedAnnotation";
+	private static final String SCHEMA_PATH = "/schema/" + PROP_KEY + "/" + PROP_KEY + ".json";
+	private static final String TEST_PACKAGE = "com.example";
+
+	@Rule
+	public Jsonschema2PojoRule schemaRule = new Jsonschema2PojoRule();
+
+	@Test
+	public void defaultConfig() throws ClassNotFoundException
+	{
+		File source = schemaRule.generate(SCHEMA_PATH, TEST_PACKAGE);
+
+		assertThat(source, FileSearchMatcher.containsText("javax.annotation.Generated"));
+	}
+
+	@Test
+	public void disabled() throws ClassNotFoundException
+	{
+		File source = schemaRule.generate(SCHEMA_PATH, TEST_PACKAGE, config(PROP_KEY, false));
+
+		assertThat(source, Matchers.not(FileSearchMatcher.containsText("javax.annotation.Generated")));
+	}
+
+	@Test
+	public void enabled() throws ClassNotFoundException
+	{
+		File source = schemaRule.generate(SCHEMA_PATH, TEST_PACKAGE, config(PROP_KEY, true));
+
+		assertThat(source, FileSearchMatcher.containsText("javax.annotation.Generated"));
+	}
+
+}
diff --git a/jsonschema2pojo-integration-tests/src/test/resources/schema/includeGeneratedAnnotation/includeGeneratedAnnotation.json b/jsonschema2pojo-integration-tests/src/test/resources/schema/includeGeneratedAnnotation/includeGeneratedAnnotation.json
new file mode 100644
index 000000000..9755a6429
--- /dev/null
+++ b/jsonschema2pojo-integration-tests/src/test/resources/schema/includeGeneratedAnnotation/includeGeneratedAnnotation.json
@@ -0,0 +1,9 @@
+{
+  "type" : "object",
+  "properties" : {
+    "enum_Property" : {
+      "type" : "string",
+      "enum" : ["1", "2", "3", "4"]
+    }
+  }
+}
\ No newline at end of file
diff --git a/jsonschema2pojo-maven-plugin/src/main/java/org/jsonschema2pojo/maven/Jsonschema2PojoMojo.java b/jsonschema2pojo-maven-plugin/src/main/java/org/jsonschema2pojo/maven/Jsonschema2PojoMojo.java
index 685f93270..29b9d9466 100644
--- a/jsonschema2pojo-maven-plugin/src/main/java/org/jsonschema2pojo/maven/Jsonschema2PojoMojo.java
+++ b/jsonschema2pojo-maven-plugin/src/main/java/org/jsonschema2pojo/maven/Jsonschema2PojoMojo.java
@@ -795,6 +795,12 @@ public class Jsonschema2PojoMojo extends AbstractMojo implements GenerationConfi
      */
     private boolean includeConstructorPropertiesAnnotation = false;
 
+    /**
+     * @parameter property="jsonschema2pojo.markGenerated"
+     *            default-value="false"
+     */
+    private boolean includeGeneratedAnnotation = true;
+
     /**
      * Executes the plugin, to read the given source and behavioural properties
      * and generate POJOs. The current implementation acts as a wrapper around
@@ -1238,4 +1244,9 @@ public Map getFormatTypeMapping() {
     public boolean isUseInnerClassBuilders() {
         return useInnerClassBuilders;
     }
+    
+    @Override
+    public boolean isIncludeGeneratedAnnotation() {
+    	return includeGeneratedAnnotation;
+    }
 }