[java unused declaration] - excluded functional ref node out of the graph

GitOrigin-RevId: 28ca95ff4709b8c7466abee73054d1bbb52ee28a
This commit is contained in:
Ilyas Selimov
2022-01-19 14:40:12 +07:00
committed by intellij-monorepo-bot
parent af5b12d340
commit c7d80fb16a
16 changed files with 123 additions and 320 deletions

View File

@@ -248,27 +248,43 @@ public class RefJavaUtilImpl extends RefJavaUtil {
//TODO support kotlin
addClassReferenceForStaticImport(node, (PsiMember)psiResolved, refFrom, decl);
}
}
@Override
public boolean visitLambdaExpression(@NotNull ULambdaExpression lambda) {
processFunctionalExpression(lambda, lambda.getFunctionalInterfaceType());
return true;
}
@Override
public boolean visitCallableReferenceExpression(@NotNull UCallableReferenceExpression methodRef) {
RefElement refMethod = refFrom.getRefManager().getReference(methodRef.getSourcePsi());
if (refFrom == refMethod) {
visitReferenceExpression(methodRef);
return false;
}
else {
processFunctionalExpression(methodRef, getFunctionalInterfaceType(methodRef));
return true;
// todo currently if psiResolved is KtParameter, it doesn't convert to UParameter, that seems wrong
UParameter uParam = UastContextKt.toUElement(psiResolved, UParameter.class);
if (uParam != null) {
addReferenceToLambdaParameter(uParam, psiResolved, decl, refFrom);
}
}
}
@Override
public boolean visitLambdaExpression(@NotNull ULambdaExpression node) {
processFunctionalExpression(node, node.getFunctionalInterfaceType());
return false;
}
@Override
public boolean visitCallableReferenceExpression(@NotNull UCallableReferenceExpression node) {
visitReferenceExpression(node);
// todo doesn't work for kotlin
PsiType interfaceType = getFunctionalInterfaceType(node);
processFunctionalExpression(node, interfaceType);
markParametersReferenced(node, interfaceType);
return false;
}
private void markParametersReferenced(@NotNull UCallableReferenceExpression node, @Nullable PsiType type) {
PsiMethod method = LambdaUtil.getFunctionalInterfaceMethod(type);
if (method == null) return;
for (PsiParameter param : method.getParameterList().getParameters()) {
RefElement paramRef = refFrom.getRefManager().getReference(param);
if (paramRef != null) {
paramRef.waitForInitialized();
refFrom.addReference(paramRef, param, decl, false, true, node);
}
}
}
private void processFunctionalExpression(@NotNull UExpression expression, @Nullable PsiType type) {
PsiElement aClass = PsiUtil.resolveClassInType(type);
if (aClass != null) {
@@ -278,11 +294,14 @@ public class RefJavaUtilImpl extends RefJavaUtil {
final RefElement refWhat = refFrom.getRefManager().getReference(aClass);
if (refWhat != null) refWhat.waitForInitialized();
refFrom.addReference(refWhat, aClass, decl, false, true, null);
final PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(type);
if (interfaceMethod != null) {
RefElement interfaceMethodRef = refFrom.getRefManager().getReference(interfaceMethod);
if (interfaceMethodRef != null) interfaceMethodRef.waitForInitialized();
refFrom.addReference(interfaceMethodRef, interfaceMethod, decl, false, true, null);
refFrom.getRefManager().fireNodeMarkedReferenced(interfaceMethod, expression.getSourcePsi());
}
}
PsiElement functionalExpr = expression.getSourcePsi();
RefElement refFunctionalExpr = refFrom.getRefManager().getReference(functionalExpr);
if (refFunctionalExpr != null) refFunctionalExpr.waitForInitialized();
refFrom.addReference(refFunctionalExpr, functionalExpr, decl, false, true, expression);
}
@Nullable
@@ -397,6 +416,27 @@ public class RefJavaUtilImpl extends RefJavaUtil {
}
}
private static void addReferenceToLambdaParameter(@NotNull UParameter uParam, @NotNull PsiElement param, @NotNull UElement decl,
@NotNull RefJavaElementImpl refFrom) {
ULambdaExpression lambda = UastUtils.getParentOfType(uParam, ULambdaExpression.class);
if (lambda == null) return;
int paramIndex = -1;
List<UParameter> lambdaParams = lambda.getParameters();
for (int i = 0; i < lambdaParams.size(); i++) {
if (lambdaParams.get(i).equals(uParam)) {
paramIndex = i;
break;
}
}
if (paramIndex == -1) return;
RefElement method = refFrom.getRefManager().getReference(LambdaUtil.getFunctionalInterfaceMethod(lambda.getFunctionalInterfaceType()));
if (method instanceof RefMethod) {
method.waitForInitialized();
RefParameter[] methodParams = ((RefMethod)method).getParameters();
refFrom.addReference(methodParams[paramIndex], param, decl, false, true, null);
}
}
private static void addClassReferenceForStaticImport(UExpression node,
PsiMember psiResolved,
RefJavaElementImpl refFrom, UElement decl) {