Skip to content

Commit 998f123

Browse files
committed
GROOVY-7940: @lazy not generating "is" property accessor for booleans (closes groovy#420)
1 parent f5a161f commit 998f123

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/main/org/codehaus/groovy/transform/LazyASTTransformation.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import static org.codehaus.groovy.ast.tools.GeneralUtils.assignS;
4545
import static org.codehaus.groovy.ast.tools.GeneralUtils.assignX;
4646
import static org.codehaus.groovy.ast.tools.GeneralUtils.block;
47+
import static org.codehaus.groovy.ast.tools.GeneralUtils.callThisX;
4748
import static org.codehaus.groovy.ast.tools.GeneralUtils.callX;
4849
import static org.codehaus.groovy.ast.tools.GeneralUtils.classX;
4950
import static org.codehaus.groovy.ast.tools.GeneralUtils.ctorX;
@@ -156,8 +157,12 @@ private static void addNonThreadSafeBody(BlockStatement body, FieldNode fieldNod
156157
private static void addMethod(FieldNode fieldNode, BlockStatement body, ClassNode type) {
157158
int visibility = ACC_PUBLIC;
158159
if (fieldNode.isStatic()) visibility |= ACC_STATIC;
159-
final String name = "get" + MetaClassHelper.capitalize(fieldNode.getName().substring(1));
160-
fieldNode.getDeclaringClass().addMethod(name, visibility, type, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, body);
160+
String propName = MetaClassHelper.capitalize(fieldNode.getName().substring(1));
161+
fieldNode.getDeclaringClass().addMethod("get" + propName, visibility, type, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, body);
162+
if (ClassHelper.boolean_TYPE.equals(type)) {
163+
fieldNode.getDeclaringClass().addMethod("is" + propName, visibility, type,
164+
Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, stmt(callThisX("get" + propName)));
165+
}
161166
}
162167

163168
private static void createSoft(FieldNode fieldNode, Expression initExpr) {

src/test/org/codehaus/groovy/transform/LazyTransformTest.groovy

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,22 @@ class LazyTransformTest extends GroovyShellTestCase {
202202
""")
203203
assertEquals([10,20,30,40,50,60], res)
204204
}
205+
206+
// GROOVY-7940
207+
void testGeneratesIsAndGetAccessorsForBooleanPrimitives() {
208+
assertScript '''
209+
class Super {
210+
boolean aBoolean = true
211+
}
212+
213+
class Testing extends Super {
214+
@Lazy
215+
boolean aBoolean = {-> false }()
216+
}
217+
218+
assert !new Testing().isaBoolean()
219+
assert !new Testing().getaBoolean()
220+
assert !new Testing().aBoolean
221+
'''
222+
}
205223
}

0 commit comments

Comments
 (0)