How does RoomEx work
The compilation of a Java project consists out of 3 phases:
- Parse and enter
- Annotation Processing
- Analyse and Generate
Just like Room itself, RoomEx does most of its work during the "Annotation Processing" phase. During this phase, the annotation processor of RoomEx checks for the following annotations:
@OneToOneEx,@OneToManyEx,@ManyToOneEx,@ManyToManyEx@DaoEx@AutoDao@RoomExDb
For every found occurrence of these annotations, RoomEx first does some validation checks. A @OneToOneEx annotation, for example, can only be used on specific properties inside a class annotated with @Entity. Should these succeed, the code generation phase will be invoked.
The annotation processing usually consists out of multiple rounds. In the initial round, all the source code from your project will be analyzed and processed. Any new file generated in the current round will be processed in a subsequent annotation processing round. This is repeated until no new files have been created.
RoomEx annotation processors
RoomEx consists out of the following annotation processors:
OneToOneExValidationProcessorOneToManyExValidationProcessorManyToOneExValidationProcessorManyToManyExValidationProcessorCollectorProcessorEntityProcessorAutoDaoProcessorDaoExProcessorRoomExDbProcessor
Relation validation processors
The relation validation processors (OneToOneExValidationProcessor, OneToManyExValidationProcessor, ManyToOneExValidationProcessor,ManyToManyExValidationProcessor) ...
CollectorProcessor
The CollectorProcessor neither generate code, nor does it run any validation checks. Its only purpose is it to collect some classes which are needed by multiple other processors.
It collects all entities, Dao's, which Dao belongs to which entity, and how to resolve any entity interface.
EntityProcessor
The EntityProcessor iterates through all the found entity classes in your project and checks for relationships. For any found relationship, a new entity will be created, which sole purpose it is to store the ids of entities which share a relation.
AutoDaoProcessor
The AutoDaoProcessor iterates through all the found entity classes annotated with @AutoDao. For each of these entities, a new DaoEx class will be generated, which extends the ICrud<T> interface. These generated DaoEx classes will then be processed by the DaoExProcessor.
DaoExProcessor
The 'DaoExProcessor' is the biggest processor which implements all the needed functionalities the operate with relations. It iterates over every found class with the @DaoEx annotation and executes the following steps:
Steps:
- Create new Dao class
- Add the Room
@Daoannotation to the new class - Set the DaoEx class as base class
- Add the database constructor argument, so that we retrieve the database from Room
- Implement the methods from the
IRoomExInternalDao<T>and theIRoomExInternalDaoRawQueries<T>interfaces - Implement methods from the
RoomExDaobase class, e.g. reading from and writing to an entity without reflection - Implement methods defined in the
ICreate<T>interface (only if the found DaoEx class inherits it) - Implement methods defined in the
IRead<T>interface (only if the found DaoEx class inherits it) - Implement methods defined in the
IUpdate<T>interface (only if the found DaoEx class inherits it) - Implement methods defined in the
IDelete<T>interface (only if the found DaoEx class inherits it) - Implement methods defined in the
IJoins<T>interface (only if the found DaoEx class inherits it) - Implement the
relationFieldsproperty, which contains all information about the relations of that entity - Inherit the
IRoomExInternalDaoRawQueries<T>interface - Generate the new source file
RoomExDbProcessor
The RoomExDbProcessor will only start its processing phase, when no new file has been generated by any of the other annotation processors during the current round of annotation processing.
Steps:
- Create new RoomExDatabase class
- Implement custom database class if one exists
- Add the Room
@Databaseannotation and set arguments - Implement the fetchDaoForEntity method
- Add the
RoomDatabasebase class - Generate the new source file