PsiField (@Nullable final PsiExpression initializer, final PsiClass parentClass) { if (initializer == null) return null; final PsiField[] refConstantFields = new PsiField[1]; initializer.accept(new JavaRecursiveElementWalkingVisitor() { @Override public void visitReferenceExpression(@NotNull PsiReferenceExpression expression) { super.visitReferenceExpression(expression); final PsiElement resolve = expression.resolve(); if (resolve instanceof PsiField && PsiTreeUtil.isAncestor(parentClass, resolve, false) && ((PsiField)resolve).hasInitializer() && !PsiTreeUtil.isAncestor(initializer, resolve, false)) { if (refConstantFields[0] == null || refConstantFields[0].getTextOffset() < resolve.getTextOffset()) { refConstantFields[0] = (PsiField)resolve; } } } }); return refConstantFields[0]; }