Get Example source ABAP code based on a different SAP table
VERSION 5 OUT • CL_DD_DDL_ANNOTATION_SERVICE ABAP_CLASS
ABAP_CDS - Evaluation of Annotations When an object defined in the CDS source code> is activated, the annotations defined in the CDS annotation syntax> there are saved in internal system tables. These tables can then be accessed to evaluate the data. This is done for every annotation with correct syntax regardless of name and value. ITOC
System Class CL_DD_DDL_ANNOTATION_SERVICE> The following documented system class is provided for evaluations of the annotations of CDS entities>: CL_DD_DDL_ANNOTATION_SERVICE>> The methods of this class return the annotations of CDS entities. By default, the annotations are returned from metadata extensions> first, and then from the CDS entity itself. In the latter case, a distinction can be made between direct, derived and inherited annotations. In the case of inherited annotations the metadata extensions> (if present) are handled first. In cases where an annotation has no value specified, the initial annotation value is returned rather than any default value defined in the annotation definition.
Latest notes:
The method GET_ANNOTATIONS> of the class CL_DD_DDL_ANALYZE>> also returns the annotations of CDS entities. However, it only analyzes the associated DDL source code. It ignores annotations from metadata extensions, derived and inherited annotations, and the translations of annotations for translatable texts.
CL_DD_DDL_ANNOTATION_SERVICE> evaluates annotations of CDS entities as specified in the DDL source code> in CDS annotation syntax >. The associated annotation definitions> are ignored by CL_DD_DDL_ANNOTATION_SERVICE>. The validity of an annotation must be checked by the responsible framework.
The annotations Semantics.amount.currencyCode>> and Semantics.quantity.unitofMeasure >> can be derived from DDIC database tables only if the name of the referred currency key or unit key field isn't changed in a CDS view entity. If a CDS view entity selecting from a database table uses an alias name for a currency key or unit key field, then the annotation cannot be evaluated by the API CL_DD_DDL_ANNOTATION_SERVICE>. ABAP_HINT_END
Metadata Extensions Metadata extensions> add further annotations to a CDS entity or override existing annotations. Only those annotations can be added by means of metadata extensions, that are not relevant during the activation of the entity in question. The methods of the class CL_DD_DDL_ANNOTATION_SERVICE>> (except GET_DIRECT_ANNOS_... >) evaluate> by default the existing metadata extensions in the current AS ABAP. There can be multiple metadata extensions for an entity. These extensions can be assigned to different layers, such as industry, partner, and customer. The methods of the class CL_DD_DDL_ANNOTATION_SERVICE > collect all annotations belonging to the CDS entity in the following order:
Annotations from metadata extensions
Direct annotations from the data definition
Indirect annotations (derived and inherited annotations) Every annotation is only returned once for its scope. If annotations have the same name, the priority is determined by the layers of the involved metadata extensions (see topic TITLE >). Afterwards, the direct and indirect annotations of the CDS entity are evaluated. In the evaluation of the annotations inherited from the other CDS entities, any metadata extensions are taken into account first. Annotations found in an earlier evaluation step always have the higher priority. An annotation from a later step is only returned if it has not already been found in an earlier step. The results of the class CL_DD_DDL_ANNOTATION_SERVICE> show the origin of each annotation.
Latest notes:
Annotations from metadata extensions are not stored in the same system tables as annotations from the DDL source code of a CDS entity. They can only be made accessible by using the class CL_DD_DDL_ANNOTATION_SERVICE>. Other classes ignore metadata extensions.
For more information and examples, see Metadata Extensions> and Evaluation of Metadata Extensions >.
The program ABAP_DOCU_MDE_ANNOS>> shows all annotations that can be specified in metadata extensions. ABAP_HINT_END
Direct, Inherited, and Derived Annotations When evaluating annotations of a CDS entity, under the condition that they are not overridden by metadata extensions, using methods of the class CL_DD_DDL_ANNOTATION_SERVICE>>, the following types of annotations can be distinguished:
Direct annotations Direct annotations are specified in the annotation syntax> directly in the DDL source code of the CDS entity currently being evaluated. The methods GET_DIRECT_ANNOS_ > of the class CL_DD_DDL_ANNOTATION_SERVICE > only read these annotations.
Indirect annotations In addition to returning direct annotations, the methods of the class CL_DD_DDL_ANNOTATION_SERVICE> (except for GET_DIRECT_ANNOS_ >) also return annotations that are not specified in the DDL source code:
Inherited annotations
If these annotations are not specified explicitly in the DDL source code of the CDS entity, they are taken from the dictionary objects used in the CDS entity, in accordance with the rules described in the class documentation of CL_DD_DDL_ANNOTATION_SERVICE>>. When other CDS entities are accessed, their annotations specified in metadata extensions, direct annotations, and derived annotations are inherited. Other dictionary objects, such as DDIC database tables and DDIC views, do not have any direct annotations. However, their annotations derived from assigned data elements are inherited. The view annotation Metadata.ignorePropagatedAnnotations> can be used to specify whether the class CL_DD_DDL_ANNOTATION_SERVICE> takes into account or ignores inherited annotations.
Derived annotations
If these annotations are not explicitly specified in the DDL source code of the CDS entity or inherited from base objects, they are derived (if possible) from assigned DDIC data elements. The relevant annotations are the element and parameter annotations @EndUserText.Label>, @EndUserText.QuickInfo>, and @EndUserText.Heading>, which are supplied with texts according to rules (described in the class documentation CL_DD_DDL_ANNOTATION_SERVICE>>) from the field labels of assigned data elements. The annotation @EndUserText.Heading> is only currently possible as an implicit annotation for this purpose. If specified directly in the DDL source code of a data definition, an annotation is not recognized as a language-dependent text. @EndUserText.Heading> is also not allowed in the DDLX source code of a metadata extension. The annotations @AbapCatalog.typeSpec.conversionExit>, @AbapCatalog.typeSpec.changeDocumentRelevant>, and @ObjectModel.upperCase> are derived as implicit annotations. They can be specified directly in the TDL source code of CDS simple types.
Latest notes:
Inheritances apply especially to those element annotations> of CDS associations> exposed in the SELECT> list of a CDS entity using a path expression path_expr >. A field from a path expression inherits the element annotations of preceding publications.
Annotations derived from data elements depend on the assignment of data elements to elements of the current CDS entity. This assignment is performed in the internal metadata of a CDS entity and is not dependent on inheritance.
The annotation types described here apply especially to evaluations using the class CL_DD_DDL_ANNOTATION_SERVICE>>. Other APIs can evaluate the underlying metadata differently. ABAP_HINT_END VX_EXA_ONLY
ABAP_EXAMPLE_VX The following three pieces of DDL source code> define three view entities; the first two view entities access the third view entity. The only difference between the first two view entities is the specification of annotation @Metadata.ignorePropagatedAnnotations:true>. Every view contains an element with a direct element annotation @EndUserText.label>. DDLS DEMO_CDS_ANNO_INHERITANCE_1 DDLS DEMO_CDS_ANNO_INHERITANCE_1A DDLS DEMO_CDS_ANNO_INHERITANCE_2 The program DEMO_CDS_DERIVED_INHERIT_ANNOS>> uses a method of the class CL_DD_DDL_ANNOTATION_SERVICE>> to read the element annotations of the first two CDS view entities:
Almost all element annotations in this example are from annotations derived from data elements. This applies especially to the annotation @EndUserText.heading>, which cannot be specified in the source code. The texts of the element destination> originate from data element demo_destination>, which overrides the automatic assignment to S_TO_CITY> in a CAST> expression.
Without the annotation @Metadata.ignorePropagatedAnnotations:true> , the derived annotations are inherited from the used objects. The SOURCEOBJECT> column contains the names of the objects.
If annotation @Metadata.ignorePropagatedAnnotations:true> is used, the derivation is performed in the current view and the column SOURCEOBJECT> only shows names.
The value of annotation @EndUserText.label> of element id> originates from the annotation specified directly in the first two view entities.
The value of annotation @EndUserText.label> of element flight > originates in the first view entity from the annotation, which it inherits from the second view entity. In the second view entity, this inheritance is suppressed by the annotation @Metadata.ignorePropagatedAnnotations:true>. The ABAPCATALOG.INTERNAL.ISMANDT> annotation that is visible in the output is an internal annotation, which indicates the client field of a client-dependent CDS entity. ABAP_EXAMPLE_END VX_EXA_ONLY
ABAP_EXAMPLE_VX The following example demonstrates how annotations are derived from DDIC objects to CDS simple types.
The CDS simple type DEMO_SIMPLE_TYPE_INHERITANCE>> is based on the data element DEMO_BT_INHERITANCE> and derives its properties and texts: DRTY DEMO_SIMPLE_TYPE_INHERITANCE
The CDS view entity DEMO_CDS_BT> uses the CDS simple type DEMO_SIMPLE_TYPE_INHERITANCE> for a cast expression. As a result, the field Char1Field> inherits the properties and texts from the CDS simple type. DDLS DEMO_CDS_BT The program DEMO_CDS_BT_ANNOS>> uses methods of the class CL_DD_DDL_ANNOTATION_SERVICE> to read the element annotations of the field Char1Field>. It can be seen that the annotation values are passed from the DDIC data element to the CDS simple type and from there are derived as element annotations in the CDS view entity. ABAP_EXAMPLE_END
Subannotations In the annotation syntax>, subannotations> are specified either as comma-separated lists> in curly brackets or using structured names>. The way this metadata is saved internally is independent of the way it is specified and the methods of the class CL_DD_DDL_ANNOTATION_SERVICE>> always return subannotations as an individual annotation with a structured name.
Example ABAP Coding
Subannotations specified as followed @DemoAnno: {subAnno1: true, subAnno2: { subAnno1: 1, subAnno2: { subAnno1: 1, subAnno2: 2 } } }> are the same as those specified as follows @DemoAnno.subAnno1: true @DemoAnno.subAnno2.subAnno1: 1 @DemoAnno.subAnno2.subAnno2.subAnno1: 1 @DemoAnno.subAnno2.subAnno2.subAnno2: 2> Evaluating either of the above using the method GET_DIRECT_ANNOS_4_ENTITY> of the class CL_DD_DDL_ANNOTATION_SERVICE>> produces the following <(>ANNONAME<)>>VALUE>> DEMOANNO.SUBANNO1>true> DEMOANNO.SUBANNO2.SUBANNO1>1> DEMOANNO.SUBANNO2.SUBANNO2.SUBANNO1>1> DEMOANNO.SUBANNO2.SUBANNO2.SUBANNO2>2> ABAP_EXAMPLE_END
Annotation Arrays In the annotation syntax>, annotation arrays> are created using comma-separated lists> in square brackets after a specified annotation. When this metadata is saved internally, the elements of annotation arrays are saved as individual annotations. Here, they are saved under the name of the array. A consecutive index enclosed in $> characters $1$> , $2$>, ... is appended to this array. Accordingly, the methods of the class CL_DD_DDL_ANNOTATION_SERVICE>> return an array as individual annotations.
Latest notes: For annotation arrays, the annotation syntax> in DDL source code for CDS> entities is not as strict as the syntax of DEFINE ANNOTATION>> for annotation definitions>.
Array elements do not always have to have the same type in the annotation syntax.
Arrays can be nested directly in the annotation syntax. In the internal metadata (and hence in the results of the methods of the class CL_DD_DDL_ANNOTATION_SERVICE>), array elements with non-matching types are returned just as specified in the annotation syntax. Directly nested arrays are not saved but they are counted when the elements are indexed. ABAP_HINT_END
Example ABAP Coding
The annotation array> specified as follows is valid annotation syntax>, but it cannot have an annotation definition>. @DemoAnno: [ true, [1,2,3], { subAnno1:1, subAnno2:2} ]> The elements of the error do not have matching types and the second element is a directly nested array. The method GET_DIRECT_ANNOS_4_ENTITY> of the class CL_DD_DDL_ANNOTATION_SERVICE>> produces the following: <(>ANNONAME<)>>VALUE>> DEMOANNO$1$>true> DEMOANNO$3$.SUBANNO1>1> DEMOANNO$3$.SUBANNO2>2> It should be noted that the nested array is counted but not returned. ABAP_EXAMPLE_END
Null Values For each element annotation> of a CDS entity that is not part of an annotation array>, the special value null>> can be specified (without quotation marks). The value null> leads to a syntax error at other positions. An annotation with the value null> by default is not returned by the methods of the class CL_DD_DDL_ANNOTATION_SERVICE>. The default setting can be changed with the method input parameter NULL_VALUES >.
Latest notes:
Null values are only intended to hide unwanted, derived and inherited annotations in the evaluation of the annotations of a CDS entity.
A null value is inherited like every value. A direct annotation of a CDS entity overwrites the annotations taken from used dictionary objects. This means any values can be overwritten with the null value; a null value itself can also be overwritten.
In the DDLX source code> of CDS metadata extensions>, null values can be specified in exactly the same way as in the DDL source code> of a CDS entity.
The evaluation filters out all annotations that have a null value specified for their identifiers.
If specified, @annotation.annotation1:null> only affects the identifier @annotation.annotation1> but does not affect, for example, @annotation.annotation1.annotation2>.
If the null value is specified for an annotation array>, this value affects the entire array. @annot:null> specified for an inherited annotation array @annot:[ ..., ..., ...]> is applied to all elements of the array.
The evaluation filters annotations with null values - completely regardless of whether they are predefined SAP annotations> or not. ABAP_HINT_END VX_EXA_ONLY
ABAP_EXAMPLE_VX The following two CDS source codes> define two views; the second view accesses the first view. In the first view, @EndUserText> annotations with literal values are specified for every element of the SELECT> list. In the second view, some of these values are overwritten with the value null> . DDLS DEMO_CDS_ANNO_NULL_VALUE_1 DDLS DEMO_CDS_ANNO_NULL_VALUE_2 The program DEMO_CDS_NULL_ANNOS>> uses a method of the class CL_DD_DDL_ANNOTATION_SERVICE>> to read the element annotations of the two CDS view entities. Annotations containing the value null > in the second view are not reset.
The annotations @EndUserText.label: null> and @EndUserText.quickInfo: null> in front of the elements carrier > or flight> overwrite the values of the annotations, which were inherited from the first view. They are not returned by the evaluation.
The derived annotation @EndUserText.Heading> is assigned the value null> for every element. This occurs for in the first view for the element id>. For this view, the evaluation returns the annotation for the other two elements. In the second view, the value null> is also set for this view and the evaluation does not return the annotation @EndUserText.Heading> for any of the elements of the CDS view. ABAP_EXAMPLE_END
Annotations for Translatable Texts The annotation values of annotations for which this is defined in their annotation definition> (using the annotation @LanguageDependency>>) are used to define translatable semantic texts for a CDS object>.