PsiElementVisitor (@NotNull final ProblemsHolder holder, boolean isOnTheFly) { return new JavaElementVisitor() { @Override public void visitReferenceExpression(@NotNull PsiReferenceExpression expression) { visitExpression(expression); } @Override public void visitExpression(@NotNull PsiExpression expression) { final PsiElement element = expression.getParent(); if (element instanceof PsiExpressionList) { // this checks method arguments check(expression, holder, false); } else if (element instanceof PsiNameValuePair valuePair) { final String name = valuePair.getName(); if (name == null || name.equals(PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME)) { // check whether @Subst complies with pattern check(expression, holder, true); } check(expression, holder, false); } } @Override public void visitReturnStatement(@NotNull PsiReturnStatement statement) { final PsiExpression returnValue = statement.getReturnValue(); if (returnValue != null) { check(returnValue, holder, false); } } @Override public void visitVariable(@NotNull PsiVariable var) { final PsiExpression initializer = var.getInitializer(); if (initializer != null) { // variable/field initializer check(initializer, holder, false); } } @Override public void visitAssignmentExpression(@NotNull PsiAssignmentExpression expression) { final PsiExpression e = expression.getRExpression(); if (e != null) { check(e, holder, false); } visitExpression(expression); } private void check(@NotNull PsiExpression expression, ProblemsHolder holder, boolean isAnnotationValue) { if (expression instanceof PsiConditionalExpression expr) { PsiExpression e = expr.getThenExpression(); if (e != null) { check(e, holder, isAnnotationValue); } e = expr.getElseExpression(); if (e != null) { check(e, holder, isAnnotationValue); } } else { final PsiType type = expression.getType(); // optimiziation: only check expressions of type String if (type != null && PsiUtilEx.isString(type)) { final PsiModifierListOwner element; if (isAnnotationValue) { final PsiAnnotation psiAnnotation = PsiTreeUtil.getParentOfType(expression, PsiAnnotation.class); if (psiAnnotation != null && Configuration.getInstance() .getAdvancedConfiguration().getSubstAnnotationClass().equals(psiAnnotation.getQualifiedName())) { element = PsiTreeUtil.getParentOfType(expression, PsiModifierListOwner.class); } else { return; } } else { element = AnnotationUtilEx.getAnnotatedElementFor(expression, AnnotationUtilEx.LookupType.PREFER_CONTEXT); } if (element != null && PsiUtilEx.isLanguageAnnotationTarget(element)) { PsiAnnotation[] annotations = AnnotationUtilEx.getAnnotationFrom(element, Configuration.getInstance().getAdvancedConfiguration() .getPatternAnnotationPair(), true); checkExpression(expression, annotations, holder); } } } } }; }