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 eea0a87 commit cfeddbc
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 33 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
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 cfeddbc

Please sign in to comment.