Skip to content

Commit 666390a

Browse files
committed
Add documentation, update test, update property generation
1 parent fda571c commit 666390a

File tree

8 files changed

+88
-15
lines changed

8 files changed

+88
-15
lines changed

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public static String determinePropertyType( final Optional<Characteristic> optio
173173
return String.format( "Either<%s,%s>", left, right );
174174
}
175175

176-
return getDataType( dataType, codeGenerationConfig.importTracker() );
176+
return getDataType( dataType, codeGenerationConfig.importTracker(), codeGenerationConfig );
177177
}
178178

179179
public static String determineCollectionAspectClassDefinition( final StructureElement element,
@@ -186,7 +186,7 @@ public static String determineCollectionAspectClassDefinition( final StructureEl
186186
final Characteristic characteristic = property.getEffectiveCharacteristic().orElseThrow( error );
187187
if ( characteristic instanceof Collection ) {
188188
final String collectionType = determineCollectionType( (Collection) characteristic, false, codeGenerationConfig );
189-
final String dataType = getDataType( characteristic.getDataType(), codeGenerationConfig.importTracker() );
189+
final String dataType = getDataType( characteristic.getDataType(), codeGenerationConfig.importTracker(), codeGenerationConfig );
190190
return String.format( "public class %s implements CollectionAspect<%s,%s>", element.getName(), collectionType, dataType );
191191
}
192192
}
@@ -199,7 +199,7 @@ public static String determineComplexTypeClassDefinition( final ComplexType elem
199199
if ( element.isAbstractEntity() ) {
200200
classDefinitionBuilder.append( "abstract " );
201201
}
202-
classDefinitionBuilder.append( "class " ).append( element.getName() );
202+
classDefinitionBuilder.append( "class " ).append( generateClassName( element, codeGenerationConfig ) );
203203
classDefinitionBuilder.append( genericClassSignature( element ) );
204204
if ( element.getExtends().isPresent() ) {
205205
final ComplexType extendedComplexType = element.getExtends().get();
@@ -265,20 +265,20 @@ private static String determineCollectionType( final Collection collection, fina
265265

266266
if ( collection.isAllowDuplicates() && collection.isOrdered() ) {
267267
codeGenerationConfig.importTracker().importExplicit( List.class );
268-
return containerType( List.class, getDataType( dataType, codeGenerationConfig.importTracker() ), elementConstraint );
268+
return containerType( List.class, getDataType( dataType, codeGenerationConfig.importTracker(), codeGenerationConfig ), elementConstraint );
269269
}
270270
if ( !collection.isAllowDuplicates() && collection.isOrdered() ) {
271271
codeGenerationConfig.importTracker().importExplicit( LinkedHashSet.class );
272-
return containerType( LinkedHashSet.class, getDataType( dataType, codeGenerationConfig.importTracker() ), elementConstraint );
272+
return containerType( LinkedHashSet.class, getDataType( dataType, codeGenerationConfig.importTracker(), codeGenerationConfig ), elementConstraint );
273273
}
274274
if ( collection.isAllowDuplicates() && !collection.isOrdered() ) {
275275
codeGenerationConfig.importTracker().importExplicit( java.util.Collection.class );
276-
return containerType( java.util.Collection.class, getDataType( dataType, codeGenerationConfig.importTracker() ),
276+
return containerType( java.util.Collection.class, getDataType( dataType, codeGenerationConfig.importTracker(), codeGenerationConfig ),
277277
elementConstraint );
278278
}
279279
if ( !collection.isAllowDuplicates() && !collection.isOrdered() ) {
280280
codeGenerationConfig.importTracker().importExplicit( Set.class );
281-
return containerType( Set.class, getDataType( dataType, codeGenerationConfig.importTracker() ), elementConstraint );
281+
return containerType( Set.class, getDataType( dataType, codeGenerationConfig.importTracker(), codeGenerationConfig ), elementConstraint );
282282
}
283283
throw new CodeGenerationException( "Could not determine Java collection type for " + collection.getName() );
284284
}
@@ -304,11 +304,15 @@ public static String containerType( final Class<?> containerClass, final String
304304
* @param importTracker the import tracker
305305
* @return a {@link String} containing the definition of the Java Data Type
306306
*/
307-
public static String getDataType( final Optional<Type> dataType, final ImportTracker importTracker ) {
307+
public static String getDataType( final Optional<Type> dataType, final ImportTracker importTracker, final JavaCodeGenerationConfig codeGenerationConfig ) {
308308
return dataType.map( type -> {
309309
final Type actualDataType = dataType.get();
310310
if ( actualDataType instanceof ComplexType ) {
311-
return ((ComplexType) actualDataType).getName();
311+
final String complexDataType = ((ComplexType) actualDataType).getName();
312+
if ( ( !codeGenerationConfig.namePrefix().isBlank() || !codeGenerationConfig.namePostfix().isBlank() ) ) {
313+
return codeGenerationConfig.namePrefix() + complexDataType + codeGenerationConfig.namePostfix();
314+
}
315+
return complexDataType;
312316
}
313317

314318
if ( actualDataType instanceof Scalar ) {
@@ -487,7 +491,7 @@ public static String generateFilterCompare( final Optional<Type> optionalDataTyp
487491
public static String getCharacteristicJavaType( final Property property, final JavaCodeGenerationConfig codeGenerationConfig ) {
488492
final Supplier<RuntimeException> error = () -> new CodeGenerationException( "No data type found for Property " + property.getName() );
489493
if ( hasContainerType( property ) ) {
490-
return getDataType( property.getCharacteristic().orElseThrow( error ).getDataType(), codeGenerationConfig.importTracker() );
494+
return getDataType( property.getCharacteristic().orElseThrow( error ).getDataType(), codeGenerationConfig.importTracker(), codeGenerationConfig );
491495
}
492496

493497
return property.getEffectiveCharacteristic().flatMap( Characteristic::getDataType ).map( type -> {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public JavaArtifact apply( final E element, final JavaCodeGenerationConfig confi
5656
.put( "className", element.getName() )
5757
.put( "codeGenerationConfig", config )
5858
.put( "currentYear", Year.now() )
59-
.put( "dataType", AspectModelJavaUtil.getDataType( element.getDataType(), config.importTracker() ) )
59+
.put( "dataType", AspectModelJavaUtil.getDataType( element.getDataType(), config.importTracker(), config ) )
6060
.put( "Entity", Entity.class )
6161
.put( "enumeration", element )
6262
.put( "importTracker", importTracker )

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,4 +1136,18 @@ void testGenerateAspectWithPrefixAndPostfix() throws IOException {
11361136
result.assertFields( "BaseAspectWithPropertyPostfix", expectedFieldsForAspectClass, new HashMap<>() );
11371137
assertConstructor( result, "BaseAspectWithPropertyPostfix", expectedFieldsForAspectClass );
11381138
}
1139+
1140+
@Test
1141+
void testGenerateEntityWithPrefixAndPostfix() throws IOException {
1142+
final ImmutableMap<String, Object> expectedFieldsForAspectClass = ImmutableMap.<String, Object> builder()
1143+
.put( "testProperty", "BaseTestEntityPostfix" )
1144+
.build();
1145+
1146+
final TestAspect aspect = TestAspect.ASPECT_WITH_ENTITY;
1147+
final GenerationResult result = TestContext.generateAspectCode()
1148+
.apply( getGenerators( aspect, "Base", "Postfix" ) );
1149+
result.assertNumberOfFiles( 2 );
1150+
result.assertFields( "BaseAspectWithEntityPostfix", expectedFieldsForAspectClass, new HashMap<>() );
1151+
assertConstructor( result, "BaseAspectWithEntityPostfix", expectedFieldsForAspectClass );
1152+
}
11391153
}

documentation/developer-guide/modules/tooling-guide/pages/maven-plugin.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ are replaced using `packageName` | `String` | none | {nok}
119119
| `executeLibraryMacros` | Execute the macros provided in the velocity macro library. | `Boolean` | `false` | {nok}
120120
| `disableJacksonAnnotations` | Leads to generated Java code that does not contain https://github.com/FasterXML/jackson[Jackson] annotations. | `Boolean` | `false` | {nok}
121121
| `skip` | Skip execution of plugin and generation | `Boolean` | `false` | {nok}
122+
| `namePrefix` | Name prefix for generated Aspect, Entity Java classes | `String` | none | {nok}
123+
| `namePostfix` | Name postfix for generated Aspect, Entity Java classes | `String` | none | {nok}
122124
|===
123125

124126
=== Generate Static Meta Classes

documentation/developer-guide/modules/tooling-guide/pages/samm-cli.adoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ The available options and their meaning can also be seen in the help text of the
7878
| _--language, -l_ : the language from the model for which the diagram should be
7979
generated (default: en) |
8080
| _--custom-resolver_ : use an external resolver for the resolution of the model elements | `samm aspect AspectModel.ttl to svg --custom-resolver "java -jar resolver.jar"`
81-
.8+| [[asepct-to-java]] aspect <model> to java | Generate Java classes from an Aspect Model | `samm aspect AspectModel.ttl to java`
81+
.10+| [[asepct-to-java]] aspect <model> to java | Generate Java classes from an Aspect Model | `samm aspect AspectModel.ttl to java`
8282
| _--output-directory, -d_ : output directory to write files to (default:
8383
current directory) |
8484
| _--package-name, -pn_ : package to use for generated Java classes | `samm aspect AspectModel.ttl to java -pn org.company.product`
@@ -90,6 +90,8 @@ The available options and their meaning can also be seen in the help text of the
9090
https://velocity.apache.org/[Velocity] macro library |
9191
| _--static, -s_ : generate Java domain classes for a Static Meta Model |
9292
| _--custom-resolver_ : use an external resolver for the resolution of the model elements |
93+
| _--name-prefix, -namePrefix_ : name prefix for generated Aspect, Entity Java classes | `samm aspect AspectModel.ttl to java -namePrefix "Prefix"`
94+
| _--name-postfix, -namePostfix_ : name postfix for generated Aspect, Entity Java classes | `samm aspect AspectModel.ttl to java -namePostfix "Postfix"`
9395
.21+| [[aspect-to-openapi]] aspect <model> to openapi | Generate https://spec.openapis.org/oas/v3.0.3[OpenAPI] specification
9496
for an Aspect Model | `samm aspect AspectModel.ttl to openapi -j`
9597
| _--output, -o_ : output file path (default: stdout) |

tools/esmf-aspect-model-maven-plugin/src/test/java/org/eclipse/esmf/aspectmodel/GenerateJavaClassesTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,19 @@ public void testSkipPluginExecution() throws Exception {
6666
}
6767

6868
@Test
69-
public void testGenerateJavaClassesWithPrefixAndPostfix() throws Exception {
69+
public void testGenerateJavaClassesAspectWithPrefixAndPostfix() throws Exception {
7070
final File testPom = getTestFile( "src/test/resources/generate-java-classes-pom-with-prefix-and-postfix.xml" );
7171
final Mojo generateJavaClasses = lookupMojo( "generateJavaClasses", testPom );
7272
assertThatCode( generateJavaClasses::execute ).doesNotThrowAnyException();
7373
assertThat( generatedFilePath( "example", "com", "BaseAspectPostfix.java" ) ).exists();
7474
}
75+
76+
@Test
77+
public void testGenerateJavaClassesEntityWithPrefixAndPostfix() throws Exception {
78+
final File testPom = getTestFile( "src/test/resources/generate-java-classes-pom-entity-with-prefix-and-postfix.xml" );
79+
final Mojo generateJavaClasses = lookupMojo( "generateJavaClasses", testPom );
80+
assertThatCode( generateJavaClasses::execute ).doesNotThrowAnyException();
81+
assertThat( generatedFilePath( "example", "com", "BaseAspectWithEntityPostfix.java" ) ).exists();
82+
assertThat( generatedFilePath( "example", "com", "BaseTestEntityPostfix.java" ) ).exists();
83+
}
7584
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright (c) 2022 Robert Bosch Manufacturing Solutions GmbH
4+
~
5+
~ See the AUTHORS file(s) distributed with this work for additional
6+
~ information regarding authorship.
7+
~
8+
~ This Source Code Form is subject to the terms of the Mozilla Public
9+
~ License, v. 2.0. If a copy of the MPL was not distributed with this
10+
~ file, You can obtain one at https://mozilla.org/MPL/2.0/.
11+
~
12+
~ SPDX-License-Identifier: MPL-2.0
13+
-->
14+
15+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
16+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
17+
<modelVersion>4.0.0</modelVersion>
18+
19+
<groupId>org.eclipse.esmf</groupId>
20+
<artifactId>test-generate-java-classes-mojo</artifactId>
21+
<version>1.0</version>
22+
<packaging>jar</packaging>
23+
<name>Test Generate Java Classes Mojo</name>
24+
25+
<build>
26+
<plugins>
27+
<plugin>
28+
<artifactId>esmf-aspect-model-maven-plugin</artifactId>
29+
<configuration>
30+
<modelsRootDirectory>${basedir}/../../core/esmf-test-aspect-models/src/main/resources/valid</modelsRootDirectory>
31+
<includes>
32+
<include>urn:samm:org.eclipse.esmf.test:1.0.0#AspectWithEntity</include>
33+
</includes>
34+
<outputDirectory>${basedir}/target/test-artifacts</outputDirectory>
35+
<packageName>example.com</packageName>
36+
<namePrefix>Base</namePrefix>
37+
<namePostfix>Postfix</namePostfix>
38+
</configuration>
39+
</plugin>
40+
</plugins>
41+
</build>
42+
</project>

tools/samm-cli/src/main/java/org/eclipse/esmf/aspect/to/AspectToJavaCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ public class AspectToJavaCommand extends AbstractCommand {
6060
@CommandLine.Option( names = { "--static", "-s" }, description = "Generate Java domain classes for a Static Meta Model" )
6161
private boolean generateStaticMetaModelJavaClasses = false;
6262

63-
@CommandLine.Option( names = { "--name-prefix", "-namePrefix" }, description = "Name prefix for generated Java class of Aspect" )
63+
@CommandLine.Option( names = { "--name-prefix", "-namePrefix" }, description = "Name prefix for generated Aspect, Entity Java classes" )
6464
private String namePrefix = "";
6565

66-
@CommandLine.Option( names = { "--name-postfix", "-namePostfix" }, description = "Name postfix for generated Java class of Aspect" )
66+
@CommandLine.Option( names = { "--name-postfix", "-namePostfix" }, description = "Name postfix for generated Aspect, Entity Java classes" )
6767
private String namePostfix = "";
6868

6969
@CommandLine.ParentCommand

0 commit comments

Comments
 (0)