diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/DefaultGenerationConfig.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/DefaultGenerationConfig.java
index e9a453ce7..fd430e630 100644
--- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/DefaultGenerationConfig.java
+++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/DefaultGenerationConfig.java
@@ -16,6 +16,9 @@
package org.jsonschema2pojo;
+import org.jsonschema2pojo.rules.RuleFactory;
+import org.jsonschema2pojo.util.JavaVersion;
+
import java.io.File;
import java.io.FileFilter;
import java.net.URL;
@@ -24,8 +27,6 @@
import java.util.Iterator;
import java.util.Map;
-import org.jsonschema2pojo.rules.RuleFactory;
-
/**
* A generation config that returns default values for all behavioural options.
*/
@@ -348,11 +349,11 @@ public boolean isIncludeSetters() {
}
/**
- * @return 1.6
+ * @return Current JVM version
*/
@Override
public String getTargetVersion() {
- return "1.6";
+ return JavaVersion.parse(System.getProperty("java.version"));
}
/**
diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/GenerationConfig.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/GenerationConfig.java
index 148954f43..e725edfb1 100644
--- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/GenerationConfig.java
+++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/GenerationConfig.java
@@ -16,14 +16,14 @@
package org.jsonschema2pojo;
+import org.jsonschema2pojo.rules.RuleFactory;
+
import java.io.File;
import java.io.FileFilter;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
-import org.jsonschema2pojo.rules.RuleFactory;
-
/**
* Defines the configuration options for Java type generation, including source
* and target paths/packages and all behavioural options (e.g should builders be
@@ -618,7 +618,7 @@ default boolean isUseInnerClassBuilders() {
/**
* Whether to mark generated classes with the annotation javax.annotation.@Generated
- *
+ * (or javax.annotation.processing.Generated
for Java 9 and later).
*/
boolean isIncludeGeneratedAnnotation();
@@ -631,4 +631,4 @@ default boolean isUseInnerClassBuilders() {
*/
boolean isUseJakartaValidation();
-}
\ No newline at end of file
+}
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 19cca2514..94137b788 100644
--- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/EnumRule.java
+++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/EnumRule.java
@@ -21,13 +21,6 @@
import static org.jsonschema2pojo.rules.PrimitiveTypes.*;
import static org.jsonschema2pojo.util.TypeUtil.*;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
import org.jsonschema2pojo.Annotator;
import org.jsonschema2pojo.RuleLogger;
import org.jsonschema2pojo.Schema;
@@ -57,6 +50,12 @@
import com.sun.codemodel.JMod;
import com.sun.codemodel.JType;
import com.sun.codemodel.JVar;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
/**
* Applies the "enum" schema rule.
@@ -151,7 +150,7 @@ 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);
+ AnnotationHelper.addGeneratedAnnotation(ruleFactory.getGenerationConfig(), _enum);
}
JFieldVar valueField = addConstructorAndFields(enumDefinition, _enum);
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 85ef5845e..bd613c829 100644
--- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/ObjectRule.java
+++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/ObjectRule.java
@@ -20,26 +20,18 @@
import static org.jsonschema2pojo.rules.PrimitiveTypes.*;
import static org.jsonschema2pojo.util.TypeUtil.*;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.Set;
-
import org.jsonschema2pojo.AnnotationStyle;
import org.jsonschema2pojo.Annotator;
import org.jsonschema2pojo.Schema;
import org.jsonschema2pojo.exception.ClassAlreadyExistsException;
import org.jsonschema2pojo.exception.GenerationException;
+import org.jsonschema2pojo.util.AnnotationHelper;
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;
@@ -55,6 +47,12 @@
import com.sun.codemodel.JPackage;
import com.sun.codemodel.JType;
import com.sun.codemodel.JVar;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
/**
* Applies the generation steps required for schemas of type "object".
@@ -133,7 +131,7 @@ public JType apply(String nodeName, JsonNode node, JsonNode parent, JPackage _pa
}
if (ruleFactory.getGenerationConfig().isIncludeGeneratedAnnotation()) {
- AnnotationHelper.addGeneratedAnnotation(jclass);
+ AnnotationHelper.addGeneratedAnnotation(ruleFactory.getGenerationConfig(), 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
index 0f4187973..672455a98 100644
--- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/util/AnnotationHelper.java
+++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/util/AnnotationHelper.java
@@ -16,6 +16,8 @@
package org.jsonschema2pojo.util;
+import org.jsonschema2pojo.GenerationConfig;
+
import com.sun.codemodel.JAnnotationUse;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JDefinedClass;
@@ -39,8 +41,10 @@ private static boolean tryToAnnotate(JDefinedClass jclass, String annotationClas
}
- public static void addGeneratedAnnotation(JDefinedClass jclass) {
- if (!tryToAnnotate(jclass, JAVA_9_GENERATED)) {
+ public static void addGeneratedAnnotation(GenerationConfig config, JDefinedClass jclass) {
+ if (JavaVersion.is9OrLater(config.getTargetVersion())) {
+ tryToAnnotate(jclass, JAVA_9_GENERATED);
+ } else {
tryToAnnotate(jclass, JAVA_8_GENERATED);
}
}
diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/util/JavaVersion.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/util/JavaVersion.java
index f5e1dfd01..a5683a98a 100644
--- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/util/JavaVersion.java
+++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/util/JavaVersion.java
@@ -39,8 +39,12 @@ public static String parse(String version) {
}
public static boolean is9OrLater(final String targetVersion) {
- final Double v = Double.valueOf(targetVersion);
- return (v >= 9) || (v < 2 && v >= 1.9);
+ if (isNotBlank(targetVersion)) {
+ final Double v = Double.valueOf(targetVersion);
+ return (v >= 9) || (v < 2 && v >= 1.9);
+ } else {
+ return false;
+ }
}
}
diff --git a/jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/util/JavaVersionTest.java b/jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/util/JavaVersionTest.java
index e60048740..5e8f5dd90 100644
--- a/jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/util/JavaVersionTest.java
+++ b/jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/util/JavaVersionTest.java
@@ -33,6 +33,8 @@ public void testParse() {
@Test
public void testIs9OrLater() {
+ assertThat(JavaVersion.is9OrLater(null), is(false));
+ assertThat(JavaVersion.is9OrLater(""), is(false));
assertThat(JavaVersion.is9OrLater("1.1"), is(false));
assertThat(JavaVersion.is9OrLater("1.2"), is(false));
assertThat(JavaVersion.is9OrLater("1.3"), is(false));
diff --git a/jsonschema2pojo-gradle-plugin/README.md b/jsonschema2pojo-gradle-plugin/README.md
index df82b2e22..710cad7ba 100644
--- a/jsonschema2pojo-gradle-plugin/README.md
+++ b/jsonschema2pojo-gradle-plugin/README.md
@@ -76,6 +76,7 @@ jsonSchema2Pojo {
// Whether to include a javax.annotation.Generated (Java 8 and lower) or
// javax.annotation.processing.Generated (Java 9+) in on generated types (default true).
+ // See also: targetVersion.
includeGeneratedAnnotation = true
// Whether to generate builder-style methods of the form withXxx(value) (that return this),
diff --git a/jsonschema2pojo-integration-tests/pom.xml b/jsonschema2pojo-integration-tests/pom.xml
index 03b24d33a..a464b39b1 100644
--- a/jsonschema2pojo-integration-tests/pom.xml
+++ b/jsonschema2pojo-integration-tests/pom.xml
@@ -102,6 +102,12 @@
${project.version}
maven-plugin
+
+ org.jsonschema2pojo
+ jsonschema2pojo-jdk-annotation
+ 0.0.1
+ jar
+
org.apache.maven
maven-plugin-api
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
index 91646a297..0d7424924 100644
--- 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
@@ -19,11 +19,11 @@
import static org.hamcrest.MatcherAssert.*;
import static org.jsonschema2pojo.integration.util.CodeGenerationHelper.*;
-import java.io.File;
-
-import org.hamcrest.Matchers;
import org.jsonschema2pojo.integration.util.FileSearchMatcher;
import org.jsonschema2pojo.integration.util.Jsonschema2PojoRule;
+
+import java.io.File;
+import org.hamcrest.Matchers;
import org.junit.Rule;
import org.junit.Test;
@@ -37,17 +37,25 @@ public class IncludeGeneratedAnnotationIT {
public Jsonschema2PojoRule schemaRule = new Jsonschema2PojoRule();
@Test
- public void defaultConfig() throws ClassNotFoundException {
+ public void defaultConfigHasGeneratedAnnotationsOn() throws ClassNotFoundException {
File source = schemaRule.generate(SCHEMA_PATH, TEST_PACKAGE);
+ assertThat(source, FileSearchMatcher.containsText("@Generated"));
- assertThat(source, FileSearchMatcher.containsText("javax.annotation.Generated"));
+ File sourceJava8 = schemaRule.generate(SCHEMA_PATH, TEST_PACKAGE, config("targetVersion", "8"));
+ assertThat(sourceJava8, FileSearchMatcher.containsText("javax.annotation.Generated"));
+
+ File sourceJava9 = schemaRule.generate(SCHEMA_PATH, TEST_PACKAGE, config("targetVersion", "9"));
+ assertThat(sourceJava9, FileSearchMatcher.containsText("javax.annotation.processing.Generated"));
+
+ File sourceJava11 = schemaRule.generate(SCHEMA_PATH, TEST_PACKAGE, config("targetVersion", "11"));
+ assertThat(sourceJava11, FileSearchMatcher.containsText("javax.annotation.processing.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")));
+ assertThat(source, Matchers.not(FileSearchMatcher.containsText("@Generated")));
}
@Test
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 216d0c07b..52c0092ba 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
@@ -764,8 +764,7 @@ public class Jsonschema2PojoMojo extends AbstractMojo implements GenerationConfi
/**
* Whether to include a javax.annotation.Generated (Java 8 and
* lower) or javax.annotation.processing.Generated (Java 9+) in
- * on generated types.
- *
+ * on generated types. See also: targetVersion.
*/
@Parameter(property = "jsonschema2pojo.includeGeneratedAnnotation", defaultValue = "true")
private boolean includeGeneratedAnnotation = true;
@@ -775,7 +774,6 @@ public class Jsonschema2PojoMojo extends AbstractMojo implements GenerationConfi
* when adding JSR-303 annotations to generated Java types.
* This property works in collaboration with the {@link #isIncludeJsr303Annotations()} configuration option.
* If the {@link #isIncludeJsr303Annotations()} returns {@code false}, then this configuration option will not affect anything.
- *
*/
@Parameter(property = "jsonschema2pojo.useJakartaValidation", defaultValue = "false")
private boolean useJakartaValidation = false;