Permalink
Browse files

fix for GRAILS-7815 "Domain object constructors effectively ignored"

  • Loading branch information...
1 parent 0a8ca2a commit 45b2a7074dad8130e484236d6e234d0df23a9fb7 @graemerocher graemerocher committed Aug 19, 2011
@@ -291,15 +291,23 @@ public static void addDelegateConstructor(ClassNode classNode, MethodNode constr
MethodCallExpression constructCallExpression = new MethodCallExpression(
new ClassExpression(constructorMethod.getDeclaringClass()), "initialize", arguments);
constructCallExpression.setMethodTarget(constructorMethod);
- constructorBody.addStatement(new ExpressionStatement(constructCallExpression));
+ ExpressionStatement constructorInitExpression = new ExpressionStatement(constructCallExpression);
+ constructorBody.addStatement(new ExpressionStatement(new ConstructorCallExpression(classNode, GrailsArtefactClassInjector.ZERO_ARGS)));
+ constructorBody.addStatement(constructorInitExpression);
if (constructorParams.length == 0) {
// handle default constructor
ConstructorNode constructorNode = getDefaultConstructor(classNode);
if (constructorNode != null) {
- constructorBody.addStatement(constructorNode.getCode());
- constructorNode.setCode(constructorBody);
+ Statement existingBodyCode = constructorNode.getCode();
+ if(existingBodyCode instanceof BlockStatement) {
+ ((BlockStatement) existingBodyCode).addStatement(constructorInitExpression);
+ }
+ else {
+
+ constructorNode.setCode(constructorBody);
+ }
} else {
classNode.addConstructor(new ConstructorNode(Modifier.PUBLIC, constructorBody));
}
@@ -39,6 +39,27 @@ p = new Permission(user:u, permission:"uber")
assertEquals "User", p.class.methods.find { it.name == 'getUser' }.returnType.name
}
+ void testDefaultConstructorBehaviourNotOverriden() {
+ def entity = evaluate("""
+ import grails.persistence.*
+ @Entity
+ class EntityTransformTest {
+
+ boolean enabled
+ int cash
+ EntityTransformTest() {
+ enabled = true
+ cash = 30
+ }
+ }
+ new EntityTransformTest()
+ """)
+
+ assert entity != null
+ assert entity.enabled
+ assert entity.cash == 30
+
+ }
void testAnnotatedEntity() {
def entity = evaluate("""
import grails.persistence.*

0 comments on commit 45b2a70

Please sign in to comment.