-
Notifications
You must be signed in to change notification settings - Fork 140
Dedicated chapter about code generation configuration #1850
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 34 commits
96f0cfe
d736bf8
c7cb8a5
28a33a9
83d4d87
99fd58d
ec69759
beecdc0
1fd9a5f
c4c734b
498299f
d61ed74
9cc0876
3457d84
75e7b7c
bfbb38e
179e9f7
44cb70c
d1494f5
9857584
723352f
593c48f
44b9f0b
16eb9d3
d8ea52e
b592f45
2efc64f
291e51d
9bb1805
7696ff0
46a8e71
d0c06c9
ae9235b
122191a
464a027
62743a8
c6ec091
8c203e7
5fed059
7b03d28
9824556
b143574
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -378,6 +378,150 @@ Use the _.cdsrc.json_ file to add project specific configuration of `@sap/cds-dk | |
| [Learn more about configuration and `cds.env`](../../node.js/cds-env){.learn-more} | ||
|
|
||
|
|
||
| ## Code Generation for Typed Access {#codegen-config} | ||
|
|
||
| The [interfaces for typed access](../cds-data#generated-accessor-interfaces) are generated at each build | ||
| by the [`cds:generate`](/java/assets/cds-maven-plugin-site/generate-mojo.html) goal of the [CDS Maven Plugin](/java/assets/cds-maven-plugin-site/plugin-info.html). | ||
|
|
||
| You configure this goal just like any other Maven plugin via its configuration options via your application's POM. For example: | ||
|
|
||
| ```xml [pom.xml] | ||
| <execution> | ||
| <id>cds.generate</id> | ||
| <goals> | ||
| <goal>generate</goal> | ||
| </goals> | ||
| <configuration> | ||
| <basePackage>cds.gen</basePackage> | ||
| ... | ||
| </configuration> | ||
| </execution> | ||
| ``` | ||
|
|
||
| Each time your application is built, these interfaces are regenerated. By default, they are excluded from your version control. | ||
|
|
||
vmikhailenko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ### Package for Generated Code | ||
|
|
||
| The option [`basePackage`](/java/assets/cds-maven-plugin-site/generate-mojo.html#basePackage) can be used to specify a base package prefix for generated code. The suffix package structure will reflect namespaces defined in your CDS model. | ||
|
|
||
| ### Filter for CDS Entities | ||
|
|
||
| By default, the complete model of your application is generated including all imported or re-used models. | ||
| You can use options [`includes`](/java/assets/cds-maven-plugin-site/generate-mojo.html#includes) and [`excludes`](/java/assets/cds-maven-plugin-site/generate-mojo.html#excludes) to specify the part of your overall model that is subject to code generation. Both inclusion and exclusion can be used together, inclusion evaluated first, then exclusion filter out included set of entities. | ||
vmikhailenko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
vmikhailenko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| These options use patterns that are applied on the fully qualified names of the entities in CDS models. For example, the pattern `my.bookshop.*` will cover all definitions with namespace `my.bookshop` and the pattern `my.bookshop.**` will cover all definitions with fully qualified name starting with `my.bookshop`. | ||
|
|
||
| :::warning Cross-namespace references are not resolved | ||
| Options `includes` and `excludes` are simple filters. If included parts of your model reference types from the excluded area, the resulting code will not compile. | ||
| ::: | ||
|
|
||
| ### Style of Interfaces | ||
|
|
||
| By default, the accessor interfaces provide the setter and getter methods inspired by the JavaBeans specification. In this style getter and setter method names are prefixed with `get` and `set`, resp.: | ||
agoerler marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| ```java | ||
| Authors author = Authors.create(); | ||
| author.setName("Emily Brontë"); | ||
|
|
||
| Books book = Books.create(); | ||
| book.setAuthor(author); | ||
| book.setTitle("Wuthering Heights"); | ||
| ``` | ||
|
|
||
| Alternatively, you can generate accessor interfaces in _fluent style_. In this mode, the getter methods are named after the property names. To enable fluent chaining, the setter methods return the accessor interface itself: | ||
|
|
||
|
|
||
| ```java | ||
| Authors author = Authors.create().name("Emily Brontë"); | ||
| Books.create().author(author).title("Wuthering Heights"); | ||
| ``` | ||
|
|
||
| The generation mode is configured by the option [`methodStyle`](/java/assets/cds-maven-plugin-site/generate-mojo.html#methodStyle). The selected style affects all entities and event contexts in your services. The default value is `BEAN`, which represents JavaBeans-style interfaces. | ||
|
|
||
| Once, when starting a project, decide on the style of the interfaces that is best for your team and project. | ||
|
|
||
| The way the interfaces are generated only determines how data is accessed by custom code. It does not affect how the data is represented in memory and handled by the CAP Java runtime. | ||
|
|
||
| Moreover, it doesn't change the way how event contexts, delivered by CAP, look like. Such interfaces from CAP are always modelled in the default JavaBeans style. | ||
|
|
||
| ### Code Generation Features | ||
|
|
||
| Other options in this goal enable or disable certain features that change the way generated code looks in a certain aspect. These changes can be incompatible with the existing code and require manual adaptation. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This also applied to the |
||
|
|
||
| - [`strictSetters`](/java/assets/cds-maven-plugin-site/generate-mojo.html#strictSetters) | ||
|
|
||
| This switch changes the signature of the setter methods in typed access interfaces so that they require concrete type instead of generic `Map` interface. | ||
| For example, instead of the following: | ||
vmikhailenko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```java | ||
| void setManager(Map<String, ?> manager); | ||
| ``` | ||
| you get the following: | ||
|
|
||
| ```java | ||
| void setManager(Manager manager); | ||
| ``` | ||
vmikhailenko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| It does not introduce any additional type checks at runtime, the correctness of the assignment is checked only at the time of compilation. | ||
|
|
||
| - [`interfacesForAspects`](/java/assets/cds-maven-plugin-site/generate-mojo.html#interfacesForAspects) | ||
vmikhailenko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| If your entity is modelled with the [composition of aspects](/cds/cdl#with-named-targets), the generated interfaces always reference original aspect as type for setters and getters. | ||
| When this switch is enabled, code generator will use type generated by the compiler instead of type of the aspect itself and will include methods to fetch keys, for example. | ||
vmikhailenko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| :::warning Limitations | ||
| This is supported only for the named aspects (inline targets are not supported) and does not respect all possible options how such entities might be exposed by services. | ||
| ::: | ||
|
|
||
| - [`betterNames`](/java/assets/cds-maven-plugin-site/generate-mojo.html#betterNames) | ||
|
|
||
| CDS models from external sources might include elements that have some special characters in their names or include elements that clash with Java keywords. Such cases always can be solved with the [renaming features](/java/cds-data#renaming-elements-in-java) provided by code generator, but in case of large models, this is tedious. | ||
| When this switch is enabled, characters `/` and `$` behave as a separators for the name during case conversions, similar to `_` and `.`. For example, `GET_MATERIAL` yields `GetMaterial` (or `getMaterial` for attributes and methods). The same now applies for the names with `/`, for example, name `/DMO/GET_MATERIAL` will be converted to `DmoGetMaterial`. | ||
|
|
||
| The following conversions are applied: | ||
| - Names from CDS model that are Java keywords are suffixed with `_`. | ||
| - Names from CDS model that use characters that are not valid as Java identifiers, are replaced by `_`. This, however, might lead to a conflicts between names that yield the same name in Java. | ||
vmikhailenko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - Leading `_` will remain in the name after conversions. This supports convention where association and its foreign key have names like `_assoc` and `assoc`. | ||
vmikhailenko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| These conversions no longer influence the splitting. | ||
|
|
||
| - ['cqnServiceGetters'](/java/assets/cds-maven-plugin-site/generate-mojo.html#cqnServiceGetters) | ||
|
|
||
| The method `getService()` in generated [event-specific Event Context interfaces](../event-handlers/#eventcontext) is overridden to return the typed service interface instead of generic `Service` type. | ||
vmikhailenko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| :::warning Check migration guides! | ||
| In major releases of CAP Java, some of these switches can be made new default and some other switches might be removed. This might introduce compile errors | ||
vmikhailenko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| in your application that needs to be fixed. | ||
| ::: | ||
|
|
||
| See [Maven Plugin Documentation](/java/assets/cds-maven-plugin-site/generate-mojo.html) for actual status of deprecation and switches that are not described here. {.learn-more} | ||
|
|
||
| ### Annotation Detail Level | ||
|
|
||
| The option [`annotationDetailLevel`](/java/assets/cds-maven-plugin-site/generate-mojo.html#annotationDetailLevel) lets you choose the amount of the details for the Java annotation [`@Generated`](https://docs.oracle.com/en/java/javase/21/docs/api/java.compiler/javax/annotation/processing/Generated.html) added to each interface. This annotation has no effect at runtime but is evaluated by static code analysis tools to identify the artifacts as generated. | ||
|
|
||
| Following levels of the details are available: | ||
| - `MINIMAL` (default) - only the annotation is added, no additional information is added. | ||
|
|
||
| ```java | ||
| @CdsName("service.Entity") | ||
| @Generated("cds-maven-plugin") | ||
| public interface Entity extends CdsData { } | ||
| ``` | ||
|
|
||
| - `FULL` - annotation contains the timestamp of the generation. | ||
|
|
||
| ```java | ||
| @CdsName("service.Entity") | ||
| @Generated( | ||
| value = "cds-maven-plugin", | ||
| date = "9999-12-31T23:59:59.999999Z", | ||
| comments = "" | ||
| ) | ||
| public interface Entity extends CdsData { } | ||
| ``` | ||
|
|
||
| - `NONE` - no `@Generated` annotation is added. This is not recommended. | ||
|
|
||
| ### Using a Local cds-dk | ||
|
|
||
| Starting with version 3.6.0 of the `cds-services-archetype`, the default setup of a newly created CAP Java project has changed. The `@sap/cds-dk` is maintained as a `devDependency` in `package.json` and installed with an `npm ci` during the Maven build. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.