private static PsiParameter createNewParameter( JavaChangeInfo changeInfo, JavaParameterInfo newParm, PsiSubstitutor substitutor) throws IncorrectOperationException { final PsiParameterList list = changeInfo.getMethod().getParameterList(); final PsiElementFactory factory = JavaPsiFacade.getInstance(list.getProject()).getElementFactory(); final PsiType type = substitutor.substitute(newParm.createType(list, list.getManager())); return factory.createParameter(newParm.getName(), type); }
private static void fixJavadocsForChangedMethod( PsiMethod method, JavaChangeInfo changeInfo, int newParamsLength) throws IncorrectOperationException { final PsiParameter[] parameters = method.getParameterList().getParameters(); final JavaParameterInfo[] newParms = changeInfo.getNewParameters(); LOG.assertTrue(parameters.length <= newParamsLength); final Set<PsiParameter> newParameters = new HashSet<PsiParameter>(); final String[] oldParameterNames = changeInfo.getOldParameterNames(); for (int i = 0; i < newParamsLength; i++) { JavaParameterInfo newParm = newParms[i]; if (newParm.getOldIndex() < 0 || !newParm.getName().equals(oldParameterNames[newParm.getOldIndex()])) { newParameters.add(parameters[i]); } } RefactoringUtil.fixJavadocsForParams(method, newParameters); }
@Nullable private static PsiExpression createActualArgument( JavaChangeInfo changeInfo, final PsiExpressionList list, final JavaParameterInfo info, final boolean toInsertDefaultValue, final PsiExpression[] args) throws IncorrectOperationException { final PsiElementFactory factory = JavaPsiFacade.getInstance(list.getProject()).getElementFactory(); final int index = info.getOldIndex(); if (index >= 0) { return args[index]; } else { if (toInsertDefaultValue) { return createDefaultValue(changeInfo, factory, info, list); } else { return factory.createExpressionFromText(info.getName(), list); } } }
private void addMethodConflicts(MultiMap<PsiElement, String> conflicts) { String newMethodName = myChangeInfo.getNewName(); if (!(myChangeInfo instanceof JavaChangeInfo)) { return; } try { PsiMethod prototype; final PsiMethod method = myChangeInfo.getMethod(); if (!StdLanguages.JAVA.equals(method.getLanguage())) return; PsiManager manager = method.getManager(); PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory(); final CanonicalTypes.Type returnType = myChangeInfo.getNewReturnType(); if (returnType != null) { prototype = factory.createMethod(newMethodName, returnType.getType(method, manager)); } else { prototype = factory.createConstructor(); prototype.setName(newMethodName); } JavaParameterInfo[] parameters = myChangeInfo.getNewParameters(); for (JavaParameterInfo info : parameters) { PsiType parameterType = info.createType(method, manager); if (parameterType == null) { parameterType = JavaPsiFacade.getElementFactory(method.getProject()) .createTypeFromText(CommonClassNames.JAVA_LANG_OBJECT, method); } PsiParameter param = factory.createParameter(info.getName(), parameterType); prototype.getParameterList().add(param); } ConflictsUtil.checkMethodConflicts( method.getContainingClass(), method, prototype, conflicts); } catch (IncorrectOperationException e) { LOG.error(e); } }
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 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; }