diff options
author | Reinier Zwitserloot <r.zwitserloot@projectlombok.org> | 2022-04-02 06:20:23 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-02 06:20:23 +0200 |
commit | 71fcc0a60b5da50e5dd0feee3b3db8bf45327fea (patch) | |
tree | d17b958094a9098fd0af1fe379cab1d286c75149 /src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java | |
parent | 1eb1a8be3c002b4573ca12f4680b724f9a35f6e1 (diff) | |
parent | d4975cf6dd959120fe91614907800de058993d50 (diff) | |
download | lombok-71fcc0a60b5da50e5dd0feee3b3db8bf45327fea.tar.gz lombok-71fcc0a60b5da50e5dd0feee3b3db8bf45327fea.tar.bz2 lombok-71fcc0a60b5da50e5dd0feee3b3db8bf45327fea.zip |
Merge pull request #3155 from Rawi01/extensionmethod-conditional-methodref
Improve the handling of ExtensionMethod arguments
Diffstat (limited to 'src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java')
-rw-r--r-- | src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java b/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java index 2e540b5e..7743f9c6 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2012-2021 The Project Lombok Authors. + * Copyright (C) 2012-2022 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -47,6 +47,7 @@ import org.eclipse.jdt.internal.compiler.ast.ASTNode; import org.eclipse.jdt.internal.compiler.ast.Annotation; import org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess; import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; +import org.eclipse.jdt.internal.compiler.ast.ConditionalExpression; import org.eclipse.jdt.internal.compiler.ast.Expression; import org.eclipse.jdt.internal.compiler.ast.MessageSend; import org.eclipse.jdt.internal.compiler.ast.NameReference; @@ -298,7 +299,7 @@ public class PatchExtensionMethod { List<TypeBinding> argumentTypes = new ArrayList<TypeBinding>(); for (Expression argument : arguments) { TypeBinding argumentType = argument.resolvedType; - if (argumentType == null && Reflection.isFunctionalExpression(argument)) { + if (argumentType == null && requiresPolyBinding(argument)) { argumentType = Reflection.getPolyTypeBinding(argument); } if (argumentType == null) { @@ -338,8 +339,8 @@ public class PatchExtensionMethod { } else { param = parameters[i]; } - // Resolve types for lambdas - if (Reflection.isFunctionalExpression(arg)) { + // Resolve types for polys + if (requiresPolyBinding(arg)) { arg.setExpectedType(param); arg.resolveType(scope); } @@ -377,6 +378,10 @@ public class PatchExtensionMethod { MessageSend_postponedErrors.clear(methodCall); return resolvedType; } + + private static boolean requiresPolyBinding(Expression argument) { + return Reflection.isFunctionalExpression(argument) || argument instanceof ConditionalExpression && Reflection.isPolyExpression(argument); + } private static NameReference createNameRef(TypeBinding typeBinding, ASTNode source) { long p = ((long) source.sourceStart << 32) | source.sourceEnd; @@ -407,6 +412,7 @@ public class PatchExtensionMethod { public static final Field argumentTypes = Permit.permissiveGetField(MessageSend.class, "argumentTypes"); public static final Field argumentsHaveErrors = Permit.permissiveGetField(MessageSend.class, "argumentsHaveErrors"); public static final Field inferenceContexts = Permit.permissiveGetField(MessageSend.class, "inferenceContexts"); + private static final Method isPolyExpression = Permit.permissiveGetMethod(Expression.class, "isPolyExpression"); private static final Class<?> functionalExpression; private static final Constructor<?> polyTypeBindingConstructor; @@ -432,6 +438,16 @@ public class PatchExtensionMethod { return functionalExpression.isInstance(expression); } + public static boolean isPolyExpression(Expression expression) { + if (isPolyExpression == null) return false; + try { + return (Boolean) isPolyExpression.invoke(expression); + } catch (Exception e) { + // Ignore + } + return false; + } + public static TypeBinding getPolyTypeBinding(Expression expression) { if (polyTypeBindingConstructor == null) return null; try { |