@Override public boolean setupDefaultValues( ChangeInfo changeInfo, Ref<UsageInfo[]> refUsages, Project project) { if (!(changeInfo instanceof JavaChangeInfo)) return true; for (UsageInfo usageInfo : refUsages.get()) { if (usageInfo instanceof GrMethodCallUsageInfo) { GrMethodCallUsageInfo methodCallUsageInfo = (GrMethodCallUsageInfo) usageInfo; if (methodCallUsageInfo.isToChangeArguments()) { final PsiElement element = methodCallUsageInfo.getElement(); if (element == null) continue; final PsiMethod caller = RefactoringUtil.getEnclosingMethod(element); final boolean needDefaultValue = !((JavaChangeInfo) changeInfo).getMethodsToPropagateParameters().contains(caller); final PsiMethod referencedMethod = methodCallUsageInfo.getReferencedMethod(); if (needDefaultValue && (caller == null || referencedMethod == null || !MethodSignatureUtil.isSuperMethod(referencedMethod, caller))) { final ParameterInfo[] parameters = changeInfo.getNewParameters(); for (ParameterInfo parameter : parameters) { final String defaultValue = parameter.getDefaultValue(); if (defaultValue == null && parameter.getOldIndex() == -1) { ((ParameterInfoImpl) parameter).setDefaultValue(""); if (!ApplicationManager.getApplication().isUnitTestMode()) { final PsiType type = ((ParameterInfoImpl) parameter) .getTypeWrapper() .getType(element, element.getManager()); final DefaultValueChooser chooser = new DefaultValueChooser( project, parameter.getName(), PsiTypesUtil.getDefaultValueOfType(type)); chooser.show(); if (chooser.isOK()) { if (chooser.feelLucky()) { parameter.setUseAnySingleVariable(true); } else { ((ParameterInfoImpl) parameter).setDefaultValue(chooser.getDefaultValue()); } } else { return false; } } } } } } } } return true; }
private static void addParameterUsages( PsiParameter parameter, ArrayList<UsageInfo> results, ParameterInfo info) { PsiManager manager = parameter.getManager(); GlobalSearchScope projectScope = GlobalSearchScope.projectScope(manager.getProject()); for (PsiReference psiReference : ReferencesSearch.search(parameter, projectScope, false)) { PsiElement parmRef = psiReference.getElement(); UsageInfo usageInfo = new ChangeSignatureParameterUsageInfo(parmRef, parameter.getName(), info.getName()); results.add(usageInfo); } }
private void findParametersUsage( final PsiMethod method, ArrayList<UsageInfo> result, PsiMethod[] overriders) { if (JavaLanguage.INSTANCE.equals(myChangeInfo.getLanguage())) { PsiParameter[] parameters = method.getParameterList().getParameters(); for (ParameterInfo info : myChangeInfo.getNewParameters()) { if (info.getOldIndex() >= 0) { PsiParameter parameter = parameters[info.getOldIndex()]; if (!info.getName().equals(parameter.getName())) { addParameterUsages(parameter, result, info); for (PsiMethod overrider : overriders) { PsiParameter parameter1 = overrider.getParameterList().getParameters()[info.getOldIndex()]; if (parameter1 != null && Comparing.strEqual(parameter.getName(), parameter1.getName())) { addParameterUsages(parameter1, result, info); } } } } } } }
private void detectLocalsCollisionsInMethod( final PsiMethod method, final ArrayList<UsageInfo> result, boolean isOriginal) { if (!JavaLanguage.INSTANCE.equals(method.getLanguage())) return; final PsiParameter[] parameters = method.getParameterList().getParameters(); final Set<PsiParameter> deletedOrRenamedParameters = new HashSet<PsiParameter>(); if (isOriginal) { ContainerUtil.addAll(deletedOrRenamedParameters, parameters); for (ParameterInfo parameterInfo : myChangeInfo.getNewParameters()) { if (parameterInfo.getOldIndex() >= 0 && parameterInfo.getOldIndex() < parameters.length) { final PsiParameter parameter = parameters[parameterInfo.getOldIndex()]; if (parameterInfo.getName().equals(parameter.getName())) { deletedOrRenamedParameters.remove(parameter); } } } } for (ParameterInfo parameterInfo : myChangeInfo.getNewParameters()) { final int oldParameterIndex = parameterInfo.getOldIndex(); final String newName = parameterInfo.getName(); if (oldParameterIndex >= 0) { if (isOriginal && oldParameterIndex < parameters.length && !newName.equals(myChangeInfo.getOldParameterNames()[oldParameterIndex])) { // Name changes take place only in primary method when name was actually changed final PsiParameter parameter = parameters[oldParameterIndex]; if (!newName.equals(parameter.getName())) { JavaUnresolvableLocalCollisionDetector.visitLocalsCollisions( parameter, newName, method.getBody(), null, new JavaUnresolvableLocalCollisionDetector.CollidingVariableVisitor() { public void visitCollidingElement(final PsiVariable collidingVariable) { if (!deletedOrRenamedParameters.contains(collidingVariable)) { result.add( new RenamedParameterCollidesWithLocalUsageInfo( parameter, collidingVariable, method)); } } }); } } } else { JavaUnresolvableLocalCollisionDetector.visitLocalsCollisions( method, newName, method.getBody(), null, new JavaUnresolvableLocalCollisionDetector.CollidingVariableVisitor() { public void visitCollidingElement(PsiVariable collidingVariable) { if (!deletedOrRenamedParameters.contains(collidingVariable)) { result.add( new NewParameterCollidesWithLocalUsageInfo( collidingVariable, collidingVariable, method)); } } }); } } }