Skip to content

Commit

Permalink
keeping datastore types to avoid unnecessary reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
musketyr committed May 20, 2019
1 parent 7bbcd17 commit b320e5b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
16 changes: 16 additions & 0 deletions core/src/main/groovyx/gaelyk/datastore/DatastoreEntity.java
Expand Up @@ -100,13 +100,29 @@ public interface DatastoreEntity<K> {
*/
List<String> getDatastoreIndexedProperties();

/**
* Returns list of the types of properties which should be saved in the data store with the index.
* This method should return same values for each instance. It cannot be static because of Java interface restrictions.
*
* @return list of the types of properties which should be saved in the data store with the index
*/
List<Class> getDatastoreIndexedPropertiesTypes();

/**
* Returns list of the names of properties which should be saved in the data store unindexed.
* This method should return same values for each instance. It cannot be static because of Java interface restrictions.
*
* @return list of the names of properties which should be saved in the data store unindexed
*/
List<String> getDatastoreUnindexedProperties();

/**
* Returns list of the types of properties which should be saved in the data store unindexed.
* This method should return same values for each instance. It cannot be static because of Java interface restrictions.
*
* @return list of the types of properties which should be saved in the data store unindexed
*/
List<Class> getDatastoreUnindexedPropertiesTypes();

// taken from GroovyObject, for java compatibility. Groovy classes has these methods
// automagically
Expand Down
44 changes: 44 additions & 0 deletions core/src/main/groovyx/gaelyk/datastore/EntityTransformation.groovy
Expand Up @@ -361,7 +361,9 @@ class EntityTransformation extends AbstractASTTransformation {
boolean defaultIndexed = memberHasValue(anno, 'unindexed', false)

List<String> indexed = []
List<ClassNode> indexedTypes = []
List<String> unindexed = []
List<ClassNode> unindexedTypes = []

eachPropertyIncludingSuper(parent) { PropertyNode prop ->
if(Modifier.isStatic(prop.modifiers) || Modifier.isFinal(prop.modifiers)) {
Expand Down Expand Up @@ -389,12 +391,15 @@ class EntityTransformation extends AbstractASTTransformation {
}
if(hasIndexedAnno){
indexed << prop.name
indexedTypes << prop.type
return
}
if(defaultIndexed){
indexed << prop.name
indexedTypes << prop.type
} else {
unindexed << prop.name
unindexedTypes << prop.type
}
}

Expand Down Expand Up @@ -426,6 +431,35 @@ class EntityTransformation extends AbstractASTTransformation {
self.columnNumber = 1
self
}

parent.addField new FieldNode('DATASTORE_INDEXED_PROPERTIES_TYPES', Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL, getBoundListNode(ClassHelper.CLASS_Type), parent, buildClassList(indexedTypes))

parent.addMethod new MethodNode(
'getDatastoreIndexedPropertiesTypes',
Modifier.PUBLIC,
getBoundListNode(ClassHelper.CLASS_Type),
Parameter.EMPTY_ARRAY,
ClassNode.EMPTY_ARRAY,
new ReturnStatement(new VariableExpression('DATASTORE_INDEXED_PROPERTIES_TYPES'))
).with { MethodNode self ->
self.lineNumber = 10013
self.columnNumber = 1
self
}

parent.addField new FieldNode('DATASTORE_UNINDEXED_PROPERTIES_TYPES', Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL, getBoundListNode(ClassHelper.CLASS_Type), parent, buildClassList(unindexedTypes))
parent.addMethod new MethodNode(
'getDatastoreUnindexedPropertiesTypes',
Modifier.PUBLIC,
getBoundListNode(ClassHelper.CLASS_Type),
Parameter.EMPTY_ARRAY,
ClassNode.EMPTY_ARRAY,
new ReturnStatement(new VariableExpression('DATASTORE_UNINDEXED_PROPERTIES_TYPES'))
).with { MethodNode self ->
self.lineNumber = 10014
self.columnNumber = 1
self
}
}

private void eachPropertyIncludingSuper(ClassNode parent, Closure iterator, List<String> alreadyProcessed = []){
Expand Down Expand Up @@ -457,6 +491,16 @@ class EntityTransformation extends AbstractASTTransformation {
list
}


private Expression buildClassList(List<ClassNode> values) {
ListExpression list = new ListExpression()
for (ClassNode value in values) {
// I'm not sure if ClassExpression is the best one
list.addExpression(new ClassExpression(value))
}
list
}

private MethodNode addDelegatedMethod(String name, ClassNode returnType = ClassHelper.DYNAMIC_TYPE) {
def helper = ClassHelper.make(EntityTransformationHelper).plainNodeReference

Expand Down

0 comments on commit b320e5b

Please sign in to comment.