}
private PsiMethod rebuildMethod(@NotNull Project project, @NotNull PsiMethod fromMethod) {
final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory();
final PsiMethod resultMethod;
final PsiType returnType = fromMethod.getReturnType();
if (null == returnType) {
resultMethod = elementFactory.createConstructor(fromMethod.getName());
} else {
resultMethod = elementFactory.createMethod(fromMethod.getName(), returnType);
}
final PsiTypeParameterList fromMethodTypeParameterList = fromMethod.getTypeParameterList();
if (null != fromMethodTypeParameterList) {
PsiTypeParameterList typeParameterList = PsiMethodUtil.createTypeParameterList(fromMethodTypeParameterList);
if (null != typeParameterList) {
resultMethod.addAfter(typeParameterList, resultMethod.getModifierList());
}
}
final PsiClassType[] referencedTypes = fromMethod.getThrowsList().getReferencedTypes();
if (referencedTypes.length > 0) {
PsiJavaCodeReferenceElement[] refs = new PsiJavaCodeReferenceElement[referencedTypes.length];
for (int i = 0; i < refs.length; i++) {
refs[i] = elementFactory.createReferenceElementByType(referencedTypes[i]);
}
resultMethod.getThrowsList().replace(elementFactory.createReferenceList(refs));
}
for (PsiParameter parameter : fromMethod.getParameterList().getParameters()) {
PsiParameter param = elementFactory.createParameter(parameter.getName(), parameter.getType());
resultMethod.getParameterList().add(param);
}
final PsiModifierList fromMethodModifierList = fromMethod.getModifierList();
final PsiModifierList resultMethodModifierList = resultMethod.getModifierList();
copyModifiers(fromMethodModifierList, resultMethodModifierList);
for (PsiAnnotation psiAnnotation : fromMethodModifierList.getAnnotations()) {
final PsiAnnotation annotation = resultMethodModifierList.addAnnotation(psiAnnotation.getQualifiedName());
for (PsiNameValuePair nameValuePair : psiAnnotation.getParameterList().getAttributes()) {
annotation.setDeclaredAttributeValue(nameValuePair.getName(), nameValuePair.getValue());
}
}
PsiCodeBlock body = fromMethod.getBody();
if (null != body) {
resultMethod.getBody().replace(body);
}
return (PsiMethod) CodeStyleManager.getInstance(project).reformat(resultMethod);
}