<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Chris Ainsley</title>
    <description>The latest articles on DEV Community by Chris Ainsley (@ainslec).</description>
    <link>https://dev.to/ainslec</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F52979%2F03d6544a-5cf2-4a92-be5f-6b008af1c229.jpeg</url>
      <title>DEV Community: Chris Ainsley</title>
      <link>https://dev.to/ainslec</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ainslec"/>
    <language>en</language>
    <item>
      <title>Code Generation Is Easy - With Picocog</title>
      <dc:creator>Chris Ainsley</dc:creator>
      <pubDate>Mon, 08 Jan 2018 13:47:18 +0000</pubDate>
      <link>https://dev.to/ainslec/metaprogramming-with-picocog-44od</link>
      <guid>https://dev.to/ainslec/metaprogramming-with-picocog-44od</guid>
      <description>&lt;p&gt;&lt;a href="https://github.com/ainslec/picocog"&gt;Picocog&lt;/a&gt; is a lightweight and new open-source Java library created to make programmatic code generation (or metaprogramming) clean, and easy to achieve.&lt;/p&gt;

&lt;p&gt;Code generation is the process by which source code is programmatically generated based on inputs (also known as model).&lt;/p&gt;

&lt;p&gt;Code generation typically works as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Define a model/use an existing model.&lt;/li&gt;
&lt;li&gt;Interrogate model and emit/generate source-code&lt;/li&gt;
&lt;li&gt;(Optionally) Upon changes to the model, regenerate source-code.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Plain Old Java Objects
&lt;/h1&gt;

&lt;p&gt;A code generator designed to leverage Picocog typically utilizes a POJO model as a source of model data.&lt;/p&gt;

&lt;p&gt;Alternative use-cases are to use annotated Java source code, via the JSR-269 features of Java (&lt;a href="https://deors.wordpress.com/2011/10/08/annotation-processors/"&gt;link&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;For simplicity sake, this article will assume the PoJo scenario.&lt;/p&gt;

&lt;p&gt;A sample POJO model is shown below (omitting getters/setters for brevity).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_YM4ZRrQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/fopn6ml21s33vjjmaunc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_YM4ZRrQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/fopn6ml21s33vjjmaunc.png" alt="Sample PoJo Model"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This POJO model is typically instantiated from the following types of sources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JSON/XML/YAML/CSV&lt;/li&gt;
&lt;li&gt;Via database query&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://en.wikipedia.org/wiki/Domain-specific_language"&gt;DSL&lt;/a&gt; (domain specific languages)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uvExmhrH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/x4zdrn01ldkifizxna4b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uvExmhrH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/x4zdrn01ldkifizxna4b.png" alt="XML representation of the model"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;NOTE: It is important that the when instantiating the POJO model that a full set of validations are performed and assumptions tested. XML schema or a bespoke DSL implementation can ensure referential integrity and other validations.&lt;/p&gt;

&lt;h1&gt;
  
  
  Code Generation
&lt;/h1&gt;

&lt;p&gt;Picocog emits text via Java code. No templates, no reflection, no tag processing. It has three core goals:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Generate cleanly indented code easily.&lt;/li&gt;
&lt;li&gt;Support out-of-sequence line injection.&lt;/li&gt;
&lt;li&gt;Be easy to debug.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Picocog in Practice
&lt;/h1&gt;

&lt;p&gt;Picocog can be thought of as a code-generation-centric alternative to StringBuilder.&lt;/p&gt;

&lt;p&gt;Picocog simply writes lines — the current indentation level is remembered.&lt;/p&gt;

&lt;p&gt;The only class that is required is the PicoWriter class.&lt;/p&gt;

&lt;p&gt;There is no need to manually place indents into string literals, but there is a need to flag the indent left and write lines of code via the writeln_r(), writeln_l() and writeln_lr() methods.&lt;/p&gt;

&lt;p&gt;Sometimes it is handy to want to write to two or more locations in a document simultaneously — and have those locations have their own indentation stack. To do this, you add a placeholder via the createDeferredWriter() method.&lt;/p&gt;

&lt;p&gt;The returned writer can be thought of as a pointer to the line on which it was created.&lt;/p&gt;

&lt;h1&gt;
  
  
  Sample Usage of Picocog
&lt;/h1&gt;

&lt;p&gt;The code generation source code:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--r6c2RS8V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/dllnl00q0uflpb357ugg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--r6c2RS8V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/dllnl00q0uflpb357ugg.png" alt="The code generation source code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Generated Classes
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;Customer.java (generated by Picocog):&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1a6iJ0Ma--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/kfcgwhgo76isdiijei9y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1a6iJ0Ma--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/kfcgwhgo76isdiijei9y.png" alt="Customer.java"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Order.java (generated by Picocog):&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nE3Gczd5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/n97rdwhv345bb0p5n8rk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nE3Gczd5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/n97rdwhv345bb0p5n8rk.png" alt="Order.java"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Escaping
&lt;/h1&gt;

&lt;p&gt;The initial release of Picocog does not support any kind of textual escaping. Either write your own escaping code (quite easy) or leverage a library such &lt;a href="https://commons.apache.org/proper/commons-lang/"&gt;Commons Lang&lt;/a&gt; for your &lt;a href="https://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/StringEscapeUtils.html"&gt;text escaping needs&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Language Support
&lt;/h1&gt;

&lt;p&gt;Picocog is written in Java, but it can &lt;strong&gt;emit code in any language&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A code generator can be created such that C#, Java, JavaScript, and more may be emitted from a single model, including annotations attached to Java code (via JSR 269).&lt;/p&gt;

&lt;p&gt;Sometimes a small implementation is all you need.&lt;/p&gt;

&lt;h1&gt;
  
  
  Links
&lt;/h1&gt;

&lt;p&gt;Picocog is available today on &lt;a href="https://github.com/ainslec/picocog"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Another non-template based approach for code generation is the excellent &lt;a href="https://www.eclipse.org/xtend/"&gt;xtend&lt;/a&gt; language.&lt;/p&gt;

&lt;p&gt;For a more in depth discussion of code generation, see the excellent article “&lt;a href="http://www.methodsandtools.com/archive/archive.php?id=86"&gt;Code Generation For Dummies&lt;/a&gt;” by Matthew Fowler.&lt;/p&gt;

&lt;p&gt;NOTE: This article is adapted (by the same author), from the following &lt;a href="https://medium.com/p/introducing-picocog-68de4978eaf4"&gt;blog post&lt;/a&gt; on medium.com&lt;/p&gt;

</description>
      <category>java</category>
      <category>meta</category>
      <category>dsl</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
