private static void processMethodUsage( PsiElement ref, JavaChangeInfo changeInfo, boolean toChangeArguments, boolean toCatchExceptions, PsiMethod callee, PsiSubstitutor subsitutor, final UsageInfo[] usages) throws IncorrectOperationException { if (changeInfo.isNameChanged()) { if (ref instanceof PsiJavaCodeReferenceElement) { PsiElement last = ((PsiJavaCodeReferenceElement) ref).getReferenceNameElement(); if (last instanceof PsiIdentifier && last.getText().equals(changeInfo.getOldName())) { last.replace(changeInfo.getNewNameIdentifier()); } } } final PsiMethod caller = RefactoringUtil.getEnclosingMethod(ref); if (toChangeArguments) { final PsiExpressionList list = RefactoringUtil.getArgumentListByMethodReference(ref); LOG.assertTrue(list != null); boolean toInsertDefaultValue = needDefaultValue(changeInfo, caller); if (toInsertDefaultValue && ref instanceof PsiReferenceExpression) { final PsiExpression qualifierExpression = ((PsiReferenceExpression) ref).getQualifierExpression(); if (qualifierExpression instanceof PsiSuperExpression && callerSignatureIsAboutToChangeToo(caller, usages)) { toInsertDefaultValue = false; } } fixActualArgumentsList(list, changeInfo, toInsertDefaultValue, subsitutor); } if (toCatchExceptions) { if (!(ref instanceof PsiReferenceExpression && ((PsiReferenceExpression) ref).getQualifierExpression() instanceof PsiSuperExpression)) { if (needToCatchExceptions(changeInfo, caller)) { PsiClassType[] newExceptions = callee != null ? getCalleeChangedExceptionInfo(callee) : getPrimaryChangedExceptionInfo(changeInfo); fixExceptions(ref, newExceptions); } } } }
private static void processPrimaryMethod( JavaChangeInfo changeInfo, PsiMethod method, PsiMethod baseMethod, boolean isOriginal) throws IncorrectOperationException { PsiElementFactory factory = JavaPsiFacade.getInstance(method.getProject()).getElementFactory(); if (changeInfo.isVisibilityChanged()) { PsiModifierList modifierList = method.getModifierList(); final String highestVisibility = isOriginal ? changeInfo.getNewVisibility() : VisibilityUtil.getHighestVisibility( changeInfo.getNewVisibility(), VisibilityUtil.getVisibilityModifier(modifierList)); VisibilityUtil.setVisibility(modifierList, highestVisibility); } if (changeInfo.isNameChanged()) { String newName = baseMethod == null ? changeInfo.getNewName() : RefactoringUtil.suggestNewOverriderName( method.getName(), baseMethod.getName(), changeInfo.getNewName()); if (newName != null && !newName.equals(method.getName())) { final PsiIdentifier nameId = method.getNameIdentifier(); assert nameId != null; nameId.replace( JavaPsiFacade.getInstance(method.getProject()) .getElementFactory() .createIdentifier(newName)); } } final PsiSubstitutor substitutor = baseMethod == null ? PsiSubstitutor.EMPTY : ChangeSignatureProcessor.calculateSubstitutor(method, baseMethod); if (changeInfo.isReturnTypeChanged()) { PsiType newTypeElement = changeInfo .getNewReturnType() .getType(changeInfo.getMethod().getParameterList(), method.getManager()); final PsiType returnType = substitutor.substitute(newTypeElement); // don't modify return type for non-Java overriders (EJB) if (method.getName().equals(changeInfo.getNewName())) { final PsiTypeElement typeElement = method.getReturnTypeElement(); if (typeElement != null) { typeElement.replace(factory.createTypeElement(returnType)); } } } PsiParameterList list = method.getParameterList(); PsiParameter[] parameters = list.getParameters(); final JavaParameterInfo[] parameterInfos = changeInfo.getNewParameters(); final int delta = baseMethod != null ? baseMethod.getParameterList().getParametersCount() - method.getParameterList().getParametersCount() : 0; PsiParameter[] newParms = new PsiParameter[Math.max(parameterInfos.length - delta, 0)]; final String[] oldParameterNames = changeInfo.getOldParameterNames(); final String[] oldParameterTypes = changeInfo.getOldParameterTypes(); for (int i = 0; i < newParms.length; i++) { JavaParameterInfo info = parameterInfos[i]; int index = info.getOldIndex(); if (index >= 0) { PsiParameter parameter = parameters[index]; newParms[i] = parameter; String oldName = oldParameterNames[index]; if (!oldName.equals(info.getName()) && oldName.equals(parameter.getName())) { PsiIdentifier newIdentifier = factory.createIdentifier(info.getName()); parameter.getNameIdentifier().replace(newIdentifier); } String oldType = oldParameterTypes[index]; if (!oldType.equals(info.getTypeText())) { parameter.normalizeDeclaration(); PsiType newType = substitutor.substitute( info.createType(changeInfo.getMethod().getParameterList(), method.getManager())); parameter.getTypeElement().replace(factory.createTypeElement(newType)); } } else { newParms[i] = createNewParameter(changeInfo, info, substitutor); } } resolveParameterVsFieldsConflicts(newParms, method, list, changeInfo.toRemoveParm()); fixJavadocsForChangedMethod(method, changeInfo, newParms.length); if (changeInfo.isExceptionSetOrOrderChanged()) { final PsiClassType[] newExceptions = getPrimaryChangedExceptionInfo(changeInfo); fixPrimaryThrowsLists(method, newExceptions); } }
private boolean needToChangeCalls() { return myChangeInfo.isNameChanged() || myChangeInfo.isParameterSetOrOrderChanged() || myChangeInfo.isExceptionSetOrOrderChanged(); }
private PsiMethod[] findSimpleUsagesWithoutParameters( final PsiMethod method, final ArrayList<UsageInfo> result, boolean isToModifyArgs, boolean isToThrowExceptions, boolean isOriginal) { GlobalSearchScope projectScope = GlobalSearchScope.projectScope(method.getProject()); PsiMethod[] overridingMethods = OverridingMethodsSearch.search(method, true).toArray(PsiMethod.EMPTY_ARRAY); for (PsiMethod overridingMethod : overridingMethods) { result.add( new OverriderUsageInfo( overridingMethod, method, isOriginal, isToModifyArgs, isToThrowExceptions)); } boolean needToChangeCalls = !myChangeInfo.isGenerateDelegate() && (myChangeInfo.isNameChanged() || myChangeInfo.isParameterSetOrOrderChanged() || myChangeInfo.isExceptionSetOrOrderChanged() || myChangeInfo.isVisibilityChanged() /*for checking inaccessible*/); if (needToChangeCalls) { int parameterCount = method.getParameterList().getParametersCount(); PsiReference[] refs = MethodReferencesSearch.search(method, projectScope, true) .toArray(PsiReference.EMPTY_ARRAY); for (PsiReference ref : refs) { PsiElement element = ref.getElement(); boolean isToCatchExceptions = isToThrowExceptions && needToCatchExceptions(RefactoringUtil.getEnclosingMethod(element)); if (!isToCatchExceptions) { if (RefactoringUtil.isMethodUsage(element)) { PsiExpressionList list = RefactoringUtil.getArgumentListByMethodReference(element); if (list == null || !method.isVarArgs() && list.getExpressions().length != parameterCount) continue; } } if (RefactoringUtil.isMethodUsage(element)) { result.add(new MethodCallUsageInfo(element, isToModifyArgs, isToCatchExceptions)); } else if (element instanceof PsiDocTagValue) { result.add(new UsageInfo(element)); } else if (element instanceof PsiMethod && ((PsiMethod) element).isConstructor()) { if (JavaLanguage.INSTANCE.equals(element.getLanguage())) { DefaultConstructorImplicitUsageInfo implicitUsageInfo = new DefaultConstructorImplicitUsageInfo( (PsiMethod) element, ((PsiMethod) element).getContainingClass(), method); result.add(implicitUsageInfo); } } else if (element instanceof PsiClass) { LOG.assertTrue(method.isConstructor()); final PsiClass psiClass = (PsiClass) element; if (JavaLanguage.INSTANCE.equals(psiClass.getLanguage())) { if (myChangeInfo instanceof JavaChangeInfo) { if (shouldPropagateToNonPhysicalMethod( method, result, psiClass, ((JavaChangeInfo) myChangeInfo).getMethodsToPropagateParameters())) { continue; } } result.add(new NoConstructorClassUsageInfo(psiClass)); } } else if (ref instanceof PsiCallReference) { result.add(new CallReferenceUsageInfo((PsiCallReference) ref)); } else { result.add(new MoveRenameUsageInfo(element, ref, method)); } } // if (method.isConstructor() && parameterCount == 0) { // RefactoringUtil.visitImplicitConstructorUsages(method.getContainingClass(), // new // DefaultConstructorUsageCollector(result)); // } } else if (myChangeInfo.isParameterTypesChanged()) { PsiReference[] refs = MethodReferencesSearch.search(method, projectScope, true) .toArray(PsiReference.EMPTY_ARRAY); for (PsiReference reference : refs) { final PsiElement element = reference.getElement(); if (element instanceof PsiDocTagValue) { result.add(new UsageInfo(reference)); } else if (element instanceof XmlElement) { result.add(new MoveRenameUsageInfo(reference, method)); } else if (element instanceof PsiMethodReferenceExpression) { result.add(new UsageInfo(reference)); } } } // Conflicts detectLocalsCollisionsInMethod(method, result, isOriginal); for (final PsiMethod overridingMethod : overridingMethods) { detectLocalsCollisionsInMethod(overridingMethod, result, isOriginal); } return overridingMethods; }
private static void processMethodUsage( PsiElement element, JavaChangeInfo changeInfo, boolean toChangeArguments, boolean toCatchExceptions, GrClosureSignatureUtil.ArgInfo<PsiElement>[] map, PsiSubstitutor substitutor) { if (map == null) return; if (changeInfo.isNameChanged()) { if (element instanceof GrReferenceElement) { element = ((GrReferenceElement) element).handleElementRename(changeInfo.getNewName()); } } if (toChangeArguments) { JavaParameterInfo[] parameters = changeInfo.getNewParameters(); GrArgumentList argumentList = PsiUtil.getArgumentsList(element); GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(element.getProject()); if (argumentList == null) { if (element instanceof GrEnumConstant) { argumentList = factory.createArgumentList(); argumentList = (GrArgumentList) element.add(argumentList); } else { return; } } Set<PsiElement> argsToDelete = new HashSet<PsiElement>(map.length * 2); for (GrClosureSignatureUtil.ArgInfo<PsiElement> argInfo : map) { argsToDelete.addAll(argInfo.args); } for (JavaParameterInfo parameter : parameters) { int index = parameter.getOldIndex(); if (index >= 0) { argsToDelete.removeAll(map[index].args); } } for (PsiElement arg : argsToDelete) { arg.delete(); } boolean skipOptionals = false; PsiElement anchor = null; // PsiTreeUtil.getChildOfAnyType(argumentList, GrExpression.class, // GrNamedArgument.class); for (int i = 0; i < parameters.length; i++) { JavaParameterInfo parameter = parameters[i]; int index = parameter.getOldIndex(); if (index >= 0) { GrClosureSignatureUtil.ArgInfo<PsiElement> argInfo = map[index]; List<PsiElement> arguments = argInfo.args; if (argInfo.isMultiArg) { // arguments for Map and varArg if ((i != 0 || !(!arguments.isEmpty() && arguments.iterator().next() instanceof GrNamedArgument)) && (i != parameters.length - 1 || !parameter.isVarargType())) { final PsiType type = parameter.createType( changeInfo.getMethod().getParameterList(), argumentList.getManager()); final GrExpression arg = GroovyRefactoringUtil.generateArgFromMultiArg( substitutor, arguments, type, element.getProject()); for (PsiElement argument : arguments) { argument.delete(); } anchor = argumentList.addAfter(arg, anchor); JavaCodeStyleManager.getInstance(anchor.getProject()).shortenClassReferences(anchor); } } else { // arguments for simple parameters if (arguments.size() == 1) { // arg exists PsiElement arg = arguments.iterator().next(); if (i == parameters.length - 1 && parameter.isVarargType()) { if (arg instanceof GrSafeCastExpression) { PsiElement expr = ((GrSafeCastExpression) arg).getOperand(); if (expr instanceof GrListOrMap && !((GrListOrMap) expr).isMap()) { final PsiElement copy = expr.copy(); PsiElement[] newVarargs = ((GrListOrMap) copy).getInitializers(); for (PsiElement vararg : newVarargs) { anchor = argumentList.addAfter(vararg, anchor); } arg.delete(); continue; } } } PsiElement curArg = getNextOfType(argumentList, anchor, GrExpression.class); if (curArg == arg) { anchor = arg; } else { final PsiElement copy = arg.copy(); anchor = argumentList.addAfter(copy, anchor); arg.delete(); } } else { // arg is skipped. Parameter is optional skipOptionals = true; } } } else { if (skipOptionals && isParameterOptional(parameter)) continue; if (forceOptional(parameter)) { skipOptionals = true; continue; } try { GrExpression value = createDefaultValue(factory, changeInfo, parameter, argumentList); if (i > 0 && (value == null || anchor == null)) { PsiElement comma = Factory.createSingleLeafElement( GroovyTokenTypes.mCOMMA, ",", 0, 1, SharedImplUtil.findCharTableByTree(argumentList.getNode()), argumentList.getManager()) .getPsi(); if (anchor == null) anchor = argumentList.getLeftParen(); anchor = argumentList.addAfter(comma, anchor); } if (value != null) { anchor = argumentList.addAfter(value, anchor); } } catch (IncorrectOperationException e) { LOG.error(e.getMessage()); } } } GrCall call = GroovyRefactoringUtil.getCallExpressionByMethodReference(element); if (argumentList.getText().trim().isEmpty() && (call == null || !PsiImplUtil.hasClosureArguments(call))) { argumentList = argumentList.replaceWithArgumentList(factory.createArgumentList()); } CodeStyleManager.getInstance(argumentList.getProject()).reformat(argumentList); } if (toCatchExceptions) { final ThrownExceptionInfo[] exceptionInfos = changeInfo.getNewExceptions(); PsiClassType[] exceptions = getExceptions(exceptionInfos, element, element.getManager()); fixExceptions(element, exceptions); } }
private static boolean processPrimaryMethodInner( JavaChangeInfo changeInfo, GrMethod method, @Nullable PsiMethod baseMethod) { if (changeInfo.isNameChanged()) { String newName = baseMethod == null ? changeInfo.getNewName() : RefactoringUtil.suggestNewOverriderName( method.getName(), baseMethod.getName(), changeInfo.getNewName()); if (newName != null && !newName.equals(method.getName())) { method.setName(changeInfo.getNewName()); } } final GrModifierList modifierList = method.getModifierList(); if (changeInfo.isVisibilityChanged()) { modifierList.setModifierProperty(changeInfo.getNewVisibility(), true); } PsiSubstitutor substitutor = baseMethod != null ? calculateSubstitutor(method, baseMethod) : PsiSubstitutor.EMPTY; final PsiMethod context = changeInfo.getMethod(); GrTypeElement oldReturnTypeElement = method.getReturnTypeElementGroovy(); if (changeInfo.isReturnTypeChanged()) { CanonicalTypes.Type newReturnType = changeInfo.getNewReturnType(); if (newReturnType == null) { if (oldReturnTypeElement != null) { oldReturnTypeElement.delete(); if (modifierList.getModifiers().length == 0) { modifierList.setModifierProperty(GrModifier.DEF, true); } } } else { PsiType type = newReturnType.getType(context, method.getManager()); GrReferenceAdjuster.shortenAllReferencesIn( method.setReturnType(substitutor.substitute(type))); if (oldReturnTypeElement == null) { modifierList.setModifierProperty(GrModifier.DEF, false); } } } JavaParameterInfo[] newParameters = changeInfo.getNewParameters(); final GrParameterList parameterList = method.getParameterList(); GrParameter[] oldParameters = parameterList.getParameters(); final PsiParameter[] oldBaseParams = baseMethod != null ? baseMethod.getParameterList().getParameters() : null; Set<GrParameter> toRemove = new HashSet<GrParameter>(oldParameters.length); ContainerUtil.addAll(toRemove, oldParameters); GrParameter anchor = null; final GrDocComment docComment = method.getDocComment(); final GrDocTag[] tags = docComment == null ? null : docComment.getTags(); for (JavaParameterInfo newParameter : newParameters) { // if old parameter name differs from base method parameter name we don't change it final String newName; final int oldIndex = newParameter.getOldIndex(); if (oldIndex >= 0 && oldBaseParams != null) { final String oldName = oldParameters[oldIndex].getName(); if (oldName.equals(oldBaseParams[oldIndex].getName())) { newName = newParameter.getName(); } else { newName = oldName; } } else { newName = newParameter.getName(); } final GrParameter oldParameter = oldIndex >= 0 ? oldParameters[oldIndex] : null; if (docComment != null && oldParameter != null) { final String oldName = oldParameter.getName(); for (GrDocTag tag : tags) { if ("@param".equals(tag.getName())) { final GrDocParameterReference parameterReference = tag.getDocParameterReference(); if (parameterReference != null && oldName.equals(parameterReference.getText())) { parameterReference.handleElementRename(newName); } } } } GrParameter grParameter = createNewParameter(substitutor, context, parameterList, newParameter, newName); if (oldParameter != null) { grParameter.getModifierList().replace(oldParameter.getModifierList()); } if ("def".equals(newParameter.getTypeText())) { grParameter.getModifierList().setModifierProperty(GrModifier.DEF, true); } else if (StringUtil.isEmpty(newParameter.getTypeText())) { grParameter.getModifierList().setModifierProperty(GrModifier.DEF, false); } anchor = (GrParameter) parameterList.addAfter(grParameter, anchor); } for (GrParameter oldParameter : toRemove) { oldParameter.delete(); } JavaCodeStyleManager.getInstance(parameterList.getProject()) .shortenClassReferences(parameterList); CodeStyleManager.getInstance(parameterList.getProject()).reformat(parameterList); if (changeInfo.isExceptionSetOrOrderChanged()) { final ThrownExceptionInfo[] infos = changeInfo.getNewExceptions(); PsiClassType[] exceptionTypes = new PsiClassType[infos.length]; for (int i = 0; i < infos.length; i++) { ThrownExceptionInfo info = infos[i]; exceptionTypes[i] = (PsiClassType) info.createType(method, method.getManager()); } PsiReferenceList thrownList = GroovyPsiElementFactory.getInstance(method.getProject()).createThrownList(exceptionTypes); thrownList = (PsiReferenceList) method.getThrowsList().replace(thrownList); JavaCodeStyleManager.getInstance(thrownList.getProject()).shortenClassReferences(thrownList); CodeStyleManager.getInstance(method.getProject()).reformat(method.getThrowsList()); } return true; }