Skip to content

Commit 04903e7

Browse files
Extend SAMM CLI
1 parent 53028be commit 04903e7

File tree

6 files changed

+95
-1
lines changed

6 files changed

+95
-1
lines changed

documentation/developer-guide/modules/tooling-guide/examples/GenerateJavaPojo.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public void generate() {
3939
final JavaCodeGenerationConfig config = JavaCodeGenerationConfigBuilder.builder()
4040
.enableJacksonAnnotations( true )
4141
.packageName( "com.example.mycompany" ) // if left out, package is taken from Aspect's namespace
42+
.enableSetters( true ) // if left out, setters are not generated
43+
.setterStyle( JavaCodeGenerationConfig.SetterStyle.FLUENT ) // if left out, "STANDARD" setter style is used
4244
.build();
4345
final AspectModelJavaGenerator generator = new AspectModelJavaGenerator( aspectModel.aspect(), config );
4446
generator.generate( qualifiedName -> {

documentation/developer-guide/modules/tooling-guide/pages/java-aspect-tooling.adoc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,19 @@ https://en.wikipedia.org/wiki/Jackson_(API)[Jackson] annotations should be gener
701701
mapping function passed to the generation method takes a `QualifiedName` instead of a String, so that you can decide how
702702
to handle the package structure of the class.
703703

704+
By default, POJO classes are generated without setters. If setters are desired, use `enableSetters( true )` on the
705+
generator config. Three setter styles are supported:
706+
707+
[width="100%", options="header", cols="33,67"]
708+
|===
709+
| Setter style | Example output
710+
| `STANDARD` (the default, if not explicitly set) | `void setTheProperty( final String theProperty )`
711+
| `FLUENT` | `TheClass setTheProperty( final String theProperty )`
712+
| `FLUENT_COMPACT` | `TheClass theProperty( final String theProperty )`
713+
|===
714+
715+
The `FLUENT_*` styles allow for method chaining, so that POJO instances can be written in a more compact way.
716+
704717
++++
705718
<details>
706719
<summary>Show used imports</summary>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ are replaced using `packageName` | `String` | none | {nok}
171171
| `skip` | Skip execution of plugin and generation | `Boolean` | `false` | {nok}
172172
| `namePrefix` | Name prefix for generated Aspect, Entity Java classes | `String` | none | {nok}
173173
| `namePostfix` | Name postfix for generated Aspect, Entity Java classes | `String` | none | {nok}
174+
| `enableSetters` | Enable setters for generated Aspect, Entity Java classes | `Boolean` | `false` | {nok}
175+
| `setterStyle` | The desired style of the generated setters, one of `STANDARD`, `FLUENT` or `FLUENT_COMPACT`. | `String` | `STANDARD` | {nok}
174176
|===
175177

176178
[[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
@@ -107,7 +107,7 @@ of model resolution] for more information.
107107
| _--language, -l_ : the language from the model for which the diagram should be
108108
generated (default: en) |
109109
| _--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"`
110-
.12+| [[aspect-to-java]] aspect <model> to java | Generate Java classes from an Aspect Model | `samm aspect AspectModel.ttl to java`
110+
.14+| [[aspect-to-java]] aspect <model> to java | Generate Java classes from an Aspect Model | `samm aspect AspectModel.ttl to java`
111111
| _--output-directory, -d_ : output directory to write files to (default:
112112
current directory) |
113113
| _--package-name, -pn_ : package to use for generated Java classes | `samm aspect AspectModel.ttl to java -pn org.company.product`
@@ -125,6 +125,8 @@ of model resolution] for more information.
125125
| _--custom-resolver_ : use an external resolver for the resolution of the model elements |
126126
| _--name-prefix, -namePrefix_ : name prefix for generated Aspect, Entity Java classes | `samm aspect AspectModel.ttl to java -namePrefix "Prefix"`
127127
| _--name-postfix, -namePostfix_ : name postfix for generated Aspect, Entity Java classes | `samm aspect AspectModel.ttl to java -namePostfix "Postfix"`
128+
| _--enable-setters, -enableSetters_ : whether setters should be generated for Aspect, Entity Java classes | `samm aspect AspectModel.ttl to java --enable-setters`
129+
| _--setter-style, -setterStyle_ : the style of setters to generate for Aspect, Entity Java classes | `samm aspect AspectModel.ttl to java --enable-setters --setter-style "FLUENT"`
128130
.21+| [[aspect-to-openapi]] aspect <model> to openapi | Generate https://spec.openapis.org/oas/v3.0.3[OpenAPI] specification
129131
for an Aspect Model | `samm aspect AspectModel.ttl to openapi -j`
130132
| _--output, -o_ : output file path (default: stdout) |

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,19 @@ public class AspectToJavaCommand extends AbstractCommand {
103103
description = "Print detailed reports on errors" )
104104
private boolean details = false;
105105

106+
@SuppressWarnings( "FieldCanBeLocal" )
107+
@CommandLine.Option(
108+
names = { "--enable-setters", "-enableSetters" },
109+
description = "Generate setter methods in Aspect, Entity Java classes" )
110+
private boolean enableSetters = false;
111+
112+
@SuppressWarnings( "FieldCanBeLocal" )
113+
@CommandLine.Option( names = { "--setter-style", "-setterStyle" },
114+
description = "The style of setters to generate, one of STANDARD, FLUENT, FLUENT_COMPACT. Default: STANDARD",
115+
converter = SetterStyleConverter.class
116+
)
117+
private JavaCodeGenerationConfig.SetterStyle setterStyle;
118+
106119
@CommandLine.ParentCommand
107120
private AspectToCommand parentCommand;
108121

@@ -121,6 +134,15 @@ public JavaCodeGenerationConfig.JsonTypeInfoType convert( final String value ) t
121134
}
122135
}
123136

137+
static class SetterStyleConverter implements CommandLine.ITypeConverter<JavaCodeGenerationConfig.SetterStyle> {
138+
@Override
139+
public JavaCodeGenerationConfig.SetterStyle convert( final String value ) throws Exception {
140+
return value == null
141+
? JavaCodeGenerationConfig.SetterStyle.STANDARD
142+
: JavaCodeGenerationConfig.SetterStyle.valueOf( value.toUpperCase() );
143+
}
144+
}
145+
124146
@Override
125147
public void run() {
126148
setDetails( details );
@@ -151,6 +173,8 @@ private JavaCodeGenerationConfig buildConfig( final Aspect aspect ) {
151173
.packageName( pkgName )
152174
.namePrefix( namePrefix )
153175
.namePostfix( namePostfix )
176+
.enableSetters( enableSetters )
177+
.setterStyle( setterStyle )
154178
.build();
155179
}
156180

tools/samm-cli/src/test/java/org/eclipse/esmf/SammCliTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,57 @@ void testAspectToJavaCustomMacroLibraryValidation() {
683683
assertThat( result.stderr() ).contains( "Missing configuration. Please provide path to velocity template library file." );
684684
}
685685

686+
@Test
687+
void testAspectToJavaWithSetters() {
688+
final File outputDir = outputDirectory.toFile();
689+
final ExecutionResult result = sammCli.runAndExpectSuccess( "--disable-color", "aspect", defaultInputFile, "to", "java",
690+
"--output-directory", outputDir.getAbsolutePath(), "--custom-resolver", resolverCommand(), "--enable-setters" );
691+
assertThat( result.stdout() ).isEmpty();
692+
assertThat( result.stderr() ).isEmpty();
693+
694+
final File directory = Paths.get( outputDir.getAbsolutePath(), "org", "eclipse", "esmf", "test" ).toFile();
695+
assertThat( directory ).exists();
696+
assertThat( directory ).isDirectoryContaining(
697+
file -> file.getName().equals( "AspectWithEntity.java" ) || file.getName().equals( "TestEntity.java" ) );
698+
699+
final File sourceFile = directory.toPath().resolve( "AspectWithEntity.java" ).toFile();
700+
assertThat( sourceFile ).content().contains( "public void setTestProperty( final TestEntity testProperty )" );
701+
}
702+
703+
@Test
704+
void testAspectToJavaWithFluentSetters() {
705+
final File outputDir = outputDirectory.toFile();
706+
final ExecutionResult result = sammCli.runAndExpectSuccess( "--disable-color", "aspect", defaultInputFile, "to", "java",
707+
"--output-directory", outputDir.getAbsolutePath(), "--custom-resolver", resolverCommand(), "--enable-setters", "--setter-style", "FLUENT" );
708+
assertThat( result.stdout() ).isEmpty();
709+
assertThat( result.stderr() ).isEmpty();
710+
711+
final File directory = Paths.get( outputDir.getAbsolutePath(), "org", "eclipse", "esmf", "test" ).toFile();
712+
assertThat( directory ).exists();
713+
assertThat( directory ).isDirectoryContaining(
714+
file -> file.getName().equals( "AspectWithEntity.java" ) || file.getName().equals( "TestEntity.java" ) );
715+
716+
final File sourceFile = directory.toPath().resolve( "AspectWithEntity.java" ).toFile();
717+
assertThat( sourceFile ).content().contains( "public AspectWithEntity setTestProperty( final TestEntity testProperty )" );
718+
}
719+
720+
@Test
721+
void testAspectToJavaWithFluentCompactSetters() {
722+
final File outputDir = outputDirectory.toFile();
723+
final ExecutionResult result = sammCli.runAndExpectSuccess( "--disable-color", "aspect", defaultInputFile, "to", "java",
724+
"--output-directory", outputDir.getAbsolutePath(), "--custom-resolver", resolverCommand(), "--enable-setters", "--setter-style", "FLUENT_COMPACT" );
725+
assertThat( result.stdout() ).isEmpty();
726+
assertThat( result.stderr() ).isEmpty();
727+
728+
final File directory = Paths.get( outputDir.getAbsolutePath(), "org", "eclipse", "esmf", "test" ).toFile();
729+
assertThat( directory ).exists();
730+
assertThat( directory ).isDirectoryContaining(
731+
file -> file.getName().equals( "AspectWithEntity.java" ) || file.getName().equals( "TestEntity.java" ) );
732+
733+
final File sourceFile = directory.toPath().resolve( "AspectWithEntity.java" ).toFile();
734+
assertThat( sourceFile ).content().contains( "public AspectWithEntity testProperty( final TestEntity testProperty )" );
735+
}
736+
686737
@Test
687738
void testAspectToJavaStaticWithDefaultPackageName() {
688739
final File outputDir = outputDirectory.toFile();

0 commit comments

Comments
 (0)