Customization

The construction of a CompilationUnit is a process which sets up an object tree such that each object in the tree represents a syntactic component in the Java Language. Each object "knows" how to render itself as text. Most of this hardcoded in the implementation (com.inxar.jenesis.*). There are two things that you can customize, however: curly brace formatting and the sorting of members in a class.

Curly Brace Formatting

Whether you place an opening brace on the same line as an "if" statement on on the next line, chances are you are very opinionated about it. With Jenesis, you can control the way these braces look on a construct-by-construct level.

When the VirtualMachine starts up, it reads a properties file called com/inxar/jenesis/blockstyle.properties. This file is located in the libjenesis jar file which is in the classpath and is used to configure the way curly braces are used.

Each syntactic object which uses curly braces (like class declarations, while statements, method declarations, etc...) delegates curly brace writing to another object which implements the interface com.inxar.jenesis.util.BlockStyle. There are currently three BlockStyle implementations:

style.standard = com.inxar.jenesis.MStyle$Standard
style.optional = com.inxar.jenesis.MStyle$Optional
style.sameline = com.inxar.jenesis.MStyle$SameLine

You can get a detailed idea of what each one of these does by looking at the sourcecode.

The blockstyle.properties file (or your customized version) sets up a table which contains BlockStyle implementations keyed by name. The first part of the file names the class to be used for a particular BlockStyle implementation and binds that to a name such that it can be referred to later in the file. Therefore, the implementation com.inxar.jenesis.MStyle$SameLine is bound to the name "style.sameline".

Subsequent entries in the properties file make the association between these BlockStyle implementations and various syntactic components.

interface = style.standard
if = style.sameline

Therefore, an "if" statement will delegate curly brace formatting to an object belonging to the class MStyle$SameLine.

You can customize this by changing the assocations between blockstyle implementations OR by writing your own BlockStyle implementation. Refer to the current implementations as a guide to write your own. Then add a line similar to the following in the blockstyle.properties file:

style.my-standard = com.example.MyBlockStyle

And then refer to it later, like so:

interface = style.my-standard

To make the VirtualMachine read your customized blockstyles file rather than the default one, you need to pass a system property to java (the real virtual machine) telling it where this file is located. For example:

$ java -Djenesis.blocktyle-properties-filename=/tmp/mystyles.properties

Member Sorting

Along with curly braces, the location of certain components within a class definition can be highly personal. Some people like to put the "main" method at the beginning of the class, others like to put it at the bottom. Many people put "static final" field members at the top of the class. This is the idea of "member sorting" (where "member" is defined as the fields and methods within type declarations (interfaces and classes)).

To customize the placement of members within a class, you need to implement an object which implements java.util.Comparator. The default comparator if one is not otherwise specified is com.inxar.jenesis.util.MemberComparator. Use this as a guide to implement your own.

To actually force the use of your customized comparator, you need to pass a system property to the JVM upon startup using the "-D" option of "bin/java". Specify the name of your class like so:

$ java -Djenesis.member-comparator-classname=com.example.MyComparator

Beyond that, you can change anything else by modifying the source code (cause hey, what's open source for anyway?). Please let me know if you have some good suggestions such that they may be included in future releases from Inxar. Have fun!