Skip to content

Commit bb17e82

Browse files
committed
Detect targetVersion using Maven/Gradle, then fall back to JVM version
1 parent 25bfc76 commit bb17e82

File tree

9 files changed

+191
-233
lines changed

9 files changed

+191
-233
lines changed

jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/DynamicPropertiesRule.java

Lines changed: 7 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@
1919
import static com.sun.codemodel.JExpr.*;
2020
import static com.sun.codemodel.JMod.*;
2121

22-
import java.util.Iterator;
23-
import java.util.Map;
24-
2522
import org.jsonschema2pojo.Schema;
26-
import org.jsonschema2pojo.util.LanguageFeatures;
2723
import org.jsonschema2pojo.util.Models;
2824

2925
import com.fasterxml.jackson.databind.JsonNode;
@@ -40,6 +36,8 @@
4036
import com.sun.codemodel.JType;
4137
import com.sun.codemodel.JTypeVar;
4238
import com.sun.codemodel.JVar;
39+
import java.util.Iterator;
40+
import java.util.Map;
4341

4442
/**
4543
* Adds methods for dynamically getting, setting, and building properties.
@@ -105,20 +103,11 @@ public JDefinedClass apply(String nodeName, JsonNode node, JsonNode parent, JDef
105103
boolean isGenerateBuilders = ruleFactory.getGenerationConfig().isGenerateBuilders();
106104

107105
if (isIncludeGetters || isIncludeSetters || isGenerateBuilders) {
108-
if (LanguageFeatures.canUseJava7(ruleFactory.getGenerationConfig())) {
109-
if (isIncludeSetters) {
110-
addInternalSetMethodJava7(jclass, node);
111-
}
112-
if (isIncludeGetters) {
113-
addInternalGetMethodJava7(jclass, node);
114-
}
115-
} else {
116-
if (isIncludeSetters) {
117-
addInternalSetMethodJava6(jclass, node);
118-
}
119-
if (isIncludeGetters) {
120-
addInternalGetMethodJava6(jclass, node);
121-
}
106+
if (isIncludeSetters) {
107+
addInternalSetMethodJava6(jclass, node);
108+
}
109+
if (isIncludeGetters) {
110+
addInternalGetMethodJava6(jclass, node);
122111
}
123112
}
124113

@@ -172,38 +161,6 @@ private JMethod addPublicGetMethod(JDefinedClass jclass, JMethod internalGetMeth
172161
return method;
173162
}
174163

175-
private JMethod addInternalGetMethodJava7(JDefinedClass jclass, JsonNode propertiesNode) {
176-
JMethod method = jclass.method(PROTECTED, jclass.owner()._ref(Object.class), DEFINED_GETTER_NAME);
177-
JVar nameParam = method.param(String.class, "name");
178-
JVar notFoundParam = method.param(jclass.owner()._ref(Object.class), "notFoundValue");
179-
JBlock body = method.body();
180-
JSwitch propertySwitch = body._switch(nameParam);
181-
if (propertiesNode != null) {
182-
for (Iterator<Map.Entry<String, JsonNode>> properties = propertiesNode.fields(); properties.hasNext();) {
183-
Map.Entry<String, JsonNode> property = properties.next();
184-
String propertyName = property.getKey();
185-
JsonNode node = property.getValue();
186-
String fieldName = ruleFactory.getNameHelper().getPropertyName(propertyName, node);
187-
JType propertyType = jclass.fields().get(fieldName).type();
188-
189-
addGetPropertyCase(jclass, propertySwitch, propertyName, propertyType, node);
190-
}
191-
}
192-
JClass extendsType = jclass._extends();
193-
if (extendsType != null && extendsType instanceof JDefinedClass) {
194-
JDefinedClass parentClass = (JDefinedClass) extendsType;
195-
JMethod parentMethod = parentClass.getMethod(DEFINED_GETTER_NAME,
196-
new JType[] { parentClass.owner()._ref(String.class), parentClass.owner()._ref(Object.class) });
197-
propertySwitch._default().body()
198-
._return(_super().invoke(parentMethod).arg(nameParam).arg(notFoundParam));
199-
} else {
200-
propertySwitch._default().body()
201-
._return(notFoundParam);
202-
}
203-
204-
return method;
205-
}
206-
207164
private JMethod addInternalGetMethodJava6(JDefinedClass jclass, JsonNode propertiesNode) {
208165
JMethod method = jclass.method(PROTECTED, jclass.owner()._ref(Object.class), DEFINED_GETTER_NAME);
209166
JVar nameParam = method.param(String.class, "name");
@@ -305,36 +262,6 @@ private JMethod addPublicWithMethod(JDefinedClass jclass, JMethod internalSetMet
305262
return method;
306263
}
307264

308-
private JMethod addInternalSetMethodJava7(JDefinedClass jclass, JsonNode propertiesNode) {
309-
JMethod method = jclass.method(PROTECTED, jclass.owner().BOOLEAN, DEFINED_SETTER_NAME);
310-
JVar nameParam = method.param(String.class, "name");
311-
JVar valueParam = method.param(Object.class, "value");
312-
JBlock body = method.body();
313-
JSwitch propertySwitch = body._switch(nameParam);
314-
if (propertiesNode != null) {
315-
for (Iterator<Map.Entry<String, JsonNode>> properties = propertiesNode.fields(); properties.hasNext();) {
316-
Map.Entry<String, JsonNode> property = properties.next();
317-
String propertyName = property.getKey();
318-
JsonNode node = property.getValue();
319-
String fieldName = ruleFactory.getNameHelper().getPropertyName(propertyName, node);
320-
JType propertyType = jclass.fields().get(fieldName).type();
321-
322-
addSetPropertyCase(jclass, propertySwitch, propertyName, propertyType, valueParam, node);
323-
}
324-
}
325-
JBlock defaultBlock = propertySwitch._default().body();
326-
JClass extendsType = jclass._extends();
327-
if (extendsType != null && extendsType instanceof JDefinedClass) {
328-
JDefinedClass parentClass = (JDefinedClass) extendsType;
329-
JMethod parentMethod = parentClass.getMethod(DEFINED_SETTER_NAME,
330-
new JType[] { parentClass.owner()._ref(String.class), parentClass.owner()._ref(Object.class) });
331-
defaultBlock._return(_super().invoke(parentMethod).arg(nameParam).arg(valueParam));
332-
} else {
333-
defaultBlock._return(FALSE);
334-
}
335-
return method;
336-
}
337-
338265
private JMethod addInternalSetMethodJava6(JDefinedClass jclass, JsonNode propertiesNode) {
339266
JMethod method = jclass.method(PROTECTED, jclass.owner().BOOLEAN, DEFINED_SETTER_NAME);
340267
JVar nameParam = method.param(String.class, "name");
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright © 2010-2020 Nokia
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.jsonschema2pojo.util;
18+
19+
import static org.apache.commons.lang3.StringUtils.*;
20+
21+
import java.util.regex.Matcher;
22+
import java.util.regex.Pattern;
23+
24+
public class JavaVersion {
25+
26+
private static final Pattern JAVA_VERSION_1_X = Pattern.compile("(^1.\\d+)");
27+
private static final Pattern JAVA_VERSION_X = Pattern.compile("(^\\d+)");
28+
29+
public static String parse(String version) {
30+
if (startsWith(version, "1.")) {
31+
Matcher m = JAVA_VERSION_1_X.matcher(version);
32+
m.find();
33+
return m.group();
34+
} else {
35+
Matcher m = JAVA_VERSION_X.matcher(version);
36+
m.find();
37+
return m.group();
38+
}
39+
}
40+
41+
public static boolean is9OrLater(final String targetVersion) {
42+
final Double v = Double.valueOf(targetVersion);
43+
return (v >= 9) || (v < 2 && v >= 1.9);
44+
}
45+
46+
}

jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/util/LanguageFeatures.java

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Copyright © 2010-2020 Nokia
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.jsonschema2pojo.util;
18+
19+
import static org.hamcrest.CoreMatchers.*;
20+
import static org.hamcrest.MatcherAssert.*;
21+
22+
import org.junit.Test;
23+
24+
public class JavaVersionTest {
25+
26+
@Test
27+
public void testParse() {
28+
assertThat(JavaVersion.parse("1.8.0_362"), is("1.8"));
29+
assertThat(JavaVersion.parse("11.0.18"), is("11"));
30+
assertThat(JavaVersion.parse("9.0.4"), is("9"));
31+
assertThat(JavaVersion.parse("10.0.1"), is("10"));
32+
}
33+
34+
@Test
35+
public void testIs9OrLater() {
36+
assertThat(JavaVersion.is9OrLater("1.1"), is(false));
37+
assertThat(JavaVersion.is9OrLater("1.2"), is(false));
38+
assertThat(JavaVersion.is9OrLater("1.3"), is(false));
39+
assertThat(JavaVersion.is9OrLater("1.4"), is(false));
40+
assertThat(JavaVersion.is9OrLater("1.5"), is(false));
41+
assertThat(JavaVersion.is9OrLater("5"), is(false));
42+
assertThat(JavaVersion.is9OrLater("1.6"), is(false));
43+
assertThat(JavaVersion.is9OrLater("6"), is(false));
44+
assertThat(JavaVersion.is9OrLater("1.7"), is(false));
45+
assertThat(JavaVersion.is9OrLater("7"), is(false));
46+
assertThat(JavaVersion.is9OrLater("1.8"), is(false));
47+
assertThat(JavaVersion.is9OrLater("8"), is(false));
48+
assertThat(JavaVersion.is9OrLater("1.9"), is(true));
49+
assertThat(JavaVersion.is9OrLater("9"), is(true));
50+
assertThat(JavaVersion.is9OrLater("10"), is(true));
51+
assertThat(JavaVersion.is9OrLater("11"), is(true));
52+
}
53+
54+
}

jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/util/LanguageFeaturesTest.java

Lines changed: 0 additions & 94 deletions
This file was deleted.

jsonschema2pojo-gradle-plugin/src/main/groovy/org/jsonschema2pojo/gradle/GenerateJsonSchemaAndroidTask.groovy

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
*/
1616
package org.jsonschema2pojo.gradle
1717

18+
import org.jsonschema2pojo.GenerationConfig
19+
import org.jsonschema2pojo.Jsonschema2Pojo
20+
1821
import org.gradle.api.tasks.OutputDirectory
1922
import org.gradle.api.tasks.SourceTask
2023
import org.gradle.api.tasks.TaskAction
2124
import org.gradle.work.InputChanges
22-
import org.jsonschema2pojo.GenerationConfig
23-
import org.jsonschema2pojo.Jsonschema2Pojo
2425

2526
/**
2627
* Task that generates java source files for an Android project
@@ -48,6 +49,7 @@ class GenerateJsonSchemaAndroidTask extends SourceTask {
4849

4950
GenerationConfig configuration = project.jsonSchema2Pojo
5051
configuration.targetDirectory = outputDir
52+
setTargetVersion configuration
5153

5254
if (Boolean.TRUE == configuration.properties.get("useCommonsLang3")) {
5355
logger.warn 'useCommonsLang3 is deprecated. Please remove it from your config.'
@@ -57,4 +59,12 @@ class GenerateJsonSchemaAndroidTask extends SourceTask {
5759

5860
Jsonschema2Pojo.generate(configuration, new GradleRuleLogger(logger))
5961
}
62+
63+
void setTargetVersion(JsonSchemaExtension configuration) {
64+
if (!configuration.targetVersion) {
65+
def compileJavaTask = project.getTasksByName("compileJava", false).first()
66+
configuration.targetVersion = compileJavaTask.getProperties().get("sourceCompatibility")
67+
logger.warn 'Using Gradle targetCompatibility as targetVersion for jsonschema2pojo: ' + configuration.targetVersion
68+
}
69+
}
6070
}

0 commit comments

Comments
 (0)