Skip to content

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
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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 Apr 24, 2015
0bb58f9
Make a separate file for each table
NCrouther Apr 28, 2015
d7e5488
Handle ForeignCollectionField annotations
NCrouther May 1, 2015
44fc71a
Add automated unit tests of annotation parsing
NCrouther May 1, 2015
d2fd0da
Add @Generated annotation to outputs
NCrouther May 1, 2015
e95821c
Revert "Add @Generated annotation to outputs"
NCrouther May 2, 2015
20f08c1
Support non public classes
NCrouther May 8, 2015
0a60583
Add processing skeleton for DatabaseTables annotation
NCrouther May 8, 2015
589ab1b
Add more consistency checks
NCrouther May 8, 2015
d0b5dbe
Fix crashes during incremental builds
NCrouther May 8, 2015
3c90306
Generate file to cache table configurations
NCrouther May 8, 2015
1ca7003
Fix spurious errors during incremental builds
NCrouther May 8, 2015
e2d2887
Add unit tests for errors
NCrouther May 9, 2015
a52ac52
Fix incremental compilation issues
NCrouther May 9, 2015
fb9e4d8
Add documentation
NCrouther May 9, 2015
5e6adac
Deprecate configuration file code
NCrouther May 9, 2015
bfd65be
Move processor from annotations to processor package
NCrouther May 12, 2015
569b041
Refactor Model inner classes into top level Bindings classes
NCrouther May 12, 2015
275a47a
Add examples to Database annotation javadoc
NCrouther May 12, 2015
a327c0f
Improve exception handling
NCrouther May 12, 2015
353c004
Remove static qualifier from helper functions
NCrouther May 12, 2015
3225a13
Change package declaration in unit test files
NCrouther May 12, 2015
6c14b03
Add documentation to writeSetterIfNotDefault
NCrouther May 12, 2015
9504f25
Update pom to new processor class path
NCrouther May 12, 2015
fb1cebd
Fix expected results files to use new packages
NCrouther May 12, 2015
5d2186c
Use JavaPoet to create source files
NCrouther May 13, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,24 @@
<source>1.5</source>
<target>1.5</target>
</configuration>
<executions>
<execution>
<id>default-compile</id>
<configuration>
<compilerArgument>-proc:none</compilerArgument>
<includes>
<include>com/j256/ormlite/android/processor/OrmLiteAnnotationProcessor.java</include>
</includes>
</configuration>
</execution>
<execution>
<id>compile-everything-else</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down Expand Up @@ -248,6 +266,33 @@
<value>$1${version}$2</value>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>com.squareup:javapoet</include>
</includes>
</artifactSet>
<minimizeJar>true</minimizeJar>
<relocations>
<relocation>
<pattern>com.squareup.javapoet</pattern>
<shadedPattern>com.j256.ormlite.android.processor.javapoet</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
Expand Down Expand Up @@ -330,6 +375,11 @@
<version>${android-support-version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.squareup</groupId>
<artifactId>javapoet</artifactId>
<version>1.0.0</version>
</dependency>

<!-- =================================================================== -->
<!-- test dependencies -->
Expand Down Expand Up @@ -386,5 +436,11 @@
<version>${easymock-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.testing.compile</groupId>
<artifactId>compile-testing</artifactId>
<version>0.6</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
135 changes: 135 additions & 0 deletions src/main/java/com/j256/ormlite/android/annotations/Database.java
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>
* &#064;DatabaseTable
* class Table {
* &#064;DatabaseField
* int field;
* }
* </pre>
*
* </blockquote>
* </p>
*
* <p>
* Example (Outer.java)
* </p>
*
* <p>
* <blockquote>
*
* <pre>
* class Outer {
* &#064;DatabaseTable
* class Inner {
* &#064;DatabaseField
* int field;
* }
* }
* </pre>
*
* </blockquote>
* </p>
*
* <p>
* Example (OpenHelper.java)
* </p>
*
* <p>
* <blockquote>
*
* <pre>
* &#064;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();
* }
*
* &#064;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 {
* &#064;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();
* }
*
* &#064;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)

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.

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 ?

Copy link
Contributor Author

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 ?

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).

Copy link
Contributor Author

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.

@Target(ElementType.TYPE)
public @interface Database {
Class<?>[] value();
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@
* </p>
*
* @author graywatson
*
* @deprecated As of version 4.49 configuration files have been replaced by
* automatic annotation processing at compile time.
* @see com.j256.ormlite.android.annotations.Database
*/
@Deprecated
public class OrmLiteConfigUtil {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,14 @@ public OrmLiteSqliteOpenHelper(Context context, String databaseName, CursorFacto
* called if the stored database is a different version.
* @param configFileId
* file-id which probably should be a R.raw.ormlite_config.txt or some static value.
*
* @deprecated As of version 4.49 configuration files have been replaced by
* automatic annotation processing at compile time. Add an
* {@link com.j256.ormlite.android.annotations.Database}
* annotation to the class that inherits from this class to
* activate annotation processing.
*/
@Deprecated
public OrmLiteSqliteOpenHelper(Context context, String databaseName, CursorFactory factory, int databaseVersion,
int configFileId) {
this(context, databaseName, factory, databaseVersion, openFileId(context, configFileId));
Expand All @@ -90,7 +97,14 @@ public OrmLiteSqliteOpenHelper(Context context, String databaseName, CursorFacto
* called if the stored database is a different version.
* @param configFile
* Configuration file to be loaded.
*
* @deprecated As of version 4.49 configuration files have been replaced by
* automatic annotation processing at compile time. Add an
* {@link com.j256.ormlite.android.annotations.Database}
* annotation to the class that inherits from this class to
* activate annotation processing.
*/
@Deprecated
public OrmLiteSqliteOpenHelper(Context context, String databaseName, CursorFactory factory, int databaseVersion,
File configFile) {
this(context, databaseName, factory, databaseVersion, openFile(configFile));
Expand All @@ -111,7 +125,14 @@ public OrmLiteSqliteOpenHelper(Context context, String databaseName, CursorFacto
* called if the stored database is a different version.
* @param stream
* Stream opened to the configuration file to be loaded. It will be closed when this method returns.
*
* @deprecated As of version 4.49 configuration files have been replaced by
* automatic annotation processing at compile time. Add an
* {@link com.j256.ormlite.android.annotations.Database}
* annotation to the class that inherits from this class to
* activate annotation processing.
*/
@Deprecated
public OrmLiteSqliteOpenHelper(Context context, String databaseName, CursorFactory factory, int databaseVersion,
InputStream stream) {
super(context, databaseName, factory, databaseVersion);
Expand Down
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;
}
}
Loading