Skip to content

Commit 77087f4

Browse files
Merge pull request #800 from bci-oss/feature/695-extend-code-generator-setters
Feature/695 extend code generator setters
2 parents 5102355 + fd9ed92 commit 77087f4

File tree

28 files changed

+561
-106
lines changed

28 files changed

+561
-106
lines changed

core/esmf-aspect-model-java-generator/src/main/java/org/eclipse/esmf/aspectmodel/java/AspectModelJavaUtil.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.stream.IntStream;
3131
import java.util.stream.Stream;
3232
import javax.xml.datatype.DatatypeConstants;
33+
3334
import org.eclipse.esmf.aspectmodel.VersionInfo;
3435
import org.eclipse.esmf.aspectmodel.java.exception.CodeGenerationException;
3536
import org.eclipse.esmf.aspectmodel.visitor.AspectStreamTraversalVisitor;
@@ -38,6 +39,7 @@
3839
import org.eclipse.esmf.metamodel.ComplexType;
3940
import org.eclipse.esmf.metamodel.Entity;
4041
import org.eclipse.esmf.metamodel.HasProperties;
42+
import org.eclipse.esmf.metamodel.ModelElement;
4143
import org.eclipse.esmf.metamodel.Property;
4244
import org.eclipse.esmf.metamodel.Scalar;
4345
import org.eclipse.esmf.metamodel.StructureElement;
@@ -121,7 +123,7 @@ public static String getPropertyType( final Property property, final JavaCodeGen
121123
*/
122124
public static boolean hasContainerType( final Property property ) {
123125
return property.isOptional()
124-
|| ( property.getEffectiveCharacteristic().map( characteristic -> characteristic.is( Collection.class ) ).orElse( false ) );
126+
|| (property.getEffectiveCharacteristic().map( characteristic -> characteristic.is( Collection.class ) ).orElse( false ));
125127
}
126128

127129
/**
@@ -194,7 +196,8 @@ public static String determineCollectionAspectClassDefinition( final StructureEl
194196
if ( characteristic instanceof Collection ) {
195197
final String collectionType = determineCollectionType( (Collection) characteristic, false, codeGenerationConfig );
196198
final String dataType = getDataType( characteristic.getDataType(), codeGenerationConfig.importTracker(), codeGenerationConfig );
197-
return String.format( "public class %s implements CollectionAspect<%s,%s>", element.getName(), collectionType, dataType );
199+
return String.format( "public class %s implements CollectionAspect<%s,%s>", generateClassName( element, codeGenerationConfig ),
200+
collectionType, dataType );
198201
}
199202
}
200203
throw error.get();
@@ -324,8 +327,8 @@ public static String getDataType( final Optional<Type> dataType, final ImportTra
324327
return dataType.map( type -> {
325328
final Type actualDataType = dataType.get();
326329
if ( actualDataType instanceof ComplexType ) {
327-
final String complexDataType = ( (ComplexType) actualDataType ).getName();
328-
if ( ( !codeGenerationConfig.namePrefix().isBlank() || !codeGenerationConfig.namePostfix().isBlank() ) ) {
330+
final String complexDataType = ((ComplexType) actualDataType).getName();
331+
if ( (!codeGenerationConfig.namePrefix().isBlank() || !codeGenerationConfig.namePostfix().isBlank()) ) {
329332
return codeGenerationConfig.namePrefix() + complexDataType + codeGenerationConfig.namePostfix();
330333
}
331334
return complexDataType;
@@ -349,7 +352,7 @@ public static String getDataType( final Optional<Type> dataType, final ImportTra
349352

350353
public static Class<?> getDataTypeClass( final Type dataType ) {
351354
if ( dataType instanceof ComplexType ) {
352-
return ( (ComplexType) dataType ).getClass();
355+
return ((ComplexType) dataType).getClass();
353356
}
354357

355358
final Resource typeResource = ResourceFactory.createResource( dataType.getUrn() );
@@ -574,8 +577,8 @@ public static String genericClassSignature( final StructureElement element ) {
574577
return generics.isEmpty() ? "" : "<" + generics + ">";
575578
}
576579

577-
public static String generateClassName( final StructureElement element, final JavaCodeGenerationConfig config ) {
578-
if ( ( !config.namePrefix().isBlank() || !config.namePostfix().isBlank() ) && element.is( StructureElement.class ) ) {
580+
public static String generateClassName( final ModelElement element, final JavaCodeGenerationConfig config ) {
581+
if ( (!config.namePrefix().isBlank() || !config.namePostfix().isBlank()) && element.is( StructureElement.class ) ) {
579582
return config.namePrefix() + element.getName() + config.namePostfix();
580583
}
581584
return element.getName();
@@ -674,6 +677,15 @@ public static String getterName( final Property property ) {
674677
return (isBooleanType ? "is" : "get") + StringUtils.capitalize( property.getPayloadName() );
675678
}
676679

680+
public static String setterName( final Property property, final JavaCodeGenerationConfig codeGenerationConfig ) {
681+
switch ( codeGenerationConfig.setterStyle() ) {
682+
case FLUENT_COMPACT:
683+
return StringUtils.uncapitalize( property.getPayloadName() );
684+
default:
685+
return "set" + StringUtils.capitalize( property.getPayloadName() );
686+
}
687+
}
688+
677689
public static String codeGeneratorName() {
678690
return "esmf-sdk " + VersionInfo.ESMF_SDK_VERSION;
679691
}

core/esmf-aspect-model-java-generator/src/main/java/org/eclipse/esmf/aspectmodel/java/JavaCodeGenerationConfig.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
* @param jsonTypeInfo Corresponds to com.fasterxml.jackson.annotation.JsonTypeInfo.Id and selects which JsonTypeInfo kind is used
2828
* @param packageName the package name that classes should be created in
2929
* @param importTracker the instance of the tracker that tracks imports during code generation
30-
* @param executeLibraryMacros determines whether template macros given in templateLibFile should be evaluatated
30+
* @param executeLibraryMacros determines whether template macros given in templateLibFile should be evaluated
3131
* @param templateLibFile a file containing velocity macros overriding sections in the default code templates
3232
* @param namePrefix custom class name prefix
3333
* @param namePostfix custom class name postfix
34+
* @param enableSetters controls whether setters should be generated for the properties of the generated Java classes
35+
* @param setterStyle the style of setters to be generated, if {@code enableSetters} is true
3436
*/
3537
@RecordBuilder
3638
public record JavaCodeGenerationConfig(
@@ -42,12 +44,18 @@ public record JavaCodeGenerationConfig(
4244
boolean executeLibraryMacros,
4345
File templateLibFile,
4446
String namePrefix,
45-
String namePostfix
47+
String namePostfix,
48+
boolean enableSetters,
49+
SetterStyle setterStyle
4650
) implements GenerationConfig {
4751
public enum JsonTypeInfoType {
4852
NONE, CLASS, MINIMAL_CLASS, NAME, SIMPLE_NAME, DEDUCTION, CUSTOM
4953
}
5054

55+
public enum SetterStyle {
56+
STANDARD, FLUENT, FLUENT_COMPACT
57+
}
58+
5159
public JavaCodeGenerationConfig {
5260
if ( enableJacksonAnnotationJsonFormatShapeObject == null ) {
5361
enableJacksonAnnotationJsonFormatShapeObject = true;
@@ -61,7 +69,7 @@ public enum JsonTypeInfoType {
6169
if ( importTracker == null ) {
6270
importTracker = new ImportTracker();
6371
}
64-
if ( executeLibraryMacros && ( templateLibFile == null || templateLibFile.toString().isEmpty() ) ) {
72+
if ( executeLibraryMacros && (templateLibFile == null || templateLibFile.toString().isEmpty()) ) {
6573
throw new CodeGenerationException( "Missing configuration. Please provide path to velocity template library file." );
6674
}
6775
if ( executeLibraryMacros && !templateLibFile.exists() ) {
@@ -73,5 +81,8 @@ public enum JsonTypeInfoType {
7381
if ( namePostfix == null ) {
7482
namePostfix = "";
7583
}
84+
if ( setterStyle == null ) {
85+
setterStyle = SetterStyle.STANDARD;
86+
}
7687
}
7788
}

core/esmf-aspect-model-java-generator/src/main/java/org/eclipse/esmf/aspectmodel/java/metamodel/StaticMetaModelJavaArtifactGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public JavaArtifact apply( final E element, final JavaCodeGenerationConfig confi
235235

236236
final String generatedSource = new TemplateEngine( context, engineConfiguration ).apply( "java-static-class" );
237237
try {
238-
return new JavaArtifact( Roaster.format( generatedSource ), "Meta" + element.getName(),
238+
return new JavaArtifact( Roaster.format( generatedSource ), "Meta" + AspectModelJavaUtil.generateClassName( element, config ),
239239
config.packageName() );
240240
} catch ( final Exception exception ) {
241241
throw new CodeGenerationException( generatedSource, exception );

core/esmf-aspect-model-java-generator/src/main/resources/java-pojo-class-body-lib.vm

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
#parse( "java-pojo-property-lib.vm" )
1414
#parse( "java-pojo-constructor-lib.vm" )
1515
#parse( "java-pojo-getter-lib.vm" )
16+
#parse( "java-pojo-setter-lib.vm" )
1617
#parse( "java-pojo-equals-method-lib.vm" )
1718
#parse( "java-pojo-hashcode-method-lib.vm" )
1819

1920
#macro( javaPojoClassBody )
21+
#set( $className = ${util.generateClassName( $element, $codeGenerationConfig )}+${util.genericClassSignature( $element )} )
2022
/**
2123
* Generated class for $element.getPreferredName( $localeEn ) (${elementUrn}).
2224
*#if ( $element.getDescription( $localeEn ) ) $element.getDescription( $localeEn )#end
@@ -29,7 +31,7 @@ ${util.determineCollectionAspectClassDefinition( $element, $codeGenerationConfig
2931
${util.generateAbstractEntityClassAnnotations( $complexElement, $codeGenerationConfig, $extendingEntities )}
3032
${util.determineComplexTypeClassDefinition( $complexElement, $codeGenerationConfig )}
3133
#else
32-
public class ${util.generateClassName( $element, $codeGenerationConfig )}${util.genericClassSignature( $element )} {
34+
public class $className {
3335
#end
3436
#foreach( $property in $element.properties )
3537
#javaPojoProperty( $property )
@@ -43,6 +45,9 @@ public class ${util.generateClassName( $element, $codeGenerationConfig )}${util.
4345
#foreach( $property in $element.properties )
4446
#set( $index = $foreach.count - 1 )
4547
#javaPojoGetter( $property, $index )
48+
#if ( $codeGenerationConfig.enableSetters() )
49+
#javaPojoSetter( $property, $index, $className )
50+
#end
4651
#end
4752
#foreach( $property in $deconstructor.allProperties )
4853
#javaPojoGetter( $property, 0 )

core/esmf-aspect-model-java-generator/src/main/resources/java-pojo-constructor-lib.vm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@
4949
}
5050
#end
5151
}
52+
53+
#if ( $codeGenerationConfig.enableSetters() )
54+
public ${util.generateClassName( $element, $codeGenerationConfig )}() {
55+
super();
56+
}
57+
#end
5258
#end
5359

5460
#macro( javaPojoConstructor )
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#**
2+
~ Copyright (c) 2021 Robert Bosch Manufacturing Solutions GmbH
3+
~
4+
~ See the AUTHORS file(s) distributed with this work for additional
5+
~ information regarding authorship.
6+
~
7+
~ This Source Code Form is subject to the terms of the Mozilla Public
8+
~ License, v. 2.0. If a copy of the MPL was not distributed with this
9+
~ file, You can obtain one at https://mozilla.org/MPL/2.0/.
10+
~
11+
~ SPDX-License-Identifier: MPL-2.0
12+
*#
13+
14+
#macro( javaPojoSetter $property $propertyIndex $className )
15+
16+
#set( $methodName = $util.setterName( $property, $codeGenerationConfig ) )
17+
#if ( $codeGenerationConfig.setterStyle().name().equals( "STANDARD" ) )
18+
#set( $returnType = "void" )
19+
#else
20+
#set( $returnType = $className )
21+
#end
22+
/**
23+
* Sets a new value for $property.getPreferredName( $localeEn )
24+
#if( !$codeGenerationConfig.setterStyle().name().equals( "STANDARD" ) )
25+
*
26+
* @return {@code this} for chaining
27+
#end
28+
*/
29+
#if( $property.isAbstract() )
30+
public abstract ${returnType} $methodName( $propertyType $property.getPayloadName() );
31+
#else
32+
#set( $propertyType = $util.getPropertyType( $property, false, $codeGenerationConfig ) )
33+
#if( $property.getExtends().isPresent() )@Override #end
34+
public $returnType $methodName( final $propertyType $property.getPayloadName() ) {
35+
this.$property.getPayloadName() = $property.getPayloadName();
36+
#if( !$codeGenerationConfig.setterStyle().name().equals( "STANDARD" ) )
37+
return this;
38+
#end
39+
}
40+
#end
41+
#end

core/esmf-aspect-model-java-generator/src/main/resources/java-static-class-body-lib.vm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static final String MODEL_ELEMENT_URN = NAMESPACE + "${element.getName()}
2525

2626
private static String CHARACTERISTIC_NAMESPACE = "${characteristicBaseUrn}";
2727

28-
#propertyDeclaration() Meta${element.getName()} INSTANCE = new Meta${element.getName()}();
28+
#propertyDeclaration() Meta${className} INSTANCE = new Meta${className}();
2929

3030
#if( $util.isXmlDatatypeFactoryRequired( $element ) )
3131
$codeGenerationConfig.importTracker().importExplicit( $DatatypeConfigurationException )

core/esmf-aspect-model-java-generator/src/main/resources/java-static-class-property-lib.vm

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -59,46 +59,47 @@
5959

6060
#macro( javaStaticClassProperty $property $codeGenerationConfig $element )
6161
#set( $propertyType = $util.getPropertyType( $property, $codeGenerationConfig ) )
62+
#set( $containingClass = $util.generateClassName( $element, $codeGenerationConfig ) )
6263

6364
## public static final $type $property = (definition)
6465
#if( $Trait.isAssignableFrom( $property.getCharacteristic().get().getClass() ) )
6566
$codeGenerationConfig.importTracker().importExplicit( $StaticConstraintProperty )
6667
#if( $util.hasContainerType( $property ) )
6768
$codeGenerationConfig.importTracker().importExplicit( $StaticConstraintContainerProperty )
6869
#set( $containedType = $util.getCharacteristicJavaType( $property, $codeGenerationConfig ) )
69-
#propertyDeclaration() StaticConstraintContainerProperty<${element.getName()}, $containedType, $propertyType, #getCharacteristicClassName()>
70-
$util.toConstant( $property.getName() ) = #staticProperty( $property $codeGenerationConfig );
70+
#propertyDeclaration() StaticConstraintContainerProperty<$containingClass, $containedType, $propertyType, #getCharacteristicClassName()>
71+
$util.toConstant( $property.getName() ) = #staticProperty( $property $containingClass $codeGenerationConfig );
7172
#elseif( $util.hasUnit( $property.getCharacteristic().get() ) )
7273
$codeGenerationConfig.importTracker().importExplicit( $StaticConstraintUnitProperty )
7374
$codeGenerationConfig.importTracker().importExplicit( $Unit )
74-
#propertyDeclaration() StaticConstraintUnitProperty<${element.getName()}, $propertyType, #getCharacteristicClassName()>
75-
$util.toConstant( $property.getName() ) = #staticProperty( $property $codeGenerationConfig );
75+
#propertyDeclaration() StaticConstraintUnitProperty<$containingClass, $propertyType, #getCharacteristicClassName()>
76+
$util.toConstant( $property.getName() ) = #staticProperty( $property $containingClass $codeGenerationConfig );
7677
#else
77-
#propertyDeclaration() StaticConstraintProperty<${element.getName()}, $propertyType, #getCharacteristicClassName()>
78-
$util.toConstant( $property.getName() ) = #staticProperty( $property $codeGenerationConfig );
78+
#propertyDeclaration() StaticConstraintProperty<$containingClass, $propertyType, #getCharacteristicClassName()>
79+
$util.toConstant( $property.getName() ) = #staticProperty( $property $containingClass $codeGenerationConfig );
7980
#end
8081
#else
8182
#if( $util.hasContainerType( $property ) && !$propertyType.startsWith( "Map" ) )
8283
$codeGenerationConfig.importTracker().importExplicit( $StaticContainerProperty )
8384
#set( $containedType = $util.getCharacteristicJavaType( $property, $codeGenerationConfig ) )
84-
#propertyDeclaration() StaticContainerProperty<${element.getName()}, $containedType, java.util.$propertyType> $util.toConstant( $property.getName() ) =
85-
#staticProperty( $property $codeGenerationConfig );
85+
#propertyDeclaration() StaticContainerProperty<$containingClass, $containedType, java.util.$propertyType> $util.toConstant( $property.getName() ) =
86+
#staticProperty( $property $containingClass $codeGenerationConfig );
8687
#elseif( $util.hasUnit( $property.getCharacteristic().get() ) )
8788
$codeGenerationConfig.importTracker().importExplicit( $StaticUnitProperty )
8889
$codeGenerationConfig.importTracker().importExplicit( $Unit )
89-
#propertyDeclaration() StaticUnitProperty<${element.getName()}, $propertyType> $util.toConstant( $property.getName() ) =
90-
#staticProperty( $property $codeGenerationConfig );
90+
#propertyDeclaration() StaticUnitProperty<$containingClass, $propertyType> $util.toConstant( $property.getName() ) =
91+
#staticProperty( $property $containingClass $codeGenerationConfig );
9192
#else
92-
#propertyDeclaration() StaticProperty<${element.getName()}, $propertyType> $util.toConstant( $property.getName() ) =
93-
#staticProperty( $property $codeGenerationConfig );
93+
#propertyDeclaration() StaticProperty<$containingClass, $propertyType> $util.toConstant( $property.getName() ) =
94+
#staticProperty( $property $containingClass $codeGenerationConfig );
9495
#end
9596
#end
9697

9798
#end
9899

99100
## -------------------------------------------------
100101

101-
#macro( staticProperty $property $codeGenerationConfig )
102+
#macro( staticProperty $property $containingClass $codeGenerationConfig )
102103
#set( $propertyType = $util.getPropertyType( $property, $codeGenerationConfig ) )
103104

104105
## new $type(
@@ -107,25 +108,25 @@
107108
#if( $util.hasContainerType( $property ) )
108109
$codeGenerationConfig.importTracker().importExplicit( $StaticConstraintContainerProperty )
109110
#set( $containedType = $util.getCharacteristicJavaType( $property, $codeGenerationConfig ) )
110-
new StaticConstraintContainerProperty<${element.getName()}, $containedType, $propertyType, #getCharacteristicClassName()>(
111+
new StaticConstraintContainerProperty<$containingClass, $containedType, $propertyType, #getCharacteristicClassName()>(
111112
#elseif( $util.hasUnit( $property.getCharacteristic().get() ) )
112113
$codeGenerationConfig.importTracker().importExplicit( $StaticConstraintUnitProperty )
113114
$codeGenerationConfig.importTracker().importExplicit( $Unit )
114-
new StaticConstraintUnitProperty<${element.getName()}, $propertyType, #getCharacteristicClassName()>(
115+
new StaticConstraintUnitProperty<$containingClass, $propertyType, #getCharacteristicClassName()>(
115116
#else
116-
new StaticConstraintProperty<${element.getName()}, $propertyType, #getCharacteristicClassName()>(
117+
new StaticConstraintProperty<$containingClass, $propertyType, #getCharacteristicClassName()>(
117118
#end
118119
#else
119120
#if( $util.hasContainerType( $property ) && !$propertyType.startsWith( "Map" ) )
120121
$codeGenerationConfig.importTracker().importExplicit( $StaticContainerProperty )
121122
#set( $containedType = $util.getCharacteristicJavaType( $property, $codeGenerationConfig ) )
122-
new StaticContainerProperty<${element.getName()}, $containedType, $propertyType> (
123+
new StaticContainerProperty<$containingClass, $containedType, $propertyType> (
123124
#elseif( $util.hasUnit( $property.getCharacteristic().get() ) )
124125
$codeGenerationConfig.importTracker().importExplicit( $StaticUnitProperty )
125126
$codeGenerationConfig.importTracker().importExplicit( $Unit )
126-
new StaticUnitProperty<${element.getName()}, $propertyType>(
127+
new StaticUnitProperty<$containingClass, $propertyType>(
127128
#else
128-
new StaticProperty<${element.getName()}, $propertyType>(
129+
new StaticProperty<$containingClass, $propertyType>(
129130
#end
130131
#end
131132

@@ -140,7 +141,7 @@ Optional.of("$property.getPayloadName()"),
140141
#if ( $property.getExtends().isEmpty() )
141142
Optional.empty()
142143
#else
143-
Optional.of( #staticProperty( $property.getExtends().get(), $codeGenerationConfig ) )
144+
Optional.of( #staticProperty( $property.getExtends().get(), $containingClass, $codeGenerationConfig ) )
144145
#end
145146
) {
146147

@@ -175,8 +176,8 @@ public Class<$propertyType> getPropertyType() {
175176
}
176177

177178
@Override
178-
public Class<${element.getName()}> getContainingType() {
179-
return ${element.getName()}.class;
179+
public Class<$containingClass> getContainingType() {
180+
return ${containingClass}.class;
180181
}
181182

182183
#if( $util.hasContainerType( $property ) && !$propertyType.startsWith( "Map" ) )
@@ -188,10 +189,18 @@ public Class<$propertyType> getPropertyType() {
188189

189190
#set( $getterName = $util.getterName( $property ) )
190191
@Override
191-
public $propertyType getValue(${element.getName()} object) {
192+
public $propertyType getValue($containingClass object) {
192193
return object.$getterName();
193194
}
194195

196+
#if ( $codeGenerationConfig.enableSetters() )
197+
#set( $setterName = $util.setterName( $property, $codeGenerationConfig ) )
198+
@Override
199+
public void setValue( $containingClass object, $propertyType value ) {
200+
object.$setterName( value );
201+
}
202+
#end
203+
195204
#if( $util.hasUnit( $property.getCharacteristic().get() ) )
196205
$codeGenerationConfig.importTracker().importExplicit( $Unit )
197206
$codeGenerationConfig.importTracker().importExplicit( $Units )

0 commit comments

Comments
 (0)