From 998f123144404bce45ba5206d214d9aca0250e3f Mon Sep 17 00:00:00 2001 From: John Wagenleitner Date: Sat, 17 Sep 2016 21:22:53 -0700 Subject: [PATCH] GROOVY-7940: @Lazy not generating "is" property accessor for booleans (closes #420) --- .../transform/LazyASTTransformation.java | 9 +++++++-- .../groovy/transform/LazyTransformTest.groovy | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/main/org/codehaus/groovy/transform/LazyASTTransformation.java b/src/main/org/codehaus/groovy/transform/LazyASTTransformation.java index 871fe7081e..480654b158 100644 --- a/src/main/org/codehaus/groovy/transform/LazyASTTransformation.java +++ b/src/main/org/codehaus/groovy/transform/LazyASTTransformation.java @@ -44,6 +44,7 @@ import static org.codehaus.groovy.ast.tools.GeneralUtils.assignS; import static org.codehaus.groovy.ast.tools.GeneralUtils.assignX; import static org.codehaus.groovy.ast.tools.GeneralUtils.block; +import static org.codehaus.groovy.ast.tools.GeneralUtils.callThisX; import static org.codehaus.groovy.ast.tools.GeneralUtils.callX; import static org.codehaus.groovy.ast.tools.GeneralUtils.classX; import static org.codehaus.groovy.ast.tools.GeneralUtils.ctorX; @@ -156,8 +157,12 @@ private static void addNonThreadSafeBody(BlockStatement body, FieldNode fieldNod private static void addMethod(FieldNode fieldNode, BlockStatement body, ClassNode type) { int visibility = ACC_PUBLIC; if (fieldNode.isStatic()) visibility |= ACC_STATIC; - final String name = "get" + MetaClassHelper.capitalize(fieldNode.getName().substring(1)); - fieldNode.getDeclaringClass().addMethod(name, visibility, type, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, body); + String propName = MetaClassHelper.capitalize(fieldNode.getName().substring(1)); + fieldNode.getDeclaringClass().addMethod("get" + propName, visibility, type, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, body); + if (ClassHelper.boolean_TYPE.equals(type)) { + fieldNode.getDeclaringClass().addMethod("is" + propName, visibility, type, + Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, stmt(callThisX("get" + propName))); + } } private static void createSoft(FieldNode fieldNode, Expression initExpr) { diff --git a/src/test/org/codehaus/groovy/transform/LazyTransformTest.groovy b/src/test/org/codehaus/groovy/transform/LazyTransformTest.groovy index de8b110cc0..8d292d997f 100644 --- a/src/test/org/codehaus/groovy/transform/LazyTransformTest.groovy +++ b/src/test/org/codehaus/groovy/transform/LazyTransformTest.groovy @@ -202,4 +202,22 @@ class LazyTransformTest extends GroovyShellTestCase { """) assertEquals([10,20,30,40,50,60], res) } + + // GROOVY-7940 + void testGeneratesIsAndGetAccessorsForBooleanPrimitives() { + assertScript ''' + class Super { + boolean aBoolean = true + } + + class Testing extends Super { + @Lazy + boolean aBoolean = {-> false }() + } + + assert !new Testing().isaBoolean() + assert !new Testing().getaBoolean() + assert !new Testing().aBoolean + ''' + } }