private static void addInheritorUsages( PsiClass aClass, final GlobalSearchScope searchScope, final List<UsageInfo> usages) { for (PsiClass inheritor : ClassInheritorsSearch.search(aClass, searchScope, false).findAll()) { if (!inheritor.isInterface()) { usages.add(new InheritorUsageInfo(inheritor)); } else { addInheritorUsages(inheritor, searchScope, usages); } } }
private static void generateDelegate(JavaChangeInfo changeInfo) throws IncorrectOperationException { final PsiMethod delegate = (PsiMethod) changeInfo.getMethod().copy(); final PsiClass targetClass = changeInfo.getMethod().getContainingClass(); LOG.assertTrue(!targetClass.isInterface()); PsiElementFactory factory = JavaPsiFacade.getElementFactory(targetClass.getProject()); ChangeSignatureProcessor.makeEmptyBody(factory, delegate); final PsiCallExpression callExpression = ChangeSignatureProcessor.addDelegatingCallTemplate(delegate, changeInfo.getNewName()); addDelegateArguments(changeInfo, factory, callExpression); targetClass.addBefore(delegate, changeInfo.getMethod()); }
protected void performRefactoring(UsageInfo[] usages) { if (!CommonRefactoringUtil.checkReadOnlyStatus(myProject, myTargetClass)) return; PsiMethod patternMethod = createMethodToAdd(); final List<PsiReference> docRefs = new ArrayList<PsiReference>(); for (UsageInfo usage : usages) { if (usage instanceof InheritorUsageInfo) { final PsiClass inheritor = ((InheritorUsageInfo) usage).getInheritor(); addMethodToClass(inheritor, patternMethod, true); } else if (usage instanceof MethodCallUsageInfo && !((MethodCallUsageInfo) usage).isInternal()) { correctMethodCall(((MethodCallUsageInfo) usage).getMethodCallExpression(), false); } else if (usage instanceof JavadocUsageInfo) { docRefs.add(usage.getElement().getReference()); } } try { if (myTargetClass.isInterface()) patternMethod.getBody().delete(); final PsiMethod method = addMethodToClass(myTargetClass, patternMethod, false); myMethod.delete(); for (PsiReference reference : docRefs) { reference.bindToElement(method); } VisibilityUtil.fixVisibility(usages, method, myNewVisibility); } catch (IncorrectOperationException e) { LOG.error(e); } }
protected boolean preprocessUsages(Ref<UsageInfo[]> refUsages) { final UsageInfo[] usages = refUsages.get(); MultiMap<PsiElement, String> conflicts = new MultiMap<PsiElement, String>(); final Set<PsiMember> members = new HashSet<PsiMember>(); members.add(myMethod); if (myTargetVariable instanceof PsiField) members.add((PsiMember) myTargetVariable); if (!myTargetClass.isInterface()) { RefactoringConflictsUtil.analyzeAccessibilityConflicts( members, myTargetClass, conflicts, myNewVisibility); } else { for (final UsageInfo usage : usages) { if (usage instanceof InheritorUsageInfo) { RefactoringConflictsUtil.analyzeAccessibilityConflicts( members, ((InheritorUsageInfo) usage).getInheritor(), conflicts, myNewVisibility); } } } if (myTargetVariable instanceof PsiParameter) { PsiParameter parameter = (PsiParameter) myTargetVariable; for (final UsageInfo usageInfo : usages) { if (usageInfo instanceof MethodCallUsageInfo) { final PsiElement methodCall = ((MethodCallUsageInfo) usageInfo).getMethodCallExpression(); if (methodCall instanceof PsiMethodCallExpression) { final PsiExpression[] expressions = ((PsiMethodCallExpression) methodCall).getArgumentList().getExpressions(); final int index = myMethod.getParameterList().getParameterIndex(parameter); if (index < expressions.length) { PsiExpression instanceValue = expressions[index]; instanceValue = RefactoringUtil.unparenthesizeExpression(instanceValue); if (instanceValue instanceof PsiLiteralExpression && ((PsiLiteralExpression) instanceValue).getValue() == null) { String message = RefactoringBundle.message( "0.contains.call.with.null.argument.for.parameter.1", RefactoringUIUtil.getDescription( ConflictsUtil.getContainer(methodCall), true), CommonRefactoringUtil.htmlEmphasize(parameter.getName())); conflicts.putValue(instanceValue, message); } } } else if (methodCall instanceof PsiMethodReferenceExpression) { conflicts.putValue(methodCall, "Method reference would be broken after move"); } } } } try { ConflictsUtil.checkMethodConflicts(myTargetClass, myMethod, getPatternMethod(), conflicts); } catch (IncorrectOperationException e) { } return showConflicts(conflicts, usages); }
private void detectAccessibilityConflicts( final UsageInfo[] usageArray, MultiMap<PsiElement, String> conflicts) { if (myParameterInitializer != null) { final ReferencedElementsCollector collector = new ReferencedElementsCollector(); myParameterInitializer.accept(collector); final Set<PsiElement> result = collector.myResult; if (!result.isEmpty()) { for (final UsageInfo usageInfo : usageArray) { if (usageInfo instanceof ExternalUsageInfo && IntroduceParameterUtil.isMethodUsage(usageInfo)) { final PsiElement place = usageInfo.getElement(); for (PsiElement element : result) { if (element instanceof PsiField && myReplaceFieldsWithGetters != IntroduceParameterRefactoring .REPLACE_FIELDS_WITH_GETTERS_NONE) { // check getter access instead final PsiClass psiClass = ((PsiField) element).getContainingClass(); LOG.assertTrue(psiClass != null); final PsiMethod method = psiClass.findMethodBySignature( PropertyUtil.generateGetterPrototype((PsiField) element), true); if (method != null) { element = method; } } if (element instanceof PsiMember && !JavaPsiFacade.getInstance(myProject) .getResolveHelper() .isAccessible((PsiMember) element, place, null)) { String message = RefactoringBundle.message( "0.is.not.accessible.from.1.value.for.introduced.parameter.in.that.method.call.will.be.incorrect", RefactoringUIUtil.getDescription(element, true), RefactoringUIUtil.getDescription(ConflictsUtil.getContainer(place), true)); conflicts.putValue(element, message); } } } } } } }
private static void addDefaultConstructor( JavaChangeInfo changeInfo, PsiClass aClass, final UsageInfo[] usages) throws IncorrectOperationException { if (!(aClass instanceof PsiAnonymousClass)) { PsiElementFactory factory = JavaPsiFacade.getElementFactory(aClass.getProject()); PsiMethod defaultConstructor = factory.createMethodFromText(aClass.getName() + "(){}", aClass); defaultConstructor = (PsiMethod) CodeStyleManager.getInstance(aClass.getProject()).reformat(defaultConstructor); defaultConstructor = (PsiMethod) aClass.add(defaultConstructor); PsiUtil.setModifierProperty( defaultConstructor, VisibilityUtil.getVisibilityModifier(aClass.getModifierList()), true); addSuperCall(changeInfo, defaultConstructor, null, usages); } else { final PsiElement parent = aClass.getParent(); if (parent instanceof PsiNewExpression) { final PsiExpressionList argumentList = ((PsiNewExpression) parent).getArgumentList(); final PsiClass baseClass = changeInfo.getMethod().getContainingClass(); final PsiSubstitutor substitutor = TypeConversionUtil.getSuperClassSubstitutor(baseClass, aClass, PsiSubstitutor.EMPTY); fixActualArgumentsList(argumentList, changeInfo, true, substitutor); } } }
@NotNull protected UsageInfo[] findUsages() { final PsiManager manager = myMethod.getManager(); final GlobalSearchScope searchScope = GlobalSearchScope.allScope(manager.getProject()); final List<UsageInfo> usages = new ArrayList<UsageInfo>(); for (PsiReference ref : ReferencesSearch.search(myMethod, searchScope, false)) { final PsiElement element = ref.getElement(); if (element instanceof PsiReferenceExpression) { boolean isInternal = PsiTreeUtil.isAncestor(myMethod, element, true); usages.add(new MethodCallUsageInfo((PsiReferenceExpression) element, isInternal)); } else if (element instanceof PsiDocTagValue) { usages.add(new JavadocUsageInfo(((PsiDocTagValue) element))); } else { throw new UnknownReferenceTypeException(element.getLanguage()); } } if (myTargetClass.isInterface()) { addInheritorUsages(myTargetClass, searchScope, usages); } final PsiCodeBlock body = myMethod.getBody(); if (body != null) { body.accept( new JavaRecursiveElementWalkingVisitor() { @Override public void visitNewExpression(PsiNewExpression expression) { if (MoveInstanceMembersUtil.getClassReferencedByThis(expression) != null) { usages.add(new InternalUsageInfo(expression)); } super.visitNewExpression(expression); } @Override public void visitReferenceExpression(PsiReferenceExpression expression) { if (MoveInstanceMembersUtil.getClassReferencedByThis(expression) != null) { usages.add(new InternalUsageInfo(expression)); } else if (!expression.isQualified()) { final PsiElement resolved = expression.resolve(); if (myTargetVariable.equals(resolved)) { usages.add(new InternalUsageInfo(expression)); } } super.visitReferenceExpression(expression); } }); } return usages.toArray(new UsageInfo[usages.size()]); }
private PsiMethod getPatternMethod() throws IncorrectOperationException { final PsiMethod methodCopy = (PsiMethod) myMethod.copy(); String name = myTargetClass.isInterface() ? PsiModifier.PUBLIC : !Comparing.strEqual(myNewVisibility, VisibilityUtil.ESCALATE_VISIBILITY) ? myNewVisibility : null; if (name != null) { PsiUtil.setModifierProperty(methodCopy, name, true); } if (myTargetVariable instanceof PsiParameter) { final int index = myMethod.getParameterList().getParameterIndex((PsiParameter) myTargetVariable); methodCopy.getParameterList().getParameters()[index].delete(); } addParameters( JavaPsiFacade.getInstance(myProject).getElementFactory(), methodCopy, myTargetClass.isInterface()); return methodCopy; }
private static PsiMethod addMethodToClass( final PsiClass aClass, final PsiMethod patternMethod, boolean canAddOverride) { try { final PsiMethod method = (PsiMethod) aClass.add(patternMethod); ChangeContextUtil.decodeContextInfo(method, null, null); if (canAddOverride && OverrideImplementUtil.isInsertOverride(method, aClass)) { method.getModifierList().addAnnotation(CommonClassNames.JAVA_LANG_OVERRIDE); } return method; } catch (IncorrectOperationException e) { LOG.error(e); } return null; }
protected void performRefactoring(UsageInfo[] usages) { if (!CommonRefactoringUtil.checkReadOnlyStatus(myProject, myTargetClass)) return; PsiMethod patternMethod = createMethodToAdd(); final List<PsiReference> docRefs = new ArrayList<PsiReference>(); for (UsageInfo usage : usages) { if (usage instanceof InheritorUsageInfo) { final PsiClass inheritor = ((InheritorUsageInfo) usage).getInheritor(); addMethodToClass(inheritor, patternMethod, true); } else if (usage instanceof MethodCallUsageInfo && !((MethodCallUsageInfo) usage).isInternal()) { final PsiElement expression = ((MethodCallUsageInfo) usage).getMethodCallExpression(); if (expression instanceof PsiMethodCallExpression) { correctMethodCall((PsiMethodCallExpression) expression, false); } else if (expression instanceof PsiMethodReferenceExpression) { PsiExpression newQualifier = JavaPsiFacade.getInstance(myProject) .getElementFactory() .createExpressionFromText(myTargetVariable.getType().getCanonicalText(), null); ((PsiMethodReferenceExpression) expression).setQualifierExpression(newQualifier); } } else if (usage instanceof JavadocUsageInfo) { docRefs.add(usage.getElement().getReference()); } } try { if (myTargetClass.isInterface()) patternMethod.getBody().delete(); final PsiMethod method = addMethodToClass(myTargetClass, patternMethod, false); myMethod.delete(); for (PsiReference reference : docRefs) { reference.bindToElement(method); } VisibilityUtil.fixVisibility(UsageViewUtil.toElements(usages), method, myNewVisibility); if (myOpenInEditor) { EditorHelper.openInEditor(method); } } catch (IncorrectOperationException e) { LOG.error(e); } }
public boolean processUsage( ChangeInfo changeInfo, UsageInfo usage, boolean beforeMethodChange, UsageInfo[] usages) { if (!isJavaUsage(usage)) return false; if (!(changeInfo instanceof JavaChangeInfo)) return false; if (beforeMethodChange) { if (usage instanceof CallerUsageInfo) { final CallerUsageInfo callerUsageInfo = (CallerUsageInfo) usage; processCallerMethod( (JavaChangeInfo) changeInfo, callerUsageInfo.getMethod(), null, callerUsageInfo.isToInsertParameter(), callerUsageInfo.isToInsertException()); return true; } else if (usage instanceof OverriderUsageInfo) { OverriderUsageInfo info = (OverriderUsageInfo) usage; final PsiMethod method = info.getElement(); final PsiMethod baseMethod = info.getBaseMethod(); if (info.isOriginalOverrider()) { processPrimaryMethod((JavaChangeInfo) changeInfo, method, baseMethod, false); } else { processCallerMethod( (JavaChangeInfo) changeInfo, method, baseMethod, info.isToInsertArgs(), info.isToCatchExceptions()); } return true; } } else { PsiElement element = usage.getElement(); LOG.assertTrue(element != null); if (usage instanceof DefaultConstructorImplicitUsageInfo) { final DefaultConstructorImplicitUsageInfo defConstructorUsage = (DefaultConstructorImplicitUsageInfo) usage; PsiMethod constructor = defConstructorUsage.getConstructor(); if (!constructor.isPhysical()) { final boolean toPropagate = changeInfo instanceof JavaChangeInfoImpl && ((JavaChangeInfoImpl) changeInfo) .propagateParametersMethods.remove(constructor); final PsiClass containingClass = defConstructorUsage.getContainingClass(); constructor = (PsiMethod) containingClass.add(constructor); PsiUtil.setModifierProperty( constructor, VisibilityUtil.getVisibilityModifier(containingClass.getModifierList()), true); if (toPropagate) { ((JavaChangeInfoImpl) changeInfo).propagateParametersMethods.add(constructor); } } addSuperCall( (JavaChangeInfo) changeInfo, constructor, defConstructorUsage.getBaseConstructor(), usages); return true; } else if (usage instanceof NoConstructorClassUsageInfo) { addDefaultConstructor( ((JavaChangeInfo) changeInfo), ((NoConstructorClassUsageInfo) usage).getPsiClass(), usages); return true; } else if (usage instanceof MethodCallUsageInfo) { final MethodCallUsageInfo methodCallInfo = (MethodCallUsageInfo) usage; processMethodUsage( methodCallInfo.getElement(), (JavaChangeInfo) changeInfo, methodCallInfo.isToChangeArguments(), methodCallInfo.isToCatchExceptions(), methodCallInfo.getReferencedMethod(), methodCallInfo.getSubstitutor(), usages); return true; } else if (usage instanceof ChangeSignatureParameterUsageInfo) { String newName = ((ChangeSignatureParameterUsageInfo) usage).newParameterName; String oldName = ((ChangeSignatureParameterUsageInfo) usage).oldParameterName; processParameterUsage((PsiReferenceExpression) element, oldName, newName); return true; } else if (usage instanceof CallReferenceUsageInfo) { ((CallReferenceUsageInfo) usage).getReference().handleChangeSignature(changeInfo); return true; } else if (element instanceof PsiEnumConstant) { fixActualArgumentsList( ((PsiEnumConstant) element).getArgumentList(), (JavaChangeInfo) changeInfo, true, PsiSubstitutor.EMPTY); return true; } else if (!(usage instanceof OverriderUsageInfo)) { PsiReference reference = usage instanceof MoveRenameUsageInfo ? usage.getReference() : element.getReference(); if (reference != null) { PsiElement target = changeInfo.getMethod(); if (target != null) { reference.bindToElement(target); } } } } return false; }
@Nullable private static PsiExpression createDefaultValue( JavaChangeInfo changeInfo, final PsiElementFactory factory, final JavaParameterInfo info, final PsiExpressionList list) throws IncorrectOperationException { if (info.isUseAnySingleVariable()) { final PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(list.getProject()).getResolveHelper(); final PsiType type = info.getTypeWrapper().getType(changeInfo.getMethod(), list.getManager()); final VariablesProcessor processor = new VariablesProcessor(false) { protected boolean check(PsiVariable var, ResolveState state) { if (var instanceof PsiField && !resolveHelper.isAccessible((PsiField) var, list, null)) return false; if (var instanceof PsiLocalVariable && list.getTextRange().getStartOffset() <= var.getTextRange().getStartOffset()) return false; if (PsiTreeUtil.isAncestor(var, list, false)) return false; final PsiType varType = state.get(PsiSubstitutor.KEY).substitute(var.getType()); return type.isAssignableFrom(varType); } public boolean execute(PsiElement pe, ResolveState state) { super.execute(pe, state); return size() < 2; } }; PsiScopesUtil.treeWalkUp(processor, list, null); if (processor.size() == 1) { final PsiVariable result = processor.getResult(0); return factory.createExpressionFromText(result.getName(), list); } if (processor.size() == 0) { final PsiClass parentClass = PsiTreeUtil.getParentOfType(list, PsiClass.class); if (parentClass != null) { PsiClass containingClass = parentClass; final Set<PsiClass> containingClasses = new HashSet<PsiClass>(); while (containingClass != null) { if (type.isAssignableFrom(factory.createType(containingClass, PsiSubstitutor.EMPTY))) { containingClasses.add(containingClass); } containingClass = PsiTreeUtil.getParentOfType(containingClass, PsiClass.class); } if (containingClasses.size() == 1) { return RefactoringUtil.createThisExpression( parentClass.getManager(), containingClasses.contains(parentClass) ? null : containingClasses.iterator().next()); } } } } final PsiCallExpression callExpression = PsiTreeUtil.getParentOfType(list, PsiCallExpression.class); final String defaultValue = info.getDefaultValue(); return callExpression != null ? info.getValue(callExpression) : defaultValue.length() > 0 ? factory.createExpressionFromText(defaultValue, list) : null; }
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); } }