Skip to content

Support for method pointers and array constructor method references #5782

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -848,8 +848,12 @@ public boolean endsWithClosures(List<org.codehaus.groovy.ast.expr.Expression> li

@Override
public void visitClassExpression(ClassExpression clazz) {
Space prefix = whitespace();
ClassNode type = clazz.getType();
if (type.isArray()) {
queue.add(arrayType(type));
return;
}
Space prefix = whitespace();
String name = type.getNameWithoutPackage().replace('$', '.');
if (!source.startsWith(name, cursor)) {
name = type.getUnresolvedName().replace('$', '.');
Expand Down Expand Up @@ -1878,21 +1882,18 @@ public void visitStaticMethodCallExpression(StaticMethodCallExpression call) {

@Override
public void visitMethodPointerExpression(MethodPointerExpression ref) {
String referenceName = null;
if (ref.getMethodName() instanceof ConstantExpression) {
referenceName = ((ConstantExpression) ref.getMethodName()).getValue().toString();
}

boolean isMethodRef = ref instanceof MethodReferenceExpression;
String name = ref.getMethodName().getText();
queue.add(new J.MemberReference(randomId(),
whitespace(),
Markers.EMPTY,
padRight(visit(ref.getExpression()), sourceBefore("::")),
isMethodRef ? Markers.EMPTY : Markers.build(singleton(new MethodPointer(randomId()))),
padRight(visit(ref.getExpression()), sourceBefore(isMethodRef ? "::" : ".&")),
null, // not supported by Groovy
padLeft(whitespace(), new J.Identifier(randomId(),
sourceBefore(referenceName),
sourceBefore(name),
Markers.EMPTY,
emptyList(),
referenceName,
name,
null, null)),
typeMapping.type(ref.getType()),
null, // not enough information in the AST
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,21 @@ public J visitLambda(J.Lambda lambda, PrintOutputCapture<P> p) {
return lambda;
}

@Override
public J visitMemberReference(J.MemberReference memberRef, PrintOutputCapture<P> p) {
beforeSyntax(memberRef, Space.Location.MEMBER_REFERENCE_PREFIX, p);
visitRightPadded(memberRef.getPadding().getContaining(), JRightPadded.Location.MEMBER_REFERENCE_CONTAINING, p);
if (memberRef.getMarkers().findFirst(MethodPointer.class).isPresent()) {
p.append(".&");
} else {
p.append("::");
}
visitContainer("<", memberRef.getPadding().getTypeParameters(), JContainer.Location.TYPE_PARAMETERS, ",", ">", p);
visitLeftPadded("", memberRef.getPadding().getReference(), JLeftPadded.Location.MEMBER_REFERENCE_NAME, p);
afterSyntax(memberRef, p);
return memberRef;
}

@Override
public J visitFieldAccess(J.FieldAccess fieldAccess, PrintOutputCapture<P> p) {
beforeSyntax(fieldAccess, Space.Location.FIELD_ACCESS_PREFIX, p);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2025 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.groovy.marker;

import lombok.Value;
import lombok.With;
import org.openrewrite.marker.Marker;

import java.util.UUID;

@Value
@With
public class MethodPointer implements Marker {
UUID id;
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,17 @@ void useClassAsArgumentJavaStyle() {
);
}

@Test
void staticMethodPointer() {
rewriteRun(
groovy(
"""
Integer.&parseInt
"""
)
);
}

@Test
void staticMethodReference() {
rewriteRun(
Expand All @@ -209,6 +220,17 @@ void staticMethodReference() {
);
}

@Test
void instanceMethodPointer() {
rewriteRun(
groovy(
"""
["a", "b", "c"].forEach(System.out.&println)
"""
)
);
}

@Test
void instanceMethodReference() {
rewriteRun(
Expand All @@ -220,6 +242,17 @@ void instanceMethodReference() {
);
}

@Test
void constructorMethodPointer() {
rewriteRun(
groovy(
"""
ArrayList.&new
"""
)
);
}

@Test
void constructorMethodReference() {
rewriteRun(
Expand All @@ -231,6 +264,17 @@ void constructorMethodReference() {
);
}

@Test
void arrayConstructorMethodReference() {
rewriteRun(
groovy(
"""
[1, 2, 3].stream().toArray(Integer[]::new)
"""
)
);
}

@Test
@SuppressWarnings("GroovyAssignabilityCheck")
void closureWithImplicitParameter() {
Expand Down