Skip to content

Commit

Permalink
GROOVY-7940: @lazy not generating "is" property accessor for booleans (
Browse files Browse the repository at this point in the history
…closes groovy#420)
  • Loading branch information
jwagenleitner committed Sep 18, 2016
1 parent f5a161f commit 998f123
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
18 changes: 18 additions & 0 deletions src/test/org/codehaus/groovy/transform/LazyTransformTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
'''
}
}

0 comments on commit 998f123

Please sign in to comment.