public static PsiReferenceExpression qualifyReference( PsiReferenceExpression referenceExpression, final PsiMember member, @Nullable final PsiClass qualifyingClass) throws IncorrectOperationException { PsiManager manager = referenceExpression.getManager(); PsiMethodCallExpression methodCallExpression = PsiTreeUtil.getParentOfType(referenceExpression, PsiMethodCallExpression.class, true); while ((methodCallExpression) != null) { if (HighlightUtil.isSuperOrThisMethodCall(methodCallExpression)) { return referenceExpression; } methodCallExpression = PsiTreeUtil.getParentOfType(methodCallExpression, PsiMethodCallExpression.class, true); } PsiReferenceExpression expressionFromText; final PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory(); if (qualifyingClass == null) { PsiClass parentClass = PsiTreeUtil.getParentOfType(referenceExpression, PsiClass.class); final PsiClass containingClass = member.getContainingClass(); if (parentClass != null && !InheritanceUtil.isInheritorOrSelf(parentClass, containingClass, true)) { while (parentClass != null && !InheritanceUtil.isInheritorOrSelf(parentClass, containingClass, true)) { parentClass = PsiTreeUtil.getParentOfType(parentClass, PsiClass.class, true); } LOG.assertTrue(parentClass != null); expressionFromText = (PsiReferenceExpression) factory.createExpressionFromText("A.this." + member.getName(), null); ((PsiThisExpression) expressionFromText.getQualifierExpression()) .getQualifier() .replace(factory.createClassReferenceElement(parentClass)); } else { expressionFromText = (PsiReferenceExpression) factory.createExpressionFromText("this." + member.getName(), null); } } else { expressionFromText = (PsiReferenceExpression) factory.createExpressionFromText("A." + member.getName(), null); expressionFromText.setQualifierExpression(factory.createReferenceExpression(qualifyingClass)); } CodeStyleManager codeStyleManager = manager.getCodeStyleManager(); expressionFromText = (PsiReferenceExpression) codeStyleManager.reformat(expressionFromText); return (PsiReferenceExpression) referenceExpression.replace(expressionFromText); }
@Override public void visitField(@NotNull PsiField field) { super.visitField(field); if (!field.hasModifierProperty(PsiModifier.PRIVATE)) { return; } if (HighlightUtil.isSerializationImplicitlyUsedField(field)) { return; } final PsiClass containingClass = PsiUtil.getTopLevelClass(field); if (!shouldCheckVariable(field, containingClass)) { return; } final ArrayReadWriteVisitor visitor = new ArrayReadWriteVisitor(field, !isSimpleArrayExpression(field.getInitializer())); containingClass.accept(visitor); final boolean written = visitor.isWritten(); if (!visitor.isReferenced() || written == visitor.isRead()) { return; } registerFieldError(field, Boolean.valueOf(written)); }