-
Notifications
You must be signed in to change notification settings - Fork 19
How does it work ?
The main idea is to create a reflection compatible API using annotation processing and byte code weaving.
Annotation processing is used to create an "Annotation Database" : a Java class that contains all the information related to :
- the fields using a given annotation in a given class
- the methods using a given annotation in a given class
- the constructors using a given annotation in a given class
It also creates implementations of Field for every annotation field.
Right after annotation processing, the fields contained in the annotation database are almost working correctly, they have the exact same methods as a pure Java core reflection Field. But a few methods still don't work at this stage :
- set and all other setters (like setInt, setFloat, etc)
- get and all other getters (like getInt, getFloat, etc).
Those methods can't compile because they need some way to set/get the fields of the annotated Pojos, but at this stage they can still be private or protected, and can't be accessed directly.
The byte code weaving step will modify the byte code of the Java classes being compiled. The weaver will apply a strategy to make the fields accessible. There a few possibilities to do so :
- make all fields public
- add public methods to access the fields (read & write accesses)
- make the fields protected and create inner classes that can access the fields.
We currently use the second strategy as the first one would leak public fields in the API of the classes being weaved, and the third one is not possible with Javassist (it cannot manipulate inner classes).
Once Java classes have been weaved, the annotation database's fields can now be compiled (against the weaved byte code of the classes to be compiled).
The weaved classes and the annotation databases are not passed to dex and will not be part of the final APK.