Skip to content

Commit

Permalink
Use targetVersion to decide which Generated annotation to add
Browse files Browse the repository at this point in the history
Closes #1474, closes #1473, closes #1361
  • Loading branch information
joelittlejohn committed Feb 14, 2023
1 parent d0d507c commit 89a97a9
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*/
Expand Down Expand Up @@ -348,11 +349,11 @@ public boolean isIncludeSetters() {
}

/**
* @return <code>1.6</code>
* @return Current JVM version
*/
@Override
public String getTargetVersion() {
return "1.6";
return JavaVersion.parse(System.getProperty("java.version"));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -618,7 +618,7 @@ default boolean isUseInnerClassBuilders() {

/**
* Whether to mark generated classes with the annotation <code>javax.annotation.@Generated</code>
*
* (or <code>javax.annotation.processing.Generated</code> for Java 9 and later).
*/
boolean isIncludeGeneratedAnnotation();

Expand All @@ -631,4 +631,4 @@ default boolean isUseInnerClassBuilders() {
*/
boolean isUseJakartaValidation();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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".
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
1 change: 1 addition & 0 deletions jsonschema2pojo-gradle-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
6 changes: 6 additions & 0 deletions jsonschema2pojo-integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@
<version>${project.version}</version>
<type>maven-plugin</type>
</dependency>
<dependency>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-jdk-annotation</artifactId>
<version>0.0.1</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -775,7 +774,6 @@ public class Jsonschema2PojoMojo extends AbstractMojo implements GenerationConfi
* when adding <a href="http://jcp.org/en/jsr/detail?id=303">JSR-303</a> 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;
Expand Down

0 comments on commit 89a97a9

Please sign in to comment.