Skip to content

Commit 47034bd

Browse files
committed
Make generated code - Java 8 based by default
Closes #424
1 parent d6fea7c commit 47034bd

File tree

34 files changed

+183
-563
lines changed

34 files changed

+183
-563
lines changed

modello-plugins/modello-plugin-dom4j/src/test/java/org/codehaus/modello/plugin/dom4j/FeaturesDom4jGeneratorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void testJavaGenerator() throws Throwable {
4949

5050
addDependency("dom4j", "dom4j");
5151
addDependency("org.xmlunit", "xmlunit-core");
52-
compileGeneratedSources(5);
52+
compileGeneratedSources(8);
5353

5454
verifyCompiledGeneratedSources("org.codehaus.modello.generator.xml.dom4j.Dom4jFeaturesVerifier");
5555
}

modello-plugins/modello-plugin-jackson/src/main/java/org/codehaus/modello/plugin/jackson/JacksonReaderGenerator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,8 +549,7 @@ private void processField(
549549
if (ModelDefault.SET.equals(type)) {
550550
key = "?";
551551
} else {
552-
key = (hasJavaSourceSupport(5) ? "Integer.valueOf" : "new java.lang.Integer") + "( "
553-
+ associationName + ".size() )";
552+
key = "Integer.valueOf(" + associationName + ".size())";
554553
}
555554
writePrimitiveField(
556555
association,

modello-plugins/modello-plugin-jackson/src/main/java/org/codehaus/modello/plugin/jackson/JacksonWriterGenerator.java

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,6 @@ private void writeClass(ModelClass modelClass, JClass jClass) throws ModelloExce
195195

196196
List<ModelField> modelFields = getFieldsForXml(modelClass, getGeneratedVersion());
197197

198-
final boolean useJava5 = hasJavaSourceSupport(5);
199-
200198
// XML tags
201199
for (ModelField field : modelFields) {
202200
XmlFieldMetadata xmlFieldMetadata = (XmlFieldMetadata) field.getMetadata(XmlFieldMetadata.ID);
@@ -237,19 +235,11 @@ private void writeClass(ModelClass modelClass, JClass jClass) throws ModelloExce
237235

238236
sc.add("generator.writeArrayFieldStart( \"" + fieldTagName + "\" );");
239237

240-
if (useJava5) {
241-
sc.add("for ( " + toType + " o : " + value + " )");
242-
} else {
243-
sc.add("for ( java.util.Iterator it = " + value + ".iterator(); it.hasNext(); )");
244-
}
238+
sc.add("for ( " + toType + " o : " + value + " )");
245239

246240
sc.add("{");
247241
sc.indent();
248242

249-
if (!useJava5) {
250-
sc.add(toType + " o = (" + toType + " ) it.next();");
251-
}
252-
253243
if (isClassInModel(association.getTo(), modelClass.getModel())) {
254244
sc.add("write" + toType + "( o, generator );");
255245
} else {
@@ -279,32 +269,21 @@ private void writeClass(ModelClass modelClass, JClass jClass) throws ModelloExce
279269

280270
StringBuilder entryTypeBuilder = new StringBuilder("java.util.Map.Entry");
281271

282-
if (useJava5) {
283-
entryTypeBuilder.append('<');
284-
285-
if (association.getType().equals(ModelDefault.PROPERTIES)) {
286-
entryTypeBuilder.append("Object, Object");
287-
} else {
288-
entryTypeBuilder.append("String, ").append(association.getTo());
289-
}
272+
entryTypeBuilder.append('<');
290273

291-
entryTypeBuilder.append('>');
292-
}
293-
294-
if (useJava5) {
295-
sc.add("for ( " + entryTypeBuilder + " entry : " + value + ".entrySet() )");
274+
if (association.getType().equals(ModelDefault.PROPERTIES)) {
275+
entryTypeBuilder.append("Object, Object");
296276
} else {
297-
sc.add("for ( java.util.Iterator it = " + value
298-
+ ".entrySet().iterator(); it.hasNext(); )");
277+
entryTypeBuilder.append("String, ").append(association.getTo());
299278
}
300279

280+
entryTypeBuilder.append('>');
281+
282+
sc.add("for ( " + entryTypeBuilder + " entry : " + value + ".entrySet() )");
283+
301284
sc.add("{");
302285
sc.indent();
303286

304-
if (!useJava5) {
305-
sc.add(entryTypeBuilder + " entry = (" + entryTypeBuilder + ") it.next();");
306-
}
307-
308287
sc.add("final String key = String.valueOf( entry.getKey() );");
309288
sc.add("final String value = String.valueOf( entry.getValue() );");
310289

modello-plugins/modello-plugin-jackson/src/test/java/org/codehaus/modello/plugin/jackson/JacksonGeneratorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void testJacksonGenerator() throws Throwable {
5050
assertEquals(28, classesList.size());
5151

5252
// now generate sources and test them
53-
Properties parameters = getModelloParameters("4.0.0", 5);
53+
Properties parameters = getModelloParameters("4.0.0", 8);
5454

5555
modello.generate(model, "java", parameters);
5656
modello.generate(model, "jackson-writer", parameters);
@@ -60,7 +60,7 @@ public void testJacksonGenerator() throws Throwable {
6060
addDependency("com.fasterxml.jackson.core", "jackson-databind");
6161
// looks like jackson-databind requires jackson-annotations to run...
6262
addDependency("com.fasterxml.jackson.core", "jackson-annotations");
63-
compileGeneratedSources(5);
63+
compileGeneratedSources(8);
6464

6565
// TODO: see why without this, version system property is set to "2.4.1" value after verify
6666
System.setProperty("version", getModelloVersion());

modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/AbstractJavaModelloGenerator.java

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import java.util.Date;
3232
import java.util.List;
3333
import java.util.Locale;
34-
import java.util.Optional;
3534
import java.util.Properties;
3635

3736
import org.codehaus.modello.ModelloException;
@@ -56,23 +55,20 @@
5655
import org.codehaus.modello.plugin.model.ModelClassMetadata;
5756
import org.codehaus.plexus.util.StringUtils;
5857

58+
import static org.codehaus.plexus.util.StringUtils.replaceOnce;
59+
5960
/**
6061
* AbstractJavaModelloGenerator - similar in scope to {@link AbstractModelloGenerator} but with features that
6162
* java generators can use.
6263
*
6364
* @author <a href="mailto:[email protected]">Joakim Erdfelt</a>
6465
*/
6566
public abstract class AbstractJavaModelloGenerator extends AbstractModelloGenerator {
66-
private Optional<Integer> javaSource;
6767

6868
protected boolean domAsXpp3 = true;
6969

7070
protected void initialize(Model model, Properties parameters) throws ModelloException {
7171
super.initialize(model, parameters);
72-
73-
javaSource = Optional.ofNullable(getParameter(parameters, ModelloParameterConstants.OUTPUT_JAVA_SOURCE, null))
74-
.map(Integer::valueOf);
75-
7672
domAsXpp3 = !"false".equals(parameters.getProperty(ModelloParameterConstants.DOM_AS_XPP3));
7773
}
7874

@@ -110,14 +106,10 @@ protected void initHeader(JInterface interfaze) {
110106
interfaze.setHeader(getHeaderComment());
111107
}
112108

113-
protected final boolean hasJavaSourceSupport(int source) {
114-
return javaSource.map(i -> i >= source).orElse(false);
115-
}
116-
117109
protected void suppressAllWarnings(Model objectModel, JStructure structure) {
118110
JavaModelMetadata javaModelMetadata = (JavaModelMetadata) objectModel.getMetadata(JavaModelMetadata.ID);
119111

120-
if (hasJavaSourceSupport(5) && javaModelMetadata.isSuppressAllWarnings()) {
112+
if (javaModelMetadata.isSuppressAllWarnings()) {
121113
structure.appendAnnotation("@SuppressWarnings( \"all\" )");
122114
}
123115
}
@@ -152,13 +144,7 @@ protected String getPrefix(JavaFieldMetadata javaFieldMetadata) {
152144
}
153145

154146
protected String getDefaultValue(ModelAssociation association) {
155-
String value = association.getDefaultValue();
156-
157-
if (hasJavaSourceSupport(5)) {
158-
value = StringUtils.replaceOnce(StringUtils.replaceOnce(value, "/*", ""), "*/", "");
159-
}
160-
161-
return value;
147+
return replaceOnce(replaceOnce(association.getDefaultValue(), "/*", ""), "*/", "");
162148
}
163149

164150
protected static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";
@@ -183,38 +169,29 @@ protected String getJavaDefaultValue(ModelField modelField) throws ModelloExcept
183169
} catch (ParseException pe) {
184170
throw new ModelloException("Unparseable default date: " + value, pe);
185171
}
186-
} else if (value != null && value.length() > 0) {
187-
boolean useJava5 = hasJavaSourceSupport(5);
172+
} else if (value != null && !value.isEmpty()) {
188173
if ("Character".equals(type) && !value.contains(type)) {
189-
return newPrimitiveWrapper(type, "'" + escapeStringLiteral(value) + "'", useJava5);
174+
return "Character.valueOf( " + "'" + escapeStringLiteral(value) + "'" + ")";
190175
} else if ("Boolean".equals(type) && !value.contains(type)) {
191-
return newPrimitiveWrapper(type, value, true);
176+
return "Boolean.valueOf( " + value + ")";
192177
} else if ("Byte".equals(type) && !value.contains(type)) {
193-
return newPrimitiveWrapper(type, "(byte) " + value, useJava5);
178+
return "Byte.valueOf((byte) " + value + ")";
194179
} else if ("Short".equals(type) && !value.contains(type)) {
195-
return newPrimitiveWrapper(type, "(short) " + value, useJava5);
180+
return "Short.valueOf((short) " + value + ")";
196181
} else if ("Integer".equals(type) && !value.contains(type)) {
197-
return newPrimitiveWrapper(type, value, useJava5);
182+
return "Integer.valueOf(" + value + ")";
198183
} else if ("Long".equals(type) && !value.contains(type)) {
199-
return newPrimitiveWrapper(type, value + 'L', useJava5);
184+
return "Long.valueOf(" + value + 'L' + ")";
200185
} else if ("Float".equals(type) && !value.contains(type)) {
201-
return newPrimitiveWrapper(type, value + 'f', useJava5);
186+
return "Float.valueOf(" + value + 'f' + ")";
202187
} else if ("Double".equals(type) && !value.contains(type)) {
203-
return newPrimitiveWrapper(type, value, useJava5);
188+
return "Double.valueOf(" + value + ")";
204189
}
205190
}
206191

207192
return value;
208193
}
209194

210-
private String newPrimitiveWrapper(String type, String value, boolean useJava5) {
211-
if (useJava5) {
212-
return type + ".valueOf( " + value + " )";
213-
} else {
214-
return "new " + type + "( " + value + " )";
215-
}
216-
}
217-
218195
private String escapeStringLiteral(String str) {
219196
StringBuilder buffer = new StringBuilder(str.length() + 32);
220197

0 commit comments

Comments
 (0)