Skip to content

Commit 0706ccc

Browse files
committed
Fix Optional.of for samm:optional true; property
1 parent 69dc2c9 commit 0706ccc

File tree

5 files changed

+35
-24
lines changed

5 files changed

+35
-24
lines changed

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public static String getPropertyType( final Property property, final JavaCodeGen
119119
*/
120120
public static boolean hasContainerType( final Property property ) {
121121
return property.isOptional()
122-
|| ( property.getEffectiveCharacteristic().map( characteristic -> characteristic.is( Collection.class ) ).orElse( false ) );
122+
|| (property.getEffectiveCharacteristic().map( characteristic -> characteristic.is( Collection.class ) ).orElse( false ));
123123
}
124124

125125
/**
@@ -322,8 +322,8 @@ public static String getDataType( final Optional<Type> dataType, final ImportTra
322322
return dataType.map( type -> {
323323
final Type actualDataType = dataType.get();
324324
if ( actualDataType instanceof ComplexType ) {
325-
final String complexDataType = ( (ComplexType) actualDataType ).getName();
326-
if ( ( !codeGenerationConfig.namePrefix().isBlank() || !codeGenerationConfig.namePostfix().isBlank() ) ) {
325+
final String complexDataType = ((ComplexType) actualDataType).getName();
326+
if ( (!codeGenerationConfig.namePrefix().isBlank() || !codeGenerationConfig.namePostfix().isBlank()) ) {
327327
return codeGenerationConfig.namePrefix() + complexDataType + codeGenerationConfig.namePostfix();
328328
}
329329
return complexDataType;
@@ -347,7 +347,7 @@ public static String getDataType( final Optional<Type> dataType, final ImportTra
347347

348348
public static Class<?> getDataTypeClass( final Type dataType ) {
349349
if ( dataType instanceof ComplexType ) {
350-
return ( (ComplexType) dataType ).getClass();
350+
return ((ComplexType) dataType).getClass();
351351
}
352352

353353
final Resource typeResource = ResourceFactory.createResource( dataType.getUrn() );
@@ -473,7 +473,10 @@ public static String generateInitializer( final Property property, final String
473473
final Resource typeResource = ResourceFactory.createResource( type.getUrn() );
474474
final Class<?> result = SammXsdType.getJavaTypeForMetaModelType( typeResource );
475475
codeGenerationConfig.importTracker().importExplicit( result );
476-
return valueInitializer.apply( typeResource, value );
476+
final String initializer = valueInitializer.apply( typeResource, value );
477+
return property.isOptional()
478+
? optionalExpression( initializer, codeGenerationConfig )
479+
: initializer;
477480
} ).orElseThrow( () -> new CodeGenerationException(
478481
"The Either Characteristic is not allowed for Properties used as elements in a StructuredValue" ) );
479482
}
@@ -572,7 +575,7 @@ public static String genericClassSignature( final StructureElement element ) {
572575
}
573576

574577
public static String generateClassName( final StructureElement element, final JavaCodeGenerationConfig config ) {
575-
if ( ( !config.namePrefix().isBlank() || !config.namePostfix().isBlank() ) && element.is( StructureElement.class ) ) {
578+
if ( (!config.namePrefix().isBlank() || !config.namePostfix().isBlank()) && element.is( StructureElement.class ) ) {
576579
return config.namePrefix() + element.getName() + config.namePostfix();
577580
}
578581
return element.getName();
@@ -668,10 +671,18 @@ public static String getterName( final Property property ) {
668671
.filter( Type::isScalar )
669672
.map( type -> XSD.xboolean.getURI().equals( type.getUrn() ) )
670673
.orElse( false );
671-
return ( isBooleanType ? "is" : "get" ) + StringUtils.capitalize( property.getPayloadName() );
674+
return (isBooleanType ? "is" : "get") + StringUtils.capitalize( property.getPayloadName() );
672675
}
673676

674677
public static String codeGeneratorName() {
675678
return "esmf-sdk " + VersionInfo.ESMF_SDK_VERSION;
676679
}
680+
681+
public static String optionalExpression(
682+
final String expression,
683+
final JavaCodeGenerationConfig codeGenerationConfig
684+
) {
685+
codeGenerationConfig.importTracker().importExplicit( Optional.class );
686+
return "Optional.of(" + expression + ")";
687+
}
677688
}

core/esmf-aspect-model-java-generator/src/test/java/org/eclipse/esmf/aspectmodel/java/AspectModelJavaUtilTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import org.junit.jupiter.params.provider.Arguments;
3030
import org.junit.jupiter.params.provider.MethodSource;
3131

32-
public class AspectModelJavaUtilTest extends PropertyBasedTest {
32+
class AspectModelJavaUtilTest extends PropertyBasedTest {
3333
private boolean isValidJavaIdentifier( final String value ) {
3434
if ( value == null || value.isEmpty() ) {
3535
return false;

core/esmf-aspect-model-java-generator/src/test/java/org/eclipse/esmf/aspectmodel/java/ExtendedStaticMetaModelFunctionalityTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import org.apache.commons.lang3.reflect.ConstructorUtils;
3232
import org.junit.jupiter.api.Test;
3333

34-
public class ExtendedStaticMetaModelFunctionalityTest extends StaticMetaModelGeneratorTest {
34+
class ExtendedStaticMetaModelFunctionalityTest extends StaticMetaModelGeneratorTest {
3535
@Test
3636
void testComputedProperties() throws IOException, ReflectiveOperationException {
3737
final TestAspect aspect = TestAspect.ASPECT_WITH_EXTENDED_ENUMS;

core/esmf-aspect-model-java-generator/src/test/java/org/eclipse/esmf/aspectmodel/java/JavaCodeGenerationConfigurationTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222

2323
import org.junit.jupiter.api.Test;
2424

25-
public class JavaCodeGenerationConfigurationTest {
25+
class JavaCodeGenerationConfigurationTest {
2626
final String currentWorkingDirectory = System.getProperty( "user.dir" );
2727
private final File templateLibFile = Path.of( currentWorkingDirectory, "/templates", "/test-macro-lib.vm" ).toFile();
2828
private final File emptyTemplateLibFile = Path.of( "" ).toFile();
2929
private final File nonExistingTemplateLibPath = Path.of( "/templates", "/non-existing.vm" ).toFile();
3030

3131
@Test
32-
public void testValidTemplateLibConfig() {
32+
void testValidTemplateLibConfig() {
3333
assertThatCode( () ->
3434
JavaCodeGenerationConfigBuilder.builder()
3535
.enableJacksonAnnotations( true )
@@ -54,7 +54,7 @@ public void testValidTemplateLibConfig() {
5454
}
5555

5656
@Test
57-
public void testTemplateLibConfigMissingFile() {
57+
void testTemplateLibConfigMissingFile() {
5858
assertThatCode( () ->
5959
JavaCodeGenerationConfigBuilder.builder()
6060
.enableJacksonAnnotations( true )
@@ -67,7 +67,7 @@ public void testTemplateLibConfigMissingFile() {
6767
}
6868

6969
@Test
70-
public void testTemplateLibConfigNonExistingFile() {
70+
void testTemplateLibConfigNonExistingFile() {
7171
assertThatCode( () ->
7272
JavaCodeGenerationConfigBuilder.builder()
7373
.enableJacksonAnnotations( true )

core/esmf-aspect-model-java-generator/src/test/java/org/eclipse/esmf/aspectmodel/java/StaticMetaModelBaseAttributesTest.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
import com.google.common.collect.ImmutableSet;
2323
import org.junit.jupiter.api.Test;
2424

25-
public class StaticMetaModelBaseAttributesTest extends StaticMetaModelGeneratorTest {
25+
class StaticMetaModelBaseAttributesTest extends StaticMetaModelGeneratorTest {
2626

2727
@Test
28-
public void testMetaModelBaseAttributesOfGeneratedProperty() throws IOException {
28+
void testMetaModelBaseAttributesOfGeneratedProperty() throws IOException {
2929
final TestAspect aspect = TestAspect.ASPECT_WITH_BOOLEAN;
3030
final StaticClassGenerationResult result = TestContext.generateStaticAspectCode().apply( getGenerators( aspect ) );
3131
result.assertNumberOfFiles( 2 );
@@ -40,7 +40,7 @@ public void testMetaModelBaseAttributesOfGeneratedProperty() throws IOException
4040
}
4141

4242
@Test
43-
public void testMetaModelBaseAttributesOfGeneratedPropertyWithAllAttributes() throws IOException {
43+
void testMetaModelBaseAttributesOfGeneratedPropertyWithAllAttributes() throws IOException {
4444
final TestAspect aspect = TestAspect.ASPECT_WITH_PROPERTY_WITH_ALL_BASE_ATTRIBUTES;
4545
final StaticClassGenerationResult result = TestContext.generateStaticAspectCode().apply( getGenerators( aspect ) );
4646
result.assertNumberOfFiles( 2 );
@@ -60,7 +60,7 @@ public void testMetaModelBaseAttributesOfGeneratedPropertyWithAllAttributes() th
6060
}
6161

6262
@Test
63-
public void testMetaModelBaseAttributesOfGeneratedPropertyWithPreferredNames() throws IOException {
63+
void testMetaModelBaseAttributesOfGeneratedPropertyWithPreferredNames() throws IOException {
6464
final TestAspect aspect = TestAspect.ASPECT_WITH_PROPERTY_WITH_PREFERRED_NAMES;
6565
final StaticClassGenerationResult result = TestContext.generateStaticAspectCode().apply( getGenerators( aspect ) );
6666
result.assertNumberOfFiles( 2 );
@@ -76,7 +76,7 @@ public void testMetaModelBaseAttributesOfGeneratedPropertyWithPreferredNames() t
7676
}
7777

7878
@Test
79-
public void testMetaModelBaseAttributesOfGeneratedPropertyWithDescriptions() throws IOException {
79+
void testMetaModelBaseAttributesOfGeneratedPropertyWithDescriptions() throws IOException {
8080
final TestAspect aspect = TestAspect.ASPECT_WITH_PROPERTY_WITH_DESCRIPTIONS;
8181
final StaticClassGenerationResult result = TestContext.generateStaticAspectCode()
8282
.apply( getGenerators( aspect ) );
@@ -93,7 +93,7 @@ public void testMetaModelBaseAttributesOfGeneratedPropertyWithDescriptions() thr
9393
}
9494

9595
@Test
96-
public void testMetaModelBaseAttributesOfGeneratedPropertyWithSee() throws IOException {
96+
void testMetaModelBaseAttributesOfGeneratedPropertyWithSee() throws IOException {
9797
final TestAspect aspect = TestAspect.ASPECT_WITH_PROPERTY_WITH_SEE;
9898
final StaticClassGenerationResult result = TestContext.generateStaticAspectCode()
9999
.apply( getGenerators( aspect ) );
@@ -110,7 +110,7 @@ public void testMetaModelBaseAttributesOfGeneratedPropertyWithSee() throws IOExc
110110
}
111111

112112
@Test
113-
public void testGeneratedMetaModelContainsRequiredMethods() throws IOException {
113+
void testGeneratedMetaModelContainsRequiredMethods() throws IOException {
114114
final TestAspect aspect = TestAspect.ASPECT_WITH_BOOLEAN;
115115
final StaticClassGenerationResult result = TestContext.generateStaticAspectCode()
116116
.apply( getGenerators( aspect ) );
@@ -132,7 +132,7 @@ public void testGeneratedMetaModelContainsRequiredMethods() throws IOException {
132132
}
133133

134134
@Test
135-
public void testGeneratedMetaModelContainsOptionalMethods() throws IOException {
135+
void testGeneratedMetaModelContainsOptionalMethods() throws IOException {
136136
final TestAspect aspect = TestAspect.ASPECT_WITH_ALL_BASE_ATTRIBUTES;
137137
final StaticClassGenerationResult result = TestContext.generateStaticAspectCode()
138138
.apply( getGenerators( aspect ) );
@@ -173,7 +173,7 @@ public void testGeneratedMetaModelContainsOptionalMethods() throws IOException {
173173
}
174174

175175
@Test
176-
public void testGeneratedMetaModelContainsGetPreferredNamesMethod() throws IOException {
176+
void testGeneratedMetaModelContainsGetPreferredNamesMethod() throws IOException {
177177
final TestAspect aspect = TestAspect.ASPECT_WITH_PREFERRED_NAMES;
178178
final StaticClassGenerationResult result = TestContext.generateStaticAspectCode()
179179
.apply( getGenerators( aspect ) );
@@ -204,7 +204,7 @@ public void testGeneratedMetaModelContainsGetPreferredNamesMethod() throws IOExc
204204
}
205205

206206
@Test
207-
public void testGeneratedMetaModelContainsGetDescriptionsMethod() throws IOException {
207+
void testGeneratedMetaModelContainsGetDescriptionsMethod() throws IOException {
208208
final TestAspect aspect = TestAspect.ASPECT_WITH_DESCRIPTIONS;
209209
final StaticClassGenerationResult result = TestContext.generateStaticAspectCode()
210210
.apply( getGenerators( aspect ) );
@@ -235,7 +235,7 @@ public void testGeneratedMetaModelContainsGetDescriptionsMethod() throws IOExcep
235235
}
236236

237237
@Test
238-
public void testGeneratedMetaModelContainsGetSeeMethod() throws IOException {
238+
void testGeneratedMetaModelContainsGetSeeMethod() throws IOException {
239239
final TestAspect aspect = TestAspect.ASPECT_WITH_PROPERTY_WITH_SEE;
240240
final StaticClassGenerationResult result = TestContext.generateStaticAspectCode()
241241
.apply( getGenerators( aspect ) );

0 commit comments

Comments
 (0)