Skip to content
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

Issue 274 mark generated code #275

Merged
merged 2 commits into from Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,6 +1,7 @@
package grails.gorm.hibernate

import groovy.transform.CompileStatic
import groovy.transform.Generated
import org.grails.datastore.gorm.GormEnhancer
import org.grails.datastore.gorm.GormEntity
import org.grails.orm.hibernate.AbstractHibernateGormStaticApi
Expand All @@ -21,6 +22,7 @@ trait HibernateEntity<D> extends GormEntity<D> {
*
* @return The object
*/
@Generated
static List<D> findAllWithSql(CharSequence sql) {
currentHibernateStaticApi().findAllWithSql sql, Collections.emptyMap()
}
Expand All @@ -31,6 +33,7 @@ trait HibernateEntity<D> extends GormEntity<D> {
* @param sql The sql query
* @return The entity
*/
@Generated
static D findWithSql(CharSequence sql) {
currentHibernateStaticApi().findWithSql(sql, Collections.emptyMap())
}
Expand All @@ -42,6 +45,7 @@ trait HibernateEntity<D> extends GormEntity<D> {
*
* @return The object
*/
@Generated
static List<D> findAllWithSql(CharSequence sql, Map args) {
currentHibernateStaticApi().findAllWithSql sql, args
}
Expand All @@ -52,10 +56,12 @@ trait HibernateEntity<D> extends GormEntity<D> {
* @param sql The sql query
* @return The entity
*/
@Generated
static D findWithSql(CharSequence sql, Map args) {
currentHibernateStaticApi().findWithSql(sql, args)
}

@Generated
private static AbstractHibernateGormStaticApi currentHibernateStaticApi() {
(AbstractHibernateGormStaticApi)GormEnhancer.findStaticApi(this)
}
Expand Down
Expand Up @@ -3,6 +3,7 @@ package org.grails.orm.hibernate.compiler
import grails.gorm.dirty.checking.DirtyCheckedProperty
import groovy.transform.CompilationUnitAware
import groovy.transform.CompileStatic
import org.apache.groovy.ast.tools.AnnotatedNodeUtils
import org.codehaus.groovy.ast.*
import org.codehaus.groovy.ast.stmt.BlockStatement
import org.codehaus.groovy.ast.stmt.IfStatement
Expand Down Expand Up @@ -124,6 +125,7 @@ class HibernateEntityTransformation implements ASTTransformation, CompilationUni
returnS(varX(interceptorField))
)
classNode.addMethod(getInterceptorMethod)
AnnotatedNodeUtils.markAsGenerated(classNode, getInterceptorMethod)
staticCompilationVisitor.visitMethod(getInterceptorMethod)

// add method: void $$_hibernate_setInterceptor(PersistentAttributeInterceptor interceptor)
Expand All @@ -137,6 +139,7 @@ class HibernateEntityTransformation implements ASTTransformation, CompilationUni
assignS( varX(interceptorField), varX(p1) )
)
classNode.addMethod(setInterceptorMethod)
AnnotatedNodeUtils.markAsGenerated(classNode, setInterceptorMethod)
staticCompilationVisitor.visitMethod(setInterceptorMethod)

// add method: Object $$_hibernate_getEntityInstance()
Expand All @@ -149,6 +152,7 @@ class HibernateEntityTransformation implements ASTTransformation, CompilationUni
returnS(varX("this"))
)
classNode.addMethod(getEntityInstanceMethod)
AnnotatedNodeUtils.markAsGenerated(classNode, getEntityInstanceMethod)
staticCompilationVisitor.visitMethod(getEntityInstanceMethod)


Expand All @@ -162,6 +166,7 @@ class HibernateEntityTransformation implements ASTTransformation, CompilationUni
returnS(varX(entityEntryHolderField))
)
classNode.addMethod(getEntityEntryMethod)
AnnotatedNodeUtils.markAsGenerated(classNode, getEntityEntryMethod)
staticCompilationVisitor.visitMethod(getEntityEntryMethod)

// add method: void $$_hibernate_setEntityEntry(EntityEntry entityEntry)
Expand All @@ -175,6 +180,7 @@ class HibernateEntityTransformation implements ASTTransformation, CompilationUni
assignS( varX(entityEntryHolderField), varX(entityEntryParam) )
)
classNode.addMethod(setEntityEntryMethod)
AnnotatedNodeUtils.markAsGenerated(classNode, setEntityEntryMethod)
staticCompilationVisitor.visitMethod(setEntityEntryMethod)

// add method: ManagedEntity $$_hibernate_getPreviousManagedEntity()
Expand All @@ -187,6 +193,7 @@ class HibernateEntityTransformation implements ASTTransformation, CompilationUni
returnS(varX(previousManagedEntityField))
)
classNode.addMethod(getPreviousManagedEntityMethod)
AnnotatedNodeUtils.markAsGenerated(classNode, getPreviousManagedEntityMethod)
staticCompilationVisitor.visitMethod(getPreviousManagedEntityMethod)

// add method: ManagedEntity $$_hibernate_getNextManagedEntity() {
Expand All @@ -199,6 +206,7 @@ class HibernateEntityTransformation implements ASTTransformation, CompilationUni
returnS(varX(nextManagedEntityField))
)
classNode.addMethod(getNextManagedEntityMethod)
AnnotatedNodeUtils.markAsGenerated(classNode, getNextManagedEntityMethod)
staticCompilationVisitor.visitMethod(getNextManagedEntityMethod)

// add method: void $$_hibernate_setPreviousManagedEntity(ManagedEntity previous)
Expand All @@ -212,6 +220,7 @@ class HibernateEntityTransformation implements ASTTransformation, CompilationUni
assignS( varX(previousManagedEntityField), varX(previousParam) )
)
classNode.addMethod(setPreviousManagedEntityMethod)
AnnotatedNodeUtils.markAsGenerated(classNode, setPreviousManagedEntityMethod)
staticCompilationVisitor.visitMethod(setPreviousManagedEntityMethod)

// add method: void $$_hibernate_setNextManagedEntity(ManagedEntity next)
Expand All @@ -225,6 +234,7 @@ class HibernateEntityTransformation implements ASTTransformation, CompilationUni
assignS( varX(nextManagedEntityField), varX(nextParam) )
)
classNode.addMethod(setNextManagedEntityMethod)
AnnotatedNodeUtils.markAsGenerated(classNode, setNextManagedEntityMethod)
staticCompilationVisitor.visitMethod(setNextManagedEntityMethod)

List<MethodNode> allMethods = classNode.getMethods()
Expand Down
@@ -0,0 +1,26 @@
package grails.gorm.tests

import grails.gorm.transactions.Rollback
import groovy.transform.Generated
import org.grails.orm.hibernate.HibernateDatastore
import org.springframework.transaction.PlatformTransactionManager
import spock.lang.AutoCleanup
import spock.lang.IgnoreIf
import spock.lang.Shared
import spock.lang.Specification

@Rollback
class HibernateEntityTraitGeneratedSpec extends Specification {

@Shared @AutoCleanup HibernateDatastore datastore = new HibernateDatastore(Club)

void "test that all HibernateEntity trait methods are marked as Generated"() {
// Unfortunately static methods have to check directly one by one
expect:
Club.getMethod('findAllWithSql', CharSequence).isAnnotationPresent(Generated)
Club.getMethod('findWithSql', CharSequence).isAnnotationPresent(Generated)
Club.getMethod('findAllWithSql', CharSequence, Map).isAnnotationPresent(Generated)
Club.getMethod('findWithSql', CharSequence, Map).isAnnotationPresent(Generated)
}

}
@@ -1,5 +1,7 @@
package org.grails.orm.hibernate.compiler

import groovy.transform.Generated
import org.hibernate.engine.spi.EntityEntry
import org.hibernate.engine.spi.ManagedEntity
import org.hibernate.engine.spi.PersistentAttributeInterceptable
import org.hibernate.engine.spi.PersistentAttributeInterceptor
Expand Down Expand Up @@ -153,6 +155,17 @@ class MyEntity {

then:"The value is changed"
myEntity.name == 'changed'

and: "by transformation added methods are all marked as Generated"
cls.getMethod('$$_hibernate_getInterceptor').isAnnotationPresent(Generated)
cls.getMethod('$$_hibernate_setInterceptor', PersistentAttributeInterceptor).isAnnotationPresent(Generated)
cls.getMethod('$$_hibernate_getEntityInstance').isAnnotationPresent(Generated)
cls.getMethod('$$_hibernate_getEntityEntry').isAnnotationPresent(Generated)
cls.getMethod('$$_hibernate_setEntityEntry', EntityEntry).isAnnotationPresent(Generated)
cls.getMethod('$$_hibernate_getPreviousManagedEntity').isAnnotationPresent(Generated)
cls.getMethod('$$_hibernate_getNextManagedEntity').isAnnotationPresent(Generated)
cls.getMethod('$$_hibernate_setPreviousManagedEntity', ManagedEntity).isAnnotationPresent(Generated)
cls.getMethod('$$_hibernate_setNextManagedEntity', ManagedEntity).isAnnotationPresent(Generated)
}
}
@grails.gorm.hibernate.annotation.ManagedEntity
Expand Down