2.5. T04 Create a Snippet

In this tutorial we will create a simple snippet.

  • Open in directory metafactory-workshop-metafactory\src\patterns\package\feature1 the file called pattern1.xml and adapt line 8 so it has the highlighted content:

Listing 2.10 src/patterns/package/feature1/pattern1.xml
 1<package xmlns="http://www.firstbase.nl/xsd/personaliom/pattern"
 2         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3         xsi:schemaLocation="http://www.firstbase.nl/xsd/personaliom/pattern http://www.firstbase.nl/xsd/personaliom/pattern.xsd"
 4         name="com.company.workshop"
 5         path="src/main/java">
 6  <class name="MyClass">
 7    <operation name="constructor">
 8      <body>${fmsnippet.feature1.constructor}</body>
 9    </operation>
10  </class>
11</package>

Now:

  • Click on metafactory-workshop-metafactory in the MetaFactory Project window.

  • Click on the Play button to run the MetaFactory tool.

Once finished the MetaFactory Tool will have generated the snippet with default content.

  • Open metafactory-workshop-metafactory\src\snippets\feature1\constructor.ftl to see the newly generated snippet.

  • Open metafactory-workshop-backend\src\main\java\com\company\workshop\MyClass.java to see the intermediate result of using the new snippet.

  • Adapt the file constructor.ftl so it contains the code you would like to see in the constructor.

  • Regenerate

  • Open metafactory-workshop-backend\src\main\java\com\company\workshop\MyClass.java to see the final result of using the new snippet.