diff --git a/jsonschema2pojo-ant/src/main/java/org/jsonschema2pojo/ant/Jsonschema2PojoTask.java b/jsonschema2pojo-ant/src/main/java/org/jsonschema2pojo/ant/Jsonschema2PojoTask.java index bff4c594d..5c14c55ff 100644 --- a/jsonschema2pojo-ant/src/main/java/org/jsonschema2pojo/ant/Jsonschema2PojoTask.java +++ b/jsonschema2pojo-ant/src/main/java/org/jsonschema2pojo/ant/Jsonschema2PojoTask.java @@ -120,6 +120,8 @@ public class Jsonschema2PojoTask extends Task implements GenerationConfig { private boolean includeAdditionalProperties = true; private boolean includeAccessors = true; + + private String target = "1.6"; /** * Execute this task (it's expected that all relevant setters will have been @@ -727,4 +729,9 @@ public boolean isIncludeAdditionalProperties() { public boolean isIncludeAccessors() { return includeAccessors; } + + @Override + public String getTarget() { + return target; + } } diff --git a/jsonschema2pojo-cli/src/main/java/org/jsonschema2pojo/cli/Arguments.java b/jsonschema2pojo-cli/src/main/java/org/jsonschema2pojo/cli/Arguments.java index bb0f75f52..0512e61c1 100644 --- a/jsonschema2pojo-cli/src/main/java/org/jsonschema2pojo/cli/Arguments.java +++ b/jsonschema2pojo-cli/src/main/java/org/jsonschema2pojo/cli/Arguments.java @@ -131,6 +131,9 @@ public class Arguments implements GenerationConfig { @Parameter(names = { "-da", "--disable-accessors" }, description = "Whether to omit getter/setter methods and create public fields instead.") private boolean disableAccessors = false; + + @Parameter(names = { "-tv", "--target-version" }, description = "Target version for generated source files.") + private String target = "1.6"; private static final int EXIT_OKAY = 0; private static final int EXIT_ERROR = 1; @@ -320,5 +323,10 @@ public boolean isIncludeAdditionalProperties() { public boolean isIncludeAccessors() { return !disableAccessors; } + + @Override + public String getTarget() { + return target; + } } diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/DefaultGenerationConfig.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/DefaultGenerationConfig.java index ebbf19566..43a5cb5c1 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/DefaultGenerationConfig.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/DefaultGenerationConfig.java @@ -255,4 +255,11 @@ public boolean isIncludeAdditionalProperties() { public boolean isIncludeAccessors() { return true; } + + /** + * @return 1.6 + */ @Override + public String getTarget() { + return "1.6"; + } } \ No newline at end of file diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/GenerationConfig.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/GenerationConfig.java index 73c597161..858e2bc0c 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/GenerationConfig.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/GenerationConfig.java @@ -302,5 +302,12 @@ public interface GenerationConfig { * methods and create public fields instead. */ boolean isIncludeAccessors(); + + /** + * Gets the 'target' configuration option + * + * @return The compilation target for the generated source. + */ + String getTarget(); } diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/util/LanguageFeatures.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/util/LanguageFeatures.java new file mode 100644 index 000000000..fe3641850 --- /dev/null +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/util/LanguageFeatures.java @@ -0,0 +1,40 @@ +/** + * Copyright © 2010-2014 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 org.jsonschema2pojo.GenerationConfig; +import static java.util.Arrays.asList; + +import java.util.Collection; + +public class LanguageFeatures { + + public static final Collection LESS_THAN_8 + = asList("1.1", "1.2", "1.3", "1.4", "1.5", "5", "1.6", "6", "1.7", "7"); + public static final Collection LESS_THAN_7 + = asList("1.1", "1.2", "1.3", "1.4", "1.5", "5", "1.6", "6"); + public static final Collection LESS_THAN_6 + = asList("1.1", "1.2", "1.3", "1.4", "1.5", "5"); + + public static boolean canUseJava7( GenerationConfig config ) { + return !LESS_THAN_7.contains(config.getTarget()); + } + + public static boolean canUseJava8( GenerationConfig config ) { + return !LESS_THAN_8.contains(config.getTarget()); + } +} diff --git a/jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/util/LanguageFeaturesTest.java b/jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/util/LanguageFeaturesTest.java new file mode 100644 index 000000000..ad5c54baa --- /dev/null +++ b/jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/util/LanguageFeaturesTest.java @@ -0,0 +1,93 @@ +/** + * Copyright © 2010-2014 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 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; +import static org.jsonschema2pojo.util.LanguageFeaturesTest.VersionEnum.*; +import static org.mockito.Mockito.*; +import static org.hamcrest.Matchers.*; +import static org.hamcrest.MatcherAssert.*; + +@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 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.getTarget()).thenReturn(version); + return config; + } +} 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 acce1a495..5902f0d06 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 @@ -52,6 +52,7 @@ public class JsonSchemaExtension implements GenerationConfig { char[] propertyWordDelimiters boolean removeOldOutput SourceType sourceType + String target boolean useCommonsLang3 boolean useDoubleNumbers boolean useJodaDates @@ -91,6 +92,7 @@ public class JsonSchemaExtension implements GenerationConfig { classNameSuffix = '' includeAdditionalProperties = true includeAccessors = true + target = '1.6' } @Override @@ -152,6 +154,7 @@ public class JsonSchemaExtension implements GenerationConfig { |initializeCollections = ${initializeCollections} |classNamePrefix = ${classNamePrefix} |classNameSuffix = ${classNameSuffix} + |target = ${target} """.stripMargin() } } diff --git a/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/util/CodeGenerationHelper.java b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/util/CodeGenerationHelper.java index 57bc1ec4d..fc00ece8e 100644 --- a/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/util/CodeGenerationHelper.java +++ b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/util/CodeGenerationHelper.java @@ -121,17 +121,21 @@ private static MavenProject getMockProject() throws DependencyResolutionRequired */ public static ClassLoader compile(File sourceDirectory) { - return compile(sourceDirectory, new ArrayList()); + return compile(sourceDirectory, new ArrayList(), new HashMap()); } + + public static ClassLoader compile(File sourceDirectory, List classpath ) { + return compile(sourceDirectory, classpath, new HashMap()); + } - public static ClassLoader compile(File sourceDirectory, List classpath) { + public static ClassLoader compile(File sourceDirectory, List classpath, Map config) { List fullClasspath = new ArrayList(); fullClasspath.addAll(classpath); fullClasspath.add(System.getProperty("java.class.path")); - new Compiler().compile(sourceDirectory, join(fullClasspath, File.pathSeparatorChar)); + new Compiler().compile(sourceDirectory, join(fullClasspath, File.pathSeparatorChar), (String)config.get("target")); try { return URLClassLoader.newInstance(new URL[] { sourceDirectory.toURI().toURL() }, Thread.currentThread().getContextClassLoader()); @@ -158,7 +162,7 @@ public static ClassLoader generateAndCompile(String schema, String targetPackage File outputDirectory = generate(schema, targetPackage, configValues); - return compile(outputDirectory); + return compile(outputDirectory, new ArrayList(), configValues); } @@ -174,7 +178,7 @@ public static ClassLoader generateAndCompile(URL schema, String targetPackage, M File outputDirectory = generate(schema, targetPackage, configValues); - return compile(outputDirectory); + return compile(outputDirectory, new ArrayList(), configValues); } diff --git a/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/util/Compiler.java b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/util/Compiler.java index 4410c2101..ee4dbfb86 100644 --- a/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/util/Compiler.java +++ b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/util/Compiler.java @@ -37,6 +37,12 @@ public class Compiler { public void compile(File directory, String classpath) { + compile(directory, classpath, null); + } + + public void compile(File directory, String classpath, String target) { + + target = target == null ? "1.6" : target; JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler(); StandardJavaFileManager fileManager = javaCompiler.getStandardFileManager(null, null, null); @@ -44,10 +50,16 @@ public void compile(File directory, String classpath) { Iterable compilationUnits = fileManager.getJavaFileObjectsFromFiles(findAllSourceFiles(directory)); ArrayList options = new ArrayList(); + options.add("-source"); + options.add(target); + options.add("-target"); + options.add(target); options.add("-classpath"); options.add(classpath); options.add("-encoding"); options.add("UTF8"); + options.add("-Xlint:-options"); + options.add("-Xlint:unchecked"); if (compilationUnits.iterator().hasNext()) { Boolean success = javaCompiler.getTask(null, fileManager, null, options, null, compilationUnits).call(); assertThat("Compilation was not successful, check stdout for errors", success, is(true)); 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 454d4bca9..44f4b2b72 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 @@ -434,6 +434,15 @@ public class Jsonschema2PojoMojo extends AbstractMojo implements GenerationConfi * @since 0.4.15 */ private boolean includeAccessors = true; + + /** + * The target Java version for sources generated by this plugin. + * + * @parameter expression="${maven.compiler.target}" + * default="1.6" + * @since 0.4.17 + */ + private String target = "1.6"; /** * The project being built. @@ -720,5 +729,10 @@ public boolean isIncludeAdditionalProperties() { public boolean isIncludeAccessors() { return includeAccessors; } + + @Override + public String getTarget() { + return target; + } } diff --git a/pom.xml b/pom.xml index 0b78ada81..906aad097 100644 --- a/pom.xml +++ b/pom.xml @@ -163,6 +163,8 @@ UTF-8 1.6 1.6 + 1.7 + 1.7