Repository: joelittlejohn/jsonschema2pojo
Pull request #1478 (2023-02-08 21:09:32)

Choose default targetVersion using Gradle/Maven configuration
Relates to #1473.




Patch:
diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/DynamicPropertiesRule.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/DynamicPropertiesRule.java
index d9fde339..72768fb0 100644
--- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/DynamicPropertiesRule.java
+++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/DynamicPropertiesRule.java
@@ -19,11 +19,7 @@ package org.jsonschema2pojo.rules;
 import static com.sun.codemodel.JExpr.*;
 import static com.sun.codemodel.JMod.*;
 
-import java.util.Iterator;
-import java.util.Map;
-
 import org.jsonschema2pojo.Schema;
-import org.jsonschema2pojo.util.LanguageFeatures;
 import org.jsonschema2pojo.util.Models;
 
 import com.fasterxml.jackson.databind.JsonNode;
@@ -40,6 +36,8 @@ import com.sun.codemodel.JSwitch;
 import com.sun.codemodel.JType;
 import com.sun.codemodel.JTypeVar;
 import com.sun.codemodel.JVar;
+import java.util.Iterator;
+import java.util.Map;
 
 /**
  * Adds methods for dynamically getting, setting, and building properties.
@@ -105,20 +103,11 @@ public class DynamicPropertiesRule implements Rule<JDefinedClass, JDefinedClass>
         boolean isGenerateBuilders = ruleFactory.getGenerationConfig().isGenerateBuilders();
 
         if (isIncludeGetters || isIncludeSetters || isGenerateBuilders) {
-            if (LanguageFeatures.canUseJava7(ruleFactory.getGenerationConfig())) {
-                if (isIncludeSetters) {
-                    addInternalSetMethodJava7(jclass, node);
-                }
-                if (isIncludeGetters) {
-                    addInternalGetMethodJava7(jclass, node);
-                }
-            } else {
-                if (isIncludeSetters) {
-                    addInternalSetMethodJava6(jclass, node);
-                }
-                if (isIncludeGetters) {
-                    addInternalGetMethodJava6(jclass, node);
-                }
+            if (isIncludeSetters) {
+                addInternalSetMethodJava6(jclass, node);
+            }
+            if (isIncludeGetters) {
+                addInternalGetMethodJava6(jclass, node);
             }
         }
 
@@ -172,38 +161,6 @@ public class DynamicPropertiesRule implements Rule<JDefinedClass, JDefinedClass>
         return method;
     }
 
-    private JMethod addInternalGetMethodJava7(JDefinedClass jclass, JsonNode propertiesNode) {
-        JMethod method = jclass.method(PROTECTED, jclass.owner()._ref(Object.class), DEFINED_GETTER_NAME);
-        JVar nameParam = method.param(String.class, "name");
-        JVar notFoundParam = method.param(jclass.owner()._ref(Object.class), "notFoundValue");
-        JBlock body = method.body();
-        JSwitch propertySwitch = body._switch(nameParam);
-        if (propertiesNode != null) {
-            for (Iterator<Map.Entry<String, JsonNode>> properties = propertiesNode.fields(); properties.hasNext();) {
-                Map.Entry<String, JsonNode> property = properties.next();
-                String propertyName = property.getKey();
-                JsonNode node = property.getValue();
-                String fieldName = ruleFactory.getNameHelper().getPropertyName(propertyName, node);
-                JType propertyType = jclass.fields().get(fieldName).type();
-
-                addGetPropertyCase(jclass, propertySwitch, propertyName, propertyType, node);
-            }
-        }
-        JClass extendsType = jclass._extends();
-        if (extendsType != null && extendsType instanceof JDefinedClass) {
-            JDefinedClass parentClass = (JDefinedClass) extendsType;
-            JMethod parentMethod = parentClass.getMethod(DEFINED_GETTER_NAME,
-                    new JType[] { parentClass.owner()._ref(String.class), parentClass.owner()._ref(Object.class) });
-            propertySwitch._default().body()
-            ._return(_super().invoke(parentMethod).arg(nameParam).arg(notFoundParam));
-        } else {
-            propertySwitch._default().body()
-            ._return(notFoundParam);
-        }
-
-        return method;
-    }
-
     private JMethod addInternalGetMethodJava6(JDefinedClass jclass, JsonNode propertiesNode) {
         JMethod method = jclass.method(PROTECTED, jclass.owner()._ref(Object.class), DEFINED_GETTER_NAME);
         JVar nameParam = method.param(String.class, "name");
@@ -305,36 +262,6 @@ public class DynamicPropertiesRule implements Rule<JDefinedClass, JDefinedClass>
         return method;
     }
 
-    private JMethod addInternalSetMethodJava7(JDefinedClass jclass, JsonNode propertiesNode) {
-        JMethod method = jclass.method(PROTECTED, jclass.owner().BOOLEAN, DEFINED_SETTER_NAME);
-        JVar nameParam = method.param(String.class, "name");
-        JVar valueParam = method.param(Object.class, "value");
-        JBlock body = method.body();
-        JSwitch propertySwitch = body._switch(nameParam);
-        if (propertiesNode != null) {
-            for (Iterator<Map.Entry<String, JsonNode>> properties = propertiesNode.fields(); properties.hasNext();) {
-                Map.Entry<String, JsonNode> property = properties.next();
-                String propertyName = property.getKey();
-                JsonNode node = property.getValue();
-                String fieldName = ruleFactory.getNameHelper().getPropertyName(propertyName, node);
-                JType propertyType = jclass.fields().get(fieldName).type();
-
-                addSetPropertyCase(jclass, propertySwitch, propertyName, propertyType, valueParam, node);
-            }
-        }
-        JBlock defaultBlock = propertySwitch._default().body();
-        JClass extendsType = jclass._extends();
-        if (extendsType != null && extendsType instanceof JDefinedClass) {
-            JDefinedClass parentClass = (JDefinedClass) extendsType;
-            JMethod parentMethod = parentClass.getMethod(DEFINED_SETTER_NAME,
-                    new JType[] { parentClass.owner()._ref(String.class), parentClass.owner()._ref(Object.class) });
-            defaultBlock._return(_super().invoke(parentMethod).arg(nameParam).arg(valueParam));
-        } else {
-            defaultBlock._return(FALSE);
-        }
-        return method;
-    }
-
     private JMethod addInternalSetMethodJava6(JDefinedClass jclass, JsonNode propertiesNode) {
         JMethod method = jclass.method(PROTECTED, jclass.owner().BOOLEAN, DEFINED_SETTER_NAME);
         JVar nameParam = method.param(String.class, "name");
diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/util/JavaVersion.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/util/JavaVersion.java
new file mode 100644
index 00000000..f5e1dfd0
--- /dev/null
+++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/util/JavaVersion.java
@@ -0,0 +1,46 @@
+/**
+ * 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 static org.apache.commons.lang3.StringUtils.*;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class JavaVersion {
+
+    private static final Pattern JAVA_VERSION_1_X = Pattern.compile("(^1.\\d+)");
+    private static final Pattern JAVA_VERSION_X = Pattern.compile("(^\\d+)");
+
+    public static String parse(String version) {
+        if (startsWith(version, "1.")) {
+            Matcher m = JAVA_VERSION_1_X.matcher(version);
+            m.find();
+            return m.group();
+        } else {
+            Matcher m = JAVA_VERSION_X.matcher(version);
+            m.find();
+            return m.group();
+        }
+    }
+
+    public static boolean is9OrLater(final String targetVersion) {
+        final Double v = Double.valueOf(targetVersion);
+        return (v >= 9) || (v < 2 && v >= 1.9);
+    }
+
+}
diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/util/LanguageFeatures.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/util/LanguageFeatures.java
deleted file mode 100644
index 2a1fb2a9..00000000
--- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/util/LanguageFeatures.java
+++ /dev/null
@@ -1,37 +0,0 @@
-/**
- * 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 static java.util.Arrays.*;
-
-import java.util.Collection;
-
-import org.jsonschema2pojo.GenerationConfig;
-
-public class LanguageFeatures {
-
-    private static final Collection<String> LESS_THAN_8 = asList("1.1", "1.2", "1.3", "1.4", "1.5", "5", "1.6", "6", "1.7", "7");
-    private static final Collection<String> LESS_THAN_7 = asList("1.1", "1.2", "1.3", "1.4", "1.5", "5", "1.6", "6");
-
-    public static boolean canUseJava7(GenerationConfig config) {
-        return !LESS_THAN_7.contains(config.getTargetVersion());
-    }
-
-    public static boolean canUseJava8(GenerationConfig config) {
-        return !LESS_THAN_8.contains(config.getTargetVersion());
-    }
-}
diff --git a/jsonschema2pojo-gradle-plugin/README.md b/jsonschema2pojo-gradle-plugin/README.md
index d47d0c56..df82b2e2 100644
--- a/jsonschema2pojo-gradle-plugin/README.md
+++ b/jsonschema2pojo-gradle-plugin/README.md
@@ -56,7 +56,6 @@ dependencies {
   implementation 'joda-time:joda-time:2.2'
 }
 
-// Each configuration is set to the default value
 jsonSchema2Pojo {
 
   // Location of the JSON Schema file(s). This may refer to a single file or a directory of files.
@@ -292,8 +291,10 @@ jsonSchema2Pojo {
   // properties to exclude from generated toString
   toStringExcludes = ["someProperty"]
 
-  // What java source version to target with generated output (1.6, 1.8, 9, 11, etc)
-  targetVersion = "1.6"
+    // What Java version to target with generated source code (1.6, 1.8, 9, 11, etc).
+    // By default, the version will be taken from the Gradle Java plugin's 'sourceCompatibility',
+    // which (if unset) itself defaults to the current JVM version
+  targetVersion = "1.8"
 
   // deprecated, since we no longer use commons-lang for equals, hashCode, toString
   useCommonsLang3 = false
diff --git a/jsonschema2pojo-gradle-plugin/src/main/groovy/org/jsonschema2pojo/gradle/GenerateJsonSchemaAndroidTask.groovy b/jsonschema2pojo-gradle-plugin/src/main/groovy/org/jsonschema2pojo/gradle/GenerateJsonSchemaAndroidTask.groovy
index eb3ce4f4..d722a495 100644
--- a/jsonschema2pojo-gradle-plugin/src/main/groovy/org/jsonschema2pojo/gradle/GenerateJsonSchemaAndroidTask.groovy
+++ b/jsonschema2pojo-gradle-plugin/src/main/groovy/org/jsonschema2pojo/gradle/GenerateJsonSchemaAndroidTask.groovy
@@ -15,12 +15,13 @@
  */
 package org.jsonschema2pojo.gradle
 
+import org.jsonschema2pojo.GenerationConfig
+import org.jsonschema2pojo.Jsonschema2Pojo
+
 import org.gradle.api.tasks.OutputDirectory
 import org.gradle.api.tasks.SourceTask
 import org.gradle.api.tasks.TaskAction
 import org.gradle.work.InputChanges
-import org.jsonschema2pojo.GenerationConfig
-import org.jsonschema2pojo.Jsonschema2Pojo
 
 /**
  * Task that generates java source files for an Android project
@@ -48,6 +49,7 @@ class GenerateJsonSchemaAndroidTask extends SourceTask {
 
     GenerationConfig configuration = project.jsonSchema2Pojo
     configuration.targetDirectory = outputDir
+    setTargetVersion configuration
 
     if (Boolean.TRUE == configuration.properties.get("useCommonsLang3")) {
       logger.warn 'useCommonsLang3 is deprecated. Please remove it from your config.'
@@ -57,4 +59,12 @@ class GenerateJsonSchemaAndroidTask extends SourceTask {
 
     Jsonschema2Pojo.generate(configuration, new GradleRuleLogger(logger))
   }
+
+  void setTargetVersion(JsonSchemaExtension configuration) {
+    if (!configuration.targetVersion) {
+      def compileJavaTask = project.getTasksByName("compileJava", false).first()
+      configuration.targetVersion = compileJavaTask.getProperties().get("sourceCompatibility")
+      logger.warn 'Using Gradle targetCompatibility as targetVersion for jsonschema2pojo: ' + configuration.targetVersion
+    }
+  }
 }
diff --git a/jsonschema2pojo-gradle-plugin/src/main/groovy/org/jsonschema2pojo/gradle/GenerateJsonSchemaJavaTask.groovy b/jsonschema2pojo-gradle-plugin/src/main/groovy/org/jsonschema2pojo/gradle/GenerateJsonSchemaJavaTask.groovy
index 25b2d41d..f46b4544 100644
--- a/jsonschema2pojo-gradle-plugin/src/main/groovy/org/jsonschema2pojo/gradle/GenerateJsonSchemaJavaTask.groovy
+++ b/jsonschema2pojo-gradle-plugin/src/main/groovy/org/jsonschema2pojo/gradle/GenerateJsonSchemaJavaTask.groovy
@@ -17,11 +17,12 @@ package org.jsonschema2pojo.gradle
 
 import org.jsonschema2pojo.GenerationConfig
 import org.jsonschema2pojo.Jsonschema2Pojo
+
 import org.gradle.api.DefaultTask
 import org.gradle.api.GradleException
-import org.gradle.api.tasks.TaskAction
-import org.gradle.api.tasks.Input
 import org.gradle.api.model.ReplacedBy
+import org.gradle.api.tasks.Input
+import org.gradle.api.tasks.TaskAction
 
 /**
  * A task that performs code generation.
@@ -52,6 +53,7 @@ class GenerateJsonSchemaJavaTask extends DefaultTask {
         throw new GradleException('generateJsonSchema: Java plugin is required')
       }
       outputs.dir configuration.targetDirectory
+      setTargetVersion configuration
 
       inputs.property("configuration", configuration.toString())
       inputs.files project.files(configuration.sourceFiles)
@@ -79,4 +81,12 @@ class GenerateJsonSchemaJavaTask extends DefaultTask {
 
     Jsonschema2Pojo.generate(configuration, new GradleRuleLogger(logger))
   }
+
+  void setTargetVersion(JsonSchemaExtension configuration) {
+    if (!configuration.targetVersion) {
+      def compileJavaTask = project.getTasksByName("compileJava", false).first()
+      configuration.targetVersion = compileJavaTask.getProperties().get("sourceCompatibility")
+      logger.warn 'Using Gradle targetCompatibility as targetVersion for jsonschema2pojo: ' + configuration.targetVersion
+    }
+  }
 }
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 324f3cad..eb06e24b 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
@@ -15,9 +15,10 @@
  */
 package org.jsonschema2pojo.gradle
 
+
+import org.jsonschema2pojo.AllFileFilter
 import org.jsonschema2pojo.AnnotationStyle
 import org.jsonschema2pojo.Annotator
-import org.jsonschema2pojo.AllFileFilter
 import org.jsonschema2pojo.GenerationConfig
 import org.jsonschema2pojo.InclusionLevel
 import org.jsonschema2pojo.NoopAnnotator
@@ -29,7 +30,7 @@ import org.jsonschema2pojo.rules.RuleFactory
  * The configuration properties.
  *
  * @author Ben Manes (ben.manes@gmail.com)
- * @see https://github.com/joelittlejohn/jsonschema2pojo
+ * @see <a href="https://github.com/joelittlejohn/jsonschema2pojo">https://github.com/joelittlejohn/jsonschema2pojo</a>
  */
 public class JsonSchemaExtension implements GenerationConfig {
   Iterable<File> sourceFiles
@@ -145,7 +146,6 @@ public class JsonSchemaExtension implements GenerationConfig {
     includeAdditionalProperties = true
     includeGetters = true
     includeSetters = true
-    targetVersion = '1.6'
     includeDynamicAccessors = false
     includeDynamicGetters = false
     includeDynamicSetters = false
@@ -220,6 +220,10 @@ public class JsonSchemaExtension implements GenerationConfig {
     targetLangauge = Langauge.valueOf(language.toUpperCase())
   }
 
+  public void setTargetVersion(String targetVersion) {
+    this.targetVersion = targetVersion
+  }
+
   public void setIncludeConstructorPropertiesAnnotation(boolean enabled) {
     includeConstructorPropertiesAnnotation = enabled
   }
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 263a48c4..216d0c07 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
@@ -19,6 +19,21 @@ package org.jsonschema2pojo.maven;
 import static org.apache.commons.lang3.ArrayUtils.isEmpty;
 import static org.apache.commons.lang3.StringUtils.*;
 
+import org.jsonschema2pojo.AllFileFilter;
+import org.jsonschema2pojo.AnnotationStyle;
+import org.jsonschema2pojo.Annotator;
+import org.jsonschema2pojo.AnnotatorFactory;
+import org.jsonschema2pojo.GenerationConfig;
+import org.jsonschema2pojo.InclusionLevel;
+import org.jsonschema2pojo.Jsonschema2Pojo;
+import org.jsonschema2pojo.NoopAnnotator;
+import org.jsonschema2pojo.RuleLogger;
+import org.jsonschema2pojo.SourceSortOrder;
+import org.jsonschema2pojo.SourceType;
+import org.jsonschema2pojo.rules.RuleFactory;
+import org.jsonschema2pojo.util.JavaVersion;
+import org.jsonschema2pojo.util.URLUtil;
+
 import java.io.File;
 import java.io.FileFilter;
 import java.io.IOException;
@@ -29,9 +44,9 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-
 import org.apache.commons.io.FilenameUtils;
 import org.apache.maven.artifact.DependencyResolutionRequiredException;
+import org.apache.maven.model.Plugin;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugins.annotations.LifecyclePhase;
@@ -39,19 +54,7 @@ import org.apache.maven.plugins.annotations.Mojo;
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.plugins.annotations.ResolutionScope;
 import org.apache.maven.project.MavenProject;
-import org.jsonschema2pojo.AllFileFilter;
-import org.jsonschema2pojo.AnnotationStyle;
-import org.jsonschema2pojo.Annotator;
-import org.jsonschema2pojo.AnnotatorFactory;
-import org.jsonschema2pojo.GenerationConfig;
-import org.jsonschema2pojo.InclusionLevel;
-import org.jsonschema2pojo.Jsonschema2Pojo;
-import org.jsonschema2pojo.NoopAnnotator;
-import org.jsonschema2pojo.RuleLogger;
-import org.jsonschema2pojo.SourceSortOrder;
-import org.jsonschema2pojo.SourceType;
-import org.jsonschema2pojo.rules.RuleFactory;
-import org.jsonschema2pojo.util.URLUtil;
+import org.apache.maven.shared.utils.xml.Xpp3Dom;
 
 /**
  * When invoked, this goal reads one or more
@@ -599,16 +602,26 @@ public class Jsonschema2PojoMojo extends AbstractMojo implements GenerationConfi
      * create public fields instead
      *
      */
-    @Parameter(property = "jsonschame2pojo.includeSetters", defaultValue = "true")
+    @Parameter(property = "jsonschema2pojo.includeSetters", defaultValue = "true")
     private boolean includeSetters = true;
 
     /**
-     * The target version for generated source files.
-     *
-     * @since 0.4.17
+     * The target version for generated source files, used whenever decisions are made
+     * about generating source code that may be incompatible with older JVMs. Acceptable values
+     * include e.g. 1.6, 1.8, 8, 9, 10, 11.
+     * <p/>
+     * If not set, the value of targetVersion is auto-detected. For auto-detection, the first
+     * value found in the following list will be used:
+     * <ol>
+     *     <li>maven.compiler.source property</li>
+     *     <li>maven.compiler.release property</li>
+     *     <li>maven-compiler-plugin 'source' configuration option</li>
+     *     <li>maven-compiler-plugin 'release' configuration option</li>
+     *     <li>the current JVM version</li>
+     * </ol>
      */
-    @Parameter(property = "jsonschema2pojo.targetJavaVersion", defaultValue = "${maven.compiler.target}")
-    private String targetVersion = "1.6";
+    @Parameter(property = "jsonschema2pojo.targetVersion")
+    private String targetVersion;
 
     /**
      * Whether to include dynamic getters, setters, and builders or to omit
@@ -778,6 +791,7 @@ public class Jsonschema2PojoMojo extends AbstractMojo implements GenerationConfi
     public void execute() throws MojoExecutionException {
 
         addProjectDependenciesToClasspath();
+        setTargetVersion();
 
         try {
             getAnnotationStyle();
@@ -1114,6 +1128,45 @@ public class Jsonschema2PojoMojo extends AbstractMojo implements GenerationConfi
     @Override
     public boolean isIncludeSetters() { return includeSetters; }
 
+    private void setTargetVersion() {
+        if (isNotBlank(this.targetVersion)) {
+            return;
+        }
+
+        if (project.getProperties() != null && project.getProperties().containsKey("maven.compiler.source")) {
+            this.targetVersion = project.getProperties().get("maven.compiler.source").toString();
+            getLog().debug("Using maven.compiler.source to set targetVersion for generated sources (" + this.targetVersion + ")");
+            return;
+        }
+
+        if (project.getProperties() != null && project.getProperties().containsKey("maven.compiler.release")) {
+            this.targetVersion = project.getProperties().get("maven.compiler.release").toString();
+            getLog().debug("Using maven.compiler.release to set targetVersion for generated sources (" + this.targetVersion + ")");
+            return;
+        }
+
+        for (Plugin p : (List<Plugin>) project.getBuildPlugins()) {
+            if (p.getKey().equals("org.apache.maven.plugins:maven-compiler-plugin") && p.getConfiguration() instanceof Xpp3Dom) {
+                final Xpp3Dom compilerSourceConfig = ((Xpp3Dom) p.getConfiguration()).getChild("source");
+                if (compilerSourceConfig != null) {
+                    this.targetVersion = compilerSourceConfig.getValue();
+                    getLog().debug("Using maven-compiler-plugin 'source' to set targetVersion for generated sources (" + this.targetVersion + ")");
+                    return;
+                }
+
+                final Xpp3Dom compilerReleaseConfig = ((Xpp3Dom) p.getConfiguration()).getChild("release");
+                if (compilerReleaseConfig != null) {
+                    this.targetVersion = compilerReleaseConfig.getValue();
+                    getLog().debug("Using maven-compiler-plugin 'release' to set targetVersion for generated sources (" + this.targetVersion + ")");
+                    return;
+                }
+            }
+        }
+
+        this.targetVersion = JavaVersion.parse(System.getProperty("java.version"));
+        getLog().debug("Using JVM to set targetVersion for generated sources (" + this.targetVersion + ")");
+    }
+
     @Override
     public String getTargetVersion() {
         return targetVersion;


Test patch:
diff --git a/jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/util/JavaVersionTest.java b/jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/util/JavaVersionTest.java
new file mode 100644
index 00000000..e6004874
--- /dev/null
+++ b/jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/util/JavaVersionTest.java
@@ -0,0 +1,54 @@
+/**
+ * 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 static org.hamcrest.CoreMatchers.*;
+import static org.hamcrest.MatcherAssert.*;
+
+import org.junit.Test;
+
+public class JavaVersionTest {
+
+    @Test
+    public void testParse() {
+        assertThat(JavaVersion.parse("1.8.0_362"), is("1.8"));
+        assertThat(JavaVersion.parse("11.0.18"), is("11"));
+        assertThat(JavaVersion.parse("9.0.4"), is("9"));
+        assertThat(JavaVersion.parse("10.0.1"), is("10"));
+    }
+
+    @Test
+    public void testIs9OrLater() {
+        assertThat(JavaVersion.is9OrLater("1.1"), is(false));
+        assertThat(JavaVersion.is9OrLater("1.2"), is(false));
+        assertThat(JavaVersion.is9OrLater("1.3"), is(false));
+        assertThat(JavaVersion.is9OrLater("1.4"), is(false));
+        assertThat(JavaVersion.is9OrLater("1.5"), is(false));
+        assertThat(JavaVersion.is9OrLater("5"), is(false));
+        assertThat(JavaVersion.is9OrLater("1.6"), is(false));
+        assertThat(JavaVersion.is9OrLater("6"), is(false));
+        assertThat(JavaVersion.is9OrLater("1.7"), is(false));
+        assertThat(JavaVersion.is9OrLater("7"), is(false));
+        assertThat(JavaVersion.is9OrLater("1.8"), is(false));
+        assertThat(JavaVersion.is9OrLater("8"), is(false));
+        assertThat(JavaVersion.is9OrLater("1.9"), is(true));
+        assertThat(JavaVersion.is9OrLater("9"), is(true));
+        assertThat(JavaVersion.is9OrLater("10"), is(true));
+        assertThat(JavaVersion.is9OrLater("11"), is(true));
+    }
+
+}
diff --git a/jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/util/LanguageFeaturesTest.java b/jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/util/LanguageFeaturesTest.java
deleted file mode 100644
index 9a0f08ef..00000000
--- a/jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/util/LanguageFeaturesTest.java
+++ /dev/null
@@ -1,94 +0,0 @@
-/**
- * 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 static org.hamcrest.MatcherAssert.*;
-import static org.hamcrest.Matchers.*;
-import static org.jsonschema2pojo.util.LanguageFeaturesTest.VersionEnum.*;
-import static org.mockito.Mockito.*;
-
-import java.util.Arrays;
-import java.util.Collection;
-
-import org.jsonschema2pojo.GenerationConfig;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameters;
-
-@RunWith(Parameterized.class)
-public class LanguageFeaturesTest {
-
-    public static enum VersionEnum {
-
-        BEFORE_6(false, false, false),
-        MAX_6(true, false, false),
-        MAX_7(true, true, false),
-        MAX_8(true, true, true),
-        AFTER_8(true, true, true);
-
-        public final boolean canUse6;
-        public final boolean canUse7;
-        public final boolean canUse8;
-
-        VersionEnum(boolean canUse6, boolean canUse7, boolean canUse8) {
-            this.canUse6 = canUse6;
-            this.canUse7 = canUse7;
-            this.canUse8 = canUse8;
-        }
-    }
-
-    @Parameters
-    public static Collection<Object[]> parameters() {
-        return Arrays.asList(new Object[][] {
-            { "1.5", BEFORE_6 },
-            { "5", BEFORE_6 },
-            { "1.6", MAX_6 },
-            { "6", MAX_6 },
-            { "1.7", MAX_7 },
-            { "7", MAX_7 },
-            { "1.8", MAX_8 },
-            { "8", MAX_8 },
-            { "1.9", AFTER_8 },
-            { "9", AFTER_8 }
-        });
-    }
-
-    private String version;
-    private VersionEnum versionSpec;
-
-    public LanguageFeaturesTest(String version, VersionEnum versionSpec) {
-        this.version = version;
-        this.versionSpec = versionSpec;
-    }
-
-    @Test
-    public void correctTestForJava7() {
-        assertThat(LanguageFeatures.canUseJava7(mockConfig(version)), equalTo(versionSpec.canUse7));
-    }
-
-    @Test
-    public void correctTestForJava8() {
-        assertThat(LanguageFeatures.canUseJava8(mockConfig(version)), equalTo(versionSpec.canUse8));
-    }
-
-    public static GenerationConfig mockConfig(String version) {
-        GenerationConfig config = mock(GenerationConfig.class);
-        when(config.getTargetVersion()).thenReturn(version);
-        return config;
-    }
-}
diff --git a/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/util/TestableJsonschema2PojoMojo.java b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/util/TestableJsonschema2PojoMojo.java
index b69cf1a6..0540f0ca 100644
--- a/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/util/TestableJsonschema2PojoMojo.java
+++ b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/util/TestableJsonschema2PojoMojo.java
@@ -16,11 +16,13 @@
 
 package org.jsonschema2pojo.integration.util;
 
+import org.jsonschema2pojo.maven.Jsonschema2PojoMojo;
+
 import java.lang.reflect.Field;
 import java.util.Map;
 import java.util.Map.Entry;
-
-import org.jsonschema2pojo.maven.Jsonschema2PojoMojo;
+import org.apache.maven.plugin.logging.Log;
+import org.apache.maven.plugin.logging.SystemStreamLog;
 
 /**
  * A plugin mojo that allows the private property values usually only set by
@@ -28,6 +30,18 @@ import org.jsonschema2pojo.maven.Jsonschema2PojoMojo;
  */
 public class TestableJsonschema2PojoMojo extends Jsonschema2PojoMojo {
 
+    private static final Log LOGGER_NO_DEBUG = new SystemStreamLog() {
+        @Override
+        public void debug(CharSequence content) {
+        }
+        @Override
+        public void debug(Throwable error) {
+        }
+        @Override
+        public void debug(CharSequence content, Throwable error) {
+        }
+    };
+
     public TestableJsonschema2PojoMojo configure(Map<String, Object> configValues) {
         
         // this could be done with reflection, if the plugin used real annotations.
@@ -37,6 +51,8 @@ public class TestableJsonschema2PojoMojo extends Jsonschema2PojoMojo {
             setPrivateField(value.getKey(), value.getValue());
         }
 
+        this.setLog(LOGGER_NO_DEBUG);
+
         return this;
     }