dataj - simple data classes for Java 9
dataj is a library (an annotation processor to be exact) which helps to generate data classes for Java. The zen of dataj is: no magic, 100% compatibility, and full control.
What is a data class?
A data class is basically a Java class with defined getters, setters and methods to check object equality (equals
and hashCode
). It is quite common to have this combination of methods for POJO (Plain Old Java Object), DTOs (Data Transfer Objects) and data entities. All these classes can be called data classes.
Quickstart
Gradle
In build.gradle
add a new dependency:
compile 'org.dataj:dataj-core:0.1'
Maven
In pom.xml
add a new dependency:
<dependency>
<groupId>org.dataj</groupId>
<artifactId>dataj-core</artifactId>
<version>0.1</version>
</dependency>
Intellij IDEA
If you want to get full IDE support from IDEA you need to enable Annotation processing. Open settings and in Build, Execution, Deployment > Compiler > Annotation Processors enable annotation processing for the project. Also you can specify what folders should annotation processor use to put generated sources to. This folder should then be marked as Generated sources root to enable compilation and all IDE features.
FAQ
How does it work?
dataj is an annotation processor so it kicks in on annotation processing phase of building your project. It scans for all classes annotated with @Data
annotation and generates a data class for it in the same package. Then you can use generated class instead of the original class wherever you need. Consider it to be a drop-in replacement. You can even commit generated class into your source control system, if you'd like.
The original class remains in place, however it is only used as a blueprint to generated a data class. The generated class does not reference the original class in any way.
Where is generated source code stored?
Depends from the configuration. In Intellij IDEA you can configure where the files will go - source root or output folder. You can use any, just make sure target folder is marked as Generated Sources Root in your module configuration.
What is the name of the generated class?
It is <class annotated with @Data>Data
. E.g. for class
@Data
class Person {
...fields
}
dataj will generate class
final class PersonData {
...constructor
...getters
...setters
...equals
...hashcode
}
with getters, setters, equality check (equals and hashcode) methods.
Can I edit generated code?
You probably don't want to do that as it will be re-generated on next compilation.
Is it compatible with Java 9?
Yes, generated code 100% compatible with Java 9.
Why is generated class final
?
This is generally considered to be not very good idea. If you need to extend it - probably this is not a simple DTO.
Why not to use Lombok?
Lombok is truly amazing annotation processor which helps bring a lot of new and cool features into Java. Data classes is just one of them. However, it does quite a lot of hacking of Java compilation process and tries to change generated bytecode on the fly. Also this requires support from IDE, which is although provided by Lombok team. Because of quite a complex implementation and deep integration with Java compiler it is quite tricky to update it to support Java 9.
So generally if you are using Lombok and you are happy with this - you probably don't need dataj. However, if you are looking for something very simple, lightweight and compatible with Java 9 - you might be interested in dataj.
How does it work?
Following the zen, basic principle of dataj is "Do not hack the compiler, generate the code". Generated code is easy to review, it can be committed to your VCS, or generated on the fly - up to you. Because it uses simple Java annotation processing there is no need in extra plugins for any of the build systems - Gradle and Maven work with it out of the box. Similar for IDEs, annotation processing and working with generated code supported by all major IDEs - Intellij IDEA, Eclipse and NetBeans. No need to install plugins or use special tools!
Compatibility
dataj supports Java 7, 8 and 9.