Skip to content

Commit 71fcc0a

Browse files
authored
Merge pull request #3155 from Rawi01/extensionmethod-conditional-methodref
Improve the handling of ExtensionMethod arguments
2 parents 1eb1a8b + d4975cf commit 71fcc0a

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2012-2021 The Project Lombok Authors.
2+
* Copyright (C) 2012-2022 The Project Lombok Authors.
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining a copy
55
* of this software and associated documentation files (the "Software"), to deal
@@ -47,6 +47,7 @@
4747
import org.eclipse.jdt.internal.compiler.ast.Annotation;
4848
import org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess;
4949
import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
50+
import org.eclipse.jdt.internal.compiler.ast.ConditionalExpression;
5051
import org.eclipse.jdt.internal.compiler.ast.Expression;
5152
import org.eclipse.jdt.internal.compiler.ast.MessageSend;
5253
import org.eclipse.jdt.internal.compiler.ast.NameReference;
@@ -298,7 +299,7 @@ public static TypeBinding resolveType(TypeBinding resolvedType, MessageSend meth
298299
List<TypeBinding> argumentTypes = new ArrayList<TypeBinding>();
299300
for (Expression argument : arguments) {
300301
TypeBinding argumentType = argument.resolvedType;
301-
if (argumentType == null && Reflection.isFunctionalExpression(argument)) {
302+
if (argumentType == null && requiresPolyBinding(argument)) {
302303
argumentType = Reflection.getPolyTypeBinding(argument);
303304
}
304305
if (argumentType == null) {
@@ -338,8 +339,8 @@ public static TypeBinding resolveType(TypeBinding resolvedType, MessageSend meth
338339
} else {
339340
param = parameters[i];
340341
}
341-
// Resolve types for lambdas
342-
if (Reflection.isFunctionalExpression(arg)) {
342+
// Resolve types for polys
343+
if (requiresPolyBinding(arg)) {
343344
arg.setExpectedType(param);
344345
arg.resolveType(scope);
345346
}
@@ -377,6 +378,10 @@ public static TypeBinding resolveType(TypeBinding resolvedType, MessageSend meth
377378
MessageSend_postponedErrors.clear(methodCall);
378379
return resolvedType;
379380
}
381+
382+
private static boolean requiresPolyBinding(Expression argument) {
383+
return Reflection.isFunctionalExpression(argument) || argument instanceof ConditionalExpression && Reflection.isPolyExpression(argument);
384+
}
380385

381386
private static NameReference createNameRef(TypeBinding typeBinding, ASTNode source) {
382387
long p = ((long) source.sourceStart << 32) | source.sourceEnd;
@@ -407,6 +412,7 @@ private static final class Reflection {
407412
public static final Field argumentTypes = Permit.permissiveGetField(MessageSend.class, "argumentTypes");
408413
public static final Field argumentsHaveErrors = Permit.permissiveGetField(MessageSend.class, "argumentsHaveErrors");
409414
public static final Field inferenceContexts = Permit.permissiveGetField(MessageSend.class, "inferenceContexts");
415+
private static final Method isPolyExpression = Permit.permissiveGetMethod(Expression.class, "isPolyExpression");
410416
private static final Class<?> functionalExpression;
411417
private static final Constructor<?> polyTypeBindingConstructor;
412418

@@ -432,6 +438,16 @@ public static boolean isFunctionalExpression(Expression expression) {
432438
return functionalExpression.isInstance(expression);
433439
}
434440

441+
public static boolean isPolyExpression(Expression expression) {
442+
if (isPolyExpression == null) return false;
443+
try {
444+
return (Boolean) isPolyExpression.invoke(expression);
445+
} catch (Exception e) {
446+
// Ignore
447+
}
448+
return false;
449+
}
450+
435451
public static TypeBinding getPolyTypeBinding(Expression expression) {
436452
if (polyTypeBindingConstructor == null) return null;
437453
try {

test/transform/resource/after-delombok/ExtensionMethodFunctional.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public void test() {
1111
test = ExtensionMethodFunctional.Extensions.map(test, s -> ExtensionMethodFunctional.Extensions.reverse(s));
1212
ExtensionMethodFunctional.Extensions.consume(test, s -> System.out.println("1: " + s), s -> System.out.println("2: " + s));
1313
ExtensionMethodFunctional.Extensions.consume(test, System.out::println, System.out::println);
14+
ExtensionMethodFunctional.Extensions.consume(test, test.length() > 0 ? System.out::println : null);
1415
ExtensionMethodFunctional.Extensions.toList1(Stream.of("a", "b", "c").map(String::toUpperCase));
1516
List<Integer> i2 = ExtensionMethodFunctional.Extensions.toList2(Stream.of("a", "b", "c").map(String::toUpperCase));
1617
}

test/transform/resource/after-ecj/ExtensionMethodFunctional.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public void test() {
3636
test = ExtensionMethodFunctional.Extensions.map(test, (<no type> s) -> ExtensionMethodFunctional.Extensions.reverse(s));
3737
ExtensionMethodFunctional.Extensions.consume(test, (<no type> s) -> System.out.println(("1: " + s)), (<no type> s) -> System.out.println(("2: " + s)));
3838
ExtensionMethodFunctional.Extensions.consume(test, System.out::println, System.out::println);
39+
ExtensionMethodFunctional.Extensions.consume(test, ((test.length() > 0) ? System.out::println : null));
3940
ExtensionMethodFunctional.Extensions.toList1(Stream.of("a", "b", "c").map(String::toUpperCase));
4041
List<Integer> i2 = ExtensionMethodFunctional.Extensions.toList2(Stream.of("a", "b", "c").map(String::toUpperCase));
4142
}

test/transform/resource/before/ExtensionMethodFunctional.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public void test() {
1616
test.consume(s -> System.out.println("1: " + s), s -> System.out.println("2: " + s));
1717
test.consume(System.out::println, System.out::println);
1818

19+
test.consume(test.length() > 0 ? System.out::println : null);
20+
1921
Stream.of("a", "b", "c").map(String::toUpperCase).toList1();
2022
List<Integer> i2 = Stream.of("a", "b", "c").map(String::toUpperCase).toList2();
2123
}

0 commit comments

Comments
 (0)