referenceHasMetaData (metaDataKey)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. list-table::
:widths: 20 80
:header-rows: 0
:class: tight-table
* - Category
- metadata functions
* - Signature
- referenceHasMetaData(String metadataKey, String metadataValue) : boolean
* - Description
- Checks if the model reference in the context has a certain metadata key.
* - Arguments
- metaDataKey: key of metadata to check for existence
* - Returns
- - true if the model reference in the context has the metadata key
- false if the model reference in the context does not have the metadata key
* - Exceptions
- .. error::
This function raises a *No model reference in context* exception when the context does not contain a model reference.
* - Example
- The following example shows a code instruction that generates a Java class for each object,
and conditionally adds a field for each reference that has metadata *bar*. The field will have a comment line.
Given the input shown in the input section below CodeComposer will generate the comment for reference *exampleObject2* and *exampleObject3* as shown in the output section below.
.. note::
Multiple code instructions may generate code in the same output file. The datatype of the fields shown in the output is generated by another code instruction.
**Input**
.. code-block:: xml
:caption: src/codeinstruction/examples/example-code-instruction-simple-entity-foreach-reference-and-reference-has-meta-data.xml
:name: ReferenceHasMetaData_CodeInstruction
:linenos:
:emphasize-lines: 13, 15
${fmsnippet.examples.exampleCommentForReferenceThatHasMetaDataBar}
.. code-block::
:caption: src/snippet/examples/exampleCommentForReferenceThatHasMetaDataBar.ftl
:name: ReferenceHasMetaData_Snippet1
:linenos:
:emphasize-lines: 3
<#if !(modelReference)??> <#stop "modelReference not found in context" >#if>
This is an example comment for a reference with meta data bar: ${modelReference.type} ${modelReference.name}.
.. code-block:: xml
:caption: src/model/model.xml
:name: ReferenceHasMetaData_Model
:linenos:
:emphasize-lines: 10, 11, 15, 20
**Output**
.. code-block:: java
:caption: src/main/java/io/metafactory/codecomposer_reference/entities/simple/ExampleObject4SimpleEntity.java
:name: GetMetaDataFromObject_Output1
:linenos:
:emphasize-lines: 8, 14
package io.metafactory.codecomposer_reference.entities.simple;
public class ExampleObject4SimpleEntity
{
private ExampleObject1SimpleEntity exampleObject1;
/**
* This is an example comment for a reference with meta data bar: ExampleObject2 exampleObject2.
*/
private ExampleObject2SimpleEntity exampleObject2;
/**
* This is an example comment for a reference with meta data bar: ExampleObject3 exampleObject3.
*/
private ExampleObject3SimpleEntity exampleObject3;
}