From 9405ee49d778d825657a6bbf040a3636775c80cc Mon Sep 17 00:00:00 2001 From: lingenj Date: Fri, 27 Dec 2024 14:33:06 +0100 Subject: [PATCH] support method invocation called from object in object --- .../openrewrite/groovy/GroovyParserVisitor.java | 4 +++- .../groovy/tree/MethodInvocationTest.java | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/rewrite-groovy/src/main/java/org/openrewrite/groovy/GroovyParserVisitor.java b/rewrite-groovy/src/main/java/org/openrewrite/groovy/GroovyParserVisitor.java index af660485f32..f58cf2de98f 100644 --- a/rewrite-groovy/src/main/java/org/openrewrite/groovy/GroovyParserVisitor.java +++ b/rewrite-groovy/src/main/java/org/openrewrite/groovy/GroovyParserVisitor.java @@ -2543,7 +2543,6 @@ private int sourceLengthOfString(ConstantExpression expr) { } else if (node instanceof BinaryExpression) { BinaryExpression expr = (BinaryExpression) node; return determineParenthesisLevel(expr.getLeftExpression().getLineNumber(), expr.getLineNumber(), expr.getLeftExpression().getColumnNumber(), expr.getColumnNumber()); - } return null; } @@ -2572,6 +2571,9 @@ private int determineParenthesisLevel(int childLineNumber, int parentLineNumber, if (source.charAt(i) == '(') { count++; } + if (source.charAt(i) == ')') { + count--; + } } cursor = saveCursor; return count; diff --git a/rewrite-groovy/src/test/java/org/openrewrite/groovy/tree/MethodInvocationTest.java b/rewrite-groovy/src/test/java/org/openrewrite/groovy/tree/MethodInvocationTest.java index 42a73d160a0..86208e4943f 100644 --- a/rewrite-groovy/src/test/java/org/openrewrite/groovy/tree/MethodInvocationTest.java +++ b/rewrite-groovy/src/test/java/org/openrewrite/groovy/tree/MethodInvocationTest.java @@ -191,6 +191,22 @@ def acceptsClosure(Closure cl) {} ); } + @Test + void closureInObjectInObject() { + rewriteRun( + groovy( + """ + class Test { + Test child = new Test() + def acceptsClosure(Closure cl) {} + } + + new Test().child.acceptsClosure {} + """ + ) + ); + } + @Issue("https://github.com/openrewrite/rewrite/issues/4766") @Test void gradleFileWithMultipleClosuresWithoutParentheses() {