-
-
Notifications
You must be signed in to change notification settings - Fork 367
Automatic table configuration via Annotation Processing #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
NCrouther
wants to merge
26
commits into
j256:main
Choose a base branch
from
NCrouther:AnnotationProcessing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
8f83855
Add skeleton code for automatic annotation processing
NCrouther 0bb58f9
Make a separate file for each table
NCrouther d7e5488
Handle ForeignCollectionField annotations
NCrouther 44fc71a
Add automated unit tests of annotation parsing
NCrouther d2fd0da
Add @Generated annotation to outputs
NCrouther e95821c
Revert "Add @Generated annotation to outputs"
NCrouther 20f08c1
Support non public classes
NCrouther 0a60583
Add processing skeleton for DatabaseTables annotation
NCrouther 589ab1b
Add more consistency checks
NCrouther d0b5dbe
Fix crashes during incremental builds
NCrouther 3c90306
Generate file to cache table configurations
NCrouther 1ca7003
Fix spurious errors during incremental builds
NCrouther e2d2887
Add unit tests for errors
NCrouther a52ac52
Fix incremental compilation issues
NCrouther fb9e4d8
Add documentation
NCrouther 5e6adac
Deprecate configuration file code
NCrouther bfd65be
Move processor from annotations to processor package
NCrouther 569b041
Refactor Model inner classes into top level Bindings classes
NCrouther 275a47a
Add examples to Database annotation javadoc
NCrouther a327c0f
Improve exception handling
NCrouther 353c004
Remove static qualifier from helper functions
NCrouther 3225a13
Change package declaration in unit test files
NCrouther 6c14b03
Add documentation to writeSetterIfNotDefault
NCrouther 9504f25
Update pom to new processor class path
NCrouther fb1cebd
Fix expected results files to use new packages
NCrouther 5d2186c
Use JavaPoet to create source files
NCrouther File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
src/main/java/com/j256/ormlite/android/annotations/Database.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package com.j256.ormlite.android.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* This class is applied to a class derived from | ||
* {@link com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper} and lists | ||
* the classes representing the tables in the database. Compile-time annotation | ||
* processing will generate a method that can be called from your constructor to | ||
* cache table information to avoid slow reflection on Android. This | ||
* functionality replaces table configuration files which achieved the same goal | ||
* by manually creating a text file at build time and parsing it at runtime. | ||
* | ||
* Add a call to YOUR_CLASS_NAME_TableConfig.cacheTableConfigurations() to your | ||
* constructor to make use of this functionality. You can also call | ||
* YOUR_CLASS_NAME_TableConfig.createTables(connectionSource) from your onCreate | ||
* to create all tables included in this annotation. | ||
* | ||
* For inner/nested classes, the generated class name will use underscores to | ||
* separate the classes (e.g. package.Outer.Inner will result in | ||
* package.Outer_Inner_TableConfig begin generated). | ||
* | ||
* <p> | ||
* Example (Table.java) | ||
* </p> | ||
* | ||
* <p> | ||
* <blockquote> | ||
* | ||
* <pre> | ||
* @DatabaseTable | ||
* class Table { | ||
* @DatabaseField | ||
* int field; | ||
* } | ||
* </pre> | ||
* | ||
* </blockquote> | ||
* </p> | ||
* | ||
* <p> | ||
* Example (Outer.java) | ||
* </p> | ||
* | ||
* <p> | ||
* <blockquote> | ||
* | ||
* <pre> | ||
* class Outer { | ||
* @DatabaseTable | ||
* class Inner { | ||
* @DatabaseField | ||
* int field; | ||
* } | ||
* } | ||
* </pre> | ||
* | ||
* </blockquote> | ||
* </p> | ||
* | ||
* <p> | ||
* Example (OpenHelper.java) | ||
* </p> | ||
* | ||
* <p> | ||
* <blockquote> | ||
* | ||
* <pre> | ||
* @Database({ Table.class, Outer.Inner.class }) | ||
* class OpenHelper extends OrmLiteSqliteOpenHelper { | ||
* OpenHelper(Context context, String databaseName, CursorFactory factory, | ||
* int databaseVersion) { | ||
* super(context, databaseName, factory, databaseVersion); | ||
* OpenHelper_TableConfig.cacheTableConfigurations(); | ||
* } | ||
* | ||
* @Override | ||
* public void onCreate(SQLiteDatabase database, | ||
* ConnectionSource connectionSource) { | ||
* try { | ||
* OpenHelper_TableConfig.createTables(connectionSource); | ||
* } catch (SQLException e) { | ||
* throw new RuntimeException(e); | ||
* } | ||
* } | ||
* } | ||
* </pre> | ||
* | ||
* </blockquote> | ||
* </p> | ||
* | ||
* * | ||
* <p> | ||
* Example (InnerOpenHelper.java) | ||
* </p> | ||
* | ||
* <p> | ||
* <blockquote> | ||
* | ||
* <pre> | ||
* class Outer { | ||
* @Database({ Table.class, Outer.Inner.class }) | ||
* class OpenHelper extends OrmLiteSqliteOpenHelper { | ||
* OpenHelper(Context context, String databaseName, CursorFactory factory, | ||
* int databaseVersion) { | ||
* super(context, databaseName, factory, databaseVersion); | ||
* Outer_OpenHelper_TableConfig.cacheTableConfigurations(); | ||
* } | ||
* | ||
* @Override | ||
* public void onCreate(SQLiteDatabase database, | ||
* ConnectionSource connectionSource) { | ||
* try { | ||
* Outer_OpenHelper_TableConfig.createTables(connectionSource); | ||
* } catch (SQLException e) { | ||
* throw new RuntimeException(e); | ||
* } | ||
* } | ||
* } | ||
* } | ||
* </pre> | ||
* | ||
* </blockquote> | ||
* </p> | ||
* | ||
* @author nathancrouther | ||
*/ | ||
@Retention(RetentionPolicy.SOURCE) | ||
@Target(ElementType.TYPE) | ||
public @interface Database { | ||
Class<?>[] value(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/java/com/j256/ormlite/android/processor/FieldBindings.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.j256.ormlite.android.processor; | ||
|
||
import java.util.List; | ||
|
||
class FieldBindings { | ||
private final String fieldName; | ||
private final List<SetterBindings> setters; | ||
|
||
FieldBindings(String fieldName, List<SetterBindings> setters) { | ||
this.fieldName = fieldName; | ||
this.setters = setters; | ||
} | ||
|
||
String getFieldName() { | ||
return fieldName; | ||
} | ||
|
||
List<SetterBindings> getSetters() { | ||
return setters; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good Javadoc but please add a
<pre>
block with code examples.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, here I am not sure I would use annotations more than a programmatic API to declare classes. Are both ways still supported ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The annotation processor requires all of the table classes for the database to be declared in an annotation so that it can generate the code to cache the table configurations with the DaoManager. I wanted to avoid the user needing to list the tables twice (in their onCreate [as before] and in the newly added Database annotation), so I added an autogenerated function that can be used to help implement onCreate so that the list of tables only appears once.
Old code will continue to work unchanged, but it won't get the benefit of increased performance that the annotation processors provide. The annotation processor will raise a warning in this case to encourage the user to add the annotation.
The alternative would be to not generate a class that collects all of the individual TableConfigs and just have the user register each of them with the DaoManager in their constructor body. I didn't like this approach since they would need to call a generated class function for each table in addition to the existing create call for each table. Since from an IDE point of view the generated classes aren't connected to the DatabaseTable classes, it would add burden to refactorings (e.g. renaming table classes).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added some code examples to flesh out the javadoc: 275a47a.