Skip to content

Commit ed6d474

Browse files
authored
Issue 1445 apply generated annotation (#1507)
* Issue #1445 apply Generated annotation to Traits, to improve test coverage reports * Issue #1445 apply Generated annotation to methods/constructors created with AST transformation, to improve test coverage reports Co-authored-by: Alar Aule <[email protected]>
1 parent 5c82cb4 commit ed6d474

File tree

50 files changed

+722
-46
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+722
-46
lines changed

grails-datastore-async/src/main/groovy/org/grails/datastore/gorm/async/transform/DelegateAsyncTransformation.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import static org.codehaus.groovy.ast.tools.GenericsUtils.correctToGenericsSpecRecurse;
2525
import static org.codehaus.groovy.ast.tools.GenericsUtils.createGenericsSpec;
2626

27+
import static org.apache.groovy.ast.tools.AnnotatedNodeUtils.markAsGenerated;
28+
2729
@GroovyASTTransformation(phase = CompilePhase.CANONICALIZATION)
2830
public class DelegateAsyncTransformation implements ASTTransformation {
2931
private static final ArgumentListExpression NO_ARGS = new ArgumentListExpression();
@@ -122,6 +124,8 @@ private void applyDelegateAsyncTransform(ClassNode classNode, ClassNode targetAp
122124
MethodCallExpression delegateMethodCall = new MethodCallExpression(new VariableExpression(fieldName), candidate.getName(), arguments);
123125
promiseBody.addStatement(new ExpressionStatement(delegateMethodCall));
124126
MethodNode newMethodNode = new MethodNode(candidate.getName(), Modifier.PUBLIC,promiseNode, parameters,null, methodBody);
127+
128+
markAsGenerated(classNode, newMethodNode);
125129
classNode.addMethod(newMethodNode);
126130
}
127131
}

grails-datastore-async/src/test/groovy/org/grails/datastore/gorm/async/transform/DelegateAsyncSpec.groovy

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package org.grails.datastore.gorm.async.transform
22

33
import grails.async.Promise
4+
import groovy.transform.Generated
45
import org.codehaus.groovy.ast.ClassNode
56
import spock.lang.Specification
67

8+
import java.lang.reflect.Method
9+
710
/**
811
* Created by graemerocher on 01/07/16.
912
*/
@@ -25,7 +28,12 @@ interface Foo<D> {
2528
''')
2629
expect:"The method is retrieved"
2730
new ClassNode(cls).methods
28-
Promise.isAssignableFrom( cls.getMethod("withTransaction", Closure).returnType )
31+
32+
Method method = cls.getMethod("withTransaction", Closure)
33+
Promise.isAssignableFrom( method.returnType )
34+
35+
and: 'marked as Generated'
36+
method.isAnnotationPresent(Generated)
2937
}
3038

3139
}

grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/dirty/checking/DirtyCheckable.groovy

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.grails.datastore.mapping.dirty.checking
22

33
import groovy.transform.CompileStatic
4+
import groovy.transform.Generated
45
import org.grails.datastore.mapping.proxy.EntityProxy
56

67
import javax.persistence.Transient
@@ -22,6 +23,7 @@ trait DirtyCheckable {
2223
* Indicates that the instance should start tacking changes. Note that if the instance is dirty this will clear any previously tracked
2324
* changes
2425
*/
26+
@Generated
2527
void trackChanges() {
2628
$changedProperties = new LinkedHashMap<String, Object>()
2729
}
@@ -31,6 +33,7 @@ trait DirtyCheckable {
3133
*
3234
* @param o a given object
3335
*/
36+
@Generated
3437
void syncChangedProperties(Object o) {
3538
if (o instanceof DirtyCheckable) {
3639
o.trackChanges($changedProperties)
@@ -42,13 +45,15 @@ trait DirtyCheckable {
4245
*
4346
* @param changedProperties The changes.
4447
*/
45-
void trackChanges(Map<String, Object> changedProperties) {
48+
@Generated
49+
void trackChanges(Map<String, Object> changedProperties) {
4650
$changedProperties = changedProperties
4751
}
4852

4953
/**
5054
* @return True if the instance has any changes
5155
*/
56+
@Generated
5257
boolean hasChanged() {
5358
if(this instanceof EntityProxy && !((EntityProxy)this).isInitialized()) {
5459
return false
@@ -62,6 +67,7 @@ trait DirtyCheckable {
6267
* @param propertyName The name of the property
6368
* @return True if the given property has any changes
6469
*/
70+
@Generated
6571
boolean hasChanged(String propertyName) {
6672
if(this instanceof EntityProxy && !((EntityProxy)this).isInitialized()) {
6773
return false
@@ -74,6 +80,7 @@ trait DirtyCheckable {
7480
/**
7581
* Marks the whole class and all its properties as dirty. When called any future call to any of the hasChanged methods will return true.
7682
*/
83+
@Generated
7784
void markDirty() {
7885
if( $changedProperties != null && $changedProperties.isEmpty()) {
7986
$changedProperties = DirtyCheckingSupport.DIRTY_CLASS_MARKER
@@ -84,6 +91,7 @@ trait DirtyCheckable {
8491
* Marks the given property name as dirty
8592
* @param propertyName The property name
8693
*/
94+
@Generated
8795
void markDirty(String propertyName) {
8896
if( $changedProperties != null && !$changedProperties.containsKey(propertyName)) {
8997
if (DirtyCheckingSupport.DIRTY_CLASS_MARKER.is($changedProperties)) {
@@ -98,6 +106,7 @@ trait DirtyCheckable {
98106
* @param propertyName The property name
99107
* @param newValue The new value
100108
*/
109+
@Generated
101110
void markDirty(String propertyName, newValue) {
102111
if( $changedProperties != null && !$changedProperties.containsKey(propertyName)) {
103112
def oldValue = ((GroovyObject) this).getProperty(propertyName)
@@ -110,6 +119,7 @@ trait DirtyCheckable {
110119
* @param propertyName The property name
111120
* @param newValue The new value
112121
*/
122+
@Generated
113123
void markDirty(String propertyName, newValue, oldValue) {
114124
if( $changedProperties != null && !$changedProperties.containsKey(propertyName)) {
115125
boolean isNull = newValue == null
@@ -127,6 +137,7 @@ trait DirtyCheckable {
127137
/**
128138
* @return A list of the dirty property names
129139
*/
140+
@Generated
130141
List<String> listDirtyPropertyNames() {
131142
if(this instanceof EntityProxy && !((EntityProxy)this).isInitialized()) {
132143
return Collections.emptyList()
@@ -146,6 +157,7 @@ trait DirtyCheckable {
146157
* @param propertyName The property name
147158
* @return The original value
148159
*/
160+
@Generated
149161
Object getOriginalValue(String propertyName) {
150162
if($changedProperties != null && $changedProperties.containsKey(propertyName)) {
151163
return $changedProperties.get(propertyName)

grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/services/Service.groovy

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.grails.datastore.mapping.services
1717

18+
import groovy.transform.Generated
1819
import org.grails.datastore.mapping.core.Datastore
1920

2021
/**
@@ -30,5 +31,15 @@ trait Service<T> {
3031
/**
3132
* The datastore that this service is related to
3233
*/
33-
Datastore datastore
34+
private Datastore datastore
35+
36+
@Generated
37+
Datastore getDatastore() {
38+
return datastore
39+
}
40+
41+
@Generated
42+
void setDatastore(Datastore datastore) {
43+
this.datastore = datastore
44+
}
3445
}

grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/dirty/checking/DirtyCheckableSpec.groovy

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package org.grails.datastore.mapping.dirty.checking
22

3-
43
import groovy.transform.Sortable
4+
import groovy.transform.Generated
55
import spock.lang.Issue
66
import spock.lang.Specification
77

8+
import java.lang.reflect.Method
9+
810
class DirtyCheckableSpec extends Specification {
911

1012
@Issue('https://github.com/grails/grails-data-mapping/issues/1231')
@@ -59,6 +61,13 @@ class DirtyCheckableSpec extends Specification {
5961
animal.hasChanged("barks")
6062

6163
}
64+
65+
void "test that all DirtyCheckable trait methods are marked as Generated"() {
66+
expect: "all DirtyCheckable methods are marked as Generated on implementation class"
67+
DirtyCheckable.getMethods().each { Method traitMethod ->
68+
assert Animal.class.getMethod(traitMethod.name, traitMethod.parameterTypes).isAnnotationPresent(Generated)
69+
}
70+
}
6271
}
6372

6473
class Animal implements DirtyCheckable {

grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/services/DefaultServiceRegistrySpec.groovy

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package org.grails.datastore.mapping.services
22

3+
import groovy.transform.Generated
34
import org.grails.datastore.mapping.core.Datastore
5+
import org.grails.datastore.mapping.dirty.checking.DirtyCheckable
46
import spock.lang.Specification
57

8+
import java.lang.reflect.Method
9+
610
/**
711
* Created by graemerocher on 11/01/2017.
812
*/
@@ -22,6 +26,13 @@ class DefaultServiceRegistrySpec extends Specification {
2226
reg.getService(TestService) != reg2.getService(TestService)
2327
reg.getService(TestService).datastore != reg2.getService(TestService).datastore
2428
}
29+
30+
void "test that all Service trait methods are marked as Generated"() {
31+
expect: "all Service methods are marked as Generated on implementation class"
32+
Service.getMethods().each { Method traitMethod ->
33+
assert TestService.class.getMethod(traitMethod.name, traitMethod.parameterTypes).isAnnotationPresent(Generated)
34+
}
35+
}
2536
}
2637

2738
class TestService implements Service, ITestService {

grails-datastore-gorm-async/src/main/groovy/grails/gorm/async/AsyncEntity.groovy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package grails.gorm.async
22

33
import groovy.transform.CompileStatic
4+
import groovy.transform.Generated
45
import org.grails.datastore.gorm.GormEnhancer
56
import org.grails.datastore.gorm.GormEntity
67
import org.grails.datastore.gorm.async.GormAsyncStaticApi
@@ -16,6 +17,7 @@ trait AsyncEntity<D> extends GormEntity<D> {
1617
/**
1718
* @return The async version of the GORM static API
1819
*/
20+
@Generated
1921
static GormAsyncStaticApi<D> getAsync() {
2022
return new GormAsyncStaticApi(GormEnhancer.findStaticApi(this))
2123
}

grails-datastore-gorm-rx/src/main/groovy/grails/gorm/rx/MultiTenant.groovy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package grails.gorm.rx
33
import grails.gorm.api.GormAllOperations
44
import grails.gorm.rx.api.RxGormAllOperations
55
import groovy.transform.CompileStatic
6+
import groovy.transform.Generated
67
import org.grails.datastore.gorm.GormEnhancer
78
import org.grails.datastore.mapping.core.connections.ConnectionSource
89
import org.grails.gorm.rx.api.RxGormEnhancer
@@ -22,6 +23,7 @@ trait MultiTenant<D> extends RxEntity<D> {
2223
* @param callable The closure
2324
* @return The result of the closure
2425
*/
26+
@Generated
2527
static <T> T withTenant(Serializable tenantId, @DelegatesTo(RxGormAllOperations) Closure<T> callable) {
2628
RxGormEnhancer.findStaticApi(this).withTenant tenantId, callable
2729
}
@@ -32,6 +34,7 @@ trait MultiTenant<D> extends RxEntity<D> {
3234
* @param callable The closure
3335
* @return The result of the closure
3436
*/
37+
@Generated
3538
static RxGormAllOperations<D> eachTenant( @DelegatesTo(RxGormAllOperations) Closure callable) {
3639
RxGormEnhancer.findStaticApi(this, ConnectionSource.DEFAULT).eachTenant callable
3740
}
@@ -42,6 +45,7 @@ trait MultiTenant<D> extends RxEntity<D> {
4245
* @param tenantId The tenant id
4346
* @return The operations
4447
*/
48+
@Generated
4549
static RxGormAllOperations<D> withTenant(Serializable tenantId) {
4650
(RxGormAllOperations<D>)RxGormEnhancer.findStaticApi(this).withTenant(tenantId)
4751
}

0 commit comments

Comments
 (0)