Skip to content

Commit

Permalink
added config option for target Java version
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrimble committed Nov 28, 2015
1 parent 97f01ca commit 3ca46db
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -727,4 +729,9 @@ public boolean isIncludeAdditionalProperties() {
public boolean isIncludeAccessors() {
return includeAccessors;
}

@Override
public String getTarget() {
return target;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -320,5 +323,10 @@ public boolean isIncludeAdditionalProperties() {
public boolean isIncludeAccessors() {
return !disableAccessors;
}

@Override
public String getTarget() {
return target;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,11 @@ public boolean isIncludeAdditionalProperties() {
public boolean isIncludeAccessors() {
return true;
}

/**
* @return <code>1.6</code>
*/ @Override
public String getTarget() {
return "1.6";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();

}
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> LESS_THAN_7
= asList("1.1", "1.2", "1.3", "1.4", "1.5", "5", "1.6", "6");
public static final Collection<String> 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());
}
}
Original file line number Diff line number Diff line change
@@ -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<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.getTarget()).thenReturn(version);
return config;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class JsonSchemaExtension implements GenerationConfig {
char[] propertyWordDelimiters
boolean removeOldOutput
SourceType sourceType
String target
boolean useCommonsLang3
boolean useDoubleNumbers
boolean useJodaDates
Expand Down Expand Up @@ -91,6 +92,7 @@ public class JsonSchemaExtension implements GenerationConfig {
classNameSuffix = ''
includeAdditionalProperties = true
includeAccessors = true
target = '1.6'
}

@Override
Expand Down Expand Up @@ -152,6 +154,7 @@ public class JsonSchemaExtension implements GenerationConfig {
|initializeCollections = ${initializeCollections}
|classNamePrefix = ${classNamePrefix}
|classNameSuffix = ${classNameSuffix}
|target = ${target}
""".stripMargin()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,21 @@ private static MavenProject getMockProject() throws DependencyResolutionRequired
*/
public static ClassLoader compile(File sourceDirectory) {

return compile(sourceDirectory, new ArrayList<String>());
return compile(sourceDirectory, new ArrayList<String>(), new HashMap<String, Object>());

}

public static ClassLoader compile(File sourceDirectory, List<String> classpath ) {
return compile(sourceDirectory, classpath, new HashMap<String, Object>());
}

public static ClassLoader compile(File sourceDirectory, List<String> classpath) {
public static ClassLoader compile(File sourceDirectory, List<String> classpath, Map<String, Object> config) {

List<String> fullClasspath = new ArrayList<String>();
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());
Expand All @@ -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<String>(), configValues);

}

Expand All @@ -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<String>(), configValues);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,29 @@
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);

Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(findAllSourceFiles(directory));

ArrayList<String> options = new ArrayList<String>();
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -720,5 +729,10 @@ public boolean isIncludeAdditionalProperties() {
public boolean isIncludeAccessors() {
return includeAccessors;
}

@Override
public String getTarget() {
return target;
}

}
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@
<encoding>UTF-8</encoding>
<source>1.6</source>
<target>1.6</target>
<testSource>1.7</testSource>
<testTarget>1.7</testTarget>
</configuration>
</plugin>
<plugin>
Expand Down

0 comments on commit 3ca46db

Please sign in to comment.