@Nullable private static PsiClass getFieldOrMethodAccessedClass( PsiReferenceExpression ref, PsiClass fieldOrMethodClass) { PsiElement[] children = ref.getChildren(); if (children.length > 1 && children[0] instanceof PsiExpression) { PsiExpression expr = (PsiExpression) children[0]; PsiType type = expr.getType(); if (type != null) { if (!(type instanceof PsiClassType)) return null; return PsiUtil.resolveClassInType(type); } else { if (expr instanceof PsiReferenceExpression) { PsiElement refElement = ((PsiReferenceExpression) expr).resolve(); if (refElement instanceof PsiClass) return (PsiClass) refElement; } return null; } } PsiManager manager = ref.getManager(); for (PsiElement parent = ref; parent != null; parent = parent.getParent()) { if (parent instanceof PsiClass && (manager.areElementsEquivalent(parent, fieldOrMethodClass) || ((PsiClass) parent).isInheritor(fieldOrMethodClass, true))) { return (PsiClass) parent; } } return null; }
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); }