The latest release version is 0.1.0 and is available on Maven Central at the following coordinates:
groupId: me.shils, artifactId: morphia-type-checker, version: 0.1.0
Within the Morphia library, there are many places where String
objects are used to refer to a field of a class. These usages aren't normally validated by the compiler; for example, the following code compiles without any indication that the User
class doesn't have a userd
field:
import org.mongodb.morphia.annotations.*
import groovy.transform.CompileStatic
import org.bson.types.ObjectId
@Entity
@Indexes(@Index(fields = @Field('userd')))
@CompileStatic
class User {
@Id
ObjectId id
int userId
}
The error would be discovered at runtime, hopefully before it reached a production environment. If only the compiler knew that 'userd'
was supposed to be the name of a field of the User
class...
Fortunately, using Groovy type checking extensions, we can make the compiler aware of what we already know. If we apply the EntityTypeCheckingExtension
to our User
class,
@Entity
@Indexes(@Index(fields = @Field('userd')))
@CompileStatic(extensions = 'me.shils.morphia.EntityTypeCheckingExtension')
class User {
@Id
ObjectId id
int userId
}
our code will fail compilation with the following error:
[Static type checking] - No such persisted field: userd for class: User
To use Morphia Type Checker, you'll first need it on your compiler classpath. You can add it as a dependency with Maven or Gradle, or build the jar from source if you prefer.
You must also be using (at the minimum):
Java: 7
Groovy: 2.4.0
Morphia: 0.107
The easiest way to validate your Morphia Entities (classes annotated with @Entity
) and DAOs (classes that implement the DAO
interface) at compile time is by using a config script. Just add an instance of MorphiaTypeCheckingCustomizer
to the bound configuration
variable and the appropriate type checking extensions will be applied to your Entities and DAOs:
import me.shils.morphia.MorphiaTypeCheckingCustomizer
configuration.addCompilationCustomizers(new MorphiaTypeCheckingCustomizer())
Alternatively, you can add the appropriate extensions to your statically type checked classes yourself - EntityTypeCheckingExtension
for Entities, and DAOTypeCheckingExtension
for DAOs.
The following type checking extensions are used to validate the usage of strings referencing fields in different contexts.
When applied to DAOs, this extension validates:
- arguments to
Query
methodscriteria
,field
,filter
, andorder
- arguments to
UpdateOperations
methodsadd
,addAll
,dec
,inc
,max
,min
,removeAll
,removeFirst
,removeLast
,set
,setOnInsert
, andunset
It is assumed that queries and updates within the DAO target the DAO's entity class.
When applied to Entities, this extension validates:
value
members of@Index
and@Field
<dependency>
<groupId>me.shils</groupId>
<artifactId>morphia-type-checker</artifactId>
<version>0.1.0</version>
</dependency>
dependencies {
compile 'me.shils:morphia-type-checker:0.1.0'
}
To build from source, just clone this repo and run ./gradlew build
from the root directory of the project.