DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • While Performing Dependency Selection, I Avoid the Loss Of Sleep From Node.js Libraries' Dangers
  • Understanding ldd: The Linux Dynamic Dependency Explorer
  • Practical Use of Weak Symbols
  • Python and Open-Source Libraries for Efficient PDF Management

Trending

  • Manual Sharding in PostgreSQL: A Step-by-Step Implementation Guide
  • How to Merge HTML Documents in Java
  • Agile’s Quarter-Century Crisis
  • Metrics at a Glance for Production Clusters

Introducing Picocog: A Lightweight Code Generation Library

Take a look at Picocog, a new open source library designed to make code generation easy, support out-of-sequence line injection, and be simple to debug.

By 
Chris Ainsley user avatar
Chris Ainsley
·
Updated Jan. 17, 20 · News
Likes (10)
Comment
Save
Tweet
Share
56.9K Views

Join the DZone community and get the full member experience.

Join For Free

Picocog is a lightweight and new open-source Java library created to make programmatic code generation quite easy.

Code generation is the process by which source code can be automatically generated based on inputs.

Code generation typically works as follows:

  • Define a model/use an existing model.
  • Interrogate model and generate source-code
  • Upon changes to the model, regenerate source-code.

Plain Old Java Objects

A code generator designed to leverage Picocog typically utilizes a POJO model as a source of model data.

A sample POJO model is shown below (omitting getters/setters for brevity).

A sample POJO model

This POJO model is typically instantiated from the following types of sources:

  • JSON/XML/YAML/CSV
  • Via database query
  • DSL (domain specific languages)

For example:

XML representation of the model

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.

Code Generation

Picocog emits text via Java code. No templates, no reflection, no tag processing. It has three core goals:

  • Generate cleanly indented code easily.
  • Support out-of-sequence line injection.
  • Be easy to debug.

Picocog in Practice

Picocog can be thought of as a code-generation-centric alternative to StringBuilder. Picocog simply writes lines — the current indentation level is remembered.

The only class that is required is the PicoWriter class.

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.

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. The returned writer can be thought of as a pointer to the line on which it was created.

Sample Usage of Picocog

The code generation source code:

Generated Classes

Customer.java (generated by Picocog):


Order.java (generated by Picocog):

Escaping

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 Commons Lang for your text escaping needs.

Language Support

Picocog is written in Java, but it can emit code in any language. 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).

Sometimes a small implementation is all you need.

Another non-template based approach for code generation is the excellent xtend language.

For a more in depth discussion of code generation, see the excellent article “Code Generation For Dummies” by Matthew Fowler.

Availability


Picocog is available today on GitHub.

Picocog is available on Maven Central at the following coordinates (jarfile can be downloaded here too):


XML




x


 
1
   <dependency>
2
      <groupId>org.ainslec</groupId>
3
      <artifactId>picocog</artifactId>
4
      <version>1.0.2</version>
5
   </dependency>



Library

Published at DZone with permission of Chris Ainsley. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • While Performing Dependency Selection, I Avoid the Loss Of Sleep From Node.js Libraries' Dangers
  • Understanding ldd: The Linux Dynamic Dependency Explorer
  • Practical Use of Weak Symbols
  • Python and Open-Source Libraries for Efficient PDF Management

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!