protected void performRefactoring(@NotNull UsageInfo[] usageInfos) { final PsiClass psiClass = buildClass(); if (psiClass != null) { fixJavadocForConstructor(psiClass); super.performRefactoring(usageInfos); if (!myUseExistingClass) { for (PsiReference reference : ReferencesSearch.search(method)) { final PsiElement place = reference.getElement(); VisibilityUtil.escalateVisibility(psiClass, place); for (PsiMethod constructor : psiClass.getConstructors()) { VisibilityUtil.escalateVisibility(constructor, place); } } } } }
@Nullable private static PsiMethodCallExpression createGetterCall( FieldDescriptor fieldDescriptor, PsiReferenceExpression expr, PsiClass aClass, PsiMethod getter) throws IncorrectOperationException { final String getterName = fieldDescriptor.getGetterName(); @NonNls String text = getterName + "()"; PsiMethodCallExpression methodCall = prepareMethodCall(expr, text); methodCall = checkMethodResolvable(methodCall, getter, expr, aClass); if (methodCall == null) { VisibilityUtil.escalateVisibility(fieldDescriptor.getField(), expr); } return methodCall; }
private static PsiMethodCallExpression createSetterCall( FieldDescriptor fieldDescriptor, PsiExpression setterArgument, PsiReferenceExpression expr, PsiClass aClass, PsiMethod setter) throws IncorrectOperationException { final String setterName = fieldDescriptor.getSetterName(); @NonNls String text = setterName + "(a)"; PsiMethodCallExpression methodCall = prepareMethodCall(expr, text); methodCall.getArgumentList().getExpressions()[0].replace(setterArgument); methodCall = checkMethodResolvable(methodCall, setter, expr, aClass); if (methodCall == null) { VisibilityUtil.escalateVisibility(fieldDescriptor.getField(), expr); } return methodCall; }
private void correctMethodCall( final PsiMethodCallExpression expression, final boolean isInternalCall) { try { final PsiManager manager = myMethod.getManager(); PsiReferenceExpression methodExpression = expression.getMethodExpression(); if (!methodExpression.isReferenceTo(myMethod)) return; final PsiExpression oldQualifier = methodExpression.getQualifierExpression(); PsiExpression newQualifier = null; final PsiClass classReferencedByThis = MoveInstanceMembersUtil.getClassReferencedByThis(methodExpression); if (myTargetVariable instanceof PsiParameter) { final int index = myMethod.getParameterList().getParameterIndex((PsiParameter) myTargetVariable); final PsiExpression[] arguments = expression.getArgumentList().getExpressions(); if (index < arguments.length) { newQualifier = (PsiExpression) arguments[index].copy(); arguments[index].delete(); } } else { VisibilityUtil.escalateVisibility((PsiField) myTargetVariable, expression); String newQualifierName = myTargetVariable.getName(); if (myTargetVariable instanceof PsiField && oldQualifier != null) { final PsiClass aClass = PsiUtil.resolveClassInClassTypeOnly(oldQualifier.getType()); if (aClass == ((PsiField) myTargetVariable).getContainingClass()) { newQualifierName = oldQualifier.getText() + "." + newQualifierName; } } newQualifier = JavaPsiFacade.getInstance(manager.getProject()) .getElementFactory() .createExpressionFromText(newQualifierName, null); } PsiExpression newArgument = null; if (classReferencedByThis != null) { @NonNls String thisArgumentText = null; if (manager.areElementsEquivalent(myMethod.getContainingClass(), classReferencedByThis)) { if (myOldClassParameterNames.containsKey(myMethod.getContainingClass())) { thisArgumentText = "this"; } } else { thisArgumentText = classReferencedByThis.getName() + ".this"; } if (thisArgumentText != null) { newArgument = JavaPsiFacade.getInstance(manager.getProject()) .getElementFactory() .createExpressionFromText(thisArgumentText, null); } } else { if (!isInternalCall && oldQualifier != null) { final PsiType type = oldQualifier.getType(); if (type instanceof PsiClassType) { final PsiClass resolved = ((PsiClassType) type).resolve(); if (resolved != null && getParameterNameToCreate(resolved) != null) { newArgument = replaceRefsToTargetVariable( oldQualifier); // replace is needed in case old qualifier is e.g. the same as // field as target variable } } } } if (newArgument != null) { expression.getArgumentList().add(newArgument); } if (newQualifier != null) { if (newQualifier instanceof PsiThisExpression && ((PsiThisExpression) newQualifier).getQualifier() == null) { // Remove now redundant 'this' qualifier if (oldQualifier != null) oldQualifier.delete(); } else { final PsiReferenceExpression refExpr = (PsiReferenceExpression) JavaPsiFacade.getInstance(manager.getProject()) .getElementFactory() .createExpressionFromText("q." + myMethod.getName(), null); refExpr.getQualifierExpression().replace(newQualifier); methodExpression.replace(refExpr); } } } catch (IncorrectOperationException e) { LOG.error(e); } }