void () { PsiElement firstOperand = myOperands[0]; PsiElement topBlock = PsiUtil.getTopLevelEnclosingCodeBlock(firstOperand, null); LocalSearchScope searchScope = new LocalSearchScope(new PsiElement[]{topBlock instanceof PsiCodeBlock ? topBlock : firstOperand.getContainingFile()}, "", true); List<PsiElement> places = new ArrayList<>(5); places.add(firstOperand); Set<PsiModifierListOwner> visitedVars = new HashSet<>(); class MyAnnoVisitor implements AnnotationUtilEx.AnnotatedElementVisitor { @Override public boolean visitMethodParameter(@NotNull PsiExpression expression, @NotNull PsiCall psiCallExpression) { PsiExpressionList list = psiCallExpression.getArgumentList(); assert list != null; int index = ArrayUtil.indexOf(list.getExpressions(), expression); String methodName; if (psiCallExpression instanceof PsiMethodCallExpression) { String referenceName = ((PsiMethodCallExpression)psiCallExpression).getMethodExpression().getReferenceName(); if ("super".equals(referenceName) || "this".equals(referenceName)) { // constructor call PsiClass psiClass = PsiTreeUtil.getParentOfType(psiCallExpression, PsiClass.class, true); PsiClass psiTargetClass = "super".equals(referenceName)? psiClass == null ? null : psiClass.getSuperClass() : psiClass; methodName = psiTargetClass == null? null : psiTargetClass.getName(); } else { methodName = referenceName; } } else if (psiCallExpression instanceof PsiNewExpression) { PsiJavaCodeReferenceElement classRef = ((PsiNewExpression)psiCallExpression).getClassOrAnonymousClassReference(); methodName = classRef == null ? null : classRef.getReferenceName(); } else if (psiCallExpression instanceof PsiEnumConstant) { PsiMethod method = psiCallExpression.resolveMethod(); methodName = method != null ? method.getName() : null; } else { methodName = null; } if (methodName != null && index >= 0 && areThereInjectionsWithName(methodName, false)) { PsiMethod method = psiCallExpression.resolveMethod(); if (method != null) { PsiParameter[] parameters = method.getParameterList().getParameters(); if (index < parameters.length) { process(parameters[index], method, index); } else if (method.isVarArgs()) { process(parameters[parameters.length - 1], method, parameters.length - 1); } } } return false; } @Override public boolean visitMethodReturnStatement(@NotNull PsiElement source, @NotNull PsiMethod method) { if (areThereInjectionsWithName(method.getName(), false)) { process(method, method, -1); } return false; } private void visitVariableUsages(PsiVariable variable) { if (variable == null) return; if (myConfiguration.getAdvancedConfiguration().getDfaOption() != Configuration.DfaOption.OFF) { ReferencesSearch.search(variable, searchScope).forEach(psiReference -> { PsiElement element = psiReference.getElement(); if (element instanceof PsiExpression refExpression) { places.add(refExpression); if (!myUnparsable) { myUnparsable = checkUnparsableReference(refExpression); } } return true; }); } } @Override public boolean visitVariable(@NotNull PsiVariable variable) { if (!visitedVars.add(variable)) return false; visitVariableUsages(variable); PsiElement anchor = !(variable.getFirstChild() instanceof PsiComment) ? variable : variable.getModifierList() != null ? variable.getModifierList() : variable.getTypeElement(); if (anchor != null && !processCommentInjection(anchor)) { myShouldStop = true; } else { process(variable, null, -1); } return false; } @Override public boolean visitAnnotationParameter(@NotNull PsiNameValuePair nameValuePair, @NotNull PsiAnnotation psiAnnotation) { String paramName = nameValuePair.getName(); String methodName = paramName != null ? paramName : PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME; if (areThereInjectionsWithName(methodName, false)) { PsiReference reference = nameValuePair.getReference(); PsiElement element = reference == null ? null : reference.resolve(); if (element instanceof PsiMethod) { process((PsiMethod)element, (PsiMethod)element, -1); } } return false; } @Override public boolean visitReference(@NotNull PsiReferenceExpression expression) { if (myConfiguration.getAdvancedConfiguration().getDfaOption() == Configuration.DfaOption.OFF) return true; PsiElement e = expression.resolve(); if (e instanceof PsiVariable) { if (e instanceof PsiParameter p) { PsiElement declarationScope = p.getDeclarationScope(); PsiMethod method = declarationScope instanceof PsiMethod ? (PsiMethod)declarationScope : null; PsiParameterList parameterList = method == null ? null : method.getParameterList(); // don't check catchblock parameters & etc. if (!(parameterList == null || parameterList != e.getParent()) && areThereInjectionsWithName(method.getName(), false)) { int parameterIndex = parameterList.getParameterIndex((PsiParameter)e); process((PsiModifierListOwner)e, method, parameterIndex); } } visitVariable((PsiVariable)e); } return !myShouldStop; } private boolean processCommentInjection(@NotNull PsiElement anchor) { Ref<PsiElement> causeRef = Ref.create(); BaseInjection injection = mySupport.findCommentInjection(anchor, causeRef); if (injection != null) { PsiVariable variable = PsiTreeUtil.getParentOfType(anchor, PsiVariable.class); visitVariableUsages(variable); return processCommentInjectionInner(causeRef.get(), injection); } return true; } } MyAnnoVisitor visitor = new MyAnnoVisitor(); if (!visitor.processCommentInjection(firstOperand)) { return; } while (!places.isEmpty() && !myShouldStop) { PsiElement curPlace = places.remove(0); AnnotationUtilEx.visitAnnotatedElements(curPlace, visitor); } }