From 37ee4f6e1f4192401b0a85aece33673763aac021 Mon Sep 17 00:00:00 2001 From: Joe Littlejohn Date: Tue, 14 Feb 2023 20:55:07 +0000 Subject: [PATCH] Use targetVersion to decide which Generated annotation to add Closes #1474, closes #1473, closes #1361 --- .../jsonschema2pojo/DefaultGenerationConfig.java | 5 +++-- .../java/org/jsonschema2pojo/GenerationConfig.java | 4 ++-- .../java/org/jsonschema2pojo/rules/EnumRule.java | 2 +- .../java/org/jsonschema2pojo/rules/ObjectRule.java | 2 +- .../org/jsonschema2pojo/util/AnnotationHelper.java | 8 ++++++-- .../java/org/jsonschema2pojo/util/JavaVersion.java | 8 ++++++-- .../org/jsonschema2pojo/util/JavaVersionTest.java | 2 ++ jsonschema2pojo-gradle-plugin/README.md | 1 + jsonschema2pojo-integration-tests/pom.xml | 6 ++++++ .../config/IncludeGeneratedAnnotationIT.java | 14 +++++++++++--- .../jsonschema2pojo/maven/Jsonschema2PojoMojo.java | 4 +--- 11 files changed, 40 insertions(+), 16 deletions(-) diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/DefaultGenerationConfig.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/DefaultGenerationConfig.java index e9a453ce7..02cab71af 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/DefaultGenerationConfig.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/DefaultGenerationConfig.java @@ -25,6 +25,7 @@ import java.util.Map; import org.jsonschema2pojo.rules.RuleFactory; +import org.jsonschema2pojo.util.JavaVersion; /** * 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..f466b9cbd 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/GenerationConfig.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/GenerationConfig.java @@ -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 779fa28e9..b02adfba4 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/EnumRule.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/EnumRule.java @@ -151,7 +151,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 28173c5b0..852016c2f 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/ObjectRule.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/ObjectRule.java @@ -132,7 +132,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..870ad5811 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 @@ -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;