Skip to content

Commit

Permalink
Fix for #1569: find init method for @Lazy static field
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Apr 22, 2024
1 parent 4b4f447 commit 49da260
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,14 @@ public void visitField(FieldNode node) {
// visit lazy field initializer inline with field
for (ASTNode anno : GroovyUtils.getTransformNodes(node.getDeclaringClass(), LazyASTTransformation.class)) {
if (node.getAnnotations().contains(anno)) {
MethodNode init = node.getDeclaringClass().getDeclaredMethod(
"get" + org.apache.groovy.util.BeanUtils.capitalize(node.getName().substring(1)), Parameter.EMPTY_ARRAY);
String name = node.getName().substring(1);
if (!node.isStatic()) {
name = "get" + org.apache.groovy.util.BeanUtils.capitalize(name);
} else {
name = node.getDeclaringClass().getName().replace('.', '_') + "$" +
node.getType().getNameWithoutPackage() + "Holder_" + name + "_initExpr";
}
MethodNode init = node.getDeclaringClass().getDeclaredMethod(name, Parameter.EMPTY_ARRAY);
if (init != null && init.getEnd() < 1) {
visitMethod(init);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2316,7 +2316,14 @@ private List<MethodNode> findMethods(final ClassNode classNode, final String met

private MethodNode findLazyMethod(final String fieldName) {
ClassNode classNode = (ClassNode) enclosingDeclarationNode;
return classNode.getDeclaredMethod("get" + org.apache.groovy.util.BeanUtils.capitalize(fieldName), NO_PARAMETERS);
FieldNode fieldNode = classNode.getDeclaredField("$" + fieldName);
String methodName;
if (!fieldNode.isStatic()) {
methodName = "get" + org.apache.groovy.util.BeanUtils.capitalize(fieldName);
} else {
methodName = classNode.getName().replace('.', '_') + "$" + fieldNode.getType().getNameWithoutPackage() + "Holder_" + fieldName + "_initExpr";
}
return classNode.getDeclaredMethod(methodName, NO_PARAMETERS);
}

private Expression findLeafNode(final Expression expression) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4127,6 +4127,28 @@ final class SemanticHighlightingTests extends GroovyEclipseTestSuite {
new HighlightedTypedPosition(contents.lastIndexOf('id'), 2, FIELD))
}

@Test
void testLazyInitExpr3() {
String contents = '''\
|package p
|class X {
| @Lazy static Map values = new HashMap().tap {
| it.put('foo','bar')
| }
|}
|'''.stripMargin()

assertHighlighting(contents,
new HighlightedTypedPosition(contents.indexOf('X'), 1, CLASS),
new HighlightedTypedPosition(contents.indexOf('Map'), 3, INTERFACE),
new HighlightedTypedPosition(contents.indexOf('values'), 6, STATIC_FIELD),
new HighlightedTypedPosition(contents.indexOf('HashMap'), 7, CTOR_CALL),
new HighlightedTypedPosition(contents.indexOf('HashMap'), 7, CLASS),
new HighlightedTypedPosition(contents.indexOf('tap'), 3, GROOVY_CALL),
new HighlightedTypedPosition(contents.indexOf('it'), 2, GROOVY_CALL),
new HighlightedTypedPosition(contents.indexOf('put'), 3, METHOD_CALL))
}

@Test
void testFieldInitExpr() {
addGroovySource '''\
Expand Down

0 comments on commit 49da260

Please sign in to comment.