-
Notifications
You must be signed in to change notification settings - Fork 8
Java Source Code Generation
Messages declared in Helium spec can be transformed to Java classes with EntitiesGenerator
.
This handler create a separate Java class for each message defined in specification.
Message fields are turned into class fields. Sequences are represented with class fields of Collection type.
There is a set of generation options that can be set with EntitiesGeneratorOptions
:
// set package name for generated classes
options.setPackageName("com.example");
// define modifiers to be applied to class fields declarations
options.setFieldModifiers(EnumSet.of(Modifier.PRIVATE));
// generate setter methods
options.setAddSetters(true);
// generate getter methods
options.setAddGetters(true);
// set collection type to represent sequences (default is List)
options.setSequenceCollectionName("java.util.ArrayList") // will make ArrayList<SomeType> field;
// or use arrays instead
options.useArraysForSequences(); // will make SomeType[] field
// set mapping of custom primitive types to Java classes
options.setCustomPrimitivesMapping(Collections.singletonMap("timestamp", "java.util.Date"));
// ask to prettify Java field names (will make fieldName in Java class from field_name in spec)
options.setPrettifyNames(true);
Default behaviour of the handler is to create a POJO classes. However it's possible to add additional
features to generated classes specifying class writer wrapper.
Available wrappers can be found in Writers
class:
-
gson()
- will add Google Gson's@SerializedName
annotations to generated fields -
androidParcelable()
- will make generated classes implementandroid.os.Parcelable
and generate required methods -
chain()
- will allow to combine multiple wrappers (likechain(gson(), androidParcelable())
, which will both add Gson annotations and implement Android'sParcelable
)
Wrapper can be set in generator options:
import static com.stanfy.helium.handler.codegen.java.entity.Writers.*;
options.setWriterWrapper(chain(gson(), androidParcelable()));
Entity generator handler can be used as follows:
helium.processBy(new EntitiesGenerator(new File("output"), options));
Handler JavaConstantsGenerator
can generate classes with defined message field names as constants.
Considering a message in a spec
type 'Book' message {
title 'string'
annotation 'string'
author 'string'
}
the handler can generate the following class:
public class BookConstants {
public static final String TITLE = "title";
public static final String ANNOTATION = "annotation";
public static final String AUTHOR = "author";
}
You may also customize how to generate constant names with handler options (ConstantsGeneratorOptions
):
options.setPackageName("com.example");
options.setNameConverter(new ConstantNameConverter() {
@Override
public String constantFrom(final Field field) {
return "COLUMN_" + field.getCanonicalName().toUpperCase(Locale.US);
}
});
Then generated class will be:
public class BookConstants {
public static final String COLUMN_TITLE = "title";
public static final String COLUMN_ANNOTATION = "annotation";
public static final String COLUMN_AUTHOR = "author";
}