public class LambdaUtil
extends java.lang.Object
Constructor and Description |
---|
LambdaUtil() |
Modifier and Type | Method and Description |
---|---|
static java.util.Map<PsiElement,java.lang.String> |
checkReturnTypeCompatible(PsiLambdaExpression lambdaExpression,
PsiType functionalInterfaceReturnType) |
static PsiCall |
copyTopLevelCall(PsiCall call) |
static PsiElement |
copyWithExpectedType(PsiElement expression,
PsiType type) |
static java.lang.String |
createLambda(PsiVariable variable,
PsiExpression expression)
Generate lambda text for single argument expression lambda
|
static java.lang.String |
createLambdaParameterListWithFormalTypes(PsiType functionalInterfaceType,
PsiLambdaExpression lambdaExpression,
boolean checkApplicability) |
static PsiExpression |
extractSingleExpressionFromBody(PsiElement body) |
static java.util.List<HierarchicalMethodSignature> |
findFunctionCandidates(PsiClass psiClass) |
static MethodSignature |
getFunction(PsiClass psiClass) |
static PsiMethod |
getFunctionalInterfaceMethod(PsiClass aClass) |
static PsiMethod |
getFunctionalInterfaceMethod(PsiClassType.ClassResolveResult result) |
static PsiMethod |
getFunctionalInterfaceMethod(PsiElement element) |
static PsiMethod |
getFunctionalInterfaceMethod(PsiType functionalInterfaceType) |
static PsiType |
getFunctionalInterfaceReturnType(PsiFunctionalExpression expr) |
static PsiType |
getFunctionalInterfaceReturnType(PsiType functionalInterfaceType) |
static PsiType |
getFunctionalInterfaceType(PsiElement expression,
boolean tryToSubstitute) |
static int |
getLambdaIdx(PsiExpressionList expressionList,
PsiElement element) |
static PsiType |
getLambdaParameterFromType(PsiType functionalInterfaceType,
int parameterIndex) |
static java.util.List<PsiExpression> |
getReturnExpressions(PsiLambdaExpression lambdaExpression) |
static PsiReturnStatement [] |
getReturnStatements(PsiLambdaExpression lambdaExpression) |
static PsiSubstitutor |
getSubstitutor(PsiMethod method,
PsiClassType.ClassResolveResult resolveResult) |
static boolean |
isCapturingLambda(PsiLambdaExpression lambda)
Returns true if given lambda captures any variable or "this" reference.
|
static boolean |
isExpressionStatementExpression(PsiElement body) |
static boolean |
isFunctionalClass(PsiClass aClass) |
static boolean |
isFunctionalType(PsiType type) |
static boolean |
isIdentityLambda(PsiLambdaExpression lambda)
Returns true if lambda has single parameter and its return value is the same as parameter.
|
static boolean |
isPotentiallyCompatibleWithTypeParameter(PsiFunctionalExpression expression,
PsiExpressionList argsList,
PsiMethod method) |
static boolean |
isSafeLambdaBodyReplacement(PsiLambdaExpression lambda,
java.util.function.Supplier<? extends PsiElement> newBodySupplier)
Returns false if after suggested replacement of lambda body, containing method call would resolve to something else
or its return type will change.
|
static boolean |
isSafeLambdaReplacement(PsiLambdaExpression lambda,
java.lang.String replacementText)
Returns false if after suggested replacement of lambda body, containing method call would resolve to something else
or its return type will change.
|
static boolean |
isSafeLambdaReplacement(PsiLambdaExpression lambda,
java.util.function.Supplier<? extends PsiExpression> newFunctionSupplier)
Returns false if after suggested replacement of lambda body, containing method call would resolve to something else
or its return type will change.
|
static boolean |
isSafeLambdaReturnValueReplacement(PsiExpression lambdaReturnExpression,
PsiExpression replacement)
isSafeLambdaBodyReplacement(PsiLambdaExpression, Supplier) overload to test the same lambda body,
but with only return value expression changed to replacement |
static boolean |
isValidLambdaContext(PsiElement context) |
static boolean |
notInferredType(PsiType typeByExpression) |
static <T> T |
performWithSubstitutedParameterBounds(PsiTypeParameter[] typeParameters,
PsiSubstitutor substitutor,
java.util.function.Supplier<? extends T> producer) |
static <T> T |
performWithTargetType(PsiElement element,
PsiType targetType,
java.util.function.Supplier<? extends T> producer) |
static boolean |
processParentOverloads(PsiFunctionalExpression functionalExpression,
Consumer<? super PsiType> overloadProcessor) |
static PsiClass |
resolveFunctionalInterfaceClass(PsiFunctionalExpression expression)
Resolves a functional interface class for given functional expression
|
static PsiParameterList |
specifyLambdaParameterTypes(PsiLambdaExpression lambdaExpression) |
static PsiParameterList |
specifyLambdaParameterTypes(PsiType functionalInterfaceType,
PsiLambdaExpression lambdaExpression) |
static PsiCall |
treeWalkUp(PsiElement context) |
public static PsiType getFunctionalInterfaceReturnType(PsiFunctionalExpression expr)
public static PsiType getFunctionalInterfaceReturnType(PsiType functionalInterfaceType)
public static PsiMethod getFunctionalInterfaceMethod(PsiType functionalInterfaceType)
public static PsiMethod getFunctionalInterfaceMethod(PsiElement element)
public static PsiMethod getFunctionalInterfaceMethod(PsiClassType.ClassResolveResult result)
public static PsiSubstitutor getSubstitutor(PsiMethod method, PsiClassType.ClassResolveResult resolveResult)
public static boolean isFunctionalType(PsiType type)
public static boolean isFunctionalClass(PsiClass aClass)
public static boolean isValidLambdaContext(PsiElement context)
public static MethodSignature getFunction(PsiClass psiClass)
public static java.util.List<HierarchicalMethodSignature> findFunctionCandidates(PsiClass psiClass)
public static int getLambdaIdx(PsiExpressionList expressionList, PsiElement element)
public static PsiType getFunctionalInterfaceType(PsiElement expression, boolean tryToSubstitute)
public static boolean processParentOverloads(PsiFunctionalExpression functionalExpression, Consumer<? super PsiType> overloadProcessor)
public static boolean notInferredType(PsiType typeByExpression)
public static PsiReturnStatement [] getReturnStatements(PsiLambdaExpression lambdaExpression)
public static java.util.List<PsiExpression> getReturnExpressions(PsiLambdaExpression lambdaExpression)
public static boolean isExpressionStatementExpression(PsiElement body)
public static PsiExpression extractSingleExpressionFromBody(PsiElement body)
public static boolean isPotentiallyCompatibleWithTypeParameter(PsiFunctionalExpression expression, PsiExpressionList argsList, PsiMethod method)
public static java.util.Map<PsiElement,java.lang.String> checkReturnTypeCompatible(PsiLambdaExpression lambdaExpression, PsiType functionalInterfaceReturnType)
public static PsiType getLambdaParameterFromType(PsiType functionalInterfaceType, int parameterIndex)
public static PsiCall treeWalkUp(PsiElement context)
public static <T> T performWithSubstitutedParameterBounds(PsiTypeParameter[] typeParameters, PsiSubstitutor substitutor, java.util.function.Supplier<? extends T> producer)
public static <T> T performWithTargetType(PsiElement element, PsiType targetType, java.util.function.Supplier<? extends T> producer)
public static java.lang.String createLambda(PsiVariable variable, PsiExpression expression)
variable
- lambda sole argumentexpression
- lambda body (expression)public static boolean isIdentityLambda(PsiLambdaExpression lambda)
The lambdas like this are considered identity lambda: x -> x
, x -> {return x;}
(String x) -> (x)
, etc.
This method does not check the lambda type, also it does not check whether auto-(un)boxing occurs,
so a lambda like ((Predicate<Boolean>)b -> b)
is also identity lambda even though it performs
auto-unboxing.
lambda
- a lambda to checkpublic static boolean isSafeLambdaReplacement(PsiLambdaExpression lambda, java.util.function.Supplier<? extends PsiExpression> newFunctionSupplier)
lambda
- a lambda whose body is going to be replacednewFunctionSupplier
- replacement for lambda to check,
lazy computed for lambdas in invocation context only.
Replacement evaluated to null
is treated as invalid overloadpublic static boolean isSafeLambdaReplacement(PsiLambdaExpression lambda, java.lang.String replacementText)
lambda
- a lambda whose body is going to be replacedreplacementText
- a text of new expression to replace lambdapublic static boolean isSafeLambdaBodyReplacement(PsiLambdaExpression lambda, java.util.function.Supplier<? extends PsiElement> newBodySupplier)
True will be returned for lambdas in non-invocation context as well as for lambdas in invocation context, when invoked method is not overloaded or all overloads are 'lambda friendly'
Value-compatible lambda like () -> foo() == true
can be converted to value-compatible AND void-compatible
() -> foo()
during simplification. This could lead to ambiguity during containing method call resolution and thus
to the errors after applying the suggestion.
lambda
- a lambda whose body is going to be replacednewBodySupplier
- replacement for lambda's body to check,
lazy computed for lambdas in invocation context only.
Replacement evaluated to null
is treated as invalid overloadpublic static boolean isSafeLambdaReturnValueReplacement(PsiExpression lambdaReturnExpression, PsiExpression replacement)
isSafeLambdaBodyReplacement(PsiLambdaExpression, Supplier)
overload to test the same lambda body,
but with only return value expression
changed to replacement
lambdaReturnExpression
- a return expression inside lambda bodyreplacement
- a replacement for return expressionpublic static PsiElement copyWithExpectedType(PsiElement expression, PsiType type)
public static boolean isCapturingLambda(PsiLambdaExpression lambda)
lambda
- lambda to checkpublic static PsiClass resolveFunctionalInterfaceClass(PsiFunctionalExpression expression)
expression
- functional expressionpublic static java.lang.String createLambdaParameterListWithFormalTypes(PsiType functionalInterfaceType, PsiLambdaExpression lambdaExpression, boolean checkApplicability)
public static PsiParameterList specifyLambdaParameterTypes(PsiLambdaExpression lambdaExpression)
public static PsiParameterList specifyLambdaParameterTypes(PsiType functionalInterfaceType, PsiLambdaExpression lambdaExpression)